diff options
Diffstat (limited to 'tests/javascript/unit/store/feed.spec.ts')
-rw-r--r-- | tests/javascript/unit/store/feed.spec.ts | 150 |
1 files changed, 148 insertions, 2 deletions
diff --git a/tests/javascript/unit/store/feed.spec.ts b/tests/javascript/unit/store/feed.spec.ts index db4431b20..ede4307d1 100644 --- a/tests/javascript/unit/store/feed.spec.ts +++ b/tests/javascript/unit/store/feed.spec.ts @@ -1,9 +1,10 @@ import { Feed } from '../../../../src/types/Feed' import { AppState } from '../../../../src/store' import { FEED_ACTION_TYPES, mutations, actions } from '../../../../src/store/feed' -import { FeedService } from '../../../../src/dataservices/feed.service' +import { FEED_ORDER, FEED_UPDATE_MODE, FeedService } from '../../../../src/dataservices/feed.service' -import { FEED_ITEM_MUTATION_TYPES, FEED_MUTATION_TYPES } from '../../../../src/types/MutationTypes' +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' @@ -42,6 +43,113 @@ describe('feed.ts', () => { }) }) + 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', () => { @@ -71,5 +179,43 @@ describe('feed.ts', () => { 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) + }) + }) }) }) |