Skip to content

Commit d49b2b4

Browse files
vaebekagol
authored andcommitted
feat(tag-input): 测试用例中的 class 写成变量方便维护
1 parent e03d2b8 commit d49b2b4

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ jest.mock('../../locale/create', () => ({
1414
}));
1515

1616
const ns = useNamespace('tag-input', true);
17+
const suggestionListCls = ns.e('suggestion-list');
18+
const suggestionListItemCls = ns.e('suggestion-list__item');
19+
const tagsItemCls = ns.e('tags__item');
20+
const inputCls = ns.e('input');
1721

1822
const customMount = (state: StateType) => mount({
1923
components: { DTagInput },
@@ -48,9 +52,9 @@ describe('DTagInput', () => {
4852
const wrapper = customMount(state);
4953
expect(wrapper.find(ns.b()).exists()).toBe(true);
5054
expect(wrapper.find(ns.e('tags')).exists()).toBe(true);
51-
expect(wrapper.find(ns.e('input')).exists()).toBe(true);
55+
expect(wrapper.find(inputCls).exists()).toBe(true);
5256

53-
const itemA = wrapper.find(ns.e('tags__item'));
57+
const itemA = wrapper.find(tagsItemCls);
5458
expect(itemA.exists()).toBe(true);
5559
expect(itemA.text()).toBe('Y.Chen');
5660

@@ -71,13 +75,13 @@ describe('DTagInput', () => {
7175
],
7276
});
7377
const wrapper = customMount(state);
74-
const input = wrapper.find(ns.e('input'));
78+
const input = wrapper.find(inputCls);
7579

76-
expect(wrapper.find(ns.e('suggestion-list')).exists()).toBe(false);
80+
expect(wrapper.find(suggestionListCls).exists()).toBe(false);
7781
await input.trigger('focus');
7882

7983
// 是否存在 devui-suggestion-list
80-
const suggestionList = !!document.querySelectorAll(ns.e('suggestion-list'))[0];
84+
const suggestionList = !!document.querySelectorAll(suggestionListCls)[0];
8185
expect(suggestionList).toBe(true);
8286

8387
wrapper.unmount();
@@ -99,7 +103,7 @@ describe('DTagInput', () => {
99103
});
100104

101105
expect(wrapper.find('.is-disabled').exists()).toBe(false);
102-
expect(wrapper.find(ns.e('input')).isVisible()).toBe(true);
106+
expect(wrapper.find(inputCls).isVisible()).toBe(true);
103107

104108
await wrapper.setProps({
105109
disabled: true,
@@ -150,7 +154,7 @@ describe('DTagInput', () => {
150154
wrapper.find('.remove-button').trigger('click');
151155

152156
nextTick(() => {
153-
expect(wrapper.findAll(ns.e('tags__item')).length).toBe(1);
157+
expect(wrapper.findAll(tagsItemCls).length).toBe(1);
154158
expect(state.tags.length).toBe(1);
155159
expect(state.suggestionList.length).toBe(2);
156160

@@ -201,17 +205,17 @@ describe('DTagInput', () => {
201205
const input = wrapper.find('input');
202206

203207
await input.trigger('focus');
204-
let suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
208+
let suggestionList = document.querySelectorAll(suggestionListItemCls);
205209
expect(suggestionList.length).toBe(3);
206210

207211
await input.setValue('xy');
208212
await input.trigger('input');
209-
suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
213+
suggestionList = document.querySelectorAll(suggestionListItemCls);
210214
expect(suggestionList.length).toBe(2);
211215

212216
await input.setValue('xxx');
213217
await input.trigger('input');
214-
suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
218+
suggestionList = document.querySelectorAll(suggestionListItemCls);
215219
expect(suggestionList.length).toBe(1);
216220

217221
wrapper.unmount();
@@ -231,7 +235,7 @@ describe('DTagInput', () => {
231235
});
232236
const wrapper = customMount(state);
233237
await wrapper.find('input').trigger('focus');
234-
const suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
238+
const suggestionList = document.querySelectorAll(suggestionListItemCls);
235239
const yyy = suggestionList[1];
236240
yyy.dispatchEvent(new Event('click'));
237241

@@ -257,18 +261,18 @@ describe('DTagInput', () => {
257261
const wrapper = customMount(state);
258262
const input = wrapper.find('input');
259263
await input.trigger('focus');
260-
let suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
264+
let suggestionList = document.querySelectorAll(suggestionListItemCls);
261265
// 获取焦点默认第一个选中
262266
expect(suggestionList[0].className).toContain('selected');
263267

264268
// 按下 下箭头,选中第二个数组第一个
265269
await input.trigger('keydown', { key: 'ArrowDown' });
266-
suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
270+
suggestionList = document.querySelectorAll(suggestionListItemCls);
267271
expect(suggestionList[1].className).toContain('selected');
268272

269273
await input.trigger('keydown', { key: 'ArrowUp' });
270274
await input.trigger('keydown', { key: 'ArrowUp' });
271-
suggestionList = document.querySelectorAll(ns.e('suggestion-list__item'));
275+
suggestionList = document.querySelectorAll(suggestionListItemCls);
272276
expect(suggestionList[2].className).toContain('selected');
273277

274278
// 按下Enter选中数据

0 commit comments

Comments
 (0)