Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit eeb1f67

Browse files
committed
test: add unit test
1 parent c0a7bb8 commit eeb1f67

13 files changed

+437
-23
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ node_modules
55
.env.local
66
.env.*.local
77

8+
# Dev/Build Artifacts
9+
/tests/unit/coverage/
10+
811
# Log files
912
npm-debug.log*
1013
yarn-debug.log*

babel.config.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
module.exports = {
22
presets: [
33
'@vue/app'
4-
]
4+
],
5+
env: {
6+
test: {
7+
presets: [
8+
[
9+
'@vue/app',
10+
{
11+
useBuiltIns: 'usage',
12+
modules: 'commonjs'
13+
}
14+
]
15+
]
16+
}
17+
}
518
}

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ module.exports = {
1414
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
1515
],
1616
collectCoverageFrom: ['src/**/*.{js,vue}'],
17+
coverageDirectory: '<rootDir>/tests/unit/coverage',
1718
testURL: 'http://localhost/'
1819
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"build:example": "vue-cli-service build --dest demo",
1717
"build:lib": "vue-cli-service build --target lib --name vue-item-list-selector src/index.vue",
1818
"lint": "cross-env NODE_ENV=production vue-cli-service lint",
19-
"test:unit": "vue-cli-service test:unit",
19+
"test:unit": "vue-cli-service test:unit --coverage",
2020
"release": "node scripts/release.js"
2121
},
2222
"author": "laomao800 <[email protected]>",

src/index.vue

