1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2024-12-23 23:47:57 +01:00

refs #209 Add unit/integration tests for GlobalHeader

This commit is contained in:
AkiraFukushima 2018-12-31 00:03:36 +09:00
parent 5380cf12ab
commit bfa362fec5
7 changed files with 119 additions and 17 deletions

View File

@ -92,6 +92,7 @@
},
"jest": {
"moduleNameMapper": {
"@/router": "<rootDir>/spec/mock/router.js",
"^@/(.+)": "<rootDir>/src/renderer/$1",
"^~/(.+)": "<rootDir>/$1",
"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')()
// export const ipcRenderer = {
// send: jest.fn(),
// on: jest.fn(),
// once: jest.fn(),
// removeAllListeners: jest.fn()
// }
module.exports.ipcRenderer = ipcRenderer
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 () {
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')

View File

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

View File

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