Repository navigation
Expand file tree
/
Copy pathApp.jsx
More file actions
223 lines (182 loc) · 6.63 KB
/
Copy pathApp.jsx
File metadata and controls
223 lines (182 loc) · 6.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
export default App;
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
// ReactButton组件测试
describe('ReactButton组件', () => {
// 基础渲染测试
describe('基础渲染', () => {
test('应该使用默认props正确渲染', () => {
render(<ReactButton />);
const button = screen.getByRole('button');
expect(button).toBeInTheDocument();
expect(button).toHaveTextContent('Click me');
expect(button).toHaveClass('custom-btn', 'primary');
expect(button).not.toBeDisabled();
});
test('应该使用自定义text正确渲染', () => {
render(<ReactButton text="自定义按钮" />);
const button = screen.getByRole('button');
expect(button).toHaveTextContent('自定义按钮');
});
});
// type属性测试
describe('type属性', () => {
test('应该正确应用primary类型样式', () => {
render(<ReactButton type="primary" />);
const button = screen.getByRole('button');
expect(button).toHaveClass('primary');
});
test('应该正确应用danger类型样式', () => {
render(<ReactButton type="danger" />);
const button = screen.getByRole('button');
expect(button).toHaveClass('danger');
});
test('应该正确应用success类型样式', () => {
render(<ReactButton type="success" />);
const button = screen.getByRole('button');
expect(button).toHaveClass('success');
});
test('应该对无效type使用默认primary样式', () => {
render(<ReactButton type="invalid" />);
const button = screen.getByRole('button');
expect(button).toHaveClass('primary');
});
});
// disabled属性测试
describe('disabled属性', () => {
test('应该正确禁用按钮', () => {
render(<ReactButton disabled={true} />);
const button = screen.getByRole('button');
expect(button).toBeDisabled();
});
test('应该正确启用按钮', () => {
render(<ReactButton disabled={false} />);
const button = screen.getByRole('button');
expect(button).not.toBeDisabled();
});
});
// 事件处理测试
describe('事件处理', () => {
test('应该正确触发onBtnClick回调', () => {
const mockClickHandler = jest.fn();
render(<ReactButton onBtnClick={mockClickHandler} />);
const button = screen.getByRole('button');
fireEvent.click(button);
expect(mockClickHandler).toHaveBeenCalledTimes(1);
expect(mockClickHandler).toHaveBeenCalledWith(
expect.objectContaining({
time: expect.any(Date)
})
);
});
test('禁用状态下不应该触发onBtnClick回调', () => {
const mockClickHandler = jest.fn();
render(<ReactButton disabled={true} onBtnClick={mockClickHandler} />);
const button = screen.getByRole('button');
fireEvent.click(button);
expect(mockClickHandler).not.toHaveBeenCalled();
});
test('没有onBtnClick时不应该报错', () => {
render(<ReactButton />);
const button = screen.getByRole('button');
expect(() => {
fireEvent.click(button);
}).not.toThrow();
});
});
// 组合测试
describe('props组合测试', () => {
test('应该正确处理所有props组合', () => {
const mockClickHandler = jest.fn();
render(
<ReactButton
text="测试按钮"
type="danger"
disabled={false}
onBtnClick={mockClickHandler}
/>
);
const button = screen.getByRole('button');
expect(button).toHaveTextContent('测试按钮');
expect(button).toHaveClass('danger');
expect(button).not.toBeDisabled();
fireEvent.click(button);
expect(mockClickHandler).toHaveBeenCalled();
});
test('应该正确处理禁用状态下的所有props', () => {
const mockClickHandler = jest.fn();
render(
<ReactButton
text="禁用测试"
type="success"
disabled={true}
onBtnClick={mockClickHandler}
/>
);
const button = screen.getByRole('button');
expect(button).toHaveTextContent('禁用测试');
expect(button).toHaveClass('success');
expect(button).toBeDisabled();
fireEvent.click(button);
expect(mockClickHandler).not.toHaveBeenCalled();
});
});
});
// App组件测试
describe('App组件', () => {
test('应该正确渲染所有按钮', () => {
render(<App />);
// 检查标题
expect(screen.getByText('按钮组件测试')).toBeInTheDocument();
// 检查主要按钮
const primaryButton = screen.getByText('主要按钮');
expect(primaryButton).toBeInTheDocument();
expect(primaryButton).toHaveClass('primary');
// 检查删除按钮
const dangerButton = screen.getByText('删除按钮');
expect(dangerButton).toBeInTheDocument();
expect(dangerButton).toHaveClass('danger');
// 检查禁用按钮
const disabledButton = screen.getByText('禁用按钮');
expect(disabledButton).toBeInTheDocument();
expect(disabledButton).toHaveClass('success');
expect(disabledButton).toBeDisabled();
});
test('应该正确处理按钮点击事件', () => {
// 模拟console.log和alert
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
const alertSpy = jest.spyOn(window, 'alert').mockImplementation();
render(<App />);
// 点击主要按钮
const primaryButton = screen.getByText('主要按钮');
fireEvent.click(primaryButton);
expect(consoleSpy).toHaveBeenCalledWith(
'Button clicked at:',
expect.any(Date)
);
expect(alertSpy).toHaveBeenCalledWith(
expect.stringMatching(/Button clicked at \d{1,2}:\d{2}:\d{2}/)
);
// 点击删除按钮
const dangerButton = screen.getByText('删除按钮');
fireEvent.click(dangerButton);
expect(consoleSpy).toHaveBeenCalledTimes(2);
expect(alertSpy).toHaveBeenCalledTimes(2);
// 清理模拟
consoleSpy.mockRestore();
alertSpy.mockRestore();
});
test('禁用按钮不应该触发事件', () => {
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
const alertSpy = jest.spyOn(window, 'alert').mockImplementation();
render(<App />);
// 尝试点击禁用按钮
const disabledButton = screen.getByText('禁用按钮');
fireEvent.click(disabledButton);
expect(consoleSpy).not.toHaveBeenCalled();
expect(alertSpy).not.toHaveBeenCalled();
// 清理模拟
consoleSpy.mockRestore();
alertSpy.mockRestore();
});
});