Skip to content

Commit 409c0b0

Browse files
authored
test: refactor tests for Tag component (#328)
1 parent 340dbfe commit 409c0b0

1 file changed

Lines changed: 231 additions & 20 deletions

File tree

packages/arcodesign/components/tag/__test__/index.spec.js

Lines changed: 231 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,138 @@ demoTest('tag');
1414
mountTest(Tag, 'Tag');
1515

1616
describe('Tag', () => {
17-
mountTest(() => <Tag type="hollow">Tag</Tag>, 'should mount correctly when set type prop');
18-
mountTest(() => <Tag size="small">Tag</Tag>, 'should mount correctly when set size prop');
17+
// 测试组件在不同属性下的挂载和卸载
18+
// Test component mounting and unmounting with different props
19+
it('should mount and unmount correctly with different props', () => {
20+
const { unmount: unmount1 } = render(<Tag type="hollow">Tag</Tag>);
21+
const { unmount: unmount2 } = render(<Tag size="small">Tag</Tag>);
22+
expect(() => {
23+
unmount1();
24+
unmount2();
25+
}).not.toThrow();
26+
});
27+
28+
// 测试点击事件处理
29+
// Test click event handling
30+
it('should handle click events correctly', () => {
31+
const mockOnClick = jest.fn();
32+
const mockOnClose = jest.fn();
1933

20-
it('should callback correctly when close icon be clicked', () => {
21-
const mockFn = jest.fn();
2234
const { container } = render(
23-
<Tag closeable onClose={mockFn}>
35+
<Tag closeable onClick={mockOnClick} onClose={mockOnClose}>
2436
标签
2537
</Tag>,
2638
);
27-
expect(container.querySelectorAll('.tag-close-wrap').length).toBe(1);
28-
userEvent.click(container.querySelector('.tag-close-wrap'));
29-
expect(mockFn).toBeCalled();
30-
});
3139

32-
it('should callback correctly when click tag', () => {
33-
const mockFn = jest.fn();
34-
const { container } = render(<Tag onClick={mockFn}>标签</Tag>);
40+
// 测试标签点击
41+
// Test tag click
3542
userEvent.click(container.firstChild);
36-
expect(mockFn).toBeCalled();
43+
expect(mockOnClick).toBeCalled();
44+
45+
// 测试关闭按钮点击
46+
// Test close button click
47+
expect(container.querySelectorAll('.tag-close-wrap').length).toBe(1);
48+
userEvent.click(container.querySelector('.tag-close-wrap'));
49+
expect(mockOnClose).toBeCalled();
3750
});
3851

39-
it('should render correctly when set filleted/halfBorder/borderStyle', () => {
52+
// 测试各种样式属性的渲染
53+
// Test rendering with various style properties
54+
it('should render correctly with various style props', () => {
4055
const { container } = render(
41-
<Tag filleted halfBorder borderStyle="solid">
56+
<Tag
57+
type="primary"
58+
size="small"
59+
filleted
60+
halfBorder
61+
borderStyle="solid"
62+
color="#ff0000"
63+
bgColor="#00ff00"
64+
borderColor="#0000ff"
65+
>
4266
标签
4367
</Tag>,
4468
);
45-
const relevantComponent = container.querySelector(`.${prefix}`);
46-
expect(relevantComponent).toHaveClass('half-border');
47-
expect(relevantComponent.style.borderStyle).toBe('solid');
48-
expect(relevantComponent).toHaveClass('filleted');
69+
70+
const tagElement = container.querySelector(`.${prefix}`);
71+
72+
// 测试类名
73+
// Test class names
74+
expect(tagElement).toHaveClass('half-border');
75+
expect(tagElement).toHaveClass('filleted');
76+
expect(tagElement).toHaveClass(`${prefix}-primary`);
77+
expect(tagElement).toHaveClass('size-small');
78+
79+
// 测试样式
80+
// Test styles
81+
expect(tagElement.style.borderStyle).toBe('solid');
82+
expect(tagElement.style.color).toMatch(/^(rgb\(255, 0, 0\)|#ff0000)$/);
83+
expect(tagElement.style.background).toMatch(/^(rgb\(0, 255, 0\)|#00ff00)$/);
84+
expect(tagElement.style.borderColor).toMatch(/^(rgb\(0, 0, 255\)|#0000ff)$/);
85+
});
86+
87+
// 测试不同类型和尺寸的组合渲染
88+
// Test rendering different type and size combinations
89+
it('should render different types and sizes correctly', () => {
90+
const { container: container1 } = render(
91+
<Tag type="hollow" size="medium">
92+
Hollow Medium
93+
</Tag>,
94+
);
95+
const { container: container2 } = render(
96+
<Tag type="solid" size="large">
97+
Solid Large
98+
</Tag>,
99+
);
100+
101+
expect(container1.querySelector(`.${prefix}-hollow.size-medium`)).toBeInTheDocument();
102+
expect(container2.querySelector(`.${prefix}-solid.size-large`)).toBeInTheDocument();
103+
});
104+
105+
// 测试边框样式处理
106+
// Test border style handling
107+
it('should handle border styles correctly', () => {
108+
const { container: container1 } = render(
109+
<Tag borderStyle="dashed" halfBorder={false}>
110+
Dashed
111+
</Tag>,
112+
);
113+
const { container: container2 } = render(<Tag borderStyle="dotted">Dotted</Tag>);
114+
115+
const tag1 = container1.querySelector(`.${prefix}`);
116+
const tag2 = container2.querySelector(`.${prefix}`);
117+
118+
expect(tag1.style.borderStyle).toBe('dashed');
119+
expect(tag1).not.toHaveClass('half-border');
120+
expect(tag2.style.borderStyle).toBe('dotted');
121+
});
122+
123+
// 测试图标渲染
124+
// Test icon rendering
125+
it('should render icons correctly', () => {
126+
const TestIcon = () => <span data-testid="test-icon">🏷️</span>;
127+
const CustomCloseIcon = () => <span data-testid="custom-close"></span>;
128+
129+
const { getByTestId, container } = render(
130+
<Tag closeable icon={<TestIcon />} closeIcon={<CustomCloseIcon />} closeColor="#ff0000">
131+
With Icons
132+
</Tag>,
133+
);
134+
135+
expect(getByTestId('test-icon')).toBeInTheDocument();
136+
expect(getByTestId('custom-close')).toBeInTheDocument();
49137
});
50138

51-
it('should render correctly when use tag list', () => {
139+
// 测试可关闭属性处理
140+
// Test closeable property handling
141+
it('should handle closeable prop correctly', () => {
142+
const { container } = render(<Tag closeable={false}>No Close</Tag>);
143+
expect(container.querySelector('.tag-close-wrap')).not.toBeInTheDocument();
144+
});
145+
146+
// 测试标签列表渲染
147+
// Test tag list rendering
148+
it('should render tag list correctly', () => {
52149
const list = [1, 1].map(item => ({
53150
closeable: true,
54151
children: `标签${item}`,
@@ -58,3 +155,117 @@ describe('Tag', () => {
58155
expect(container.querySelectorAll('.tag-list-add-wrap').length).toBe(1);
59156
});
60157
});
158+
159+
describe('Tag.List', () => {
160+
// 测试内边距配置处理
161+
// Test padding configuration handling
162+
it('should handle padding configurations correctly', () => {
163+
const list = [{ children: 'Tag 1' }, { children: 'Tag 2' }];
164+
165+
// 测试字符串和数字padding
166+
// Test string and number padding
167+
const { container: container1 } = render(
168+
<Tag.List list={list} verticalPadding="10px" horizontalPadding="5px" />,
169+
);
170+
const { container: container2 } = render(
171+
<Tag.List list={list} verticalPadding={20} horizontalPadding={10} />,
172+
);
173+
174+
const listElement1 = container1.querySelector(`.${defaultContext.prefixCls}-tag-list`);
175+
const listElement2 = container2.querySelector(`.${defaultContext.prefixCls}-tag-list`);
176+
177+
expect(listElement1.style.marginBottom).toBe('-10px');
178+
expect(listElement2.style.marginBottom).toBe('-20px');
179+
180+
// 测试标签间距
181+
// Test tag spacing
182+
const tags2 = container2.querySelectorAll(`.${prefix}`);
183+
expect(tags2[0].style.marginRight).toBe('10px');
184+
expect(tags2[0].style.marginBottom).toBe('20px');
185+
});
186+
187+
// 测试添加按钮功能
188+
// Test add button functionality
189+
it('should handle add button functionality', () => {
190+
const mockAddFn = jest.fn();
191+
const list = [{ children: 'Tag 1' }];
192+
const CustomAddArea = () => <div data-testid="custom-add">Custom Add</div>;
193+
194+
// 测试默认添加按钮
195+
// Test default add button
196+
const { container: container1 } = render(<Tag.List list={list} onAdd={mockAddFn} />);
197+
userEvent.click(container1.querySelector('.tag-list-add-wrap'));
198+
expect(mockAddFn).toBeCalled();
199+
200+
// 测试自定义添加区域
201+
// Test custom add area
202+
const { getByTestId } = render(<Tag.List list={list} addArea={<CustomAddArea />} />);
203+
expect(getByTestId('custom-add')).toBeInTheDocument();
204+
205+
// 测试隐藏添加按钮
206+
// Test hide add button
207+
const { container: container3 } = render(<Tag.List list={list} showAddButton={false} />);
208+
expect(container3.querySelector('.tag-list-add-wrap')).not.toBeInTheDocument();
209+
});
210+
211+
// 测试标签交互和样式
212+
// Test tag interactions and styling
213+
it('should handle tag interactions and styling', () => {
214+
const mockCloseFn = jest.fn();
215+
const mockTagCloseFn = jest.fn();
216+
const customStyle = { backgroundColor: 'red' };
217+
218+
const list = [
219+
{
220+
children: 'Tag 1',
221+
closeable: true,
222+
onClose: mockTagCloseFn,
223+
style: { color: 'blue' },
224+
},
225+
];
226+
227+
const { container } = render(
228+
<Tag.List
229+
list={list}
230+
onClose={mockCloseFn}
231+
type="hollow"
232+
className="custom-tag-list"
233+
style={customStyle}
234+
horizontalPadding={15}
235+
verticalPadding={10}
236+
/>,
237+
);
238+
239+
// 测试关闭功能
240+
// Test close functionality
241+
userEvent.click(container.querySelector('.tag-close-wrap'));
242+
expect(mockCloseFn).toBeCalledWith(0, expect.any(Object));
243+
expect(mockTagCloseFn).toBeCalled();
244+
245+
// 测试样式继承
246+
// Test style inheritance
247+
expect(container.querySelector(`.${prefix}-hollow`)).toBeInTheDocument();
248+
249+
// 测试自定义样式
250+
// Test custom styles
251+
const listElement = container.querySelector(`.${defaultContext.prefixCls}-tag-list`);
252+
expect(listElement).toHaveClass('custom-tag-list');
253+
expect(listElement.style.backgroundColor).toBe('red');
254+
255+
// 测试标签个性化样式
256+
// Test tag personalized styles
257+
const tags = container.querySelectorAll(`.${prefix}`);
258+
expect(tags[0].style.color).toBe('blue');
259+
});
260+
261+
// 测试边界情况处理
262+
// Test edge case handling
263+
it('should handle edge cases correctly', () => {
264+
// 测试空列表
265+
// Test empty list
266+
const { container } = render(<Tag.List list={[]} />);
267+
const listElement = container.querySelector(`.${defaultContext.prefixCls}-tag-list`);
268+
expect(listElement).toBeInTheDocument();
269+
expect(container.querySelector('.tag-list-add-wrap')).toBeInTheDocument();
270+
});
271+
});

0 commit comments

Comments
 (0)