From ba36babbfc36df36f7445ba808f9e43a002c0b63 Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 12:02:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E9=80=9A=E7=9F=A5Hook?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E4=B8=8E=E5=88=A0=E9=99=A4=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E6=B5=8B=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 + .../app/features/notifications/hooks.test.tsx | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index bf171b6..517ba7f 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -684,3 +684,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成业务测试覆盖切片“订单筛选、通知数组与默认回复归一化” | 补充订单账号/状态筛选参数、通知事件数组格式和默认回复空字段保存载荷测试;覆盖 API 适配层剩余确定性分支,不依赖真实订单或推送服务;前端 API 测试、类型和注释门禁通过 | 下一步:继续补齐剩余 API 适配器与请求取消分支 | | 2026-08-15 | 完成业务测试覆盖切片“请求层认证并发与预取消边界” | 补充并发认证失败去重、上传认证失败、非 JSON 上传错误读取失败和请求开始前已取消测试;验证统一请求层的认证事件、状态码兜底和取消错误收口,不依赖真实服务;前端请求测试、类型和注释门禁通过 | 下一步:继续补齐 Chat/Notifications/Orders 等 Hook 的可控生命周期分支 | | 2026-08-15 | 完成业务测试覆盖切片“Chat Hook 初始化、轮询与实时生命周期” | 补充聊天初始化失败、运行状态轮询失败重调度、历史消息成功滚动恢复、未知实时会话刷新、空消息帧和 WebSocket 错误关闭测试;仅使用 API、定时器、WebSocket 与 DOM 替身,不触发真实账号会话;前端 Chat Hook 测试、类型和注释门禁通过 | 下一步:继续补齐 Notifications/Orders/Items 等 Hook 的可控生命周期分支 | +| 2026-08-15 | 完成业务测试覆盖切片“Notifications Hook 定时器、SMTP 与删除边界” | 补充 SMTP 初始加载失败、提示自动消失、弹窗关闭、用户拒绝删除和删除请求失败测试;验证通知状态清理和错误收口,不触发真实推送或邮件服务;前端 Notifications Hook 测试、类型和注释门禁通过 | 下一步:继续补齐 Orders/Items/Settings 等 Hook 的可控生命周期分支 | diff --git a/frontend/app/features/notifications/hooks.test.tsx b/frontend/app/features/notifications/hooks.test.tsx index 18b9f68..c4a7f54 100644 --- a/frontend/app/features/notifications/hooks.test.tsx +++ b/frontend/app/features/notifications/hooks.test.tsx @@ -193,4 +193,73 @@ describe('useNotifications', /* 当前回调处理通知渠道、SMTP 和动作 expect(hook.result.current.toast).toEqual({ type: 'error', text: 'SMTP保存失败' }); hook.unmount(); }); + + test('SMTP 初始加载失败时记录错误并保持渠道加载完成', /* 当前回调验证管理员 SMTP 初始化错误分支。 */ async () => { + getSmtpMock.mockRejectedValueOnce(new Error('SMTP读取失败')); + // consoleError 是 SMTP 加载错误日志的可控替身。 + const consoleError = vi.spyOn(console, 'error').mockImplementation(/* errorLogger 忽略测试日志输出。 */ () => undefined); + // hook 是 SMTP 初始化失败场景的通知 Hook 渲染结果。 + const hook = renderHook( + // smtpFailureHookFactory 创建 SMTP 初始化失败场景的 Hook。 + () => useNotifications(true), + ); + await waitFor( + // loadingAssertion 等待通知渠道列表加载完成。 + () => expect(hook.result.current.loading).toBe(false), + ); + expect(consoleError).toHaveBeenCalledWith('加载 SMTP 配置失败', expect.any(Error)); + hook.unmount(); + consoleError.mockRestore(); + }); + + test('提示自动消失、弹窗关闭和删除拒绝均清理状态', /* 当前回调验证通知 Hook 的定时器与守卫分支。 */ async () => { + vi.useFakeTimers(); + try { + // hook 是通知提示和弹窗状态场景的 Hook 渲染结果。 + const hook = renderHook( + // toastHookFactory 创建通知提示场景的 Hook。 + () => useNotifications(false), + ); + await act( + // toastAction 展示一条短暂提示。 + () => hook.result.current.showToast('success', '短暂提示'), + ); + expect(hook.result.current.toast).toEqual({ type: 'success', text: '短暂提示' }); + await act( + // toastTimerAction 推进三秒自动清理提示。 + async () => { await vi.advanceTimersByTimeAsync(3_000); }, + ); + expect(hook.result.current.toast).toBeNull(); + await act( + // openAction 打开渠道弹窗。 + () => hook.result.current.openCreate(), + ); + await act( + // closeAction 关闭渠道弹窗并取消动作。 + () => hook.result.current.closeModal(), + ); + expect(hook.result.current.showModal).toBe(false); + // confirmMock 是用户取消删除时的浏览器确认框替身。 + vi.mocked(window.confirm).mockReturnValue(false); + await act( + // rejectedDeleteAction 触发用户拒绝删除。 + async () => hook.result.current.handleDelete(channelFixture), + ); + expect(deleteChannelMock).not.toHaveBeenCalled(); + vi.mocked(window.confirm).mockReturnValue(true); + deleteChannelMock.mockRejectedValueOnce(new Error('删除失败')); + await act( + // failedDeleteAction 触发删除请求失败。 + async () => hook.result.current.handleDelete(channelFixture), + ); + expect(hook.result.current.toast).toEqual({ type: 'error', text: '删除失败' }); + await act( + // cleanupToastAction 创建未到期提示后卸载 Hook,验证定时器清理。 + () => hook.result.current.showToast('success', '卸载清理'), + ); + hook.unmount(); + } finally { + vi.useRealTimers(); + } + }); });