OfficialSiteMethodologyUploadSave.test.tsx
25.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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 { getMethodologyConfig, saveMethodologyConfig, uploadMethodologyAsset } from '../../api/methodology';
import { generateDefaultIconPngFile } from '../../utils/defaultIcon';
import { App } from '../../App';
vi.mock('../../api/methodology', () => ({
getMethodologyConfig: vi.fn(),
saveMethodologyConfig: vi.fn(),
uploadMethodologyAsset: vi.fn(),
}));
vi.mock('../../utils/defaultIcon', () => ({
buildDefaultIconDataUrl: (stepOrder: number) => `data:image/svg+xml,default-icon-${stepOrder}`,
generateDefaultIconPngFile: vi.fn(),
}));
const mockMethodologyConfig = {
config_id: 'methodology_001',
display_status: 'enable' as const,
main_title: '四步打造可复用的岗位 Agent',
sub_title: '从目标澄清到上线复盘,帮助团队沉淀一套稳定可复制的方法论。',
steps: [
{ step_id: 's1', step_icon: 'https://cdn.example.com/icon1.png', step_name: '需求澄清', step_order: 1, step_image: 'https://cdn.example.com/image1.png' },
{ step_id: 's2', step_icon: 'https://cdn.example.com/icon2.png', step_name: '方案设计', step_order: 2, step_image: 'https://cdn.example.com/image2.png' },
],
updated_at: '2026-05-16T10:00:00Z',
};
function renderMethodology(path = '/official-site/methodology') {
window.localStorage.setItem('agent-admin-local-login', 'true');
return render(
<MemoryRouter initialEntries={[path]}>
<App />
</MemoryRouter>,
);
}
/** 获取步骤图标的文件输入(第一个 MediaWall) */
function getStepIconInput(card: HTMLElement): HTMLInputElement | null {
// 每个 card 有两个 MediaWall:步骤图标和内容图
// 步骤图标的 input 在第一个 .t-upload 容器中
const uploadContainers = card.querySelectorAll('.t-upload');
if (uploadContainers.length < 1) return null;
const stepIconUpload = uploadContainers[0];
return stepIconUpload.querySelector('input[type="file"]') as HTMLInputElement;
}
/** 获取内容图的文件输入(第二个 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;
}
/** 点击 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);
}
}
describe('OfficialSiteMethodologyPage upload, save, and state handling', () => {
beforeEach(() => {
vi.useRealTimers();
window.localStorage.clear();
vi.clearAllMocks();
vi.mocked(getMethodologyConfig).mockResolvedValue(mockMethodologyConfig);
vi.mocked(saveMethodologyConfig).mockResolvedValue({ config_id: 'methodology_001', updated_at: '2026-05-16T12:00:00Z' });
vi.mocked(uploadMethodologyAsset).mockResolvedValue({
file_id: 'file_123',
file_url: 'https://cdn.example.com/uploaded.png',
file_type: 'png',
file_size: 500000,
});
vi.mocked(generateDefaultIconPngFile).mockResolvedValue(new File(['png content'], 'step-1-default-icon.png', { type: 'image/png' }));
});
it('renders a recoverable load error state', async () => {
vi.mocked(getMethodologyConfig).mockRejectedValueOnce(new Error('Load error'));
renderMethodology('/official-site/methodology');
await waitFor(() => {
expect(screen.getByRole('alert', { name: '加载失败' })).toBeInTheDocument();
});
// Reset mock for retry
vi.mocked(getMethodologyConfig).mockResolvedValueOnce(mockMethodologyConfig);
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: '保存配置' }));
await waitFor(() => {
expect(screen.getByText('请配置步骤图标')).toBeInTheDocument();
expect(screen.getByText('请上传内容图')).toBeInTheDocument();
});
expect(screen.getByTestId('methodology-step-icon-error-2')).toHaveStyle({ color: '#d94841' });
expect(screen.getByTestId('methodology-content-image-error-2')).toHaveStyle({ color: '#d94841' });
});
it('keeps upload helper prompts as ordinary help text', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const stepIconHelpers = screen.getAllByText('支持 PNG/JPG/JPEG,最大 2MB');
const contentImageHelpers = screen.getAllByText('支持 PNG/JPG/JPEG,最大 10MB');
expect(stepIconHelpers).toHaveLength(2);
expect(contentImageHelpers).toHaveLength(2);
expect(screen.queryByTestId('methodology-step-icon-help-1')).not.toBeInTheDocument();
expect(screen.queryByTestId('methodology-step-icon-help-2')).not.toBeInTheDocument();
expect(screen.queryByTestId('methodology-content-image-help-1')).not.toBeInTheDocument();
expect(screen.queryByTestId('methodology-content-image-help-2')).not.toBeInTheDocument();
[...stepIconHelpers, ...contentImageHelpers].forEach((helper) => {
expect(helper).not.toHaveClass('text-[var(--gl-color-warning-text)]');
expect(helper).not.toHaveClass('bg-[var(--gl-color-warning-bg)]');
});
});
it('caps step name input at 20 characters before save validation', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
const stepNameInput = within(firstCard).getByLabelText('步骤2名称');
fireEvent.change(stepNameInput, { target: { value: '1234567890123456789012345' } });
expect(stepNameInput).toHaveValue('12345678901234567890');
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(saveMethodologyConfig).toHaveBeenCalled();
});
expect(screen.queryByText('步骤名称不能超过 20 字')).not.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');
}
// BMP is not supported
fireEvent.change(contentImageInput, {
target: { files: [new File(['demo'], 'cover.bmp', { type: 'image/bmp' })] },
});
await waitFor(() => {
expect(within(firstCard).getByText('内容图仅支持 PNG/JPG/JPEG')).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 () => {
vi.mocked(uploadMethodologyAsset).mockRejectedValueOnce(new Error('Upload failed'));
const uploadErrorView = renderMethodology('/official-site/methodology');
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();
vi.mocked(saveMethodologyConfig).mockRejectedValueOnce(new Error('Save failed'));
const saveErrorView = renderMethodology('/official-site/methodology');
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();
vi.mocked(getMethodologyConfig).mockResolvedValueOnce({
...mockMethodologyConfig,
steps: [],
});
const emptyStepsView = renderMethodology('/official-site/methodology');
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 },
);
});
describe('步骤图标校验', () => {
it('步骤图标仅支持 PNG/JPG/JPEG,不支持 BMP', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
const stepIconInput = getStepIconInput(firstCard);
if (!stepIconInput) {
throw new Error('Step icon input not found');
}
// BMP 不支持
fireEvent.change(stepIconInput, {
target: { files: [new File(['demo'], 'icon.bmp', { type: 'image/bmp' })] },
});
await waitFor(() => {
expect(within(firstCard).getByText('步骤图标仅支持 PNG/JPG/JPEG')).toBeInTheDocument();
});
});
it('步骤图标大小不能超过 2MB', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
const stepIconInput = getStepIconInput(firstCard);
if (!stepIconInput) {
throw new Error('Step icon input not found');
}
// 超过 2MB
const oversized = new File(['demo'], 'icon.png', { type: 'image/png' });
Object.defineProperty(oversized, 'size', { value: 2 * 1024 * 1024 + 1 });
fireEvent.change(stepIconInput, {
target: { files: [oversized] },
});
await waitFor(() => {
expect(within(firstCard).getByText('步骤图标大小不能超过 2MB')).toBeInTheDocument();
});
});
it('步骤图标上传成功后使用 file_url,不读取不存在的 file_id', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
const stepIconInput = getStepIconInput(firstCard);
if (!stepIconInput) {
throw new Error('Step icon input not found');
}
// 上传成功
vi.mocked(uploadMethodologyAsset).mockResolvedValueOnce({
file_id: 'file_icon_new',
file_url: 'https://cdn.example.com/new-icon.png',
file_type: 'png',
file_size: 50000,
});
const validFile = new File(['icon content'], 'new-icon.png', { type: 'image/png' });
fireEvent.change(stepIconInput, {
target: { files: [validFile] },
});
await waitFor(() => {
expect(uploadMethodologyAsset).toHaveBeenCalled();
const formDataArg = vi.mocked(uploadMethodologyAsset).mock.calls[0][0] as FormData;
expect(formDataArg.get('usage_type')).toBe('methodology_icon');
expect(formDataArg.get('file')).not.toBeNull();
});
// 验证预览使用 file_url
await waitFor(() => {
const images = within(firstCard).getAllByRole('img');
const newIconImg = images.find(img => img.getAttribute('src') === 'https://cdn.example.com/new-icon.png');
expect(newIconImg).toBeInTheDocument();
});
// 保存时使用 file_url,不依赖 file_id
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(saveMethodologyConfig).toHaveBeenCalled();
const savePayload = vi.mocked(saveMethodologyConfig).mock.calls[0][0];
// 第一张可见卡是倒序展示的步骤 2,保存 payload 仍按正序提交
expect(savePayload.steps[1].step_icon).toBe('https://cdn.example.com/new-icon.png');
});
});
});
describe('内容图上传成功状态', () => {
it('内容图上传成功后使用 file_url,不读取不存在的 file_id', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有内容图
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 2) {
clickDeleteIcon(uploadContainers[1] as HTMLElement);
}
const contentImageInput = getContentImageInput(firstCard);
if (!contentImageInput) {
throw new Error('Content image input not found');
}
// 上传成功
vi.mocked(uploadMethodologyAsset).mockResolvedValueOnce({
file_id: 'file_content_new',
file_url: 'https://cdn.example.com/new-content.png',
file_type: 'png',
file_size: 500000,
});
const validFile = new File(['content'], 'new-content.png', { type: 'image/png' });
fireEvent.change(contentImageInput, {
target: { files: [validFile] },
});
await waitFor(() => {
expect(uploadMethodologyAsset).toHaveBeenCalled();
const formDataArg = vi.mocked(uploadMethodologyAsset).mock.calls[0][0] as FormData;
expect(formDataArg.get('usage_type')).toBe('methodology_image');
expect(formDataArg.get('file')).not.toBeNull();
});
// 验证预览使用 file_url
await waitFor(() => {
const images = within(firstCard).getAllByRole('img');
const newContentImg = images.find(img => img.getAttribute('src') === 'https://cdn.example.com/new-content.png');
expect(newContentImg).toBeInTheDocument();
});
// 保存时使用 file_url,不依赖 file_id
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(saveMethodologyConfig).toHaveBeenCalled();
const savePayload = vi.mocked(saveMethodologyConfig).mock.calls[0][0];
// 第一张可见卡是倒序展示的步骤 2,保存 payload 仍按正序提交
expect(savePayload.steps[1].step_image).toBe('https://cdn.example.com/new-content.png');
});
});
});
describe('使用默认图标', () => {
it('点击「使用默认图标」后应生成 PNG 文件并上传,使用返回 URL 作为 step_icon', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 先删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
// 模拟默认图标上传成功
vi.mocked(uploadMethodologyAsset).mockResolvedValueOnce({
file_id: 'file_default_icon_1',
file_url: 'https://cdn.example.com/default-icon-1.png',
file_type: 'png',
file_size: 5000,
});
// 点击「使用默认图标」按钮
const defaultIconButton = within(firstCard).getByRole('button', { name: '使用默认图标 步骤 2' });
fireEvent.click(defaultIconButton);
await waitFor(() => {
expect(uploadMethodologyAsset).toHaveBeenCalled();
const formDataArg = vi.mocked(uploadMethodologyAsset).mock.calls[0][0] as FormData;
expect(formDataArg.get('usage_type')).toBe('methodology_icon');
expect(formDataArg.get('file')).not.toBeNull();
const file = formDataArg.get('file') as File;
expect(file.type).toBe('image/png');
expect(file.name).toMatch(/step-\d+-default-icon\.png/);
});
// 验证预览使用上传返回的 URL,而非 data: URL
await waitFor(() => {
const images = within(firstCard).getAllByRole('img');
const defaultIconImg = images.find(img => img.getAttribute('src') === 'https://cdn.example.com/default-icon-1.png');
expect(defaultIconImg).toBeInTheDocument();
});
// 保存时 step_icon 应为上传返回的 URL
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
expect(saveMethodologyConfig).toHaveBeenCalled();
const savePayload = vi.mocked(saveMethodologyConfig).mock.calls[0][0];
expect(savePayload.steps[1].step_icon).toBe('https://cdn.example.com/default-icon-1.png');
// 不应为 data: URL
expect(savePayload.steps[1].step_icon).not.toMatch(/^data:/);
});
});
it('保存时校验 step_icon 必须是真实 URL,不能是 data: URL 或 SVG 内容', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
// 模拟默认图标上传失败,此时 step_icon 可能仍为 data: URL
vi.mocked(uploadMethodologyAsset).mockRejectedValueOnce(new Error('Upload failed'));
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
const defaultIconButton = within(firstCard).getByRole('button', { name: '使用默认图标 步骤 2' });
fireEvent.click(defaultIconButton);
// 等待上传失败
await waitFor(() => {
expect(within(firstCard).getByText('默认图标上传失败')).toBeInTheDocument();
});
// 此时预览可能仍显示 data: URL,但保存应被阻止
fireEvent.click(screen.getByRole('button', { name: '保存配置' }));
await waitFor(() => {
// 应显示校验错误
expect(within(firstCard).getByText('请上传步骤图标或等待默认图标上传完成')).toBeInTheDocument();
});
// saveMethodologyConfig 不应被调用
expect(saveMethodologyConfig).not.toHaveBeenCalled();
});
it('默认图标上传失败时展示错误反馈,允许重试', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const firstCard = screen.getAllByTestId('methodology-step-card')[0];
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
// 模拟上传失败
vi.mocked(uploadMethodologyAsset).mockRejectedValueOnce(new Error('Upload failed'));
const defaultIconButton = within(firstCard).getByRole('button', { name: '使用默认图标 步骤 2' });
fireEvent.click(defaultIconButton);
await waitFor(() => {
expect(within(firstCard).getByText('默认图标上传失败')).toBeInTheDocument();
});
// 模拟重试成功
vi.mocked(uploadMethodologyAsset).mockResolvedValueOnce({
file_id: 'file_default_icon_retry',
file_url: 'https://cdn.example.com/default-icon-retry.png',
file_type: 'png',
file_size: 5000,
});
// 再次点击「使用默认图标」重试
fireEvent.click(within(firstCard).getByRole('button', { name: '使用默认图标 步骤 2' }));
await waitFor(() => {
const images = within(firstCard).getAllByRole('img');
const retryIconImg = images.find(img => img.getAttribute('src') === 'https://cdn.example.com/default-icon-retry.png');
expect(retryIconImg).toBeInTheDocument();
});
// 错误提示消失
expect(within(firstCard).queryByText('默认图标上传失败')).not.toBeInTheDocument();
});
it('默认图标上传中禁用重复提交', async () => {
renderMethodology();
await waitFor(() => {
expect(screen.getAllByTestId('methodology-step-card')).toHaveLength(2);
});
const getFirstCard = () => screen.getAllByTestId('methodology-step-card')[0];
const firstCard = getFirstCard();
// 删除已有步骤图标
const uploadContainers = firstCard.querySelectorAll('.t-upload__card');
if (uploadContainers.length >= 1) {
clickDeleteIcon(uploadContainers[0] as HTMLElement);
}
// 模拟上传延迟
vi.mocked(uploadMethodologyAsset).mockImplementationOnce(() => new Promise(resolve => setTimeout(() => resolve({
file_id: 'file_default_icon_delayed',
file_url: 'https://cdn.example.com/default-icon-delayed.png',
file_type: 'png',
file_size: 5000,
}), 1000)));
const defaultIconButton = within(firstCard).getByRole('button', { name: '使用默认图标 步骤 2' });
fireEvent.click(defaultIconButton);
// 等待上传开始
await waitFor(() => {
expect(uploadMethodologyAsset).toHaveBeenCalledTimes(1);
});
// 上传中按钮应禁用
expect(within(getFirstCard()).getByLabelText('使用默认图标 步骤 2')).toHaveAttribute('disabled');
// 等待上传完成(需要额外 timeout 以覆盖 mock 延迟)
await waitFor(() => {
const images = within(getFirstCard()).getAllByRole('img');
const delayedIconImg = images.find(img => img.getAttribute('src') === 'https://cdn.example.com/default-icon-delayed.png');
expect(delayedIconImg).toBeInTheDocument();
}, { timeout: 2000 });
// 上传完成后按钮恢复可用
expect(within(getFirstCard()).getByRole('button', { name: '使用默认图标 步骤 2' })).not.toBeDisabled();
});
});
});