Merge pull request #810 from h3poteto/iss-209

refs #209 Add Login store unit tests
This commit is contained in:
AkiraFukushima 2018-12-29 00:24:37 +09:00 committed by GitHub
commit 8a5b8f9992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 124 additions and 49 deletions

14
package-lock.json generated
View File

@ -2915,6 +2915,13 @@
"requires": {
"core-js": "2.5.7",
"regenerator-runtime": "0.11.1"
},
"dependencies": {
"regenerator-runtime": {
"version": "0.11.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz",
"integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg=="
}
}
},
"babel-template": {
@ -15677,9 +15684,10 @@
"dev": true
},
"regenerator-runtime": {
"version": "0.11.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz",
"integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg=="
"version": "0.13.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.1.tgz",
"integrity": "sha512-5KzMIyPLvfdPmvsdlYsHqITrDfK9k7bmvf97HvHSN4810i254ponbxCQ1NukpRWlu6en2MBWzAlhDExEKISwAA==",
"dev": true
},
"regenerator-transform": {
"version": "0.10.1",

View File

@ -30,7 +30,7 @@
"build:mas": "npm run build:clean && npm run pack && electron-packager ./ 'Whalebird' --platform=mas --arch=x64 --electron-version=3.0.10 --asar.unpackDir='build/sounds' --out=packages --ignore='^/src' --ignore='^/test' --ignore='^/.electron-vue' --ignore='^/.envrc' --ignore='^/packages' --ignore='^/plist' --ignore='^/static' --ignore='^/whalebird.db' --ignore='^/screenshot.png' --prune=true --icon=./build/icons/icon.icns --overwrite --app-bundle-id=org.whalebird.desktop --app-version=$npm_package_config_appVersion --build-version=$npm_package_config_buildVersion --extend-info='./plist/team.plist' --osx-sign --app-category-type=public.app-category.social-networking",
"dev": "node .electron-vue/dev-runner.js",
"e2e": "npm run pack && mocha test/e2e",
"lint": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter src test",
"lint": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter src spec",
"lint:fix": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter --fix src test",
"stylelint": "stylelint '**/*.vue' --syntax scss",
"pack": "npm run pack:main && npm run pack:renderer",
@ -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": "BABEL_ENV=test jest -u ./spec/unit/*/**.spec.js"
},
"build": {
"productName": "Whalebird",
@ -90,6 +90,13 @@
"category": "Network"
}
},
"jest": {
"moduleNameMapper": {
"^@/(.+)": "<rootDir>/src/renderer/$1",
"^~/(.+)": "<rootDir>/$1",
"electron": "<rootDir>/spec/mock/electron.js"
}
},
"dependencies": {
"@panter/vue-i18next": "^0.13.0",
"@trodi/electron-splashscreen": "^0.3.4",
@ -138,6 +145,7 @@
"ajv": "^6.6.1",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.4",
"babel-minify-webpack-plugin": "^0.3.1",
"babel-plugin-istanbul": "^5.1.0",
@ -184,6 +192,7 @@
"multispinner": "^0.2.1",
"node-loader": "^0.6.0",
"node-sass": "^4.10.0",
"regenerator-runtime": "^0.13.1",
"sass-loader": "^7.0.3",
"style-loader": "^0.23.1",
"stylelint": "^9.9.0",

View File

@ -4,6 +4,7 @@ linter:
dir:
- src
- test
- spec
options:
config: '.eslintrc.js'
ext: '.js,.vue'

5
spec/.eslintrc Normal file
View File

@ -0,0 +1,5 @@
{
"env": {
"jest": true
}
}

6
spec/mock/electron.js Normal file
View File

@ -0,0 +1,6 @@
export const ipcRenderer = {
send: jest.fn(),
on: jest.fn(),
once: jest.fn(),
removeAllListeners: jest.fn()
}

View File

@ -0,0 +1,56 @@
import axios from 'axios'
import Login from '@/store/Login'
import { ipcRenderer } from '~/spec/mock/electron'
jest.mock('axios')
describe('Login', () => {
describe('mutations', () => {
let state
beforeEach(() => {
state = {
instances: [],
selectedInstance: null,
searching: false
}
})
describe('changeInstance', () => {
it('should be changed', () => {
Login.mutations.changeInstance(state, 'pleroma.io')
expect(state.selectedInstance).toEqual('pleroma.io')
})
})
describe('changeSearching', () => {
it('should be changed', () => {
Login.mutations.changeSearching(state, true)
expect(state.searching).toEqual(true)
})
})
})
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('pageBack', () => {
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')
})
})
})

View File

@ -1,5 +1,4 @@
import assert from 'assert'
import emojify from '../../src/renderer/utils/emojify'
import emojify from '@/utils/emojify'
describe('emojify', () => {
const emoji = [
@ -20,18 +19,14 @@ describe('emojify', () => {
const str = 'I have a pen.'
it('should not change', () => {
const result = emojify(str, emoji)
assert.strictEqual(
result,
str
)
expect(result).toEqual(str)
})
})
describe('Contain a shortcode', () => {
const str = 'I like :python:'
it('should replace', () => {
const result = emojify(str, emoji)
assert.strictEqual(
result,
expect(result).toEqual(
'I like <img draggable="false" class="emojione" alt="python" title="python" src="https://example.com/python" />'
)
})
@ -40,8 +35,7 @@ describe('emojify', () => {
const str = 'I like :python: , :nodejs: and :slack:'
it('should replace', () => {
const result = emojify(str, emoji)
assert.strictEqual(
result,
expect(result).toEqual(
'I like <img draggable="false" class="emojione" alt="python" title="python" src="https://example.com/python" /> , <img draggable="false" class="emojione" alt="nodejs" title="nodejs" src="https://example.com/nodejs" /> and <img draggable="false" class="emojione" alt="slack" title="slack" src="https://example.com/slack" />'
)
})
@ -50,8 +44,7 @@ describe('emojify', () => {
const str = 'I like :python: , I love :python:'
it('should replace', () => {
const result = emojify(str, emoji)
assert.strictEqual(
result,
expect(result).toEqual(
'I like <img draggable="false" class="emojione" alt="python" title="python" src="https://example.com/python" /> , I love <img draggable="false" class="emojione" alt="python" title="python" src="https://example.com/python" />'
)
})

View File

@ -1,45 +1,44 @@
import assert from 'assert'
import suggestText from '../../src/renderer/utils/suggestText'
import suggestText from '@/utils/suggestText'
describe('account', () => {
describe('Only account name', () => {
const str = '@h3pote'
it('should match', () => {
const [start, word] = suggestText(str, 7)
assert.strictEqual(str, word)
assert.strictEqual(start, 1)
expect(str).toEqual(word)
expect(start).toEqual(1)
})
})
describe('Beginning of the sentence', () => {
const str = '@h3pote toot body'
it('should match', () => {
const [start, word] = suggestText(str, 7)
assert.strictEqual(word, '@h3pote')
assert.strictEqual(start, 1)
expect(word).toEqual('@h3pote')
expect(start).toEqual(1)
})
})
describe('Halfway of the sentence', () => {
const str = 'toot body @h3pote toot'
it('should match', () => {
const [start, word] = suggestText(str, 17)
assert.strictEqual(word, '@h3pote')
assert.strictEqual(start, 11)
expect(word).toEqual('@h3pote')
expect(start).toEqual(11)
})
})
describe('End of the sentence', () => {
const str = 'toot body @h3pote'
it('should match', () => {
const [start, word] = suggestText(str, 17)
assert.strictEqual(word, '@h3pote')
assert.strictEqual(start, 11)
expect(word).toEqual('@h3pote')
expect(start).toEqual(11)
})
})
describe('No space', () => {
const str = 'tootbody@h3pote'
it('should not match', () => {
const [start, word] = suggestText(str, 15)
assert.strictEqual(word, null)
assert.strictEqual(start, null)
expect(word).toEqual(null)
expect(start).toEqual(null)
})
})
})

View File

@ -1,6 +1,5 @@
import assert from 'assert'
import { JSDOM } from 'jsdom'
import { findLink, findTag, findAccount } from '../../src/renderer/utils/tootParser'
import { findLink, findTag, findAccount } from '@/utils/tootParser'
describe('findLink', () => {
describe('Pleroma', () => {
@ -16,7 +15,7 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
const target = doc.getElementById('link')
it('should find', () => {
const res = findLink(target)
assert.strictEqual(res, 'https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1')
expect(res).toEqual('https://github.com/h3poteto/whalebird-desktop/releases/tag/2.4.1')
})
})
})
@ -34,7 +33,7 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
const target = doc.getElementById('tag')
it('should find', () => {
const res = findTag(target)
assert.strictEqual(res, 'whalebird')
expect(res).toEqual('whalebird')
})
})
@ -50,7 +49,7 @@ I released Whalebird version 2.4.1. In version 2.4.0, Whalebird supports streami
const target = doc.getElementById('tag')
it('should find', () => {
const res = findTag(target)
assert.strictEqual(res, 'whalebird')
expect(res).toEqual('whalebird')
})
})
})
@ -67,8 +66,8 @@ describe('findAccount', () => {
const target = doc.getElementById('user')
it('should find', () => {
const res = findAccount(target)
assert.strictEqual(res.username, '@h3_poteto')
assert.strictEqual(res.acct, '@h3_poteto@social.mikutter.hachune.net')
expect(res.username).toEqual('@h3_poteto')
expect(res.acct).toEqual('@h3_poteto@social.mikutter.hachune.net')
})
})
@ -82,8 +81,8 @@ describe('findAccount', () => {
const target = doc.getElementById('user')
it('should find', () => {
const res = findAccount(target)
assert.strictEqual(res.username, '@h3poteto')
assert.strictEqual(res.acct, '@h3poteto@pleroma.io')
expect(res.username).toEqual('@h3poteto')
expect(res.acct).toEqual('@h3poteto@pleroma.io')
})
})
})

View File

@ -1,5 +1,4 @@
import assert from 'assert'
import { domainFormat } from '../../src/renderer/utils/validator'
import { domainFormat } from '@/utils/validator'
describe('validator', () => {
describe('domainFormat', () => {
@ -7,56 +6,56 @@ describe('validator', () => {
const domain = 'https://mastodon.social'
it('should not match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, -1)
expect(res).toEqual(-1)
})
})
describe('string contains account name', () => {
const domain = 'h3_poteto@mastodon.social'
it('should not match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, -1)
expect(res).toEqual(-1)
})
})
describe('string is gTLD domain', () => {
const domain = 'mastodon.social'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
describe('string is subdomain', () => {
const domain = 'music.mastodon.social'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
describe('string is subdomain', () => {
const domain = 'social.tchncs.de'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
describe('string is jp domain', () => {
const domain = 'mstdn.co.jp'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
describe('string contains hyphone', () => {
const domain = 'music-mastodon.social'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
describe('string is short domain', () => {
const domain = 'id.cc'
it('should match', () => {
const res = domain.search(domainFormat)
assert.strictEqual(res, 0)
expect(res).toEqual(0)
})
})
})

View File

@ -17,7 +17,7 @@ const Login = {
}
},
actions: {
fetchLogin ({ commit }, instance) {
fetchLogin (_, instance) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-auth-url', instance)
ipcRenderer.once('error-get-auth-url', (event, err) => {