basic-controls.test.tsx
5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import {
Button,
Checkbox,
Input,
Radio,
SearchInput,
Switch,
} from '../index';
describe('shared UI basic controls', () => {
it('exports the first task baseline from the shared UI boundary', () => {
expect(Button).toBeTypeOf('function');
expect(Input).toBeTypeOf('object');
expect(SearchInput).toBeTypeOf('object');
expect(Checkbox).toBeTypeOf('object');
expect(Radio).toBeTypeOf('object');
expect(Switch).toBeTypeOf('object');
});
it('renders Button variants and blocks clicks while loading or disabled', () => {
const onClick = vi.fn();
render(
<>
<Button variant="primary" size="large" onClick={onClick}>
保存
</Button>
<Button loading onClick={onClick}>
提交中
</Button>
<Button disabled onClick={onClick}>
不可用
</Button>
</>,
);
fireEvent.click(screen.getByRole('button', { name: '保存' }));
fireEvent.click(screen.getByRole('button', { name: /提交中/ }));
fireEvent.click(screen.getByRole('button', { name: '不可用' }));
expect(onClick).toHaveBeenCalledTimes(1);
expect(screen.getByRole('button', { name: /提交中/ })).toBeDisabled();
expect(screen.getByRole('button', { name: /提交中/ })).toHaveAttribute('aria-busy', 'true');
});
it('supports controlled and uncontrolled Input values with clear and status states', () => {
const onControlledChange = vi.fn();
const onClear = vi.fn();
render(
<>
<Input
aria-label="受控输入"
value="已填写"
allowClear
status="error"
onChange={onControlledChange}
onClear={onClear}
/>
<Input aria-label="非受控输入" defaultValue="初始值" prefix="¥" suffix="元" />
</>,
);
fireEvent.click(screen.getByRole('button', { name: '清空受控输入' }));
fireEvent.change(screen.getByLabelText('非受控输入'), { target: { value: '99' } });
expect(onControlledChange).toHaveBeenCalledWith('');
expect(onClear).toHaveBeenCalledTimes(1);
expect(screen.getByLabelText('非受控输入')).toHaveValue('99');
expect(screen.getByLabelText('受控输入')).toHaveAttribute('aria-invalid', 'true');
expect(screen.getByText('¥')).toBeInTheDocument();
expect(screen.getByText('元')).toBeInTheDocument();
});
it('triggers SearchInput search from enter, button click, and clear', () => {
const onSearch = vi.fn();
const onClear = vi.fn();
render(
<SearchInput
aria-label="搜索关键字"
defaultValue="合同"
allowClear
onSearch={onSearch}
onClear={onClear}
/>,
);
fireEvent.keyDown(screen.getByLabelText('搜索关键字'), { key: 'Enter' });
fireEvent.click(screen.getByRole('button', { name: '搜索' }));
fireEvent.click(screen.getByRole('button', { name: '清空搜索关键字' }));
expect(onSearch).toHaveBeenNthCalledWith(1, '合同');
expect(onSearch).toHaveBeenNthCalledWith(2, '合同');
expect(onClear).toHaveBeenCalledTimes(1);
});
it('renders SearchInput with a design-sized search icon asset', () => {
render(<SearchInput aria-label="搜索关键字" defaultValue="合同" />);
const icon = screen.getByLabelText('搜索图标');
expect(icon).toHaveAttribute('data-icon', 'search');
expect(icon).toHaveAttribute('width', '18');
expect(icon).toHaveAttribute('height', '18');
});
it('renders Checkbox checked, indeterminate, disabled states and change callback', () => {
const onChange = vi.fn();
render(
<>
<Checkbox label="启用" checked onChange={onChange} />
<Checkbox label="部分选择" indeterminate />
<Checkbox label="禁用" disabled />
</>,
);
fireEvent.click(screen.getByLabelText('启用'));
fireEvent.click(screen.getByLabelText('禁用'));
expect(onChange).toHaveBeenCalledWith(false);
expect(screen.getByLabelText('部分选择')).toHaveAttribute('aria-checked', 'mixed');
expect(screen.getByLabelText('禁用')).toBeDisabled();
});
it('renders Radio as a controlled single option and respects disabled state', () => {
const onChange = vi.fn();
render(
<>
<Radio label="按项目" name="range" value="project" checked onChange={onChange} />
<Radio label="按组织" name="range" value="org" disabled onChange={onChange} />
</>,
);
fireEvent.click(screen.getByLabelText('按项目'));
fireEvent.click(screen.getByLabelText('按组织'));
expect(onChange).toHaveBeenCalledWith('project');
expect(screen.getByLabelText('按组织')).toBeDisabled();
});
it('toggles Switch and ignores interaction while loading or disabled', () => {
const onChange = vi.fn();
render(
<>
<Switch aria-label="启用审批" checked={false} onChange={onChange} />
<Switch aria-label="同步中" checked loading onChange={onChange} />
<Switch aria-label="禁止切换" checked disabled onChange={onChange} />
</>,
);
fireEvent.click(screen.getByRole('switch', { name: '启用审批' }));
fireEvent.click(screen.getByRole('switch', { name: '同步中' }));
fireEvent.click(screen.getByRole('switch', { name: '禁止切换' }));
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(true);
expect(screen.getByRole('switch', { name: '同步中' })).toHaveAttribute('aria-busy', 'true');
expect(screen.getByRole('switch', { name: '禁止切换' })).toBeDisabled();
});
});