Skip to content

Commit ed8b5a9

Browse files
vaebekagol
authored andcommitted
test(tag-input): 修复因为逻辑更改导致的测试错误
1 parent 961bad4 commit ed8b5a9

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

packages/devui-vue/devui/tag-input/__tests__/tag-input.spec.ts

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import { mount } from '@vue/test-utils';
22
import { reactive, nextTick } from 'vue';
33
import DTagInput from '../src/tag-input';
4+
import { useNamespace } from '../../shared/hooks/use-namespace';
45

56
jest.mock('../../locale/create', () => ({
67
createI18nTranslate: () => jest.fn(),
78
}));
89

10+
const ns = useNamespace('tag-input', true);
11+
912
const customMount = (state) => mount({
1013
components: { DTagInput },
1114
template: `
1215
<d-tag-input
1316
v-model:tags="state.tags"
1417
v-model:suggestionList="state.suggestionList"
15-
displayProperty="cname"></d-tag-input>
18+
displayProperty="cname"
19+
></d-tag-input>
1620
`,
17-
setup () {
21+
setup() {
1822
return {
19-
state
23+
state,
2024
};
21-
}
25+
},
2226
});
2327

2428
describe('DTagInput', () => {
@@ -27,17 +31,16 @@ describe('DTagInput', () => {
2731
tags: [
2832
{ cname: 'Y.Chen' },
2933
{ cname: 'b' },
30-
{ cname: 'c' }
34+
{ cname: 'c' },
3135
],
3236
suggestionList: [
3337
{ cname: 'd' },
3438
{ cname: 'e' },
3539
{ cname: 'f' },
36-
]
40+
],
3741
});
3842
const wrapper = customMount(state);
39-
40-
expect(wrapper.find('.devui-tags-host').exists()).toBe(true);
43+
expect(wrapper.find(ns.b()).exists()).toBe(true);
4144
expect(wrapper.find('.devui-tags').exists()).toBe(true);
4245
expect(wrapper.find('.devui-tag-list').exists()).toBe(true);
4346
expect(wrapper.find('.devui-input').exists()).toBe(true);
@@ -49,6 +52,8 @@ describe('DTagInput', () => {
4952
state.tags[0] = { cname: 'X.Zhang' };
5053
await nextTick();
5154
expect(itemA.text()).toBe('X.Zhang');
55+
56+
wrapper.unmount();
5257
});
5358

5459
it('tag-input show suggestion work', async () => {
@@ -58,14 +63,19 @@ describe('DTagInput', () => {
5863
],
5964
suggestionList: [
6065
{ cname: 'b' },
61-
]
66+
],
6267
});
6368
const wrapper = customMount(state);
6469
const input = wrapper.find('input.devui-input');
6570

6671
expect(wrapper.find('.devui-suggestion-list').exists()).toBe(false);
6772
await input.trigger('focus');
68-
expect(wrapper.find('.devui-suggestion-list').exists()).toBe(true);
73+
74+
// 是否存在 devui-suggestion-list
75+
const suggestionList = !!document.querySelectorAll('.devui-suggestion-list')[0];
76+
expect(suggestionList).toBe(true);
77+
78+
wrapper.unmount();
6979
});
7080

7181
it('tag-input disabled work', async () => {
@@ -79,19 +89,21 @@ describe('DTagInput', () => {
7989
props: {
8090
tags,
8191
suggestionList,
82-
disabled: false
83-
}
92+
disabled: false,
93+
},
8494
});
8595

8696
expect(wrapper.find('.devui-disabled').exists()).toBe(false);
8797
expect(wrapper.find('.devui-input').isVisible()).toBe(true);
8898

8999
await wrapper.setProps({
90-
disabled: true
100+
disabled: true,
91101
});
92102
expect(wrapper.find('.devui-disabled').exists()).toBe(true);
93103
expect(wrapper.find('.devui-input').isVisible()).toBe(false);
94104
expect(wrapper.find('.remove-button').exists()).toBe(false);
105+
106+
wrapper.unmount();
95107
});
96108

97109
it('tag-input maxTags work', () => {
@@ -106,11 +118,13 @@ describe('DTagInput', () => {
106118
props: {
107119
tags,
108120
suggestionList,
109-
maxTags: 1
110-
}
121+
maxTags: 1,
122+
},
111123
});
112124

113125
expect(wrapper.find('input').attributes('disabled')).toBe('');
126+
127+
wrapper.unmount();
114128
});
115129

116130
it('tag-input removeTag work', async () => {
@@ -121,14 +135,16 @@ describe('DTagInput', () => {
121135
],
122136
suggestionList: [
123137
{ cname: 'c' },
124-
]
138+
],
125139
});
126140
const wrapper = customMount(state);
127141
const removeSvg = wrapper.find('.remove-button');
128-
await removeSvg.trigger('mousedown');
142+
await removeSvg.trigger('click');
129143
expect(wrapper.findAll('.devui-tag-item').length).toBe(1);
130144
expect(state.tags.length).toBe(1);
131145
expect(state.suggestionList.length).toBe(2);
146+
147+
wrapper.unmount();
132148
});
133149

