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" "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": { "electron-is-accelerator": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "https://registry.npmjs.org/electron-is-accelerator/-/electron-is-accelerator-0.1.2.tgz", "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", "test": "npm run unit && npm run e2e",
"unit": "karma start test/unit/karma.conf.js", "unit": "karma start test/unit/karma.conf.js",
"postinstall": "npm run lint:fix", "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": { "build": {
"productName": "Whalebird", "productName": "Whalebird",
@ -165,6 +165,7 @@
"electron-builder": "^20.38.2", "electron-builder": "^20.38.2",
"electron-debug": "^2.0.0", "electron-debug": "^2.0.0",
"electron-devtools-installer": "^2.2.4", "electron-devtools-installer": "^2.2.4",
"electron-ipc-mock": "0.0.3",
"electron-packager": "^12.2.0", "electron-packager": "^12.2.0",
"eslint": "^5.9.0", "eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0", "eslint-config-standard": "^12.0.0",

View File

@ -1,6 +1,9 @@
export const ipcRenderer = { const { ipcRenderer, ipcMain } = require('electron-ipc-mock')()
send: jest.fn(), // export const ipcRenderer = {
on: jest.fn(), // send: jest.fn(),
once: jest.fn(), // on: jest.fn(),
removeAllListeners: 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 axios from 'axios'
import Login from '@/store/Login' import Login from '@/store/Login'
import { ipcRenderer } from '~/spec/mock/electron' import { ipcMain } from '~/spec/mock/electron'
jest.mock('axios') jest.mock('axios')
@ -29,28 +29,53 @@ describe('Login', () => {
}) })
describe('actions', () => { describe('actions', () => {
describe('fetchLogin', async () => { describe('fetchLogin', () => {
const commitMock = jest.fn() describe('error', () => {
await Login.actions.fetchLogin({ commit: commitMock }, 'pleroma.io') it('should return error', async () => {
expect(ipcRenderer.send).toHaveBeenCalledWith('get-auth-url', 'pleroma.io') 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', () => { describe('pageBack', () => {
const commitMock = jest.fn() it('should reset instance', () => {
Login.actions.pageBack({ commit: commitMock }) const commitMock = jest.fn()
expect(commitMock).toHaveBeenCalledWith('changeInstance', null) Login.actions.pageBack({ commit: commitMock })
expect(commitMock).toHaveBeenCalledWith('changeInstance', null)
})
}) })
describe('confirmInstance', async () => { describe('confirmInstance', () => {
const resp = { it('should change instance', async () => {
data: 'test' const resp = {
} data: 'test'
// Provide Promise.resolve for finally keywrod. }
// https://github.com/facebook/jest/issues/6552 // Provide Promise.resolve for finally keywrod.
axios.get.mockReturnValue(Promise.resolve(resp)) // https://github.com/facebook/jest/issues/6552
const commitMock = jest.fn() axios.get.mockReturnValue(Promise.resolve(resp))
const data = await Login.actions.confirmInstance({ commit: commitMock }, 'pleroma.io') const commitMock = jest.fn()
expect(data).toEqual('test') const data = await Login.actions.confirmInstance({ commit: commitMock }, 'pleroma.io')
// ref: https://eddyerburgh.me/how-to-unit-test-a-vuex-store expect(data).toEqual('test')
expect(commitMock).toHaveBeenCalledWith('changeInstance', 'pleroma.io') // ref: https://eddyerburgh.me/how-to-unit-test-a-vuex-store
expect(commitMock).toHaveBeenCalledWith('changeInstance', 'pleroma.io')
})
}) })
}) })
}) })
class AuthError extends Error {}