From 9377e89a2eb3b0025b6eafabdcbea5bc9dd07c68 Mon Sep 17 00:00:00 2001 From: Devlin Junker Date: Mon, 7 Aug 2023 17:50:04 -0700 Subject: finished unit tests Signed-off-by: Devlin Junker --- tests/javascript/helpers/CSSStub.js | 0 tests/javascript/unit/components/AddFeed.spec.ts | 79 +++++++++++++++++++++--- tests/javascript/unit/store/feed.spec.ts | 71 ++++++++++++--------- 3 files changed, 114 insertions(+), 36 deletions(-) create mode 100644 tests/javascript/helpers/CSSStub.js (limited to 'tests') diff --git a/tests/javascript/helpers/CSSStub.js b/tests/javascript/helpers/CSSStub.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/javascript/unit/components/AddFeed.spec.ts b/tests/javascript/unit/components/AddFeed.spec.ts index 66105e3f8..ce1a9581d 100644 --- a/tests/javascript/unit/components/AddFeed.spec.ts +++ b/tests/javascript/unit/components/AddFeed.spec.ts @@ -1,23 +1,86 @@ import { shallowMount, createLocalVue } from '@vue/test-utils' - import AddFeed from '../../../../src/components/AddFeed.vue' +import { FEED_ACTION_TYPES } from '@/store/feed' describe('AddFeed.vue', () => { 'use strict' - it('should initialize without showing createNewFolder', () => { + let mockDispatch = jest.fn(); + let mockStore = { + state: { + folders: { folders: [] }, + feeds: { feeds: [] } + }, + dispatch: mockDispatch + } + + let wrapper: any + beforeEach(() => { const localVue = createLocalVue() - const wrapper = shallowMount(AddFeed, { + wrapper = shallowMount(AddFeed, { localVue, mocks: { - $store: { - state: { - folders: [], - }, - }, + $store: mockStore, }, }) + }) + it('should initialize with default values', () => { expect(wrapper.vm.$data.createNewFolder).toBeFalsy() + expect(wrapper.vm.$data.autoDiscover).toBeTruthy() + expect(wrapper.vm.$data.withBasicAuth).toBeFalsy() + }) + + it('should dispatch ADD_FEED action to store', async () => { + wrapper.vm.$emit = jest.fn() + + await wrapper.vm.addFeed() + + expect(wrapper.vm.$emit).toBeCalled() + expect(mockDispatch).toBeCalled() + expect(mockDispatch.mock.calls[0][0]).toEqual(FEED_ACTION_TYPES.ADD_FEED); + }) + + it('should check if feed url exists and return true', () => { + wrapper.vm.$data.feedUrl = '' + let response = wrapper.vm.feedUrlExists() + + expect(response).toBeFalsy() + + wrapper.vm.$data.feedUrl = 'http://test.com' + response = wrapper.vm.feedUrlExists() + + expect(response).toBeFalsy() + + wrapper.vm.$data.feedUrl = 'http://test.com' + wrapper.vm.$store.state.feeds.feeds = [{ url: 'http://test.com'}] + response = wrapper.vm.feedUrlExists() + + expect(response).toBeTruthy() + }) + + it('should check if folder name exists when creating folder and return true', () => { + wrapper.vm.$data.newFolderName = '' + let response = wrapper.vm.folderNameExists() + + expect(response).toBeFalsy() + + wrapper.vm.$data.newFolderName = 'test' + response = wrapper.vm.folderNameExists() + + expect(response).toBeFalsy() + + wrapper.vm.$data.newFolderName = 'test' + wrapper.vm.$store.state.folders.folders = [{ name: 'test'}] + response = wrapper.vm.folderNameExists() + + expect(response).toBeFalsy() + + wrapper.vm.$data.newFolderName = 'test' + wrapper.vm.$data.createNewFolder = 'test' + wrapper.vm.$store.state.folders.folders = [{ name: 'test'}] + response = wrapper.vm.folderNameExists() + + expect(response).toBeTruthy() }) }) diff --git a/tests/javascript/unit/store/feed.spec.ts b/tests/javascript/unit/store/feed.spec.ts index 29df4b105..928820085 100644 --- a/tests/javascript/unit/store/feed.spec.ts +++ b/tests/javascript/unit/store/feed.spec.ts @@ -9,14 +9,27 @@ describe('feed.ts', () => { 'use strict' describe('actions', () => { - it('ADD_FEED should call POST and commit feed to state', async () => { - (axios as any).post.mockResolvedValue() - const commit = jest.fn() - await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit }, { feedReq: { url: '' } }) - expect(axios.post).toBeCalled() - expect(commit).toBeCalled() + describe('ADD_FEED', () => { + it('should call POST and commit feed to state', async () => { + (axios as any).post.mockResolvedValue({ data: { feeds: [] }}) + const commit = jest.fn() + await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit }, { feedReq: { url: '' } }) + expect(axios.post).toBeCalled() + expect(commit).toBeCalled() + }) + + it('should call POST and not call commit if error', async () => { + (axios as any).post.mockRejectedValue() + const commit = jest.fn() + await actions[FEED_ACTION_TYPES.ADD_FEED]({ commit }, { feedReq: { url: '' } }) + expect(axios.post).toBeCalled() + + expect(commit).not.toBeCalled() + }) }) + + it('FETCH_FEEDS should call GET and commit returned feeds to state', async () => { (axios as any).get.mockResolvedValue({ data: { feeds: [] } }) const commit = jest.fn() @@ -27,29 +40,31 @@ describe('feed.ts', () => { }) describe('mutations', () => { - it('SET_FEEDS should add feeds to state', () => { - const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState - let feeds = [] as Feed[] - - 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('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]) + }) }) - - it('SET_FEEDS should add feeds and unreadCount to folder if exists and folder set', () => { - const state = { feeds: [] as Feed[], folders: [{ id: 1, feedCount: 3, feeds: [] as Feed[] }] } as AppState - const feeds = [{ title: 'test', folderId: 1, unreadCount: 2 }] as Feed[] - - mutations[FEED_MUTATION_TYPES.SET_FEEDS](state, feeds) - expect(state.feeds.length).toEqual(1) - expect(state.feeds[0]).toEqual(feeds[0]) - expect(state.folders[0].feeds[0]).toEqual(feeds[0]) - expect(state.folders[0].feedCount).toEqual(5) + + describe('ADD_FEED', () => { + it('should add a single feed to state', () => { + const state = { feeds: [] as Feed[], folders: [] as any[] } as AppState + let 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) + }) }) }) }) -- cgit v1.2.3