+9-9
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,8 @@ export default {
108108
)
109109
}
110110
},
111-
selectionArr: {
112-
get() {
113-
return this.multiple ? this.selection : [this.selection]
114-
},
115-
set(val) {
116-
this.syncSelection(val)
117-
}
111+
selectionArr() {
112+
return this.multiple ? this.selection : [this.selection]
118113
}
119114
},
120115
@@ -135,7 +130,7 @@ export default {
135130
136131
// 重置组件状态
137132
reset() {
138-
this.debounceKeyword = ''
133+
this.keyword = ''
139134
this.syncSelection([])
140135
},
141136
@@ -170,14 +165,18 @@ export default {
170165
},
171166
172167
activePrevOptions() {
173-
if (this.optionActiveIndex > 0) {
168+
if (this.optionActiveIndex === 0) {
169+
this.optionActiveIndex = this.filtedData.length - 1
170+
} else {
174171
this.optionActiveIndex--
175172
}
176173
},
177174
178175
activeNextOptions() {
179176
if (this.optionActiveIndex < this.filtedData.length - 1) {
180177
this.optionActiveIndex++
178+
} else {
179+
this.optionActiveIndex = 0
181180
}
182181
},
183182
@@ -217,6 +216,7 @@ export default {
217216
218217
// 添加当前组件选项值,在原有已选项上添加
219218
addSelection(filterFunc) {
219+
// istanbul ignore if
220220
if (!this.multiple) {
221221
// 单选模式下无法使用本方法
222222
throw Error(

tests/unit/basic.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { mount } from '@vue/test-utils'
2+
import ItemListSelector from '@/index.vue'
3+
4+
describe('Basic', () => {
5+
it('Data check', () => {
6+
const wrapper = mount(ItemListSelector)
7+
expect(wrapper.vm.keyword).toEqual('')
8+
expect(wrapper.vm.optionActiveIndex).toEqual(-1)
9+
})
10+
11+
it('Reset data check', () => {
12+
const wrapper = mount(ItemListSelector)
13+
wrapper.setData({
14+
keyword: 'test'
15+
})
16+
wrapper.setProps({
17+
selection: ['test']
18+
})
19+
wrapper.vm.reset()
20+
const emitted = wrapper.emittedByOrder()
21+
expect(wrapper.vm.keyword).toEqual('')
22+
expect(emitted.length).toEqual(1)
23+
expect(emitted[0].name).toEqual('selection-change')
24+
expect(emitted[0].args[0]).toEqual([])
25+
})
26+
27+
it('无数据空白组件', () => {
28+
const wrapper = mount(ItemListSelector)
29+
expect(wrapper.findAll('.item-selector__option').length).toEqual(0)
30+
})
31+
})

tests/unit/event.spec.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { mount } from '@vue/test-utils'
2+
import ItemListSelector from '@/index.vue'
3+
import getTestData from './getTestData.js'
4+
5+
describe('Events', () => {
6+
const wrapper = mount(ItemListSelector, {
7+
propsData: {
8+
data: getTestData(),
9+
selection: []
10+
}
11+
})
12+
const $input = wrapper.find('.item-selector__searchbar input')
13+
14+
it('鼠标点击选项', () => {
15+
wrapper
16+
.findAll('.item-selector__option')
17+
.at(0)
18+
.element.click()
19+
wrapper.setProps({ selection: wrapper.emitted()['selection-change'][0][0] })
20+
wrapper
21+
.findAll('.item-selector__option')
22+
.at(1)
23+
.element.click()
24+
wrapper.setProps({ selection: wrapper.emitted()['selection-change'][1][0] })
25+
wrapper
26+
.findAll('.item-selector__option')
27+
.at(1)
28+
.element.click()
29+
wrapper.setProps({ selection: wrapper.emitted()['selection-change'][2][0] })
30+
expect(wrapper.vm.selection.length).toEqual(1)
31+
expect(wrapper.vm.selection[0]).toEqual(wrapper.vm.data[0])
32+
expect(wrapper.emittedByOrder().map(r => r.name)).toEqual([
33+
'selection-change',
34+
'selection-change',
35+
'selection-change'
36+
])
37+
})
38+
39+
it('下箭头移动高亮选项', () => {
40+
wrapper.setData({
41+
optionActiveIndex: 2
42+
})
43+
expect(wrapper.findAll('.item-selector__option--active').length).toEqual(1)
44+
$input.trigger('keyup.down')
45+
expect(wrapper.vm.optionActiveIndex).toEqual(3)
46+
$input.trigger('keyup.down')
47+
expect(wrapper.vm.optionActiveIndex).toEqual(4)
48+
})
49+
50+
it('在底部选项边缘,下箭头移动高亮选项到第一项', () => {
51+
wrapper.setData({
52+
optionActiveIndex: wrapper.vm.filtedData.length - 1
53+
})
54+
$input.trigger('keyup.down')
55+
expect(wrapper.vm.optionActiveIndex).toEqual(0)
56+
})
57+
58+
it('上箭头移动高亮选项', () => {
59+
wrapper.setData({
60+
optionActiveIndex: 2
61+
})
62+
$input.trigger('keyup.up')
63+
expect(wrapper.vm.optionActiveIndex).toEqual(1)
64+
$input.trigger('keyup.up')
65+
expect(wrapper.vm.optionActiveIndex).toEqual(0)
66+
})
67+
68+
it('在底部选项边缘,上箭头移动高亮选项到第一项', () => {
69+
wrapper.setData({
70+
optionActiveIndex: 0
71+
})
72+
$input.trigger('keyup.up')
73+
expect(wrapper.vm.optionActiveIndex).toEqual(
74+
wrapper.vm.filtedData.length - 1
75+
)
76+
})
77+
})

tests/unit/example.spec.js

-12
This file was deleted.

tests/unit/getTestData.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default function getTestData() {
2+
return [
3+
{ label: 'data-01', value: '01' },
4+
{ label: 'data-02', value: '02' },
5+
{ label: 'data-03', value: '03' },
6+
{ label: 'data-04', value: '04' },
7+
{ label: 'data-05', value: '05' },
8+
{ label: 'data-06', value: '06' },
9+
{ label: 'data-07', value: '07' },
10+
{ label: 'data-08', value: '08' },
11+
{ label: 'data-09', value: '09' },
12+
{ label: 'data-10', value: '10' },
13+
{ label: 'data-11', value: '11' },
14+
{ label: 'data-12', value: '12' },
15+
{ label: 'data-13', value: '13' },
16+
{ label: 'data-14', value: '14' },
17+
{ label: 'data-15', value: '15' },
18+
{ label: 'data-16', value: '16' },
19+
{ label: 'data-17', value: '17' },
20+
{ label: 'data-18', value: '18' },
21+
{ label: 'data-19', value: '19' },
22+
{ label: 'data-20', value: '20' }
23+
]
24+
}

tests/unit/markMatch.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
describe('markMatch', () => {
2+
const markMatch = require('@/markMatch').default
3+
const testText = '1234567890'
4+
5+
it('Empty keyword', () => {
6+
const markResult = markMatch(testText, '')
7+
expect(markResult).toEqual(testText)
8+
})
9+
10+
it('Unmatch', () => {
11+
const markResult = markMatch(testText, 'a')
12+
expect(markResult).toEqual(testText)
13+
})
14+
15+
it('Mark match', () => {
16+
const markResult = markMatch(testText, '3')
17+
expect(markResult).toEqual('12<span class="match">3</span>4567890')
18+
})
19+
20+
it('Config', () => {
21+
const markResult = markMatch(testText, '3', {
22+
tag: 'div',
23+
className: 'keyword'
24+
})
25+
expect(markResult).toEqual('12<div class="keyword">3</div>4567890')
26+
})
27+
})

tests/unit/methods.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { mount } from '@vue/test-utils'
2+
import ItemListSelector from '@/index.vue'
3+
import getTestData from './getTestData.js'
4+
5+
describe('Methods', () => {
6+
it('setSelection', () => {
7+
const wrapper = mount(ItemListSelector, {
8+
propsData: {
9+
data: getTestData()
10+
}
11+
})
12+
const filterFunc = r => r.value.toString()[0] === '0'
13+
wrapper.vm.setSelection(filterFunc)
14+
expect(wrapper.emittedByOrder().length).toEqual(1)
15+
expect(wrapper.emittedByOrder()[0].name).toEqual('selection-change')
16+
expect(wrapper.emittedByOrder()[0].args[0].every(filterFunc)).toBeTruthy()
17+
})
18+
19+
it('addSelection', () => {
20+
const wrapper = mount(ItemListSelector, {
21+
propsData: {
22+
data: getTestData()
23+
}
24+
})
25+
wrapper.vm.addSelection(r => r.value.toString() === '10')
26+
wrapper.vm.addSelection(r => r.value.toString() === '20')
27+
expect(wrapper.emitted()['selection-change'].length).toEqual(2)
28+
expect(
29+
wrapper.emitted()['selection-change'][0][0][0].value.toString()
30+
).toEqual('10')
31+
expect(
32+
wrapper.emitted()['selection-change'][1][0][0].value.toString()
33+
).toEqual('20')
34+
})
35+
36+
it('removeSelection', () => {
37+
const wrapper = mount(ItemListSelector, {
38+
propsData: {
39+
data: getTestData()
40+
}
41+
})
42+
wrapper.vm.addSelection(r => r.value.toString() === '10')
43+
wrapper.setProps({
44+
selection: wrapper.emitted()['selection-change'][0][0]
45+
})
46+
wrapper.vm.removeSelection(r => r.value.toString() === '10')
47+
expect(wrapper.emitted()['selection-change'].length).toEqual(2)
48+
expect(
49+
wrapper.emitted()['selection-change'][0][0][0].value.toString()
50+
).toEqual('10')
51+
expect(wrapper.emitted()['selection-change'][1][0]).toEqual([])
52+
})
53+
})

0 commit comments

Comments
 (0)