Skip to content

Commit 51f9977

Browse files
committed
add test
1 parent aefd8bb commit 51f9977

File tree

15 files changed

+172
-22
lines changed

15 files changed

+172
-22
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ yarn-debug.log*
66
yarn-error.log*
77
**/*.log
88

9-
test/unit/coverage
10-
test/e2e/reports
9+
tests/**/coverage/
10+
tests/e2e/reports
1111
selenium-debug.log
1212

1313
# Editor directories and files

babel.config.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
module.exports = {
22
presets: [
3-
['@vue/app', { modules: 'commonjs' }]
4-
],
5-
plugins: [],
6-
env: {
7-
test: {
8-
presets: [
9-
['@vue/app', { modules: 'commonjs' }]
10-
],
11-
plugins: ['istanbul']
12-
}
13-
}
3+
'@vue/app'
4+
]
145
}

jest.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
verbose: true,
3+
moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
4+
transform: {
5+
'^.+\\.vue$': 'vue-jest',
6+
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
7+
'^.+\\.jsx?$': 'babel-jest'
8+
},
9+
moduleNameMapper: {
10+
'^@/(.*)$': '<rootDir>/src/$1'
11+
},
12+
snapshotSerializers: ['jest-serializer-vue'],
13+
testMatch: [
14+
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
15+
],
16+
collectCoverageFrom: ['src/utils/**/*.{js,vue}', '!src/utils/auth.js', '!src/utils/request.js', 'src/components/**/*.{js,vue}'],
17+
coverageDirectory: '<rootDir>/tests/unit/coverage',
18+
// 'collectCoverage': true,
19+
'coverageReporters': [
20+
'lcov',
21+
'text-summary'
22+
],
23+
testURL: 'http://localhost/'
24+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:sit": "vue-cli-service build --mode text",
1111
"lint": "eslint --ext .js,.vue src",
1212
"test": "npm run lint",
13+
"test:unit": "vue-cli-service test:unit",
1314
"precommit": "lint-staged",
1415
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml"
1516
},

src/components/Hamburger/index.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ export default {
3333
isActive: {
3434
type: Boolean,
3535
default: false
36-
},
37-
toggleClick: {
38-
type: Function,
39-
default: null
36+
}
37+
},
38+
methods: {
39+
toggleClick() {
40+
this.$emit('toggleClick')
4041
}
4142
}
4243
}

src/utils/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export function parseTime(time, cFormat) {
3636
}
3737

