From dce0701f73b4dcba4ce6efd0b80aadfc4f97c7ce Mon Sep 17 00:00:00 2001 From: Devlin Junker Date: Sun, 3 Sep 2023 18:08:21 -0700 Subject: add unit tests and little more cleanup of uncessary lines Signed-off-by: Devlin Junker --- .../javascript/unit/components/routes/All.spec.ts | 65 ++++++++++++++++ .../unit/components/routes/Folder.spec.ts | 89 ++++++++++++++++++++++ .../javascript/unit/services/item.service.spec.ts | 33 +++++++- tests/javascript/unit/store/item.spec.ts | 15 ++++ 4 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 tests/javascript/unit/components/routes/All.spec.ts create mode 100644 tests/javascript/unit/components/routes/Folder.spec.ts (limited to 'tests') diff --git a/tests/javascript/unit/components/routes/All.spec.ts b/tests/javascript/unit/components/routes/All.spec.ts new file mode 100644 index 000000000..ff9b2b169 --- /dev/null +++ b/tests/javascript/unit/components/routes/All.spec.ts @@ -0,0 +1,65 @@ +import Vuex, { Store } from 'vuex' +import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils' + +import All from '../../../../../src/components/routes/All.vue' +import FeedItemDisplayList from '../../../../../src/components/feed-display/FeedItemDisplayList.vue' + +jest.mock('@nextcloud/axios') + +describe('All.vue', () => { + 'use strict' + const localVue = createLocalVue() + localVue.use(Vuex) + let wrapper: Wrapper + + const mockItem = { + feedId: 1, + title: 'feed item', + pubDate: Date.now() / 1000, + } + + let store: Store + beforeAll(() => { + store = new Vuex.Store({ + state: { + items: { + fetchingItems: { + all: false, + }, + }, + }, + actions: { + }, + getters: { + allItems: () => [mockItem, mockItem, mockItem], + }, + }) + + store.dispatch = jest.fn() + store.commit = jest.fn() + + wrapper = shallowMount(All, { + propsData: { + item: mockItem, + }, + localVue, + store, + }) + }) + + it('should get all items from state', () => { + expect((wrapper.findComponent(FeedItemDisplayList)).props().items.length).toEqual(3) + }) + + it('should dispatch FETCH_ITEMS action if not fetchingItems.all', () => { + (wrapper.vm as any).$store.state.items.fetchingItems.all = true; + + (wrapper.vm as any).fetchMore() + expect(store.dispatch).not.toBeCalled(); + + (wrapper.vm as any).$store.state.items.fetchingItems.all = false; + + (wrapper.vm as any).fetchMore() + expect(store.dispatch).toBeCalled() + }) +}) diff --git a/tests/javascript/unit/components/routes/Folder.spec.ts b/tests/javascript/unit/components/routes/Folder.spec.ts new file mode 100644 index 000000000..c2c966adb --- /dev/null +++ b/tests/javascript/unit/components/routes/Folder.spec.ts @@ -0,0 +1,89 @@ +import Vuex, { Store } from 'vuex' +import { shallowMount, createLocalVue, Wrapper } from '@vue/test-utils' + +import Folder from '../../../../../src/components/routes/Folder.vue' +import FeedItemDisplayList from '../../../../../src/components/feed-display/FeedItemDisplayList.vue' + +jest.mock('@nextcloud/axios') + +describe('Folder.vue', () => { + 'use strict' + const localVue = createLocalVue() + localVue.use(Vuex) + let wrapper: Wrapper + + const mockFeed = { + id: 789, + title: 'feed name', + unreadCount: 2, + folderId: 123, + } + + const mockFeed2 = { + id: 456, + title: 'feed name 2', + unreadCount: 2, + folderId: 123, + } + + const mockFolder = { + id: 123, + name: 'folder name', + } + + let store: Store + beforeAll(() => { + store = new Vuex.Store({ + state: { + items: { + fetchingItems: { + 'folder-123': false, + }, + allItems: [{ + feedId: 789, + title: 'feed item', + }, { + feedId: 456, + title: 'feed item 2', + }], + }, + }, + actions: { + }, + getters: { + feeds: () => [mockFeed, mockFeed2], + folders: () => [mockFolder], + }, + }) + + store.dispatch = jest.fn() + store.commit = jest.fn() + + wrapper = shallowMount(Folder, { + propsData: { + folderId: '123', + }, + mocks: { + $route: { + params: {}, + }, + }, + localVue, + store, + }) + }) + + it('should display feed title and unread count', () => { + expect(wrapper.find('.header').text()).toContain(mockFolder.name) + expect(wrapper.find('.header').text()).toContain((mockFeed.unreadCount + mockFeed2.unreadCount).toString()) + }) + + it('should get folder items from state', () => { + expect((wrapper.findComponent(FeedItemDisplayList)).props().items.length).toEqual(2) + }) + + it('should dispatch FETCH_FOLDER_FEED_ITEMS action on fetchMore', () => { + (wrapper.vm as any).fetchMore() + expect(store.dispatch).toBeCalled() + }) +}) diff --git a/tests/javascript/unit/services/item.service.spec.ts b/tests/javascript/unit/services/item.service.spec.ts index 005248709..66a5b5484 100644 --- a/tests/javascript/unit/services/item.service.spec.ts +++ b/tests/javascript/unit/services/item.service.spec.ts @@ -11,6 +11,20 @@ describe('item.service.ts', () => { (axios.post as any).mockReset() }) + describe('fetchAll', () => { + it('should call GET with offset set to start param, ALL item type', async () => { + (axios as any).get.mockResolvedValue({ data: { feeds: [] } }) + + await ItemService.fetchAll(0) + + expect(axios.get).toBeCalled() + const queryParams = (axios.get as any).mock.calls[0][1].params + + expect(queryParams.offset).toEqual(0) + expect(queryParams.type).toEqual(ITEM_TYPES.ALL) + }) + }) + describe('fetchStarred', () => { it('should call GET with offset set to start param and STARRED item type', async () => { (axios as any).get.mockResolvedValue({ data: { feeds: [] } }) @@ -40,7 +54,7 @@ describe('item.service.ts', () => { }) describe('fetchFeedItems', () => { - it('should call GET with offset set to start param, UNREAD item type, and id set to feedId', async () => { + it('should call GET with offset set to start param, FEED item type, and id set to feedId', async () => { (axios as any).get.mockResolvedValue({ data: { feeds: [] } }) await ItemService.fetchFeedItems(123, 0) @@ -50,7 +64,22 @@ describe('item.service.ts', () => { expect(queryParams.id).toEqual(123) expect(queryParams.offset).toEqual(0) - expect(queryParams.type).toEqual(ITEM_TYPES.ALL) + expect(queryParams.type).toEqual(ITEM_TYPES.FEED) + }) + }) + + describe('fetchFolderItems', () => { + it('should call GET with offset set to start param, FOLDER item type, and id set to folderId', async () => { + (axios as any).get.mockResolvedValue({ data: { feeds: [] } }) + + await ItemService.fetchFolderItems(123, 0) + + expect(axios.get).toBeCalled() + const queryParams = (axios.get as any).mock.calls[0][1].params + + expect(queryParams.id).toEqual(123) + expect(queryParams.offset).toEqual(0) + expect(queryParams.type).toEqual(ITEM_TYPES.FOLDER) }) }) diff --git a/tests/javascript/unit/store/item.spec.ts b/tests/javascript/unit/store/item.spec.ts index 7866cff5b..8d22d69a1 100644 --- a/tests/javascript/unit/store/item.spec.ts +++ b/tests/javascript/unit/store/item.spec.ts @@ -52,6 +52,21 @@ describe('item.ts', () => { }) }) + 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 } const commit = jest.fn() -- cgit v1.2.3