summaryrefslogtreecommitdiffstats
path: root/tests/javascript/unit/store
diff options
context:
space:
mode:
Diffstat (limited to 'tests/javascript/unit/store')
-rw-r--r--tests/javascript/unit/store/app.spec.ts26
-rw-r--r--tests/javascript/unit/store/feed.spec.ts221
-rw-r--r--tests/javascript/unit/store/folder.spec.ts106
-rw-r--r--tests/javascript/unit/store/item.spec.ts243
4 files changed, 596 insertions, 0 deletions
diff --git a/tests/javascript/unit/store/app.spec.ts b/tests/javascript/unit/store/app.spec.ts
new file mode 100644
index 000000000..a083db3f0
--- /dev/null
+++ b/tests/javascript/unit/store/app.spec.ts
@@ -0,0 +1,26 @@
+import { AppInfoState, mutations } from '../../../../src/store/app'
+import { APPLICATION_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
+
+jest.mock('@nextcloud/router')
+
+describe('app.ts', () => {
+ 'use strict'
+
+ // describe('actions', () => {
+
+ // })
+
+ describe('mutations', () => {
+ it('SET_ERROR should update the error in the state', () => {
+ const state = { error: undefined } as AppInfoState
+
+ const error = { message: 'test err' };
+
+ (mutations[APPLICATION_MUTATION_TYPES.SET_ERROR] as any)(state, error)
+ expect(state.error).toEqual(error);
+
+ (mutations[APPLICATION_MUTATION_TYPES.SET_ERROR] as any)(state, undefined)
+ expect(state.error).toEqual(undefined)
+ })
+ })
+})
diff --git a/tests/javascript/unit/store/feed.spec.ts b/tests/javascript/unit/store/feed.spec.ts
new file mode 100644
index 000000000..ede4307d1
--- /dev/null
+++ b/tests/javascript/unit/store/feed.spec.ts
@@ -0,0 +1,221 @@
+import { Feed } from '../../../../src/types/Feed'
+import { AppState } from '../../../../src/store'
+import { FEED_ACTION_TYPES, mutations, actions } from '../../../../src/store/feed'
+import { FEED_ORDER, FEED_UPDATE_MODE, FeedService } from '../../../../src/dataservices/feed.service'
+
+import { FEED_ITEM_MUTATION_TYPES, FEED_MUTATION_TYPES, FOLDER_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
+import { ItemService } from '../../../../src/dataservices/item.service'
+
+describe('feed.ts', () => {
+ 'use strict'
+
+ describe('actions', () => {
+ describe('FETCH_FEEDS', () => {
+ it('should call FeedService.fetchAllFeeds and commit returned feeds to state', async () => {
+ FeedService.fetchAllFeeds = jest.fn();
+ (FeedService.fetchAllFeeds as any).mockResolvedValue({ data: { feeds: [] } })
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FETCH_FEEDS] as any)({ commit })
+ expect(FeedService.fetchAllFeeds).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.SET_FEEDS, [])
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_UNREAD_COUNT, 0)
+ })
+ })
+
+ describe('ADD_FEED', () => {
+ it('should call FeedService.addFeed and commit feed to state', async () => {
+ FeedService.addFeed = jest.fn();
+ (FeedService.addFeed as any).mockResolvedValue({ data: { feeds: [] } })
+ const commit = jest.fn()
+ await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit } as any, { feedReq: { url: '' } } as any)
+ expect(FeedService.addFeed).toBeCalled()
+ expect(commit).toBeCalled()
+ })
+
+ it('should call FeedService.addFeed and not call commit if error', async () => {
+ FeedService.addFeed = jest.fn();
+ (FeedService.addFeed as any).mockRejectedValue()
+ const commit = jest.fn()
+ await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit } as any, { feedReq: { url: '' } } as any)
+ expect(FeedService.addFeed).toBeCalled()
+
+ expect(commit).not.toBeCalled()
+ })
+ })
+
+ describe('FEED_MARK_READ', () => {
+ it('should call FeedService.markRead and commit all items read to state', async () => {
+ ItemService.fetchFeedItems = jest.fn();
+ (ItemService.fetchFeedItems as any).mockResolvedValue({ data: { items: [{ id: 123 }] } })
+ FeedService.markRead = jest.fn()
+ const commit = jest.fn()
+ const feed = { id: 1, title: 'feed' }
+
+ await (actions[FEED_ACTION_TYPES.FEED_MARK_READ] as any)({ commit }, { feed })
+ expect(FeedService.markRead).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.SET_FEED_ALL_READ, feed)
+ })
+
+ it('should commit MODIFY_FOLDER_UNREAD_COUNT with feed unreadCount if folderId exists on feed ', async () => {
+ ItemService.fetchFeedItems = jest.fn();
+ (ItemService.fetchFeedItems as any).mockResolvedValue({ data: { items: [{ id: 123 }] } })
+ FeedService.markRead = jest.fn()
+ const commit = jest.fn()
+ const feed = { id: 1, title: 'feed', folderId: 234, unreadCount: 2 }
+
+ await (actions[FEED_ACTION_TYPES.FEED_MARK_READ] as any)({ commit }, { feed })
+ expect(FeedService.markRead).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.SET_FEED_ALL_READ, feed)
+ expect(commit).toBeCalledWith(FOLDER_MUTATION_TYPES.MODIFY_FOLDER_UNREAD_COUNT, { folderId: 234, delta: -2 })
+ })
+ })
+
+ describe('FEED_SET_PINNED', () => {
+ it('should call FeedService.updateFeed and commit updated `pinned` property to state', async () => {
+ FeedService.updateFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_SET_PINNED] as any)({ commit }, { feed: { id: 1 }, pinned: true })
+ expect(FeedService.updateFeed).toBeCalledWith({ feedId: 1, pinned: true })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.UPDATE_FEED, { id: 1, pinned: true })
+ })
+ })
+
+ describe('FEED_SET_ORDERING', () => {
+ it('should call FeedService.updateFeed and commit updated `ordering` property to state', async () => {
+ FeedService.updateFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_SET_ORDERING] as any)({ commit }, { feed: { id: 1 }, ordering: FEED_ORDER.DEFAULT })
+ expect(FeedService.updateFeed).toBeCalledWith({ feedId: 1, ordering: FEED_ORDER.DEFAULT })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.UPDATE_FEED, { id: 1, ordering: FEED_ORDER.DEFAULT })
+ })
+ })
+
+ describe('FEED_SET_FULL_TEXT', () => {
+ it('should call FeedService.updateFeed and commit updated `fullTextEnabled` property to state', async () => {
+ FeedService.updateFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_SET_FULL_TEXT] as any)({ commit }, { feed: { id: 1 }, fullTextEnabled: true })
+ expect(FeedService.updateFeed).toBeCalledWith({ feedId: 1, fullTextEnabled: true })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.UPDATE_FEED, { id: 1, fullTextEnabled: true })
+ })
+ })
+
+ describe('FEED_SET_UPDATE_MODE', () => {
+ it('should call FeedService.updateFeed and commit updated `updateMode` property to state', async () => {
+ FeedService.updateFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_SET_UPDATE_MODE] as any)({ commit }, { feed: { id: 1 }, updateMode: FEED_UPDATE_MODE.IGNORE })
+ expect(FeedService.updateFeed).toBeCalledWith({ feedId: 1, updateMode: FEED_UPDATE_MODE.IGNORE })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.UPDATE_FEED, { id: 1, updateMode: FEED_UPDATE_MODE.IGNORE })
+ })
+ })
+
+ describe('FEED_SET_TITLE', () => {
+ it('should call FeedService.updateFeed and commit updated `title` property to state', async () => {
+ FeedService.updateFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_SET_TITLE] as any)({ commit }, { feed: { id: 1 }, title: 'newTitle' })
+ expect(FeedService.updateFeed).toBeCalledWith({ feedId: 1, title: 'newTitle' })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.UPDATE_FEED, { id: 1, title: 'newTitle' })
+ })
+ })
+
+ describe('FEED_DELETE', () => {
+ it('should call FeedService.deleteFeed and commit to state', async () => {
+ FeedService.deleteFeed = jest.fn()
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.FEED_DELETE] as any)({ commit }, { feed: { id: 1 } })
+ expect(FeedService.deleteFeed).toBeCalledWith({ feedId: 1 })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.FEED_DELETE, 1)
+ })
+ })
+
+ describe('MODIFY_FEED_UNREAD_COUNT', () => {
+ const state = {
+ feeds: [{ id: 1 }],
+ }
+ it('should commit to state', async () => {
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.MODIFY_FEED_UNREAD_COUNT] as any)({ commit, state }, { feedId: 1, delta: -2 })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.MODIFY_FEED_UNREAD_COUNT, { feedId: 1, delta: -2 })
+ })
+
+ it('should commit folder to state if feed has folderId', async () => {
+ const state = {
+ feeds: [{ id: 1, folderId: 234 }],
+ }
+ const commit = jest.fn()
+ await (actions[FEED_ACTION_TYPES.MODIFY_FEED_UNREAD_COUNT] as any)({ commit, state }, { feedId: 1, delta: -2 })
+ expect(commit).toBeCalledWith(FEED_MUTATION_TYPES.MODIFY_FEED_UNREAD_COUNT, { feedId: 1, delta: -2 })
+ expect(commit).toBeCalledWith(FOLDER_MUTATION_TYPES.MODIFY_FOLDER_UNREAD_COUNT, { folderId: 234, delta: -2 })
+ })
+ })
+ })
+
+ describe('mutations', () => {
+ describe('SET_FEEDS', () => {
+ it('should add feeds to state', () => {
+ const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState
+ let feeds = [] as any
+
+ mutations[FEED_MUTATION_TYPES.SET_FEEDS](state, feeds)
+ expect(state.feeds.length).toEqual(0)
+
+ feeds = [{ title: 'test' }] as Feed[]
+
+ mutations[FEED_MUTATION_TYPES.SET_FEEDS](state, feeds)
+ expect(state.feeds.length).toEqual(1)
+ expect(state.feeds[0]).toEqual(feeds[0])
+ })
+ })
+
+ describe('ADD_FEED', () => {
+ it('should add a single feed to state', () => {
+ const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState
+ const feed = { title: 'test' } as any
+
+ mutations[FEED_MUTATION_TYPES.ADD_FEED](state, feed)
+ expect(state.feeds.length).toEqual(1)
+ expect(state.feeds[0]).toEqual(feed)
+ })
+ })
+
+ describe('UPDATE_FEED', () => {
+ it('should update a feed in the state', () => {
+ const state = { feeds: [{ title: 'oldName', id: 1 }] as Feed[], folders: [] as any[] } as AppState
+ const feed = { title: 'test', id: 1 } as any
+
+ mutations[FEED_MUTATION_TYPES.UPDATE_FEED](state, feed)
+ expect(state.feeds[0].title).toEqual('test')
+ })
+ })
+
+ describe('SET_FEED_ALL_READ', () => {
+ it('should update a feed unreadCount to 0 in the state', () => {
+ const state = { feeds: [{ title: 'oldName', id: 1, unreadCount: 4 }] as Feed[], folders: [] as any[] } as AppState
+ const feed = { title: 'test', id: 1 } as any
+
+ mutations[FEED_MUTATION_TYPES.SET_FEED_ALL_READ](state, feed)
+ expect(state.feeds[0].unreadCount).toEqual(0)
+ })
+ })
+
+ describe('MODIFY_FEED_UNREAD_COUNT', () => {
+ it('should update a feed unreadCount to 0 in the state', () => {
+ const state = { feeds: [{ title: 'oldName', id: 1, unreadCount: 4 }] as Feed[], folders: [] as any[] } as AppState
+
+ mutations[FEED_MUTATION_TYPES.MODIFY_FEED_UNREAD_COUNT](state, { feedId: 1, delta: -1 } as any)
+ expect(state.feeds[0].unreadCount).toEqual(3)
+ })
+ })
+
+ describe('FEED_DELETE', () => {
+ it('should update a feed unreadCount to 0 in the state', () => {
+ const state = { feeds: [{ title: 'oldName', id: 1, unreadCount: 4 }] as Feed[], folders: [] as any[] } as AppState
+
+ mutations[FEED_MUTATION_TYPES.FEED_DELETE](state, 1 as any)
+ expect(state.feeds.length).toEqual(0)
+ })
+ })
+ })
+})
diff --git a/tests/javascript/unit/store/folder.spec.ts b/tests/javascript/unit/store/folder.spec.ts
new file mode 100644
index 000000000..8150ed28f
--- /dev/null
+++ b/tests/javascript/unit/store/folder.spec.ts
@@ -0,0 +1,106 @@
+import { Folder } from '../../../../src/types/Folder'
+import { AppState } from '../../../../src/store'
+import { FOLDER_ACTION_TYPES, mutations, actions } from '../../../../src/store/folder'
+import { FEED_MUTATION_TYPES, FOLDER_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
+import { FolderService } from '../../../../src/dataservices/folder.service'
+
+jest.mock('@nextcloud/router')
+
+describe('folder.ts', () => {
+ 'use strict'
+
+ describe('actions', () => {
+ it('FETCH_FOLDERS should call FolderService.fetchAllFolders and then commit folders returned to state', async () => {
+ FolderService.fetchAllFolders = jest.fn();
+ (FolderService.fetchAllFolders as any).mockResolvedValue({ data: { folders: [] } })
+
+ const commit = jest.fn()
+
+ await (actions[FOLDER_ACTION_TYPES.FETCH_FOLDERS] as any)({ commit })
+ expect(FolderService.fetchAllFolders).toBeCalled()
+ expect(commit).toBeCalled()
+ })
+
+ it('ADD_FOLDERS should call FolderService.createFolder and then commit the folders returned to state', async () => {
+ FolderService.createFolder = jest.fn();
+ (FolderService.createFolder as any).mockResolvedValue({ data: { folders: [] } })
+
+ const folder = {} as Folder
+ const commit = jest.fn()
+
+ await actions[FOLDER_ACTION_TYPES.ADD_FOLDERS]({ commit } as any, { folder })
+ expect(FolderService.createFolder).toBeCalled()
+ expect(commit).toBeCalled()
+ })
+
+ it('DELETE_FOLDER should call FolderService.deleteFolder and then commit deleted folder to state', async () => {
+ FolderService.deleteFolder = jest.fn();
+ (FolderService.deleteFolder as any).mockResolvedValue()
+
+ const folder = {} as Folder
+ const commit = jest.fn()
+
+ await actions[FOLDER_ACTION_TYPES.DELETE_FOLDER]({ commit } as any, { folder })
+ expect(FolderService.deleteFolder).toBeCalled()
+ expect(commit).toBeCalled()
+ })
+
+ it('FOLDER_SET_NAME should call FolderService.renameFolder and then commit deleted folder to state', async () => {
+ FolderService.renameFolder = jest.fn();
+ (FolderService.renameFolder as any).mockResolvedValue()
+
+ const folder = {} as Folder
+ const commit = jest.fn()
+
+ await actions[FOLDER_ACTION_TYPES.FOLDER_SET_NAME]({ commit } as any, { folder, name: 'newName' } as any)
+ expect(FolderService.renameFolder).toBeCalledWith({ id: folder.id, name: 'newName' })
+ expect(commit).toBeCalled()
+ })
+ })
+
+ describe('mutations', () => {
+ it('SET_FOLDERS should add the passed in folders to the state', () => {
+ const state = { folders: [] as Folder[] } as AppState
+ let folders = [] as Folder[]
+
+ (mutations[FOLDER_MUTATION_TYPES.SET_FOLDERS] as any)(state, folders)
+ expect(state.folders.length).toEqual(0)
+
+ folders = [{ name: 'test' }] as Folder[]
+
+ (mutations[FOLDER_MUTATION_TYPES.SET_FOLDERS] as any)(state, folders)
+ expect(state.folders.length).toEqual(1)
+ expect(state.folders[0]).toEqual(folders[0])
+ })
+
+ it('DELETE_FOLDER should remove the passed in folder from the state', () => {
+ const state = { folders: [{ name: 'test' }] as Folder[] } as AppState
+ const folders = [state.folders[0]] as Folder[]
+
+ (mutations[FOLDER_MUTATION_TYPES.DELETE_FOLDER] as any)(state, folders)
+ expect(state.folders.length).toEqual(0)
+ })
+
+ it('UPDATE_FOLDER should update the folder properties in the state', () => {
+ const state = { folders: [{ name: 'test', id: 123 }] as Folder[] } as AppState
+ const newFolder = { id: 123, name: 'newName' };
+
+ (mutations[FOLDER_MUTATION_TYPES.UPDATE_FOLDER] as any)(state, newFolder)
+ expect(state.folders[0].name).toEqual('newName')
+ })
+
+ it('MODIFY_FOLDER_UNREAD_COUNT should update the folder feedCount in the state based on the delta', () => {
+ const state = { folders: [{ name: 'test', id: 123, feedCount: 10 }] as Folder[] } as AppState
+
+ (mutations[FOLDER_MUTATION_TYPES.MODIFY_FOLDER_UNREAD_COUNT] as any)(state, { folderId: 123, delta: -3 })
+ expect(state.folders[0].feedCount).toEqual(7)
+ })
+
+ it('SET_FEED_ALL_READ should update the folder feedCount in the state based on the feed unreadCount', () => {
+ const state = { folders: [{ name: 'test', id: 123, feedCount: 10 }] as Folder[] } as AppState
+
+ (mutations[FEED_MUTATION_TYPES.SET_FEED_ALL_READ] as any)(state, { id: 1, folderId: 123, unreadCount: 2 })
+ expect(state.folders[0].feedCount).toEqual(8)
+ })
+ })
+})
diff --git a/tests/javascript/unit/store/item.spec.ts b/tests/javascript/unit/store/item.spec.ts
new file mode 100644
index 000000000..f194ad223
--- /dev/null
+++ b/tests/javascript/unit/store/item.spec.ts
@@ -0,0 +1,243 @@
+import { AppState } from '../../../../src/store'
+import { FEED_ITEM_ACTION_TYPES, mutations, actions } from '../../../../src/store/item'
+
+import { FEED_ITEM_MUTATION_TYPES, FEED_MUTATION_TYPES } from '../../../../src/types/MutationTypes'
+import { ItemService } from '../../../../src/dataservices/item.service'
+
+describe('item.ts', () => {
+ 'use strict'
+
+ describe('actions', () => {
+ describe('FETCH_UNREAD', () => {
+ it('should call ItemService and commit items to state', async () => {
+ const fetchMock = jest.fn()
+ fetchMock.mockResolvedValue({ data: { items: [{ id: 123 }] } })
+ ItemService.debounceFetchUnread = fetchMock as any
+ const commit = jest.fn()
+
+ await (actions[FEED_ITEM_ACTION_TYPES.FETCH_UNREAD] as any)({ commit })
+
+ expect(fetchMock).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, [{ id: 123 }])
+ })
+ })
+
+ describe('FETCH_STARRED', () => {
+ it('should call ItemService and commit items and starred count to state', async () => {
+ const fetchMock = jest.fn()
+ fetchMock.mockResolvedValue({ data: { items: [{ id: 123 }], starred: 3 } })
+ ItemService.debounceFetchStarred = fetchMock as any
+ const commit = jest.fn()
+
+ await (actions[FEED_ITEM_ACTION_TYPES.FETCH_STARRED] as any)({ commit })
+
+ expect(fetchMock).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, [{ id: 123 }])
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_STARRED_COUNT, 3)
+ })
+ })
+
+ describe('FETCH_FEED_ITEMS', () => {
+ it('should call ItemService and commit items to state', async () => {
+ const mockItems = [{ id: 123, title: 'feed item' }]
+ const fetchMock = jest.fn()
+ fetchMock.mockResolvedValue({ data: { items: mockItems } })
+ ItemService.debounceFetchFeedItems = fetchMock as any
+ const commit = jest.fn()
+
+ await (actions[FEED_ITEM_ACTION_TYPES.FETCH_FEED_ITEMS] as any)({ commit }, { feedId: 123 })
+
+ expect(fetchMock).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, mockItems)
+ })
+ })
+
+ describe('FETCH_FOLDER_FEED_ITEMS', () => {
+ it('should call ItemService and commit items to state', async () => {
+ const mockItems = [{ id: 123, title: 'feed item' }]
+ const fetchMock = jest.fn()
+ fetchMock.mockResolvedValue({ data: { items: mockItems } })
+ ItemService.debounceFetchFolderFeedItems = fetchMock as any
+ const commit = jest.fn()
+
+ await (actions[FEED_ITEM_ACTION_TYPES.FETCH_FOLDER_FEED_ITEMS] as any)({ commit }, { feedId: 123 })
+
+ expect(fetchMock).toBeCalled()
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.SET_ITEMS, mockItems)
+ })
+ })
+
+ it('MARK_READ should call GET and commit returned feeds to state', async () => {
+ const item = { id: 1, feedId: 123, unread: true }
+ const commit = jest.fn()
+ const dispatch = jest.fn()
+ const serviceMock = jest.fn()
+ ItemService.markRead = serviceMock
+
+ await (actions[FEED_ITEM_ACTION_TYPES.MARK_READ] as any)({ commit, dispatch }, { item })
+
+ expect(serviceMock).toBeCalledWith(item, true)
+ expect(commit).toBeCalled()
+ expect(dispatch).toBeCalledWith(FEED_MUTATION_TYPES.MODIFY_FEED_UNREAD_COUNT, { feedId: 123, delta: -1 })
+ })
+
+ it('MARK_UNREAD should call GET and commit returned feeds to state', async () => {
+ const item = { id: 1, feedId: 123 }
+ const commit = jest.fn()
+ const dispatch = jest.fn()
+ const serviceMock = jest.fn()
+ ItemService.markRead = serviceMock
+
+ await (actions[FEED_ITEM_ACTION_TYPES.MARK_UNREAD] as any)({ commit, dispatch }, { item })
+
+ expect(serviceMock).toBeCalledWith(item, false)
+ expect(commit).toBeCalledWith(FEED_ITEM_MUTATION_TYPES.UPDATE_ITEM, { item })
+ expect(dispatch).toBeCalledWith(FEED_MUTATION_TYPES.MODIFY_FEED_UNREAD_COUNT, { feedId: 123, delta: 1 })
+ })
+
+ it('STAR_ITEM should call GET and commit returned feeds to state', async () => {
+ const item = { id: 1 }
+ const commit = jest.fn()
+ const serviceMock = jest.fn()
+ ItemService.markStarred = serviceMock
+
+ await (actions[FEED_ITEM_ACTION_TYPES.STAR_ITEM] as any)({ commit }, { item })
+
+ expect(serviceMock).toBeCalledWith(item, true)
+ expect(commit).toBeCalled()
+ })
+
+ it('UNSTAR_ITEM should call GET and commit returned feeds to state', async () => {
+ const item = { id: 1 }
+ const commit = jest.fn()
+ const serviceMock = jest.fn()
+ ItemService.markStarred = serviceMock
+
+ await (actions[FEED_ITEM_ACTION_TYPES.UNSTAR_ITEM] as any)({ commit }, { item })
+
+ expect(serviceMock).toBeCalledWith(item, false)
+ expect(commit).toBeCalled()
+ })
+ })
+
+ describe('mutations', () => {
+ describe('SET_SELECTED_ITEM', () => {
+ it('should update selectedId on state', async () => {
+ const state = { selectedId: undefined } as any
+ const item = { id: 123 } as any
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_SELECTED_ITEM](state, item as any)
+ expect(state.selectedId).toEqual(123)
+ })
+ })
+
+ describe('SET_PLAYING_ITEM', () => {
+ it('should update selectedId on state', async () => {
+ const state = { playingItem: undefined } as any
+ const item = { id: 123 } as any
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_PLAYING_ITEM](state, item as any)
+ expect(state.playingItem).toEqual(item)
+ })
+ })
+
+ describe('SET_ITEMS', () => {
+ it('should add feeds to state', () => {
+ const state = { allItems: [] as any } as any
+ let items = [] as any
+
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(0)
+
+ items = [{ title: 'test', id: 123 }]
+
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(1)
+ expect(state.allItems[0]).toEqual(items[0])
+
+ items = [{ title: 'test2', id: 234 }]
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(2)
+ })
+
+ it('should not add duplicates', () => {
+ const state = { allItems: [] as any } as any
+ let items = [{ title: 'test', id: 123 }] as any
+
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(1)
+ expect(state.allItems[0]).toEqual(items[0])
+
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(1)
+ expect(state.allItems[0]).toEqual(items[0])
+
+ items = [{ title: 'test2', id: 234 }]
+ mutations[FEED_ITEM_MUTATION_TYPES.SET_ITEMS](state, items)
+ expect(state.allItems.length).toEqual(2)
+ })
+ })
+
+ describe('SET_STARRED_COUNT', () => {
+ it('should add a single feed to state', () => {
+ const state = { } as AppState
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_STARRED_COUNT] as any)(state, 13)
+ expect(state.starredCount).toEqual(13)
+ })
+ })
+
+ describe('SET_UNREAD_COUNT', () => {
+ it('should set unreadCount with value passed in', () => {
+ const state = { unreadCount: 0 } as AppState
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_UNREAD_COUNT] as any)(state, 123)
+ expect(state.unreadCount).toEqual(123)
+ })
+ })
+
+ describe('UPDATE_ITEM', () => {
+ it('should add a single feed to state', () => {
+ const state = { allItems: [{ id: 1, title: 'abc' }] as any } as AppState
+ const item = { title: 'test', id: 1 } as any
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.UPDATE_ITEM] as any)(state, { item })
+ expect(state.allItems[0]).toEqual(item)
+ })
+ })
+
+ describe('SET_FETCHING', () => {
+ it('should set fetchingItems value with key passed in', () => {
+ const state = { fetchingItems: {} } as AppState
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_FETCHING] as any)(state, { fetching: true, key: 'starred' })
+ expect(state.fetchingItems.starred).toEqual(true);
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_FETCHING] as any)(state, { fetching: false, key: 'starred' })
+ expect(state.fetchingItems.starred).toEqual(false)
+ })
+ })
+
+ describe('SET_ALL_LOADED', () => {
+ it('should set allItemsLoaded value with key passed in', () => {
+ const state = { allItemsLoaded: {} } as AppState
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_ALL_LOADED] as any)(state, { loaded: true, key: 'starred' })
+ expect(state.allItemsLoaded.starred).toEqual(true);
+
+ (mutations[FEED_ITEM_MUTATION_TYPES.SET_ALL_LOADED] as any)(state, { loaded: false, key: 'starred' })
+ expect(state.allItemsLoaded.starred).toEqual(false)
+ })
+ })
+
+ describe('SET_FEED_ALL_READ', () => {
+ it('should set allItems with feedId as read', () => {
+ const state = { allItems: [{ id: 1, feedId: 123, unread: true }, { id: 2, feedId: 345, unread: true }] } as any
+
+ (mutations[FEED_MUTATION_TYPES.SET_FEED_ALL_READ] as any)(state, { id: 123 })
+ expect(state.allItems[0].unread).toEqual(false);
+
+ (mutations[FEED_MUTATION_TYPES.SET_FEED_ALL_READ] as any)(state, { id: 345 })
+ expect(state.allItems[1].unread).toEqual(false)
+ })
+ })
+ })
+})