Skip to content

Commit 1d1e0ce

Browse files
committed
test(Dialog): improve dialog testcase
1 parent 05bd3cf commit 1d1e0ce

File tree

8 files changed

+2300
-305
lines changed

8 files changed

+2300
-305
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
// @ts-nocheck
2+
import { mount } from '@vue/test-utils';
3+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
4+
import { ref, nextTick } from 'vue';
5+
import { CheckCircleFilledIcon, CloseIcon } from 'tdesign-icons-vue-next';
6+
import DialogCard from '../dialog-card';
7+
8+
beforeEach(() => {
9+
document.body.innerHTML = '';
10+
});
11+
12+
afterEach(() => {
13+
document.body.innerHTML = '';
14+
});
15+
16+
describe('props', () => {
17+
it('header[string]', async () => {
18+
const wrapper = mount(() => <DialogCard header="Test Title" body="Test Content" />);
19+
20+
await nextTick();
21+
expect(wrapper.find('.t-dialog__header').exists()).toBe(true);
22+
expect(wrapper.find('.t-dialog__header').text()).toBe('Test Title');
23+
});
24+
25+
it('header[function]', async () => {
26+
const renderHeader = () => <span class="custom-header">Custom Header</span>;
27+
const wrapper = mount(() => <DialogCard header={renderHeader} body="Content" />);
28+
29+
await nextTick();
30+
expect(wrapper.find('.custom-header').exists()).toBe(true);
31+
expect(wrapper.find('.custom-header').text()).toBe('Custom Header');
32+
});
33+
34+
it('header[slot]', async () => {
35+
const wrapper = mount(() => (
36+
<DialogCard body="Content" v-slots={{ header: () => <div class="slot-header">Slot Header</div> }}></DialogCard>
37+
));
38+
39+
await nextTick();
40+
expect(wrapper.find('.slot-header').exists()).toBe(true);
41+
expect(wrapper.find('.slot-header').text()).toBe('Slot Header');
42+
});
43+
44+
it('body[string]', async () => {
45+
const wrapper = mount(() => <DialogCard header="Title" body="String Body" />);
46+
47+
await nextTick();
48+
const body = wrapper.find('.t-dialog__body');
49+
expect(body.exists()).toBe(true);
50+
expect(body.text()).toBe('String Body');
51+
});
52+
53+
it('body[function]', async () => {
54+
const renderBody = () => <div class="custom-body">Custom Body</div>;
55+
const wrapper = mount(() => <DialogCard header="Title" body={renderBody} />);
56+
57+
await nextTick();
58+
expect(wrapper.find('.custom-body').exists()).toBe(true);
59+
expect(wrapper.find('.custom-body').text()).toBe('Custom Body');
60+
});
61+
62+
it('body[slot]', async () => {
63+
const wrapper = mount(() => (
64+
<DialogCard header="Title" v-slots={{ body: () => <div class="slot-body">Slot Body</div> }}></DialogCard>
65+
));
66+
67+
await nextTick();
68+
expect(wrapper.find('.slot-body').exists()).toBe(true);
69+
expect(wrapper.find('.slot-body').text()).toBe('Slot Body');
70+
});
71+
72+
it('footer[boolean]', async () => {
73+
// footer=true 应该显示默认按钮
74+
const wrapperTrue = mount(() => <DialogCard header="Title" body="Content" footer={true} />);
75+
await nextTick();
76+
expect(wrapperTrue.find('.t-dialog__footer').exists()).toBe(true);
77+
78+
// footer=false 应该隐藏footer
79+
const wrapperFalse = mount(() => <DialogCard header="Title" body="Content" footer={false} />);
80+
await nextTick();
81+
expect(wrapperFalse.find('.t-dialog__footer').exists()).toBe(false);
82+
});
83+
84+
it('footer[function]', async () => {
85+
const renderFooter = () => <div class="custom-footer">Custom Footer</div>;
86+
const wrapper = mount(() => <DialogCard header="Title" body="Content" footer={renderFooter} />);
87+
88+
await nextTick();
89+
expect(wrapper.find('.custom-footer').exists()).toBe(true);
90+
expect(wrapper.find('.custom-footer').text()).toBe('Custom Footer');
91+
});
92+
93+
it('footer[slot]', async () => {
94+
const wrapper = mount(() => (
95+
<DialogCard
96+
header="Title"
97+
body="Content"
98+
footer={true}
99+
v-slots={{ footer: () => <div class="slot-footer">Slot Footer</div> }}
100+
></DialogCard>
101+
));
102+
103+
await nextTick();
104+
expect(wrapper.find('.slot-footer').exists()).toBe(true);
105+
expect(wrapper.find('.slot-footer').text()).toBe('Slot Footer');
106+
});
107+
108+
it('theme[string]', async () => {
109+
const themes = ['default', 'info', 'warning', 'danger', 'success', ''] as const;
110+
111+
themes.forEach(async (theme) => {
112+
const wrapper = mount(() => <DialogCard header="Title" body="Content" theme={theme} />);
113+
await nextTick();
114+
const dialog = wrapper.find('.t-dialog');
115+
expect(dialog.classes()).toContain(`t-dialog__modal-${theme}`);
116+
});
117+
});
118+
119+
it('closeBtn[boolean]', async () => {
120+
// 默认应该显示关闭按钮
121+
const wrapperDefault = mount(() => <DialogCard header="Title" body="Content" />);
122+
await nextTick();
123+
expect(wrapperDefault.find('.t-dialog__close').exists()).toBe(true);
124+
125+
// closeBtn=false 应该隐藏关闭按钮
126+
const wrapperFalse = mount(() => <DialogCard header="Title" body="Content" closeBtn={false} />);
127+
await nextTick();
128+
expect(wrapperFalse.find('.t-dialog__close').exists()).toBe(false);
129+
});
130+
131+
it('closeBtn[function]', async () => {
132+
const renderCloseBtn = () => <span class="custom-close">×</span>;
133+
const wrapper = mount(() => <DialogCard header="Title" body="Content" closeBtn={renderCloseBtn} />);
134+
135+
await nextTick();
136+
expect(wrapper.find('.custom-close').exists()).toBe(true);
137+
expect(wrapper.find('.custom-close').text()).toBe('×');
138+
});
139+
140+
it('placement[string]', async () => {
141+
const placements = ['top', 'center'] as const;
142+
143+
placements.forEach(async (placement) => {
144+
const wrapper = mount(() => <DialogCard header="Title" body="Content" placement={placement} />);
145+
await nextTick();
146+
const dialog = wrapper.find('.t-dialog');
147+
expect(dialog.classes()).toContain(`t-dialog--${placement}`);
148+
});
149+
});
150+
151+
it('width[number]', async () => {
152+
const wrapper = mount(() => <DialogCard header="Title" body="Content" width={500} />);
153+
154+
await nextTick();
155+
const dialog = wrapper.find('.t-dialog');
156+
expect(dialog.element.style.width).toBe('500px');
157+
});
158+
159+
it('width[string]', async () => {
160+
const wrapper = mount(() => <DialogCard header="Title" body="Content" width="80%" />);
161+
162+
await nextTick();
163+
const dialog = wrapper.find('.t-dialog');
164+
expect(dialog.element.style.width).toBe('80%');
165+
});
166+
167+
it('draggable[boolean]', async () => {
168+
const wrapper = mount(() => <DialogCard header="Title" body="Content" mode="modeless" draggable={true} />);
169+
170+
await nextTick();
171+
const dialog = wrapper.find('.t-dialog');
172+
expect(dialog.classes()).toContain('t-dialog--draggable');
173+
});
174+
175+
it('mode[string]', async () => {
176+
const modes = ['modal', 'modeless'] as const;
177+
178+
modes.forEach(async (mode) => {
179+
const wrapper = mount(() => <DialogCard header="Title" body="Content" mode={mode} />);
180+
await nextTick();
181+
// 这里需要根据实际的class名称进行调整
182+
const dialog = wrapper.find('.t-dialog');
183+
expect(dialog.exists()).toBe(true);
184+
});
185+
});
186+
187+
it('zIndex[number]', async () => {
188+
const wrapper = mount(() => <DialogCard header="Title" body="Content" zIndex={9999} />);
189+
190+
await nextTick();
191+
const dialog = wrapper.find('.t-dialog');
192+
expect(dialog.exists()).toBe(true);
193+
// 这里需要根据实际的样式应用方式进行调整
194+
});
195+
196+
it('preventScrollThrough[boolean]', async () => {
197+
const wrapper = mount(() => <DialogCard header="Title" body="Content" preventScrollThrough={true} />);
198+
199+
await nextTick();
200+
const dialog = wrapper.find('.t-dialog');
201+
expect(dialog.exists()).toBe(true);
202+
// 这里需要根据实际的行为进行测试调整
203+
});
204+
205+
it('should switch body className correctly in full-screen mode', async () => {
206+
// footer 存在时
207+
const wrapperWithFooter = mount(() => (
208+
<DialogCard header="Title" body="Content" mode="full-screen" footer={true} />
209+
));
210+
await nextTick();
211+
const bodyWithFooter = wrapperWithFooter.find('.t-dialog__body');
212+
expect(bodyWithFooter.exists()).toBe(true);
213+
expect(bodyWithFooter.classes()).toContain('t-dialog__body--fullscreen');
214+
215+
// footer 不存在时
216+
const wrapperWithoutFooter = mount(() => (
217+
<DialogCard header="Title" body="Content" mode="full-screen" footer={false} />
218+
));
219+
await nextTick();
220+
const bodyWithoutFooter = wrapperWithoutFooter.find('.t-dialog__body');
221+
expect(bodyWithoutFooter.exists()).toBe(true);
222+
expect(bodyWithoutFooter.classes()).toContain('t-dialog__body--fullscreen--without-footer');
223+
});
224+
});
225+
226+
describe('events', () => {
227+
it('onCloseBtnClick', async () => {
228+
const onCloseBtnClick = vi.fn();
229+
const wrapper = mount(() => <DialogCard header="Title" body="Content" onCloseBtnClick={onCloseBtnClick} />);
230+
231+
await nextTick();
232+
const closeBtn = wrapper.find('.t-dialog__close');
233+
await closeBtn.trigger('click');
234+
expect(onCloseBtnClick).toHaveBeenCalled();
235+
});
236+
237+
it('onStopDown: should call stopPropagation when mode is modeless and draggable is true', async () => {
238+
const stopPropagation = vi.fn();
239+
const wrapper = mount(() => <DialogCard header="Title" body="Content" mode="modeless" draggable={true} />);
240+
await nextTick();
241+
// 触发 body 区域的 mousedown
242+
const body = wrapper.find('.t-dialog__body');
243+
await body.trigger('mousedown', { stopPropagation });
244+
expect(stopPropagation).toHaveBeenCalled();
245+
});
246+
});
247+
248+
describe('slots', () => {
249+
it('default slot', async () => {
250+
const wrapper = mount(() => (
251+
<DialogCard header="Title">
252+
<div class="default-slot">Default Slot Content</div>
253+
</DialogCard>
254+
));
255+
256+
await nextTick();
257+
expect(wrapper.find('.default-slot').exists()).toBe(true);
258+
expect(wrapper.find('.default-slot').text()).toBe('Default Slot Content');
259+
});
260+
});
261+
262+
describe('edge cases', () => {
263+
it('handles empty props', async () => {
264+
const wrapper = mount(() => <DialogCard header="" body="" />);
265+
266+
await nextTick();
267+
// 空字符串仍应渲染对应结构
268+
expect(wrapper.find('.t-dialog__header').exists()).toBe(true);
269+
expect(wrapper.find('.t-dialog__body').exists()).toBe(true);
270+
});
271+
272+
it('handles undefined props', async () => {
273+
const wrapper = mount(() => <DialogCard />);
274+
275+
await nextTick();
276+
// 未提供props时的默认行为
277+
expect(wrapper.find('.t-dialog').exists()).toBe(true);
278+
});
279+
});

0 commit comments

Comments
 (0)