OfficialSiteMethodologyUploadSave.test.tsx
7.67 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
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { App } from '../../App';
function renderMethodology(path = '/official-site/methodology') {
window.localStorage.setItem('agent-admin-local-login', 'true');
return render(
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>,
);
}
/** 点击 TDesign image-flow 主题的删除图标 */
function clickDeleteIcon(container: HTMLElement) {
const maskItems = container.querySelectorAll('.t-upload__card-mask-item');
// 第一个 mask-item 是预览图标,第二个是删除图标
const deleteMaskItem = maskItems[1] as HTMLElement;
const deleteIcon = deleteMaskItem?.querySelector('.t-icon-delete') as HTMLElement;
if (deleteIcon) {
fireEvent.click(deleteIcon);
}
}
/** 获取内容图的文件输入(第二个 MediaWall) */
function getContentImageInput(card: HTMLElement): HTMLInputElement | null {
// 每个 card 有两个 MediaWall:步骤图标和内容图
// 内容图的 input 在第二个 .t-upload 容器中
const uploadContainers = card.querySelectorAll('.t-upload');
if (uploadContainers.length < 2) return null;
const contentImageUpload = uploadContainers[1];
return contentImageUpload.querySelector('input[type="file"]') as HTMLInputElement;
}
/** 获取步骤图标的文件输入(第一个 MediaWall) */
function getStepIconInput(card: HTMLElement): HTMLInputElement | null {
const uploadContainers = card.querySelectorAll('.t-upload');
if (uploadContainers.length < 1) return null;
const stepIconUpload = uploadContainers[0];
return stepIconUpload.querySelector('input[type="file"]') as HTMLInputElement;
}
describe('OfficialSiteMethodologyPage upload, save, and state handling', () => {
beforeEach(() => {
vi.useRealTimers();
window.localStorage.clear();
});
it('renders a recoverable load error state', async () => {
renderMethodology('/official-site/methodology?scenario=loadError');
await waitFor(() => {
expect(screen.getByRole('alert', { name: '加载失败' })).toBeInTheDocument();
});
fireEvent.click(screen.getByRole('button', { name: '重试' }));
await waitFor(() => {
expect(screen.getByLabelText('主标题')).toBeInTheDocument();
});
});
it('requires step icon and content image before saving', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
// Delete step icon and content image using TDesign's delete icons
const cards = screen.getAllByTestId('methodology-step-card');
const firstCard = cards[0];
// 找到两个 MediaWall 的容器
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
// 第一个 .t-upload__card 是步骤图标,第二个是内容图
if (uploadContainers.length >= 2) {
// 删除步骤图标
clickDeleteIcon(uploadContainers[0] as HTMLElement);
// 删除内容图
clickDeleteIcon(uploadContainers[1] as HTMLElement);
}
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
expect(screen.getByText('请配置步骤图标')).toBeInTheDocument();
expect(screen.getByText('请上传内容图')).toBeInTheDocument();
});
it('validates content image type and size before mock upload', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// First delete existing content image (since maxCount=1 hides upload button when image exists)
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 2) {
clickDeleteIcon(uploadContainers[1] as HTMLElement);
}
// Now find content image file input
const contentImageInput = getContentImageInput(firstCard);
if (!contentImageInput) {
throw new Error('Content image input not found');
}
// GIF is now supported, so test with an unsupported type
fireEvent.change(contentImageInput, {
target: { files: [new File(['demo'], 'cover.bmp', { type: 'image/bmp' })] },
});
await waitFor(() => {
expect(within(firstCard).getByText('内容图仅支持 PNG/JPG/JPEG/GIF')).toBeInTheDocument();
});
// Upload failed, no image was added, so we can directly try oversized file
const oversized = new File(['demo'], 'cover.png', { type: 'image/png' });
Object.defineProperty(oversized, 'size', { value: 10 * 1024 * 1024 + 1 });
fireEvent.change(contentImageInput, {
target: { files: [oversized] },
});
await waitFor(() => {
expect(within(firstCard).getByText('内容图大小不能超过 10MB')).toBeInTheDocument();
});
});
it('shows upload failure, save success with disabled repeat submit, and save failure while keeping edits', async () => {
const uploadErrorView = renderMethodology('/official-site/methodology?scenario=uploadError');
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// First delete existing content image (since maxCount=1 hides upload button when image exists)
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 2) {
clickDeleteIcon(uploadContainers[1] as HTMLElement);
}
// Now find content image file input
const contentImageInput = getContentImageInput(firstCard);
if (!contentImageInput) {
throw new Error('Content image input not found');
}
fireEvent.change(contentImageInput, {
target: { files: [new File(['demo'], 'cover.png', { type: 'image/png' })] },
});
await waitFor(() => {
expect(within(firstCard).getByText('图片上传失败,请重试')).toBeInTheDocument();
});
uploadErrorView.unmount();
const saveErrorView = renderMethodology('/official-site/methodology?scenario=saveError');
await waitFor(() => {
expect(screen.getByLabelText('主标题')).toBeInTheDocument();
});
fireEvent.change(screen.getByLabelText('主标题'), { target: { value: '保存失败后仍保留编辑内容' } });
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(screen.getByRole('alert', { name: '保存失败,请重试' })).toBeInTheDocument();
});
expect(screen.getByLabelText('主标题')).toHaveValue('保存失败后仍保留编辑内容');
saveErrorView.unmount();
const emptyStepsView = renderMethodology('/official-site/methodology?scenario=emptySteps');
await waitFor(() => {
expect(screen.getByRole('switch', { name: '展示状态' })).toBeInTheDocument();
});
fireEvent.click(screen.getByRole('switch', { name: '展示状态' }));
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(screen.getByRole('status', { name: '保存成功' })).toBeInTheDocument();
});
expect(screen.getByRole('button', { name: '保存配置' })).not.toBeDisabled();
emptyStepsView.unmount();
renderMethodology();
await waitFor(() => {
expect(screen.getByRole('button', { name: '保存配置' })).toBeInTheDocument();
});
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(
() => {
expect(screen.getByRole('status', { name: '保存成功' })).toBeInTheDocument();
},
{ timeout: 10000 },
);
});
});