Skip to content

Commit 7dbfa7a

Browse files
committed
test: add tests for different types, sizes, and styles of the Tag component
1 parent e04f22e commit 7dbfa7a

1 file changed

Lines changed: 186 additions & 0 deletions

File tree

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

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,190 @@ describe('Tag', () => {
5757
expect(container.querySelectorAll(`.${prefix}`).length).toBe(3);
5858
expect(container.querySelectorAll('.tag-list-add-wrap').length).toBe(1);
5959
});
60+
61+
it('should render correctly with different tag types', () => {
62+
const { container: container1 } = render(<Tag type="primary">Primary</Tag>);
63+
const { container: container2 } = render(<Tag type="hollow">Hollow</Tag>);
64+
const { container: container3 } = render(<Tag type="solid">Solid</Tag>);
65+
66+
expect(container1.querySelector(`.${prefix}-primary`)).toBeInTheDocument();
67+
expect(container2.querySelector(`.${prefix}-hollow`)).toBeInTheDocument();
68+
expect(container3.querySelector(`.${prefix}-solid`)).toBeInTheDocument();
69+
});
70+
71+
it('should render correctly with different sizes', () => {
72+
const { container: container1 } = render(<Tag size="small">Small</Tag>);
73+
const { container: container2 } = render(<Tag size="medium">Medium</Tag>);
74+
const { container: container3 } = render(<Tag size="large">Large</Tag>);
75+
76+
expect(container1.querySelector('.size-small')).toBeInTheDocument();
77+
expect(container2.querySelector('.size-medium')).toBeInTheDocument();
78+
expect(container3.querySelector('.size-large')).toBeInTheDocument();
79+
});
80+
81+
it('should render correctly with custom colors', () => {
82+
const { container } = render(
83+
<Tag color="#ff0000" bgColor="#00ff00" borderColor="#0000ff">
84+
Custom Color
85+
</Tag>,
86+
);
87+
const tagElement = container.querySelector(`.${prefix}`);
88+
// Color formats may vary between environments (RGB vs hex)
89+
expect(tagElement.style.color).toMatch(/^(rgb\(255, 0, 0\)|#ff0000)$/);
90+
expect(tagElement.style.background).toMatch(/^(rgb\(0, 255, 0\)|#00ff00)$/);
91+
expect(tagElement.style.borderColor).toMatch(/^(rgb\(0, 0, 255\)|#0000ff)$/);
92+
});
93+
94+
it('should render correctly with icon', () => {
95+
const TestIcon = () => <span data-testid="test-icon">🏷️</span>;
96+
const { getByTestId } = render(<Tag icon={<TestIcon />}>With Icon</Tag>);
97+
expect(getByTestId('test-icon')).toBeInTheDocument();
98+
});
99+
100+
it('should render correctly with custom close icon', () => {
101+
const CustomCloseIcon = () => <span data-testid="custom-close"></span>;
102+
const { getByTestId } = render(
103+
<Tag closeable closeIcon={<CustomCloseIcon />}>
104+
Custom Close
105+
</Tag>,
106+
);
107+
expect(getByTestId('custom-close')).toBeInTheDocument();
108+
});
109+
110+
it('should render correctly with close color', () => {
111+
const { container } = render(
112+
<Tag closeable closeColor="#ff0000">
113+
Close Color
114+
</Tag>,
115+
);
116+
const closeIcon = container.querySelector('.tag-close-icon');
117+
expect(closeIcon).toBeInTheDocument();
118+
});
119+
120+
it('should render correctly with different border styles', () => {
121+
const { container: container1 } = render(<Tag borderStyle="none">None</Tag>);
122+
const { container: container2 } = render(<Tag borderStyle="dotted">Dotted</Tag>);
123+
const { container: container3 } = render(<Tag borderStyle="dashed">Dashed</Tag>);
124+
125+
expect(container1.querySelector(`.${prefix}`).style.borderStyle).toBe('none');
126+
expect(container2.querySelector(`.${prefix}`).style.borderStyle).toBe('dotted');
127+
expect(container3.querySelector(`.${prefix}`).style.borderStyle).toBe('dashed');
128+
});
129+
130+
it('should render correctly without half border', () => {
131+
const { container } = render(<Tag halfBorder={false}>No Half Border</Tag>);
132+
const tagElement = container.querySelector(`.${prefix}`);
133+
expect(tagElement).not.toHaveClass('half-border');
134+
});
135+
136+
it('should not render close button when closeable is false', () => {
137+
const { container } = render(<Tag closeable={false}>No Close</Tag>);
138+
expect(container.querySelector('.tag-close-wrap')).not.toBeInTheDocument();
139+
});
140+
});
141+
142+
describe('Tag.List', () => {
143+
it('should render correctly with different padding configurations', () => {
144+
const list = [{ children: 'Tag 1' }, { children: 'Tag 2' }];
145+
146+
// Test with string verticalPadding
147+
const { container: container1 } = render(
148+
<Tag.List list={list} verticalPadding="10px" horizontalPadding="5px" />,
149+
);
150+
const listElement1 = container1.querySelector(`.${defaultContext.prefixCls}-tag-list`);
151+
expect(listElement1.style.marginBottom).toBe('-10px');
152+
153+
// Test with number verticalPadding
154+
const { container: container2 } = render(
155+
<Tag.List list={list} verticalPadding={20} horizontalPadding={10} />,
156+
);
157+
const listElement2 = container2.querySelector(`.${defaultContext.prefixCls}-tag-list`);
158+
expect(listElement2.style.marginBottom).toBe('-20px');
159+
});
160+
161+
it('should not show add button when showAddButton is false', () => {
162+
const list = [{ children: 'Tag 1' }];
163+
const { container } = render(<Tag.List list={list} showAddButton={false} />);
164+
expect(container.querySelector('.tag-list-add-wrap')).not.toBeInTheDocument();
165+
});
166+
167+
it('should render custom add area', () => {
168+
const list = [{ children: 'Tag 1' }];
169+
const CustomAddArea = () => <div data-testid="custom-add">Custom Add</div>;
170+
const { getByTestId } = render(<Tag.List list={list} addArea={<CustomAddArea />} />);
171+
expect(getByTestId('custom-add')).toBeInTheDocument();
172+
});
173+
174+
it('should callback correctly when add button clicked', () => {
175+
const mockAddFn = jest.fn();
176+
const list = [{ children: 'Tag 1' }];
177+
const { container } = render(<Tag.List list={list} onAdd={mockAddFn} />);
178+
179+
userEvent.click(container.querySelector('.tag-list-add-wrap'));
180+
expect(mockAddFn).toBeCalled();
181+
});
182+
183+
it('should callback correctly when tag close button clicked', () => {
184+
const mockCloseFn = jest.fn();
185+
const mockTagCloseFn = jest.fn();
186+
const list = [
187+
{
188+
children: 'Tag 1',
189+
closeable: true,
190+
onClose: mockTagCloseFn,
191+
},
192+
];
193+
const { container } = render(<Tag.List list={list} onClose={mockCloseFn} />);
194+
195+
userEvent.click(container.querySelector('.tag-close-wrap'));
196+
expect(mockCloseFn).toBeCalledWith(0, expect.any(Object));
197+
expect(mockTagCloseFn).toBeCalled();
198+
});
199+
200+
it('should inherit type from list to individual tags', () => {
201+
const list = [{ children: 'Tag 1' }];
202+
const { container } = render(<Tag.List list={list} type="hollow" />);
203+
expect(container.querySelector(`.${prefix}-hollow`)).toBeInTheDocument();
204+
});
205+
206+
it('should apply custom styles and class names', () => {
207+
const list = [{ children: 'Tag 1' }];
208+
const customStyle = { backgroundColor: 'red' };
209+
const { container } = render(
210+
<Tag.List list={list} className="custom-tag-list" style={customStyle} />,
211+
);
212+
213+
const listElement = container.querySelector(`.${defaultContext.prefixCls}-tag-list`);
214+
expect(listElement).toHaveClass('custom-tag-list');
215+
expect(listElement.style.backgroundColor).toBe('red');
216+
});
217+
218+
it('should handle empty list', () => {
219+
const { container } = render(<Tag.List list={[]} />);
220+
const listElement = container.querySelector(`.${defaultContext.prefixCls}-tag-list`);
221+
expect(listElement).toBeInTheDocument();
222+
expect(container.querySelector('.tag-list-add-wrap')).toBeInTheDocument();
223+
});
224+
225+
it('should handle tag with individual style correctly', () => {
226+
const list = [
227+
{
228+
children: 'Tag 1',
229+
style: { color: 'blue' },
230+
},
231+
{
232+
children: 'Tag 2',
233+
},
234+
];
235+
const { container } = render(
236+
<Tag.List list={list} horizontalPadding={15} verticalPadding={10} />,
237+
);
238+
239+
const tags = container.querySelectorAll(`.${prefix}`);
240+
expect(tags[0].style.color).toBe('blue');
241+
expect(tags[0].style.marginRight).toBe('15px');
242+
expect(tags[0].style.marginBottom).toBe('10px');
243+
expect(tags[1].style.marginBottom).toBe('10px');
244+
// Last tag should not have marginRight when showAddButton is true
245+
});
60246
});

0 commit comments

Comments
 (0)