补齐React Rules数据Hook测试
This commit is contained in:
1 parent
30762d4121
commit
623624235e
2 files changed
+150
No files matched your search
@@ -654,3 +654,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Cards Hook 库存与批量操作覆盖” | 新增卡密库存 Hook 的成功/失败加载测试;新增批量操作 Hook 的追加预览、追加成功、批量创建成功和追加失败测试;前端 223 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 38.19%;真实卡密库存和外部 API 卡密仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Chat/Items/Rules Hook 与页面行为测试 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Chat Hook 加载、分页与发送覆盖” | 新增聊天 Hook 的账号/运行状态加载、会话与消息分页、已读标记、文字/图片发送、发送失败重试和 WebSocket 清理测试;前端 225 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 44.11%;真实聊天账号、外部平台消息和 WebSocket 服务仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Items/Rules Hook、账号子模块 Hook 与页面行为测试 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Items 批量发布 Hook 行为覆盖” | 新增批量发布 Hook 的任务恢复、类目推荐、文件预检、任务启动、取消、最近结果、失败重试、关闭清理及表单守卫测试;修复批量状态变化后重试回调捕获旧状态的依赖问题;前端 227 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 46.88%;真实商品文件、账号凭证和外部发布平台仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Rules Hook、账号子模块 Hook 与页面行为测试 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Rules 数据 Hook 页签与异常隔离覆盖” | 新增规则数据 Hook 的参考数据并行加载、自动化规则分页/筛选、服务端页码修正、异常列表失败隔离、关键词规则、默认回复和无账号守卫测试;前端 229 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 48.38%;真实账号规则和外部平台数据仍不触网,仅验证可控接口契约、请求代次和页签状态机 | 下一步:继续补齐账号子模块 Hook 与页面行为测试 |
|
||||
@@ -0,0 +1,149 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { AccountDetail, Card, DefaultReply, Item, ReplyRule, ShippingRule } from '../../../types';
|
||||
import {
|
||||
getAccountDetails,
|
||||
getAutomationIssues,
|
||||
getCards,
|
||||
getDefaultReplies,
|
||||
getItems,
|
||||
getReplyRules,
|
||||
getShippingRulesPage,
|
||||
} from './api';
|
||||
import { useRulesData } from './hooks';
|
||||
|
||||
vi.mock('./api', /* rulesApiMockFactory 提供规则数据 Hook 的确定性 API 替身。 */ () => ({
|
||||
getAccountDetails: vi.fn(),
|
||||
getAutomationIssues: vi.fn(),
|
||||
getCards: vi.fn(),
|
||||
getDefaultReplies: vi.fn(),
|
||||
getItems: vi.fn(),
|
||||
getReplyRules: vi.fn(),
|
||||
getShippingRulesPage: vi.fn(),
|
||||
}));
|
||||
|
||||
// accountsMock 是账号参考数据请求的可控替身。
|
||||
const accountsMock = vi.mocked(getAccountDetails);
|
||||
// issuesMock 是自动化异常请求的可控替身。
|
||||
const issuesMock = vi.mocked(getAutomationIssues);
|
||||
// cardsMock 是卡密参考数据请求的可控替身。
|
||||
const cardsMock = vi.mocked(getCards);
|
||||
// defaultsMock 是默认回复请求的可控替身。
|
||||
const defaultsMock = vi.mocked(getDefaultReplies);
|
||||
// itemsMock 是商品参考数据请求的可控替身。
|
||||
const itemsMock = vi.mocked(getItems);
|
||||
// repliesMock 是关键词回复规则请求的可控替身。
|
||||
const repliesMock = vi.mocked(getReplyRules);
|
||||
// shippingPageMock 是自动化规则分页请求的可控替身。
|
||||
const shippingPageMock = vi.mocked(getShippingRulesPage);
|
||||
|
||||
// accountFixture 是规则筛选使用的账号摘要。
|
||||
const accountFixture = { id: 'account-1', enabled: true, auto_confirm: false, nickname: '账号一' } as AccountDetail;
|
||||
// cardFixture 是动作配置使用的卡密摘要。
|
||||
const cardFixture = { id: 1, group_id: 2, content: '卡密内容', status: 'available' } as never as Card;
|
||||
// itemFixture 是规则编辑器使用的商品摘要。
|
||||
const itemFixture = { id: 'item-1', cookie_id: 'account-1', title: '测试商品' } as never as Item;
|
||||
// defaultReplyFixture 是账号默认回复配置。
|
||||
const defaultReplyFixture = { cookie_id: 'account-1', enabled: true, reply_content: '欢迎', reply_once: false, reply_image_url: '' } as DefaultReply;
|
||||
// replyFixture 是关键词回复规则。
|
||||
const replyFixture = { id: 'reply-1', keyword: '你好', reply_content: '您好', match_type: 'fuzzy', enabled: true, item_id: '', type: 'text', image_url: '' } as ReplyRule;
|
||||
// shippingFixture 是自动化规则分页中的一条规则。
|
||||
const shippingFixture = { id: 1, cookie_id: 'account-1', name: '付款发货', trigger_type: 'order_paid', enabled: true, actions: [], variants: [], config_json: '{}' } as never as ShippingRule;
|
||||
|
||||
describe('useRulesData', /* 当前回调处理规则页参考数据、分页和页签刷新。 */ () => {
|
||||
beforeEach(/* 当前回调重置规则 API 替身为成功默认值。 */ () => {
|
||||
vi.clearAllMocks();
|
||||
accountsMock.mockResolvedValue([accountFixture]);
|
||||
cardsMock.mockResolvedValue([cardFixture]);
|
||||
itemsMock.mockResolvedValue([itemFixture]);
|
||||
defaultsMock.mockResolvedValue({ 'account-1': defaultReplyFixture });
|
||||
issuesMock.mockResolvedValue({ runs: [], pending_tasks: [] });
|
||||
repliesMock.mockResolvedValue([replyFixture]);
|
||||
shippingPageMock.mockResolvedValue({ success: true, data: [shippingFixture], total: 1, page: 1, page_size: 10, total_pages: 1, trigger_counts: { order_paid: 1 } });
|
||||
});
|
||||
|
||||
test('加载参考数据、自动化规则并处理服务端页码修正', /* 当前回调验证自动化页的主要成功路径。 */ async () => {
|
||||
// setSelectedAccountId 是参考数据加载后回填首个账号的状态替身。
|
||||
const setSelectedAccountId = vi.fn();
|
||||
// onPageChange 接收服务端修正后的页码。
|
||||
const onPageChange = vi.fn();
|
||||
// options 是自动化页的固定筛选条件。
|
||||
const options = { activeTab: 'automation' as const, selectedAccountId: 'account-1', automationTriggerFilter: '' as const, automationStatusFilter: 'all' as const, debouncedAutomationSearch: '测试', automationPage: 2, automationPageSize: 10, setSelectedAccountId, onAutomationPageChange: onPageChange };
|
||||
shippingPageMock.mockResolvedValueOnce({ success: true, data: [shippingFixture], total: 1, page: 1, page_size: 10, total_pages: 1, trigger_counts: { order_paid: 1 } });
|
||||
// hook 是规则数据 Hook 的渲染结果。
|
||||
const hook = renderHook(
|
||||
// automationHookFactory 创建自动化页规则 Hook。
|
||||
() => useRulesData(options),
|
||||
);
|
||||
await act(
|
||||
// referenceAction 加载规则编辑器参考数据。
|
||||
async () => hook.result.current.loadReferenceData(),
|
||||
);
|
||||
expect(hook.result.current.accounts).toEqual([accountFixture]);
|
||||
expect(hook.result.current.cards).toEqual([cardFixture]);
|
||||
expect(setSelectedAccountId).toHaveBeenCalled();
|
||||
await act(
|
||||
// automationAction 加载分页规则和异常。
|
||||
async () => hook.result.current.loadAutomationRules(),
|
||||
);
|
||||
expect(shippingPageMock).toHaveBeenCalledWith(expect.objectContaining({ cookieId: 'account-1', page: 2, search: '测试' }));
|
||||
expect(hook.result.current.automationRules).toEqual([shippingFixture]);
|
||||
expect(hook.result.current.automationTotalPages).toBe(1);
|
||||
expect(onPageChange).toHaveBeenCalledWith(1);
|
||||
hook.unmount();
|
||||
});
|
||||
|
||||
test('异常请求失败不阻断规则展示,关键词和默认回复按页签刷新', /* 当前回调验证异常隔离和三个页签刷新分支。 */ async () => {
|
||||
// setSelectedAccountId 是页签刷新所需的账号状态替身。
|
||||
const setSelectedAccountId = vi.fn();
|
||||
// activeTab 是当前规则页签,可在测试中切换。
|
||||
let activeTab: 'automation' | 'reply' | 'default' = 'automation';
|
||||
// optionsFactory 根据当前页签生成 Hook 参数。
|
||||
const optionsFactory = () => ({ activeTab, selectedAccountId: 'account-1', automationTriggerFilter: 'order_paid' as const, automationStatusFilter: 'enabled' as const, debouncedAutomationSearch: '', automationPage: 1, automationPageSize: 5, setSelectedAccountId });
|
||||
issuesMock.mockRejectedValueOnce(new Error('异常接口不可用'));
|
||||
// hook 是多页签规则 Hook 的渲染结果。
|
||||
const hook = renderHook(
|
||||
// multiTabHookFactory 创建可切换页签的规则 Hook。
|
||||
() => useRulesData(optionsFactory()),
|
||||
);
|
||||
await act(
|
||||
// automationRefresh 刷新自动化页并隔离异常接口失败。
|
||||
async () => hook.result.current.refresh(),
|
||||
);
|
||||
expect(hook.result.current.automationRules).toEqual([shippingFixture]);
|
||||
expect(hook.result.current.automationIssues).toEqual({ runs: [], pending_tasks: [] });
|
||||
|
||||
activeTab = 'reply';
|
||||
hook.rerender();
|
||||
await act(
|
||||
// replyRefresh 刷新关键词规则页签。
|
||||
async () => hook.result.current.refresh(),
|
||||
);
|
||||
expect(repliesMock).toHaveBeenCalledWith('account-1');
|
||||
expect(hook.result.current.replyRules).toEqual([replyFixture]);
|
||||
|
||||
activeTab = 'default';
|
||||
hook.rerender();
|
||||
await act(
|
||||
// defaultRefresh 刷新默认回复页签。
|
||||
async () => hook.result.current.refresh(),
|
||||
);
|
||||
expect(defaultsMock).toHaveBeenCalled();
|
||||
expect(hook.result.current.defaultReplies).toEqual({ 'account-1': defaultReplyFixture });
|
||||
|
||||
activeTab = 'reply';
|
||||
// emptyHook 是无账号筛选场景的规则 Hook 渲染结果。
|
||||
const emptyHook = renderHook(
|
||||
// emptyHookFactory 创建无账号筛选的规则 Hook。
|
||||
() => useRulesData({ ...optionsFactory(), selectedAccountId: '' }),
|
||||
);
|
||||
await act(
|
||||
// emptyReplyAction 验证没有账号时关键词列表直接清空。
|
||||
async () => emptyHook.result.current.loadReplyRules(),
|
||||
);
|
||||
expect(emptyHook.result.current.replyRules).toEqual([]);
|
||||
emptyHook.unmount();
|
||||
hook.unmount();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user