Skip to content

Commit ca92bb9

Browse files
vaebekagol
authored andcommitted
test(Search): 组件测试用例完善
1 parent 51da9d1 commit ca92bb9

File tree

1 file changed

+180
-43
lines changed

1 file changed

+180
-43
lines changed

packages/devui-vue/devui/search/__tests__/search.spec.ts

Lines changed: 180 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,23 @@ const disableSearchClass = searchNs.m('disabled');
1313
const dotSearchClass = dotSearchNs.b();
1414
const dotClearSearchClass = dotSearchNs.e('clear');
1515
const dotIconSearchClass = dotSearchNs.e('icon');
16+
const leftIconPositionClass = searchNs.m('left');
17+
const rightIconPositionClass = searchNs.m('right');
18+
const noBorderClass = searchNs.m('no-border');
1619

1720
describe('search test', () => {
18-
// TODO: 这个单测应该按功能进行拆分
1921
it('should render correctly', async () => {
2022
const value = ref('test');
21-
const size = ref('');
22-
const disabled = ref(false);
2323
const wrapper = mount({
2424
components: { DSearch },
2525
template: `
2626
<d-search
27-
:size="size"
28-
:disabled="disabled"
2927
v-model="value"
3028
></d-search>
3129
`,
3230
setup() {
3331
return {
3432
value,
35-
size,
36-
disabled,
3733
};
3834
},
3935
});
@@ -42,7 +38,94 @@ describe('search test', () => {
4238
const input = search.find('input');
4339
expect(input.element.value).toBe('test');
4440

45-
// test size
41+
wrapper.unmount();
42+
});
43+
44+
it('should event correctly', async () => {
45+
const value = ref('test');
46+
const onSearch = jest.fn();
47+
const wrapper = mount({
48+
components: { DSearch },
49+
template: `
50+
<d-search
51+
v-model="value"
52+
@onSearch="onSearch"
53+
></d-search>
54+
`,
55+
setup() {
56+
return {
57+
value,
58+
onSearch,
59+
};
60+
},
61+
});
62+
const search = wrapper.find(dotSearchClass);
63+
const searchBtn = search.find(dotIconSearchClass);
64+
65+
await searchBtn.trigger('click');
66+
await onSearch((str: string) => {
67+
expect(str).toBe('test');
68+
});
69+
expect(onSearch).toBeCalledTimes(1);
70+
71+
// test input focus after trigger search button
72+
// TODO: 在单元测试环境中,input虽然处于focus状态,但是无法通过document.activeElement获取到
73+
// expect(input.element === document.activeElement).toBe(true);
74+
wrapper.unmount();
75+
});
76+
77+
it('props v-model should work well.', async () => {
78+
const value = ref('test');
79+
80+
const wrapper = mount({
81+
components: { DSearch },
82+
template: `
83+
<d-search
84+
v-model="value"
85+
></d-search>
86+
`,
87+
setup() {
88+
return {
89+
value,
90+
};
91+
},
92+
});
93+
94+
const search = wrapper.find(dotSearchClass);
95+
const input = search.find('input');
96+
expect(input.element.value).toBe('test');
97+
98+
// test v-model
99+
await input.setValue('def');
100+
expect(value.value).toBe('def');
101+
102+
value.value = 'change value';
103+
await nextTick();
104+
expect(input.element.value).toBe('change value');
105+
106+
wrapper.unmount();
107+
});
108+
109+
it('props size(sm/md/lg) should work well.', async () => {
110+
const size = ref('');
111+
112+
const wrapper = mount({
113+
components: { DSearch },
114+
template: `
115+
<d-search
116+
:size="size"
117+
></d-search>
118+
`,
119+
setup() {
120+
return {
121+
size,
122+
};
123+
},
124+
});
125+
126+
const search = wrapper.find(dotSearchClass);
127+
const input = search.find('input');
128+
46129
expect(input.classes()).not.toContain(smSearchClass);
47130
expect(input.classes()).not.toContain(lgSearchClass);
48131

@@ -55,23 +138,55 @@ describe('search test', () => {
55138
expect(wrapper.classes()).not.toContain(smSearchClass);
56139
expect(wrapper.classes()).toContain(lgSearchClass);
57140

58-
// test v-model
59-
await input.setValue('def');
60-
expect(value.value).toBe('def');
141+
wrapper.unmount();
142+
});
61143

62-
value.value = 'change value';
63-
await nextTick();
64-
expect(input.element.value).toBe('change value');
144+
it('clear operation should work well.', async () => {
145+
const value = ref('test');
146+
const wrapper = mount({
147+
components: { DSearch },
148+
template: `
149+
<d-search
150+
v-model="value"
151+
></d-search>
152+
`,
153+
setup() {
154+
return {
155+
value,
156+
};
157+
},
158+
});
159+
160+
const search = wrapper.find(dotSearchClass);
161+
const input = search.find('input');
162+
expect(input.element.value).toBe('test');
65163

66164
// test clear
67165
const clear = wrapper.find(dotClearSearchClass);
68166
await clear.trigger('click');
69167
expect(input.element.value).toBe('');
70168
expect(value.value).toBe('');
71169

72-
// test input focus after trigger clear button
73-
// TODO: 在单元测试环境中,input虽然处于focus状态,但是无法通过document.activeElement获取到
74-
// expect(input.element === document.activeElement).toBe(true);
170+
wrapper.unmount();
171+
});
172+
173+
it('props disabled should work well.', async () => {
174+
const disabled = ref(false);
175+
const wrapper = mount({
176+
components: { DSearch },
177+
template: `
178+
<d-search
179+
:disabled="disabled"
180+
></d-search>
181+
`,
182+
setup() {
183+
return {
184+
disabled,
185+
};
186+
},
187+
});
188+
const search = wrapper.find(dotSearchClass);
189+
const input = search.find('input');
75190

76191
// test disabled
77192
expect(input.attributes('disabled')).toBe(undefined);
@@ -81,55 +196,77 @@ describe('search test', () => {
81196
await nextTick();
82197
expect(wrapper.classes()).toContain(disableSearchClass);
83198
expect(input.attributes('disabled')).toBe('');
199+
200+
wrapper.unmount();
84201
});
85202

86-
it('should event correctly', async () => {
87-
const value = ref('test');
88-
const onSearch = jest.fn();
203+
it('props icon-position(right/left) should work well.', async () => {
204+
const iconPosition = ref('right');
205+
89206
const wrapper = mount({
90207
components: { DSearch },
91208
template: `
92209
<d-search
93-
v-model="value"
94-
@onSearch="onSearch"
210+
:icon-position="iconPosition"
95211
></d-search>
96212
`,
97213
setup() {
98214
return {
99-
value,
100-
onSearch,
215+
iconPosition,
101216
};
102217
},
103218
});
104-
const search = wrapper.find(dotSearchClass);
105-
const searchBtn = search.find(dotIconSearchClass);
106-
// const input = search.find('input');
107-
await searchBtn.trigger('click');
108-
await onSearch((str: string) => {
109-
expect(str).toBe('test');
110-
});
111-
expect(onSearch).toBeCalledTimes(1);
112219

113-
// test input focus after trigger search button
114-
// TODO: 在单元测试环境中,input虽然处于focus状态,但是无法通过document.activeElement获取到
115-
// expect(input.element === document.activeElement).toBe(true);
116-
});
220+
const iconSearch = wrapper.find(dotIconSearchClass);
117221

118-
it.todo('props size(sm/md/lg) should work well.');
222+
expect(iconSearch.exists()).toBe(true);
119223

120-
it.todo('props auto-focus should work well.');
224+
expect(wrapper.classes()).toContain(rightIconPositionClass);
121225

122-
it.todo('props is-keyup-search should work well.');
226+
iconPosition.value = 'left';
227+
await nextTick();
228+
expect(wrapper.classes()).toContain(leftIconPositionClass);
123229

124-
it.todo('props delay should work well.');
230+
iconPosition.value = 'right';
231+
await nextTick();
232+
expect(wrapper.classes()).toContain(rightIconPositionClass);
125233

126-
it.todo('props disabled should work well.');
234+
wrapper.unmount();
235+
});
127236

128-
it.todo('props icon-position should work well.');
237+
it('props no-border should work well.', async () => {
238+
const noBorder = ref(true);
239+
240+
const wrapper = mount({
241+
components: { DSearch },
242+
template: `
243+
<d-search
244+
:no-border="noBorder"
245+
></d-search>
246+
`,
247+
setup() {
248+
return {
249+
noBorder,
250+
};
251+
},
252+
});
253+
254+
expect(wrapper.classes()).toContain(noBorderClass);
255+
256+
noBorder.value = false;
257+
await nextTick();
258+
expect(wrapper.classes()).not.toContain(noBorderClass);
259+
260+
wrapper.unmount();
261+
});
129262

130263
it.todo('props placeholder should work well.');
131264

132-
it.todo('props no-border should work well.');
265+
it.todo('props auto-focus should work well.');
266+
267+
it.todo('props is-keyup-search should work well.');
268+
269+
it.todo('props delay should work well.');
133270

134271
it.todo('props max-length should work well.');
135272
});

0 commit comments

Comments
 (0)