134150
it('tag-input keydown work', async () => {
@@ -139,8 +155,8 @@ describe('DTagInput', () => {
139155
],
140156
suggestionList: [
141157
{ cname: 'c' },
142-
{ cname: 'xyz' }
143-
]
158+
{ cname: 'xyz' },
159+
],
144160
});
145161
const wrapper = customMount(state);
146162
const input = wrapper.find('input');
@@ -154,6 +170,8 @@ describe('DTagInput', () => {
154170
expect(state.tags.length).toBe(4);
155171
expect(state.tags[3].cname).toBe('xyz');
156172
expect(state.suggestionList.length).toBe(1);
173+
174+
wrapper.unmount();
157175
});
158176

159177
it('tag-input filter suggestion work', async () => {
@@ -165,22 +183,27 @@ describe('DTagInput', () => {
165183
suggestionList: [
166184
{ cname: 'x' },
167185
{ cname: 'xy' },
168-
{ cname: 'xyz' }
169-
]
186+
{ cname: 'xyz' },
187+
],
170188
});
171189
const wrapper = customMount(state);
172190
const input = wrapper.find('input');
173191

174192
await input.trigger('focus');
175-
expect(wrapper.findAll('.devui-suggestion-item').length).toBe(3);
193+
let suggestionList = document.querySelectorAll('.devui-suggestion-item');
194+
expect(suggestionList.length).toBe(3);
176195

177196
await input.setValue('xy');
178197
await input.trigger('input');
179-
expect(wrapper.findAll('.devui-suggestion-item').length).toBe(2);
198+
suggestionList = document.querySelectorAll('.devui-suggestion-item');
199+
expect(suggestionList.length).toBe(2);
180200

181201
await input.setValue('xxx');
182202
await input.trigger('input');
183-
expect(wrapper.findAll('.devui-suggestion-item.devui-disabled').length).toBe(1);
203+
suggestionList = document.querySelectorAll('.devui-suggestion-item');
204+
expect(suggestionList.length).toBe(1);
205+
206+
wrapper.unmount();
184207
});
185208

186209
it('tag-input click suggestion work', async () => {
@@ -192,17 +215,20 @@ describe('DTagInput', () => {
192215
suggestionList: [
193216
{ cname: 'x' },
194217
{ cname: 'yyy' },
195-
{ cname: 'xyz' }
196-
]
218+
{ cname: 'xyz' },
219+
],
197220
});
198221
const wrapper = customMount(state);
199222
await wrapper.find('input').trigger('focus');
200-
const yyy = wrapper.findAll('.devui-suggestion-item')[1];
223+
const suggestionList = document.querySelectorAll('.devui-suggestion-item');
224+
const yyy = suggestionList[1];
225+
yyy.dispatchEvent(new Event('click'));
201226

202-
await yyy.trigger('mousedown');
203227
expect(state.tags.length).toBe(3);
204228
expect(state.tags[2].cname).toBe('yyy');
205229
expect(state.suggestionList.length).toBe(2);
230+
231+
wrapper.unmount();
206232
});
207233

208234
it('tag-input arrow work', async () => {
@@ -214,24 +240,31 @@ describe('DTagInput', () => {
214240
suggestionList: [
215241
{ cname: 'x' },
216242
{ cname: 'yyy' },
217-
{ cname: 'xyz' }
218-
]
243+
{ cname: 'xyz' },
244+
],
219245
});
220246
const wrapper = customMount(state);
221247
const input = wrapper.find('input');
222248
await input.trigger('focus');
249+
let suggestionList = document.querySelectorAll('.devui-suggestion-item');
250+
// 获取焦点默认第一个选中
251+
expect(suggestionList[0].className).toContain('selected');
223252

224-
expect(wrapper.findAll('.devui-suggestion-item')[0].classes()).toContain('selected');
225-
253+
// 按下 下箭头,选中第二个数组第一个
226254
await input.trigger('keydown', { key: 'ArrowDown' });
227-
expect(wrapper.findAll('.devui-suggestion-item')[1].classes()).toContain('selected');
255+
suggestionList = document.querySelectorAll('.devui-suggestion-item');
256+
expect(suggestionList[1].className).toContain('selected');
228257

229258
await input.trigger('keydown', { key: 'ArrowUp' });
230259
await input.trigger('keydown', { key: 'ArrowUp' });
231-
expect(wrapper.findAll('.devui-suggestion-item')[2].classes()).toContain('selected');
260+
suggestionList = document.querySelectorAll('.devui-suggestion-item');
261+
expect(suggestionList[2].className).toContain('selected');
232262

263+
// 按下Enter选中数据
233264
await input.trigger('keydown', { key: 'Enter' });
234265
expect(state.tags[2].cname).toBe('xyz');
235266
expect(state.suggestionList.length).toBe(2);
267+
268+
wrapper.unmount();
236269
});
237270
});

0 commit comments

Comments
 (0)