From f9debf687fed38d0b6dda5008b1030482cbfb9d2 Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 11:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E5=95=86=E5=93=81=E6=89=B9?= =?UTF-8?q?=E9=87=8FHook=E5=BC=82=E5=B8=B8=E8=BD=AE=E8=AF=A2=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture/refactoring-master-plan.md | 1 + frontend/app/features/items/hooks.test.tsx | 113 +++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index fa43082..1fbbcb8 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -662,3 +662,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成业务测试覆盖切片“请求层方法与取消/错误边界” | 新增 PUT/DELETE 请求、纯文本错误、损坏 JSON 错误体、外部 AbortSignal 及上传取消测试;验证统一错误消息、认证失败通知和超时/取消区分;前端请求测试、类型和注释门禁通过,纯 UI 组件不纳入覆盖率目标 | 下一步:补齐业务 Hook 的未覆盖错误分支 | | 2026-08-15 | 完成业务测试覆盖切片“请求层网络异常与上传载荷” | 补充普通请求/上传请求网络异常透传、上传失败响应和原始错误载荷保留测试;覆盖请求层非业务网络分支,不依赖真实服务;前端请求测试、类型和注释门禁通过 | 下一步:补齐业务 Hook 的未覆盖错误分支 | | 2026-08-15 | 完成业务测试覆盖切片“账号自动化 Hook 失败与重试” | 补充任务设置读取失败、任务执行失败和失败重试测试,覆盖业务错误提示、重试动作和状态清理;不触发真实账号任务;前端 Hook 测试、类型和注释门禁通过 | 下一步:补齐 Accounts/Items/Notifications 等 Hook 的未覆盖错误分支 | +| 2026-08-15 | 完成业务测试覆盖切片“商品批量发布 Hook 异常与轮询” | 补充类目推荐、批量预检、启动、取消、重试、最近结果和预检清理失败测试,并覆盖轮询完成后刷新商品/发货规则列表;使用 API 替身验证状态机,不触发真实商品文件或外部平台;前端 Hook 测试、类型和注释门禁通过 | 下一步:补齐 Accounts 子模块与 Notifications Hook 的未覆盖错误分支 | diff --git a/frontend/app/features/items/hooks.test.tsx b/frontend/app/features/items/hooks.test.tsx index 5c65c9c..33b2cfe 100644 --- a/frontend/app/features/items/hooks.test.tsx +++ b/frontend/app/features/items/hooks.test.tsx @@ -190,4 +190,117 @@ describe('useItemPublishBatch', /* 当前回调处理批量发布的表单、任 expect(deleteBatchMock).not.toHaveBeenCalled(); hook.unmount(); }); + + test('类目、预检、启动、取消和重试失败都会提示错误', /* 当前回调验证批量动作的异常响应分支。 */ async () => { + // hook 是默认账号异常动作场景的批量 Hook 渲染结果。 + const hook = renderHook(/* errorHookFactory 创建异常动作场景的 Hook。 */ () => useItemPublishBatch({ selectedAccount: 'account-1', loadItems: vi.fn(), loadShippingRules: vi.fn() })); + await act( + // keywordAction 写入类目关键词。 + () => hook.result.current.setBatchCategoryKeyword('服饰'), + ); + recommendCategoryMock.mockRejectedValueOnce(new Error('类目失败')); + await act( + // recommendAction 触发失败的类目推荐。 + async () => hook.result.current.handleRecommendBatchCategory(), + ); + expect(alert).toHaveBeenCalledWith('类目失败'); + + await act( + // fileAction 设置商品表格文件。 + () => hook.result.current.setBatchFile(new File(['title'], 'items.xlsx')), + ); + previewBatchMock.mockRejectedValueOnce(new Error('预检失败')); + await act( + // previewAction 触发失败的批量预检。 + async () => hook.result.current.handlePreviewBatch(), + ); + expect(alert).toHaveBeenCalledWith('预检失败'); + + await act( + // previewStateAction 注入可启动预检结果。 + () => hook.result.current.setBatchPreview(previewFixture), + ); + startBatchMock.mockRejectedValueOnce(new Error('启动失败')); + await act( + // startAction 触发失败的批量启动。 + async () => hook.result.current.handleStartBatch(), + ); + expect(alert).toHaveBeenCalledWith('启动失败'); + + await act( + // detailStateAction 注入运行中的任务详情。 + () => hook.result.current.setBatchDetail(runningBatchFixture), + ); + cancelBatchMock.mockRejectedValueOnce(new Error('取消失败')); + await act( + // cancelAction 触发失败的批量取消。 + async () => hook.result.current.handleCancelBatch(), + ); + expect(alert).toHaveBeenCalledWith('取消失败'); + + await act( + // failedDetailAction 注入存在失败行的任务详情。 + () => hook.result.current.setBatchDetail(failedBatchFixture), + ); + retryBatchMock.mockRejectedValueOnce(new Error('重试失败')); + await act( + // retryAction 触发失败行重试错误。 + async () => hook.result.current.handleRetryBatchFailed(), + ); + expect(alert).toHaveBeenCalledWith('重试失败'); + + await act( + // recentStateAction 注入最近任务以覆盖结果读取异常。 + () => hook.result.current.setRecentBatch(runningBatchFixture), + ); + getBatchMock.mockRejectedValueOnce(new Error('结果读取失败')); + await act( + // recentAction 触发最近任务读取错误。 + async () => hook.result.current.openRecentBatchResult(), + ); + expect(hook.result.current.batchLoading).toBe(false); + + await act( + // previewStateAction 注入临时预检任务。 + () => { + hook.result.current.setBatchPreview(previewFixture); + hook.result.current.setBatchPhase('preview'); + }, + ); + deleteBatchMock.mockRejectedValueOnce(new Error('清理失败')); + await act( + // abandonAction 触发临时预检清理错误。 + async () => hook.result.current.abandonBatchPreview(), + ); + expect(hook.result.current.batchPhase).toBe('upload'); + hook.unmount(); + }); + + test('轮询完成后刷新商品和发货规则列表', /* 当前回调验证批量轮询完成分支。 */ async () => { + vi.useFakeTimers(); + // loadItems 是轮询完成后的商品列表刷新替身。 + const loadItems = vi.fn().mockResolvedValue(undefined); + // loadShippingRules 是轮询完成后的规则列表刷新替身。 + const loadShippingRules = vi.fn().mockResolvedValue(undefined); + // doneBatchFixture 是轮询返回的完成任务详情。 + const doneBatchFixture = { ...runningBatchFixture, status: 'completed' as const, running: 0, success: 1 }; + getBatchMock.mockResolvedValueOnce(doneBatchFixture); + // hook 是批量轮询场景的 Hook 渲染结果。 + const hook = renderHook(/* pollingHookFactory 创建批量轮询场景的 Hook。 */ () => useItemPublishBatch({ selectedAccount: 'account-1', loadItems, loadShippingRules })); + await act( + // stateAction 打开弹窗并注入运行中的任务。 + () => { + hook.result.current.setShowBatchModal(true); + hook.result.current.setBatchDetail(runningBatchFixture); + }, + ); + await act(/* timerAction 推进批量轮询计时器。 */ async () => { + await vi.advanceTimersByTimeAsync(3_000); + }); + expect(getBatchMock).toHaveBeenCalledWith('batch-1'); + expect(loadItems).toHaveBeenCalledOnce(); + expect(loadShippingRules).toHaveBeenCalledOnce(); + hook.unmount(); + vi.useRealTimers(); + }); });