From bfa362fec5d6a472f10b0e4a4b812ef9751a2492 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Mon, 31 Dec 2018 00:03:36 +0900 Subject: [PATCH] refs #209 Add unit/integration tests for GlobalHeader --- package.json | 1 + spec/integration/store/GlobalHeader.spec.js | 97 +++++++++++++++++++ spec/mock/electron.js | 7 +- spec/mock/router.js | 3 + src/renderer/components/GlobalHeader.vue | 2 +- .../components/TimelineSpace/SideMenu.vue | 4 +- src/renderer/store/GlobalHeader.js | 22 +++-- 7 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 spec/integration/store/GlobalHeader.spec.js create mode 100644 spec/mock/router.js diff --git a/package.json b/package.json index 40322aa0..b4417d81 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ }, "jest": { "moduleNameMapper": { + "@/router": "/spec/mock/router.js", "^@/(.+)": "/src/renderer/$1", "^~/(.+)": "/$1", "electron": "/spec/mock/electron.js" diff --git a/spec/integration/store/GlobalHeader.spec.js b/spec/integration/store/GlobalHeader.spec.js new file mode 100644 index 00000000..60f2c254 --- /dev/null +++ b/spec/integration/store/GlobalHeader.spec.js @@ -0,0 +1,97 @@ +import { createLocalVue } from '@vue/test-utils' +import Vuex from 'vuex' +import { ipcMain } from '~/spec/mock/electron' +import GlobalHeader from '~/src/renderer/store/GlobalHeader' + +const state = { + accounts: [], + changing: false, + hide: false +} + +const initState = { + namespaced: true, + state: state, + actions: GlobalHeader.actions, + mutations: GlobalHeader.mutations +} + +const routerState = { + namespaced: true, + state: { + params: { + id: 'account_id' + } + } +} + +describe('GlobalHeader', () => { + let store + let localVue + + beforeEach(() => { + localVue = createLocalVue() + localVue.use(Vuex) + store = new Vuex.Store({ + modules: { + GlobalHeader: initState, + route: routerState + } + }) + }) + + describe('listAccounts', () => { + beforeEach(() => { + ipcMain.once('list-accounts', (event, _) => { + event.sender.send('response-list-accounts', ['account']) + }) + }) + it('should be updated', async () => { + await store.dispatch('GlobalHeader/listAccounts') + expect(store.state.GlobalHeader.accounts).toEqual(['account']) + }) + }) + + describe('refreshAccounts', () => { + beforeEach(() => { + ipcMain.once('refresh-accounts', (event, _) => { + event.sender.send('response-refresh-accounts', ['accounts']) + }) + }) + it('should be refreshed', async () => { + await store.dispatch('GlobalHeader/refreshAccounts') + expect(store.state.GlobalHeader.accounts).toEqual(['accounts']) + }) + }) + + describe('removeShortcutEvents', () => { + it('should be removed', async () => { + const removed = await store.dispatch('GlobalHeader/removeShortcutEvents') + expect(removed).toEqual(true) + }) + }) + + describe('loadHide', () => { + beforeEach(() => { + ipcMain.once('get-global-header', (event, _) => { + event.sender.send('response-get-global-header', true) + }) + }) + it('should be changed', async () => { + await store.dispatch('GlobalHeader/loadHide') + expect(store.state.GlobalHeader.hide).toEqual(true) + }) + }) + + describe('switchHide', () => { + beforeEach(() => { + ipcMain.once('change-global-header', (event, value) => { + event.sender.send('response-change-global-header', value) + }) + }) + it('should be switched', async () => { + await store.dispatch('GlobalHeader/switchHide', true) + expect(store.state.GlobalHeader.hide).toEqual(true) + }) + }) +}) diff --git a/spec/mock/electron.js b/spec/mock/electron.js index b206d96d..616cf5df 100644 --- a/spec/mock/electron.js +++ b/spec/mock/electron.js @@ -1,9 +1,4 @@ const { ipcRenderer, ipcMain } = require('electron-ipc-mock')() -// export const ipcRenderer = { -// send: jest.fn(), -// on: jest.fn(), -// once: jest.fn(), -// removeAllListeners: jest.fn() -// } + module.exports.ipcRenderer = ipcRenderer module.exports.ipcMain = ipcMain diff --git a/spec/mock/router.js b/spec/mock/router.js new file mode 100644 index 00000000..23cdcf5a --- /dev/null +++ b/spec/mock/router.js @@ -0,0 +1,3 @@ +export default { + push: jest.fn() +} diff --git a/src/renderer/components/GlobalHeader.vue b/src/renderer/components/GlobalHeader.vue index 44b06585..f6089edd 100644 --- a/src/renderer/components/GlobalHeader.vue +++ b/src/renderer/components/GlobalHeader.vue @@ -59,7 +59,7 @@ export default { }, async initialize () { await this.$store.dispatch('GlobalHeader/removeShortcutEvents') - this.$store.dispatch('GlobalHeader/loadHide') + await this.$store.dispatch('GlobalHeader/loadHide') this.$store.dispatch('GlobalHeader/watchShortcutEvents') try { const accounts = await this.$store.dispatch('GlobalHeader/listAccounts') diff --git a/src/renderer/components/TimelineSpace/SideMenu.vue b/src/renderer/components/TimelineSpace/SideMenu.vue index 6c236c5e..419faa8b 100644 --- a/src/renderer/components/TimelineSpace/SideMenu.vue +++ b/src/renderer/components/TimelineSpace/SideMenu.vue @@ -168,8 +168,8 @@ export default { releaseCollapse () { this.$store.dispatch('TimelineSpace/SideMenu/changeCollapse', false) }, - changeGlobalHeader (value) { - this.$store.dispatch('GlobalHeader/switchHide', value) + async changeGlobalHeader (value) { + await this.$store.dispatch('GlobalHeader/switchHide', value) } } } diff --git a/src/renderer/store/GlobalHeader.js b/src/renderer/store/GlobalHeader.js index 14745e9a..91b306c4 100644 --- a/src/renderer/store/GlobalHeader.js +++ b/src/renderer/store/GlobalHeader.js @@ -1,5 +1,5 @@ import { ipcRenderer } from 'electron' -import router from '../router' +import router from '@/router' const GlobalHeader = { namespaced: true, @@ -69,18 +69,24 @@ const GlobalHeader = { }, async removeShortcutEvents () { ipcRenderer.removeAllListeners('change-account') - return 'removeShortcutEvents' + return true }, loadHide ({ commit }) { - ipcRenderer.send('get-global-header') - ipcRenderer.once('response-get-global-header', (event, value) => { - commit('changeHide', value) + return new Promise((resolve, reject) => { + ipcRenderer.send('get-global-header') + ipcRenderer.once('response-get-global-header', (event, value) => { + commit('changeHide', value) + resolve(value) + }) }) }, switchHide ({ dispatch }, value) { - ipcRenderer.send('change-global-header', value) - ipcRenderer.once('response-change-global-header', (event, _) => { - dispatch('loadHide') + return new Promise((resolve, reject) => { + ipcRenderer.send('change-global-header', value) + ipcRenderer.once('response-change-global-header', (event, _) => { + dispatch('loadHide') + resolve(true) + }) }) } }