Skip to content

Commit 2270812

Browse files
Saraph1nesanlyyao
andauthored
test(cell): add cell test (#737)
* test(cell): add cell test * chore: formatting unit test files --------- Co-authored-by: anlyyao <[email protected]>
1 parent cb26d86 commit 2270812

File tree

3 files changed

+203
-35
lines changed

3 files changed

+203
-35
lines changed

site/test-coverage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
button: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
77
calendar: { statements: '98.45%', branches: '95.48%', functions: '100%', lines: '99.15%' },
88
cascader: { statements: '4.3%', branches: '0%', functions: '0%', lines: '4.59%' },
9-
cell: { statements: '100%', branches: '77.77%', functions: '100%', lines: '100%' },
9+
cell: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
1010
checkbox: { statements: '99.12%', branches: '98.27%', functions: '100%', lines: '100%' },
1111
collapse: { statements: '69.09%', branches: '36.66%', functions: '72.72%', lines: '72.54%' },
1212
colorPicker: { statements: '3.03%', branches: '0%', functions: '0%', lines: '3.03%' },
@@ -25,7 +25,7 @@ module.exports = {
2525
grid: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
2626
guide: { statements: '3.46%', branches: '0%', functions: '0%', lines: '3.77%' },
2727
hooks: { statements: '48.41%', branches: '23.88%', functions: '56.25%', lines: '48.33%' },
28-
image: { statements: '75%', branches: '75.75%', functions: '53.84%', lines: '73.8%' },
28+
image: { statements: '97.72%', branches: '100%', functions: '92.3%', lines: '97.61%' },
2929
imageViewer: { statements: '8.47%', branches: '2.87%', functions: '0%', lines: '8.84%' },
3030
indexes: { statements: '95.65%', branches: '69.81%', functions: '100%', lines: '96.94%' },
3131
input: { statements: '3.57%', branches: '0%', functions: '0%', lines: '3.7%' },
@@ -58,7 +58,7 @@ module.exports = {
5858
swiper: { statements: '3.77%', branches: '0.9%', functions: '1.4%', lines: '3.89%' },
5959
switch: { statements: '100%', branches: '100%', functions: '100%', lines: '100%' },
6060
tabBar: { statements: '10%', branches: '0%', functions: '0%', lines: '10.81%' },
61-
table: { statements: '100%', branches: '88.57%', functions: '100%', lines: '100%' },
61+
table: { statements: '100%', branches: '90%', functions: '100%', lines: '100%' },
6262
tabs: { statements: '5.8%', branches: '0%', functions: '0%', lines: '6.33%' },
6363
tag: { statements: '100%', branches: '96.87%', functions: '100%', lines: '100%' },
6464
textarea: { statements: '98.64%', branches: '95%', functions: '93.33%', lines: '100%' },

src/cell/__tests__/cell.test.tsx

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,104 @@ import React from 'react';
33
import Cell from '../Cell';
44

55
describe('Cell', () => {
6-
it('renders the correct content', () => {
7-
const { getByText } = render(
8-
<Cell title="Test Title" description="Test Description">
9-
Test Children
10-
</Cell>,
11-
);
12-
13-
expect(getByText('Test Title')).toBeInTheDocument();
14-
expect(getByText('Test Description')).toBeInTheDocument();
15-
expect(getByText('Test Children')).toBeInTheDocument();
16-
});
6+
describe('slots', () => {
7+
it(': default', () => {
8+
const { getByText } = render(
9+
<Cell title="Test Title" description="Test Description">
10+
Test Children
11+
</Cell>,
12+
);
1713

18-
it('renders left icon and image if provided', () => {
19-
const { getByTestId } = render(
20-
<Cell
21-
leftIcon={<div data-testid="left-icon">Left Icon</div>}
22-
image="https://tdesign.gtimg.com/mobile/demos/avatar1.png"
23-
>
24-
Test Children
25-
</Cell>,
26-
);
27-
28-
expect(getByTestId('left-icon')).toBeInTheDocument();
29-
expect(screen.getByText('Test Children')).toBeInTheDocument();
14+
expect(getByText('Test Title')).toBeInTheDocument();
15+
expect(getByText('Test Description')).toBeInTheDocument();
16+
expect(getByText('Test Children')).toBeInTheDocument();
17+
});
3018
});
3119

32-
it('renders right icon if provided', () => {
33-
const { getByTestId } = render(<Cell rightIcon={<div data-testid="right-icon">Right Icon</div>} />);
20+
describe('props', () => {
21+
it(': leftIcon and image', () => {
22+
const { getByTestId } = render(
23+
<Cell
24+
leftIcon={<div data-testid="left-icon">Left Icon</div>}
25+
image="https://tdesign.gtimg.com/mobile/demos/avatar1.png"
26+
>
27+
Test Children
28+
</Cell>,
29+
);
3430

35-
expect(getByTestId('right-icon')).toBeInTheDocument();
36-
});
31+
expect(getByTestId('left-icon')).toBeInTheDocument();
32+
expect(screen.getByText('Test Children')).toBeInTheDocument();
33+
});
34+
35+
it(': image', () => {
36+
const { container } = render(<Cell image="https://tdesign.gtimg.com/mobile/demos/avatar1.png"></Cell>);
37+
38+
expect(container.querySelector('.t-cell__left-image')).toBeInTheDocument();
39+
});
40+
41+
it(': rightIcon', () => {
42+
const { getByTestId } = render(<Cell rightIcon={<div data-testid="right-icon">Right Icon</div>} />);
43+
44+
expect(getByTestId('right-icon')).toBeInTheDocument();
45+
});
46+
47+
it(': required', () => {
48+
const { container, rerender } = render(<Cell title="Required Field" />);
49+
50+
expect(container.querySelector('.t-cell--required')).not.toBeInTheDocument();
51+
52+
rerender(<Cell title="Required Field" required />);
53+
expect(container.querySelector('.t-cell--required')).toBeInTheDocument();
54+
expect(container.querySelector('.t-cell--required')).toHaveTextContent('*');
55+
});
56+
57+
it(': arrow', () => {
58+
const { container, rerender } = render(<Cell />);
59+
60+
const rightIcon = container.querySelector('.t-cell__right-icon');
61+
expect(rightIcon).not.toBeInTheDocument();
62+
63+
rerender(<Cell arrow />);
64+
65+
expect(container.querySelector('.t-cell__right-icon')).toBeInTheDocument();
66+
// 检查是否包含箭头图标
67+
expect(container.querySelector('.t-cell__right-icon > svg')).toBeInTheDocument();
68+
});
69+
70+
it(': arrow and rightIcon', () => {
71+
const { container } = render(<Cell arrow rightIcon={<div data-testid="custom-right-icon">Custom Icon</div>} />);
72+
73+
const rightIcon = container.querySelector('.t-cell__right-icon');
74+
expect(rightIcon).toBeInTheDocument();
75+
// 应该显示箭头图标而不是自定义图标
76+
expect(rightIcon.querySelector('svg')).toBeInTheDocument();
77+
expect(container.querySelector('[data-testid="custom-right-icon"]')).not.toBeInTheDocument();
78+
});
79+
80+
it(': note', () => {
81+
const { container, getByText } = render(<Cell note="Test Note" />);
82+
83+
const noteElement = container.querySelector('.t-cell__note');
84+
expect(noteElement).toBeInTheDocument();
85+
expect(getByText('Test Note')).toBeInTheDocument();
86+
});
87+
88+
it(': renders note instead of children when both note and children are provided', () => {
89+
const { container, getByText, queryByText } = render(<Cell note="Test Note">Test Children Content</Cell>);
90+
91+
const noteElement = container.querySelector('.t-cell__note');
92+
expect(noteElement).toBeInTheDocument();
93+
expect(getByText('Test Note')).toBeInTheDocument();
94+
expect(queryByText('Test Children Content')).not.toBeInTheDocument();
95+
});
3796

38-
it('calls onClick when the cell is clicked', () => {
39-
const handleClick = vi.fn();
40-
const { container } = render(<Cell onClick={handleClick} />);
97+
it(': onClick', () => {
98+
const handleClick = vi.fn();
99+
const { container } = render(<Cell onClick={handleClick} />);
41100

42-
fireEvent.click(container.firstChild);
101+
fireEvent.click(container.firstChild);
43102

44-
expect(handleClick).toHaveBeenCalledTimes(1);
103+
expect(handleClick).toHaveBeenCalledTimes(1);
104+
});
45105
});
46106
});
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { describe, it, expect, render } from '@test/utils';
2+
import React from 'react';
3+
import CellGroup from '../CellGroup';
4+
import Cell from '../Cell';
5+
6+
describe('CellGroup', () => {
7+
describe('props', () => {
8+
it('renders correctly with default props', () => {
9+
const { container } = render(
10+
<CellGroup>
11+
<Cell title="Cell 1" />
12+
<Cell title="Cell 2" />
13+
</CellGroup>,
14+
);
15+
16+
expect(container.firstChild).toBeInTheDocument();
17+
expect(container.querySelector('.t-cell-group')).toBeInTheDocument();
18+
});
19+
20+
it('renders children correctly', () => {
21+
const { getByText } = render(
22+
<CellGroup>
23+
<Cell title="First Cell" />
24+
<Cell title="Second Cell" />
25+
<div>Custom Content</div>
26+
</CellGroup>,
27+
);
28+
29+
expect(getByText('First Cell')).toBeInTheDocument();
30+
expect(getByText('Second Cell')).toBeInTheDocument();
31+
expect(getByText('Custom Content')).toBeInTheDocument();
32+
});
33+
34+
it(': className', () => {
35+
const { container } = render(
36+
<CellGroup className="custom-class">
37+
<Cell title="Cell 1" />
38+
</CellGroup>,
39+
);
40+
41+
expect(container.firstChild).toHaveClass('custom-class');
42+
});
43+
44+
it(': style', () => {
45+
const customStyle = { backgroundColor: 'red' };
46+
const { container } = render(
47+
<CellGroup style={customStyle}>
48+
<Cell title="Cell 1" />
49+
</CellGroup>,
50+
);
51+
52+
expect(container.firstChild).toHaveStyle('background-color: rgb(255, 0, 0)');
53+
});
54+
55+
it(':title', () => {
56+
const { container, getByText, rerender } = render(
57+
<CellGroup title="Group Title">
58+
<Cell title="Cell 1" />
59+
</CellGroup>,
60+
);
61+
62+
expect(getByText('Group Title')).toBeInTheDocument();
63+
expect(getByText('Group Title')).toHaveClass('t-cell-group__title');
64+
65+
rerender(
66+
<CellGroup>
67+
<Cell title="Cell 1" />
68+
</CellGroup>,
69+
);
70+
71+
expect(container.querySelector('.t-cell-group__title')).not.toBeInTheDocument();
72+
});
73+
74+
it(': bordered', async () => {
75+
const { container, rerender } = render(
76+
<CellGroup bordered>
77+
<Cell title="Cell 1" />
78+
</CellGroup>,
79+
);
80+
expect(container.querySelector('.t-cell-group--bordered')).toBeInTheDocument();
81+
82+
rerender(
83+
<CellGroup bordered={false}>
84+
<Cell title="Cell 1" />
85+
</CellGroup>,
86+
);
87+
88+
expect(container.querySelector('.t-cell-group--bordered')).not.toBeInTheDocument();
89+
});
90+
91+
it(': theme', () => {
92+
const { container: defaultContainer } = render(
93+
<CellGroup theme="default">
94+
<Cell title="Cell 1" />
95+
</CellGroup>,
96+
);
97+
98+
const { container: cardContainer } = render(
99+
<CellGroup theme="card">
100+
<Cell title="Cell 1" />
101+
</CellGroup>,
102+
);
103+
104+
expect(defaultContainer.querySelector('.t-cell-group--default')).toBeInTheDocument();
105+
expect(cardContainer.querySelector('.t-cell-group--card')).toBeInTheDocument();
106+
});
107+
});
108+
});

0 commit comments

Comments
 (0)