Skip to content

Commit e27a07b

Browse files
committed
test(Steps): 迁移 #3757 测试
1 parent 1931170 commit e27a07b

3 files changed

Lines changed: 429 additions & 63 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import { render, waitFor } from '@test/utils';
3+
import { vi } from 'vitest';
4+
import Steps from '../Steps';
5+
import StepItem from '../StepItem';
6+
7+
const TEST_ROOT_ID = 'step-test-branches';
8+
9+
describe('Steps branches', () => {
10+
test('options reverse order and index mapping', async () => {
11+
const opts = [
12+
{ title: 'first', content: 'c1', value: 0 },
13+
{ title: 'second', content: 'c2', value: 1 },
14+
{ title: 'third', content: 'c3', value: 2 },
15+
];
16+
const { getByTestId } = render(
17+
<div data-testid={TEST_ROOT_ID}>
18+
<Steps options={opts} sequence="reverse" current={1} />
19+
</div>,
20+
);
21+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
22+
const titles = Array.from(root.querySelectorAll('.t-steps-item__title')).map((n) => n.textContent);
23+
// when reverse, display order should be same length but titles still present
24+
expect(titles.length).toBe(3);
25+
expect(titles).toContain('first');
26+
expect(titles).toContain('second');
27+
expect(titles).toContain('third');
28+
});
29+
30+
test('children cloneElement branch and FINISH current', async () => {
31+
const onChange = vi.fn?.() ?? (() => undefined);
32+
const { getByTestId } = render(
33+
<div data-testid={TEST_ROOT_ID}>
34+
<Steps current={'FINISH'} onChange={onChange}>
35+
<StepItem title="A" content="a" value={0} />
36+
<StepItem title="B" content="b" value={1} />
37+
</Steps>
38+
</div>,
39+
);
40+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
41+
// all items should be finish
42+
const finish = root.querySelectorAll('.t-steps-item--finish');
43+
expect(finish.length).toBe(2);
44+
});
45+
46+
test('StepItem default icon number rendered when icon=true and theme=default', async () => {
47+
const { getByTestId } = render(
48+
<div data-testid={TEST_ROOT_ID}>
49+
<Steps current={0}>
50+
<StepItem title="X" content="x" index={0} icon={true as any} status={'default'} />
51+
</Steps>
52+
</div>,
53+
);
54+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
55+
const number = root.querySelector('.t-steps-item__icon--number');
56+
expect(number).not.toBeNull();
57+
expect(number).toHaveTextContent('1');
58+
});
59+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React from 'react';
2+
import { render, waitFor, fireEvent, vi } from '@test/utils';
3+
import Steps from '../Steps';
4+
import StepItem from '../StepItem';
5+
import StepsContext from '../StepsContext';
6+
7+
const TEST_ROOT_ID = 'step-test-root-extra';
8+
9+
const defaultOptions = [
10+
{ title: '0', content: '这里', value: 0 },
11+
{ title: '1', content: '这里', value: 1 },
12+
{ title: '2', content: '这里', value: 2 },
13+
];
14+
15+
describe('Steps extra branches', () => {
16+
test("current='FINISH' -> all items finish (options)", async () => {
17+
const { getByTestId } = render(
18+
<div data-testid={TEST_ROOT_ID}>
19+
<Steps current={'FINISH'} options={defaultOptions} />
20+
</div>,
21+
);
22+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
23+
const finishes = root.querySelectorAll('.t-steps-item--finish');
24+
expect(finishes.length).toBe(defaultOptions.length);
25+
});
26+
27+
test('item.status override (error) renders error class', async () => {
28+
const opts = [
29+
{ title: 'a', content: 'a', value: 0, status: 'error' as any },
30+
{ title: 'b', content: 'b', value: 1 },
31+
];
32+
const { getByTestId } = render(
33+
<div data-testid={TEST_ROOT_ID}>
34+
<Steps current={0} options={opts} />
35+
</div>,
36+
);
37+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
38+
const errorItem = root.querySelector('.t-steps-item--error');
39+
expect(errorItem).not.toBeNull();
40+
});
41+
42+
test('current value not exist triggers console.warn and defaults to wait', async () => {
43+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
44+
const opts = [
45+
{ title: 'a', content: 'a', value: 'a' },
46+
{ title: 'b', content: 'b', value: 'b' },
47+
];
48+
const { getByTestId } = render(
49+
<div data-testid={TEST_ROOT_ID}>
50+
<Steps current={'not-exist'} options={opts as any} />
51+
</div>,
52+
);
53+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
54+
expect(warnSpy).toHaveBeenCalledWith('TDesign Steps Warn: The current `value` is not exist.');
55+
const waitItems = root.querySelectorAll('.t-steps-item--wait');
56+
// both should be in default/wait state
57+
expect(waitItems.length).toBe(opts.length);
58+
warnSpy.mockRestore();
59+
});
60+
61+
test('status=process prevents click triggering onChange', async () => {
62+
const onChange = vi.fn();
63+
const { getByTestId } = render(
64+
<div data-testid={TEST_ROOT_ID}>
65+
<StepsContext.Provider value={{ current: 0, theme: 'default', readonly: false, onChange }}>
66+
<StepItem title="t" index={0} status={'process'} />
67+
</StepsContext.Provider>
68+
</div>,
69+
);
70+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
71+
const inner = root.querySelector('.t-steps-item__inner');
72+
fireEvent.click(inner);
73+
expect(onChange).not.toHaveBeenCalled();
74+
});
75+
76+
test('icon=false results in no icon content', async () => {
77+
const { getByTestId } = render(
78+
<div data-testid={TEST_ROOT_ID}>
79+
<StepsContext.Provider value={{ current: 0, theme: 'default', readonly: false, onChange: null }}>
80+
<StepItem title="t" index={0} status={'default'} icon={false as any} />
81+
</StepsContext.Provider>
82+
</div>,
83+
);
84+
const root = await waitFor(() => getByTestId(TEST_ROOT_ID));
85+
const icon = root.querySelector('.t-steps-item__icon');
86+
// icon container exists but should have no child nodes when icon=false
87+
expect(icon).not.toBeNull();
88+
expect(icon.childNodes.length).toBe(0);
89+
});
90+
});

0 commit comments

Comments
 (0)