3838
export function formatTime(time, option) {
39-
time = +time * 1000
39+
if (('' + time).length === 10) {
40+
time = parseInt(time) * 1000
41+
} else {
42+
time = +time
43+
}
4044
const d = new Date(time)
4145
const now = Date.now()
4246

src/utils/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Created by jiachenpan on 16/11/18.
33
*/
44

5-
export function isvalidUsername(str) {
5+
export function validUsername(str) {
66
const valid_map = ['admin', 'editor']
77
return valid_map.indexOf(str.trim()) >= 0
88
}

src/views/layout/components/Navbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="navbar">
3-
<hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>
3+
<hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar"/>
44

55
<breadcrumb class="breadcrumb-container"/>
66

src/views/login/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
</template>
6464

6565
<script>
66-
import { isvalidUsername } from '@/utils/validate'
66+
import { validUsername } from '@/utils/validate'
6767
import LangSelect from '@/components/LangSelect'
6868
import SocialSign from './socialsignin'
6969
@@ -72,7 +72,7 @@ export default {
7272
components: { LangSelect, SocialSign },
7373
data() {
7474
const validateUsername = (rule, value, callback) => {
75-
if (!isvalidUsername(value)) {
75+
if (!validUsername(value)) {
7676
callback(new Error('Please enter the correct user name'))
7777
} else {
7878
callback()

tests/unit/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
env: {
3+
jest: true
4+
}
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { shallowMount } from '@vue/test-utils'
2+
import Hamburger from '@/components/Hamburger/index.vue'
3+
describe('Hamburger.vue', () => {
4+
it('toggle click', () => {
5+
const wrapper = shallowMount(Hamburger)
6+
const mockFn = jest.fn()
7+
wrapper.vm.$on('toggleClick', mockFn)
8+
wrapper.find('.hamburger').trigger('click')
9+
expect(mockFn).toBeCalled()
10+
})
11+
it('prop isActive', () => {
12+
const wrapper = shallowMount(Hamburger)
13+
wrapper.setProps({ isActive: true })
14+
expect(wrapper.contains('.is-active')).toBe(true)
15+
wrapper.setProps({ isActive: false })
16+
expect(wrapper.contains('.is-active')).toBe(false)
17+
})
18+
})

tests/unit/components/SvgIcon.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { shallowMount } from '@vue/test-utils'
2+
import SvgIcon from '@/components/SvgIcon/index.vue'
3+
describe('SvgIcon.vue', () => {
4+
it('iconClass', () => {
5+
const wrapper = shallowMount(SvgIcon, {
6+
propsData: {
7+
iconClass: 'test'
8+
}
9+
})
10+
expect(wrapper.find('use').attributes().href).toBe('#icon-test')
11+
})
12+
it('className', () => {
13+
const wrapper = shallowMount(SvgIcon, {
14+
propsData: {
15+
iconClass: 'test'
16+
}
17+
})
18+
expect(wrapper.classes().length).toBe(1)
19+
wrapper.setProps({ className: 'test' })
20+
expect(wrapper.classes().includes('test')).toBe(true)
21+
})
22+
})

tests/unit/utils/formatTime.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { formatTime } from '@/utils/index.js'
2+
describe('Utils:formatTime', () => {
3+
const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
4+
const retrofit = 5 * 1000
5+
6+
it('ten digits timestamp', () => {
7+
expect(formatTime((d / 1000).toFixed(0))).toBe('7月13日17时54分')
8+
})
9+
it('test now', () => {
10+
expect(formatTime(+new Date() - 1)).toBe('刚刚')
11+
})
12+
it('less two minute', () => {
13+
expect(formatTime(+new Date() - 60 * 2 * 1000 + retrofit)).toBe('2分钟前')
14+
})
15+
it('less two hour', () => {
16+
expect(formatTime(+new Date() - 60 * 60 * 2 * 1000 + retrofit)).toBe('2小时前')
17+
})
18+
it('less one day', () => {
19+
expect(formatTime(+new Date() - 60 * 60 * 24 * 1 * 1000)).toBe('1天前')
20+
})
21+
it('more than one day', () => {
22+
expect(formatTime(d)).toBe('7月13日17时54分')
23+
})
24+
it('format', () => {
25+
expect(formatTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54')
26+
expect(formatTime(d, '{y}-{m}-{d}')).toBe('2018-07-13')
27+
expect(formatTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54')
28+
})
29+
})

tests/unit/utils/parseTime.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { parseTime } from '@/utils/index.js'
2+
describe('Utils:parseTime', () => {
3+
const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
4+
it('timestamp', () => {
5+
expect(parseTime(d)).toBe('2018-07-13 17:54:01')
6+
})
7+
it('ten digits timestamp', () => {
8+
expect(parseTime((d / 1000).toFixed(0))).toBe('2018-07-13 17:54:01')
9+
})
10+
it('new Date', () => {
11+
expect(parseTime(new Date(d))).toBe('2018-07-13 17:54:01')
12+
})
13+
it('format', () => {
14+
expect(parseTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54')
15+
expect(parseTime(d, '{y}-{m}-{d}')).toBe('2018-07-13')
16+
expect(parseTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54')
17+
})
18+
it('get the day of the week', () => {
19+
expect(parseTime(d, '{a}')).toBe('五') // 星期五
20+
})
21+
it('get the day of the week', () => {
22+
expect(parseTime(+d + 1000 * 60 * 60 * 24 * 2, '{a}')).toBe('日') // 星期日
23+
})
24+
it('empty argument', () => {
25+
expect(parseTime()).toBeNull()
26+
})
27+
})

tests/unit/utils/validate.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { validUsername, validateURL, validateLowerCase, validateUpperCase, validateAlphabets } from '@/utils/validate.js'
2+
describe('Utils:validate', () => {
3+
it('validUsername', () => {
4+
expect(validUsername('admin')).toBe(true)
5+
expect(validUsername('editor')).toBe(true)
6+
expect(validUsername('xxxx')).toBe(false)
7+
})
8+
it('validateURL', () => {
9+
expect(validateURL('https://github.com/PanJiaChen/vue-element-admin')).toBe(true)
10+
expect(validateURL('http://github.com/PanJiaChen/vue-element-admin')).toBe(true)
11+
expect(validateURL('github.com/PanJiaChen/vue-element-admin')).toBe(false)
12+
})
13+
it('validateLowerCase', () => {
14+
expect(validateLowerCase('abc')).toBe(true)
15+
expect(validateLowerCase('Abc')).toBe(false)
16+
expect(validateLowerCase('123abc')).toBe(false)
17+
})
18+
it('validateUpperCase', () => {
19+
expect(validateUpperCase('ABC')).toBe(true)
20+
expect(validateUpperCase('Abc')).toBe(false)
21+
expect(validateUpperCase('123ABC')).toBe(false)
22+
})
23+
it('validateAlphabets', () => {
24+
expect(validateAlphabets('ABC')).toBe(true)
25+
expect(validateAlphabets('Abc')).toBe(true)
26+
expect(validateAlphabets('123aBC')).toBe(false)
27+
})
28+
})

0 commit comments

Comments
 (0)