Merge pull request #814 from h3poteto/iss-209

refs #209 Add unit/integration tests for GlobalHeader
This commit is contained in:
AkiraFukushima 2018-12-31 22:53:00 +09:00 committed by GitHub
commit dfa8654946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 119 additions and 17 deletions

View File

@ -92,6 +92,7 @@
}, },
"jest": { "jest": {
"moduleNameMapper": { "moduleNameMapper": {
"@/router": "<rootDir>/spec/mock/router.js",
"^@/(.+)": "<rootDir>/src/renderer/$1", "^@/(.+)": "<rootDir>/src/renderer/$1",
"^~/(.+)": "<rootDir>/$1", "^~/(.+)": "<rootDir>/$1",
"electron": "<rootDir>/spec/mock/electron.js" "electron": "<rootDir>/spec/mock/electron.js"

View File

@ -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)
})
})
})

View File

@ -1,9 +1,4 @@
const { ipcRenderer, ipcMain } = require('electron-ipc-mock')() 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.ipcRenderer = ipcRenderer
module.exports.ipcMain = ipcMain module.exports.ipcMain = ipcMain

3
spec/mock/router.js Normal file
View File

@ -0,0 +1,3 @@
export default {
push: jest.fn()
}

View File

@ -59,7 +59,7 @@ export default {
}, },
async initialize () { async initialize () {
await this.$store.dispatch('GlobalHeader/removeShortcutEvents') await this.$store.dispatch('GlobalHeader/removeShortcutEvents')
this.$store.dispatch('GlobalHeader/loadHide') await this.$store.dispatch('GlobalHeader/loadHide')
this.$store.dispatch('GlobalHeader/watchShortcutEvents') this.$store.dispatch('GlobalHeader/watchShortcutEvents')
try { try {
const accounts = await this.$store.dispatch('GlobalHeader/listAccounts') const accounts = await this.$store.dispatch('GlobalHeader/listAccounts')

View File

@ -168,8 +168,8 @@ export default {
releaseCollapse () { releaseCollapse () {
this.$store.dispatch('TimelineSpace/SideMenu/changeCollapse', false) this.$store.dispatch('TimelineSpace/SideMenu/changeCollapse', false)
}, },
changeGlobalHeader (value) { async changeGlobalHeader (value) {
this.$store.dispatch('GlobalHeader/switchHide', value) await this.$store.dispatch('GlobalHeader/switchHide', value)
} }
} }
} }

View File

@ -1,5 +1,5 @@
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import router from '../router' import router from '@/router'
const GlobalHeader = { const GlobalHeader = {
namespaced: true, namespaced: true,
@ -69,18 +69,24 @@ const GlobalHeader = {
}, },
async removeShortcutEvents () { async removeShortcutEvents () {
ipcRenderer.removeAllListeners('change-account') ipcRenderer.removeAllListeners('change-account')
return 'removeShortcutEvents' return true
}, },
loadHide ({ commit }) { loadHide ({ commit }) {
ipcRenderer.send('get-global-header') return new Promise((resolve, reject) => {
ipcRenderer.once('response-get-global-header', (event, value) => { ipcRenderer.send('get-global-header')
commit('changeHide', value) ipcRenderer.once('response-get-global-header', (event, value) => {
commit('changeHide', value)
resolve(value)
})
}) })
}, },
switchHide ({ dispatch }, value) { switchHide ({ dispatch }, value) {
ipcRenderer.send('change-global-header', value) return new Promise((resolve, reject) => {
ipcRenderer.once('response-change-global-header', (event, _) => { ipcRenderer.send('change-global-header', value)
dispatch('loadHide') ipcRenderer.once('response-change-global-header', (event, _) => {
dispatch('loadHide')
resolve(true)
})
}) })
} }
} }