From 991facf2ee638713d2f2ef8b5197f32742d01804 Mon Sep 17 00:00:00 2001 From: Devlin Junker Date: Thu, 5 Oct 2023 10:27:26 -0700 Subject: tests and some cleanup Signed-off-by: Devlin Junker --- src/App.vue | 1 - src/main.js | 24 +++++++++++--------- src/store/app.ts | 2 +- src/store/feed.ts | 8 +++++-- src/store/folder.ts | 8 +++++-- src/store/index.ts | 13 ++++------- tests/javascript/unit/components/App.spec.ts | 9 ++++++++ .../feed-display/FeedItemDisplay.spec.ts | 7 ------ tests/javascript/unit/store/app.spec.ts | 26 ++++++++++++++++++++++ 9 files changed, 66 insertions(+), 32 deletions(-) create mode 100644 tests/javascript/unit/store/app.spec.ts diff --git a/src/App.vue b/src/App.vue index cd1e3e06d..9f877a6c5 100644 --- a/src/App.vue +++ b/src/App.vue @@ -69,7 +69,6 @@ export default Vue.extend({ }, stopVideo() { const videoElements = document.getElementsByTagName('video') - for (let i = 0; i < videoElements.length; i++) { videoElements[i].pause() } diff --git a/src/main.js b/src/main.js index 98a56a743..9a70d470f 100644 --- a/src/main.js +++ b/src/main.js @@ -21,19 +21,23 @@ Vue.directive('tooltip', Tooltip) const store = new Store(mainStore) -axios.interceptors.response(undefined /* onSuccessCallback is intentionally undefined */, - // Any status codes that falls outside the range of 2xx cause this function to trigger - function(error) { +/** + * Handles errors returned during application runtime + * + * @param {Error} error Error thrown + * @return Promise + */ +const handleErrors = function(error) { + store.commit(MUTATIONS.SET_ERROR, error) + return Promise.reject(error) +} - store.commit(MUTATIONS.SET_ERROR, error) - return Promise.reject(error) - }, +axios.interceptors.response.use(undefined /* onSuccessCallback is intentionally undefined (triggers on 2xx responses) */, + // Any status codes that falls outside the range of 2xx cause this function to trigger + handleErrors, ) -Vue.config.errorHandler = function(err) { - store.commit(MUTATIONS.SET_ERROR, err) -} - +Vue.config.errorHandler = handleErrors export default new Vue({ router, store, diff --git a/src/store/app.ts b/src/store/app.ts index 5b7f4c8dc..2b470c946 100644 --- a/src/store/app.ts +++ b/src/store/app.ts @@ -4,7 +4,7 @@ export const APPLICATION_ACTION_TYPES = { SET_ERROR_MESSAGE: 'SET_ERROR_MESSAGE', } -type AppInfoState = { +export type AppInfoState = { error?: Error; } diff --git a/src/store/feed.ts b/src/store/feed.ts index 9fea76a72..2a62ff5fe 100644 --- a/src/store/feed.ts +++ b/src/store/feed.ts @@ -23,12 +23,16 @@ export const FEED_ACTION_TYPES = { FEED_DELETE: 'FEED_DELETE', } -const state = { +export type FeedState = { + feeds: Feed[] +} + +const state: FeedState = { feeds: [], } const getters = { - feeds(state: AppState) { + feeds(state: FeedState) { return state.feeds }, } diff --git a/src/store/folder.ts b/src/store/folder.ts index 680eab097..d125c5b99 100644 --- a/src/store/folder.ts +++ b/src/store/folder.ts @@ -14,12 +14,16 @@ export const FOLDER_ACTION_TYPES = { FOLDER_SET_NAME: 'FOLDER_SET_NAME', } -const state = { +export type FolderState = { + folders: Folder[] +} + +const state: FolderState = { folders: [], } const getters = { - folders(state: AppState) { + folders(state: FolderState) { return state.folders }, } diff --git a/src/store/index.ts b/src/store/index.ts index 2eea645d9..44f933740 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,10 +1,8 @@ -import { Folder } from '../types/Folder' -import { Feed } from '../types/Feed' -import feeds, { FEED_ACTION_TYPES } from './feed' -import folders, { FOLDER_ACTION_TYPES } from './folder' import { FOLDER_MUTATION_TYPES, FEED_MUTATION_TYPES, FEED_ITEM_MUTATION_TYPES, APPLICATION_MUTATION_TYPES } from '../types/MutationTypes' +import feeds, { FEED_ACTION_TYPES, FeedState } from './feed' +import folders, { FOLDER_ACTION_TYPES, FolderState } from './folder' import items, { FEED_ITEM_ACTION_TYPES, ItemState } from './item' -import app, { APPLICATION_ACTION_TYPES } from './app' +import app, { APPLICATION_ACTION_TYPES, AppInfoState } from './app' export const MUTATIONS = { ...APPLICATION_MUTATION_TYPES, @@ -20,10 +18,7 @@ export const ACTIONS = { ...FEED_ITEM_ACTION_TYPES, } -export type AppState = { - feeds: Feed[]; - folders: Folder[]; -} & ItemState; +export type AppState = FolderState & FeedState & ItemState & AppInfoState; type Func = (name: string, value: unknown) => void; export type ActionParams = { commit: Func; dispatch: Func; state: AppState }; diff --git a/tests/javascript/unit/components/App.spec.ts b/tests/javascript/unit/components/App.spec.ts index 135b41d40..f1c4096b9 100644 --- a/tests/javascript/unit/components/App.spec.ts +++ b/tests/javascript/unit/components/App.spec.ts @@ -19,6 +19,9 @@ describe('FeedItemDisplay.vue', () => { items: { playingItem: undefined, }, + app: { + error: undefined, + }, }, dispatch: dispatchStub, commit: commitStub, @@ -46,4 +49,10 @@ describe('FeedItemDisplay.vue', () => { expect(pauseStub).toBeCalled() }) + + it('should remove app state error with commit and undefined', () => { + (wrapper.vm as any).removeError() + + expect(commitStub).toBeCalledWith(MUTATIONS.SET_ERROR, undefined) + }) }) diff --git a/tests/javascript/unit/components/feed-display/FeedItemDisplay.spec.ts b/tests/javascript/unit/components/feed-display/FeedItemDisplay.spec.ts index 7d60d4a01..81c171114 100644 --- a/tests/javascript/unit/components/feed-display/FeedItemDisplay.spec.ts +++ b/tests/javascript/unit/components/feed-display/FeedItemDisplay.spec.ts @@ -59,13 +59,6 @@ describe('FeedItemDisplay.vue', () => { expect(formattedDate).toEqual(new Date(epoch).toLocaleString()) }) - it('should format datetime to match international standard', () => { - const epoch = Date.now() // Provide an epoch timestamp - const formattedDate = (wrapper.vm as any).formatDatetime(epoch) - - expect(formattedDate).toEqual(new Date(epoch).toISOString()) - }) - it('should retrieve feed by ID', () => { const feed = (wrapper.vm as any).getFeed(mockFeed.id) 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) + }) + }) +}) -- cgit v1.2.3