global-business-components.test.tsx
8.39 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
223
224
225
226
227
228
229
230
231
232
233
234
235
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { MediaWall, SearchFilter, StatePlaceholder } from '@gl/business-components';
const searchFields = [
{ key: 'keyword', label: '关键词', type: 'input' as const, defaultValue: '项目' },
{ key: 'count', label: '数量', type: 'numberInput' as const },
{ key: 'status', label: '状态', type: 'select' as const, options: [{ label: '启用', value: 'enabled' }] },
];
function mockSearchFilterLayout({ containerWidth, fieldWidth = 284, actionsWidth = 220 }: {
containerWidth: number;
fieldWidth?: number;
actionsWidth?: number;
}) {
const originalClientWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'clientWidth');
const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');
const originalScrollHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'scrollHeight');
const originalGetComputedStyle = window.getComputedStyle;
Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
configurable: true,
get() {
if ((this as HTMLElement).dataset.searchFilterFlow === 'true') return containerWidth;
return originalClientWidth?.get?.call(this) ?? 0;
},
});
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
get() {
const element = this as HTMLElement;
if (element.dataset.searchFilterActions === 'true') return actionsWidth;
if (element.dataset.fieldKey) return fieldWidth;
return originalOffsetWidth?.get?.call(this) ?? 0;
},
});
Object.defineProperty(HTMLElement.prototype, 'scrollHeight', {
configurable: true,
get() {
return 96;
},
});
window.getComputedStyle = ((element: Element) => {
const style = originalGetComputedStyle(element);
if ((element as HTMLElement).dataset.searchFilterFlow === 'true') {
return new Proxy(style, {
get(target, property, receiver) {
if (property === 'columnGap') return '16px';
if (property === 'rowGap') return '12px';
return Reflect.get(target, property, receiver);
},
});
}
return style;
}) as typeof window.getComputedStyle;
return () => {
if (originalClientWidth) Object.defineProperty(HTMLElement.prototype, 'clientWidth', originalClientWidth);
if (originalOffsetWidth) Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth);
if (originalScrollHeight) Object.defineProperty(HTMLElement.prototype, 'scrollHeight', originalScrollHeight);
window.getComputedStyle = originalGetComputedStyle;
};
}
describe('global business components', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('keeps SearchFilter draft values until search and restores defaults on reset', () => {
const onReset = vi.fn();
const onSearch = vi.fn();
render(
<SearchFilter
fields={searchFields}
onReset={onReset}
onSearch={onSearch}
/>,
);
fireEvent.change(screen.getByLabelText('数量'), { target: { value: '12a3' } });
fireEvent.click(screen.getByRole('button', { name: '查询' }));
expect(screen.getByLabelText('数量')).toHaveValue('123');
expect(onSearch).toHaveBeenCalledWith({ keyword: '项目', count: '123', status: '' });
fireEvent.change(screen.getByLabelText('关键词'), { target: { value: '合同' } });
fireEvent.click(screen.getByRole('button', { name: '重置' }));
expect(screen.getByLabelText('关键词')).toHaveValue('项目');
expect(onReset).toHaveBeenCalledTimes(1);
});
it('renders SearchFilter actions after fields in query, reset, expand order', async () => {
const restoreLayout = mockSearchFilterLayout({ containerWidth: 640, fieldWidth: 200, actionsWidth: 260 });
render(
<SearchFilter
fields={[
...searchFields,
{ key: 'owner', label: '负责人', type: 'input' },
{ key: 'department', label: '部门', type: 'input' },
]}
/>,
);
await waitFor(() => expect(screen.getByRole('button', { name: '展开' })).toBeInTheDocument());
const root = screen.getByLabelText('搜索过滤条件');
const controls = within(root).getAllByRole('button').map((button) => button.textContent?.trim());
expect(controls).toEqual(['查询', '重置', '展开']);
restoreLayout();
});
it('does not show expand or collapse when fields and actions fit in two rows', async () => {
const restoreLayout = mockSearchFilterLayout({ containerWidth: 920, fieldWidth: 200, actionsWidth: 220 });
render(
<SearchFilter
fields={[
...searchFields,
{ key: 'owner', label: '负责人', type: 'input' },
{ key: 'department', label: '部门', type: 'input' },
]}
/>,
);
await waitFor(() => expect(screen.queryByRole('button', { name: '展开' })).not.toBeInTheDocument());
expect(screen.queryByRole('button', { name: '收起' })).not.toBeInTheDocument();
restoreLayout();
});
it('only folds when the responsive layout needs more than two rows', async () => {
const restoreLayout = mockSearchFilterLayout({ containerWidth: 640, fieldWidth: 200, actionsWidth: 260 });
render(
<SearchFilter
fields={[
...searchFields,
{ key: 'owner', label: '负责人', type: 'input' },
{ key: 'department', label: '部门', type: 'input' },
]}
/>,
);
await waitFor(() => expect(screen.getByRole('button', { name: '展开' })).toBeInTheDocument());
fireEvent.click(screen.getByRole('button', { name: '展开' }));
expect(screen.getByRole('button', { name: '收起' })).toBeInTheDocument();
restoreLayout();
});
it('renders an empty range date field without TDesign value warnings', () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
render(
<SearchFilter
fields={[
{ key: 'period', label: '统计周期', type: 'datePicker', range: true },
]}
/>,
);
expect(screen.getAllByLabelText('统计周期')).toHaveLength(2);
expect(consoleError).not.toHaveBeenCalledWith(expect.stringContaining('TDesign DatePicker Error'));
consoleError.mockRestore();
});
it('renders StatePlaceholder semantic roles and actions', () => {
const onRetry = vi.fn();
render(
<>
<StatePlaceholder state="empty" />
<StatePlaceholder state="empty" title="加载中" description="正在加载" />
<StatePlaceholder
state="error"
title="列表加载失败"
description="请稍后重试"
primaryAction={<button type="button" onClick={onRetry}>重新加载</button>}
/>
</>,
);
expect(screen.getByRole('status', { name: '暂无数据' })).toBeInTheDocument();
expect(screen.getByRole('status', { name: '加载中' })).toBeInTheDocument();
expect(screen.getByText('正在加载')).toBeInTheDocument();
expect(screen.getByRole('alert', { name: '列表加载失败' })).toBeInTheDocument();
expect(screen.getByText('请稍后重试')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '重新加载' }));
expect(onRetry).toHaveBeenCalledTimes(1);
});
it('keeps MediaWall upload, preview, remove and hydrated image behavior', async () => {
const onPreview = vi.fn();
const onRemove = vi.fn();
const onSelectFiles = vi.fn();
const image = { id: 'cover', type: 'image' as const, src: '/cover.png', alt: '产品图片' };
const file = new File(['demo'], 'demo.png', { type: 'image/png' });
render(
<MediaWall
items={[image]}
maxCount={2}
onPreview={onPreview}
onRemove={onRemove}
onSelectFiles={onSelectFiles}
/>,
);
expect(document.querySelector(`img[src="${image.src}"]`)).toBeInTheDocument();
fireEvent.click(document.querySelector('.t-icon-browse') as HTMLElement);
fireEvent.click(document.querySelector('.t-icon-delete') as HTMLElement);
expect(onPreview).toHaveBeenCalledWith(image);
expect(onRemove).toHaveBeenCalledWith(image);
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
fireEvent.change(fileInput, { target: { files: [file] } });
await waitFor(() => {
expect(onSelectFiles).toHaveBeenCalledWith([file]);
});
});
});