refs #209 Fix Login spec to use ipc mock

This commit is contained in:
AkiraFukushima 2018-12-29 02:06:36 +09:00
parent fb429e4926
commit 434bdc6a5f
4 changed files with 62 additions and 27 deletions

6
package-lock.json generated
View File

@ -5671,6 +5671,12 @@
"sumchecker": "2.0.2"
}
},
"electron-ipc-mock": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/electron-ipc-mock/-/electron-ipc-mock-0.0.3.tgz",
"integrity": "sha1-7sEXxVEO7KfaaCkaikG/x4aNPgM=",
"dev": true
},
"electron-is-accelerator": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/electron-is-accelerator/-/electron-is-accelerator-0.1.2.tgz",

View File

@ -39,7 +39,7 @@
"test": "npm run unit && npm run e2e",
"unit": "karma start test/unit/karma.conf.js",
"postinstall": "npm run lint:fix",
"spec:unit": "BABEL_ENV=test jest -u ./spec/unit/*/**.spec.js"
"spec:unit": "NODE_ENV=test jest -u ./spec/unit/*/**.spec.js"
},
"build": {
"productName": "Whalebird",
@ -165,6 +165,7 @@
"electron-builder": "^20.38.2",
"electron-debug": "^2.0.0",
"electron-devtools-installer": "^2.2.4",
"electron-ipc-mock": "0.0.3",
"electron-packager": "^12.2.0",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",

View File

@ -1,6 +1,9 @@
export const ipcRenderer = {
send: jest.fn(),
on: jest.fn(),
once: jest.fn(),
removeAllListeners: jest.fn()
}
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

View File

@ -1,6 +1,6 @@
import axios from 'axios'
import Login from '@/store/Login'
import { ipcRenderer } from '~/spec/mock/electron'
import { ipcMain } from '~/spec/mock/electron'
jest.mock('axios')
@ -29,28 +29,53 @@ describe('Login', () => {
})
describe('actions', () => {
describe('fetchLogin', async () => {
const commitMock = jest.fn()
await Login.actions.fetchLogin({ commit: commitMock }, 'pleroma.io')
expect(ipcRenderer.send).toHaveBeenCalledWith('get-auth-url', 'pleroma.io')
describe('fetchLogin', () => {
describe('error', () => {
it('should return error', async () => {
ipcMain.once('get-auth-url', (event, instance) => {
event.sender.send('error-get-auth-url', new AuthError())
})
const commitMock = jest.fn()
await Login.actions.fetchLogin({ commit: commitMock }, 'pleroma.io')
.catch((err) => {
expect(err instanceof AuthError).toEqual(true)
})
})
})
describe('success', () => {
it('should return url', async () => {
ipcMain.once('get-auth-url', (event, instance) => {
event.sender.send('response-get-auth-url', 'http://example.com/auth')
})
const commitMock = jest.fn()
const url = await Login.actions.fetchLogin({ commit: commitMock }, 'pleroma.io')
expect(url).toEqual('http://example.com/auth')
})
})
})
describe('pageBack', () => {
const commitMock = jest.fn()
Login.actions.pageBack({ commit: commitMock })
expect(commitMock).toHaveBeenCalledWith('changeInstance', null)
it('should reset instance', () => {
const commitMock = jest.fn()
Login.actions.pageBack({ commit: commitMock })
expect(commitMock).toHaveBeenCalledWith('changeInstance', null)
})
})
describe('confirmInstance', async () => {
const resp = {
data: 'test'
}
// Provide Promise.resolve for finally keywrod.
// https://github.com/facebook/jest/issues/6552
axios.get.mockReturnValue(Promise.resolve(resp))
const commitMock = jest.fn()
const data = await Login.actions.confirmInstance({ commit: commitMock }, 'pleroma.io')
expect(data).toEqual('test')
// ref: https://eddyerburgh.me/how-to-unit-test-a-vuex-store
expect(commitMock).toHaveBeenCalledWith('changeInstance', 'pleroma.io')
describe('confirmInstance', () => {
it('should change instance', async () => {
const resp = {
data: 'test'
}
// Provide Promise.resolve for finally keywrod.
// https://github.com/facebook/jest/issues/6552
axios.get.mockReturnValue(Promise.resolve(resp))
const commitMock = jest.fn()
const data = await Login.actions.confirmInstance({ commit: commitMock }, 'pleroma.io')
expect(data).toEqual('test')
// ref: https://eddyerburgh.me/how-to-unit-test-a-vuex-store
expect(commitMock).toHaveBeenCalledWith('changeInstance', 'pleroma.io')
})
})
})
})
class AuthError extends Error {}