补齐 AccountList 页面行为边界测试
This commit is contained in:
1 parent
d3271e357a
commit
fae27a482b
2 files changed
+221
No files matched your search
@@ -186,6 +186,7 @@ app shell / routes
|
||||
- 已完成阶段 7 当前 PR 切片“认证后路由壳与页面组合边界”:新增 `AuthenticatedShell` 与 `AppContent`,将侧边栏、主内容布局、权限回退、商品到规则页联动和动态页面组合从 `App.tsx` 移出;更新响应式布局与路由静态契约测试,前端 326 个测试、类型检查、注释门禁、生产构建和仓库 `make check` 通过。Provider 状态边界、完整页面行为覆盖、动态 chunk 独立预算和大型页面进一步拆分仍待完成;
|
||||
- 已完成阶段 7 当前 PR 切片“认证会话 Provider 状态边界”:新增 `SessionProvider`/`useSession`,集中会话校验、AbortController 生命周期、首次管理员初始化、登录、注销和 `auth:logout` 全局事件;`App.tsx` 只保留认证表单输入、导航状态和页面展示协调,不再直接调用会话 API 或维护全局认证状态;新增 Provider 行为测试覆盖已认证、未初始化、登录/初始化成功、全局注销及注销失败清理,前端 330 个测试、类型检查、注释门禁、生产构建和仓库 `make check` 通过。页面级 Provider 组合、完整页面行为覆盖、动态 chunk 独立预算和大型页面进一步拆分仍待完成;
|
||||
- 已完成阶段 7 当前 PR 切片“React 路由组合行为与动态 chunk 独立预算”:`bundleBoundary.test.ts` 新增九个业务页面的独立原始字节预算门禁;`AuthenticatedShell.test.tsx` 新增非管理员设置回退、管理员设置加载、商品→规则目标传递及规则目标消费行为测试;前端路由组合与生产资源边界均有运行时/产物证据,页面行为和动态分片治理继续保持可回归。完整页面行为覆盖、更多大型页面边界和全局 Provider 组合仍待完成;
|
||||
- 已完成阶段 7 当前 PR 切片“React AccountList 完整页面行为边界”:新增真实挂载 `AccountList` 的组合行为测试,覆盖搜索过滤与计数提示、删除确认后的 API/本地状态更新、启用状态切换、资料刷新、二维码生成与轮询成功、二维码关闭时停止轮询和取消请求;通过替身隔离真实账号、二维码和浏览器数据,不跳过页面协调逻辑。AccountList 内部 QR 协调器进一步 feature 化、其他大型页面完整行为覆盖和全局 Provider 组合仍待完成;
|
||||
- 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。
|
||||
|
||||
## 6. 阶段 0:治理文档与强约束
|
||||
@@ -769,3 +770,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P2 React 认证后路由壳与页面组合边界 | 已完成本切片 | 新增 `frontend/app/shell/AuthenticatedShell.tsx`,由 `AuthenticatedShell` 统一组合 Sidebar、主内容布局和 `AppContent` 页面选择;`App.tsx` 只保留认证、导航状态及跨页面联动协调,权限回退和商品→规则页参数语义不变;更新 `routing.test.ts`、`rulesFeature.test.ts` 以验证新的壳边界,前端 326 个测试、类型检查、注释门禁、生产构建和 `make check` 通过。Provider 状态边界、动态 chunk 独立预算、完整页面行为覆盖和大型页面进一步拆分仍未完成,阶段 7 保持“进行中” |
|
||||
| 2026-08-15 | P2 React 认证会话 Provider 状态边界 | 已完成本切片 | 新增 `frontend/app/providers/SessionProvider.tsx`,集中会话校验、AbortController 取消、首次管理员初始化、登录、注销和 `auth:logout` 监听;`App.tsx` 通过 `useSession` 使用认证状态与动作,认证 API 不再直接耦合到页面根组件;新增 `SessionProvider.test.tsx` 覆盖已认证、未初始化、登录/初始化成功、全局注销和注销失败清理,前端 330 个测试、类型检查、注释门禁、生产构建和 `make check` 通过。更细粒度页面 Provider、完整页面行为覆盖、动态 chunk 独立预算和大型页面进一步拆分仍未完成,阶段 7 保持“进行中” |
|
||||
| 2026-08-15 | P2 React 路由组合行为与动态 chunk 独立预算 | 已完成本切片 | `bundleBoundary.test.ts` 为 Dashboard、AccountList、OrderList、CardList、ItemList、Settings、Rules、Notifications、Chat 分别设置原始字节预算并验证各自只有一个动态 chunk;新增 `AuthenticatedShell.test.tsx` 通过懒加载替身覆盖非管理员设置回退、管理员设置加载、商品→规则目标传递和规则目标消费回调;路由行为、生产构建、前端全量测试、类型检查和注释门禁通过。完整页面行为覆盖、更多大型页面边界和全局 Provider 组合仍未完成,阶段 7 保持“进行中” |
|
||||
| 2026-08-15 | P2 React AccountList 完整页面行为边界 | 已完成本切片 | 新增 `frontend/components/AccountList.test.tsx`,真实挂载页面并隔离 API、账号数据 Hook、二维码轮询器和弹窗子组件;覆盖搜索过滤/计数、删除确认与状态 Setter 过滤、账号启停、资料刷新、二维码生成→轮询→保存成功、关闭二维码停止轮询并取消请求;前端定向测试、注释门禁和类型检查通过。AccountList QR 协调器进一步 feature 化、其他大型页面完整行为覆盖和全局 Provider 组合仍未完成,阶段 7 保持“进行中” |
|
||||
@@ -0,0 +1,219 @@
|
||||
// @vitest-environment jsdom
|
||||
import React from 'react';
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { AccountDetail } from '../types';
|
||||
|
||||
// accountListMocks 保存 AccountList 页面测试使用的 API、Hook 和轮询替身。
|
||||
const accountListMocks = vi.hoisted(/* accountListMockFactory 创建页面测试共享替身。 */ () => ({
|
||||
accounts: [
|
||||
{ id: 'account-1', nickname: 'Alpha', remark: '主账号', enabled: true, runtime_message: '在线' },
|
||||
{ id: 'account-2', nickname: 'Beta', remark: '备用账号', enabled: false, runtime_message: '已停用' },
|
||||
] as AccountDetail[],
|
||||
setAccounts: vi.fn(),
|
||||
loadAccounts: vi.fn(),
|
||||
updateAccountStatus: vi.fn(),
|
||||
deleteAccount: vi.fn(),
|
||||
generateQRLogin: vi.fn(),
|
||||
checkQRLoginStatus: vi.fn(),
|
||||
completeQRVerification: vi.fn(),
|
||||
refreshAccountProfile: vi.fn(),
|
||||
pollerStart: vi.fn(),
|
||||
pollerStop: vi.fn(),
|
||||
requestNext: vi.fn(),
|
||||
requestIsCurrent: vi.fn(),
|
||||
requestCancel: vi.fn(),
|
||||
pollCallbacks: null as Record<string, (...args: any[]) => void> | null,
|
||||
}));
|
||||
|
||||
vi.mock('../app/features/accounts/hooks', /* accountsHookMockFactory 提供账号列表数据 Hook 替身。 */ () => ({
|
||||
useAccountsData: /* useAccountsDataMock 返回固定账号列表和刷新操作。 */ () => ({
|
||||
accounts: accountListMocks.accounts,
|
||||
setAccounts: accountListMocks.setAccounts,
|
||||
loading: false,
|
||||
loadAccounts: accountListMocks.loadAccounts,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../app/features/accounts/submoduleHooks', /* submoduleHookMockFactory 提供账号弹窗状态 Hook 替身。 */ () => ({
|
||||
useAccountSubmodules: /* useAccountSubmodulesMock 返回不阻断页面渲染的子模块状态。 */ () => ({
|
||||
longLogin: false,
|
||||
notifChannels: [],
|
||||
selectedChannelIds: [],
|
||||
bindingsLoaded: true,
|
||||
bindingsLoading: false,
|
||||
bindingsLoadError: '',
|
||||
aiSettings: {},
|
||||
saving: false,
|
||||
passwordLoginView: { status: 'idle', message: '' },
|
||||
setAiSettings: vi.fn(),
|
||||
setBindingsDirty: vi.fn(),
|
||||
openEditModal: vi.fn(),
|
||||
closeEditModal: vi.fn(),
|
||||
openAIModal: vi.fn(),
|
||||
closeAIModal: vi.fn(),
|
||||
loadNotificationBindings: vi.fn(),
|
||||
toggleNotificationChannel: vi.fn(),
|
||||
handleLongLoginToggle: vi.fn(),
|
||||
handleSaveAISettings: vi.fn(),
|
||||
handleSaveEdit: vi.fn(),
|
||||
handleRestartPause: vi.fn(),
|
||||
handlePasswordLogin: vi.fn(),
|
||||
handleCancelPasswordLogin: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../app/features/accounts/api', /* accountApiMockFactory 提供账号页面 API 替身。 */ () => ({
|
||||
updateAccountStatus: accountListMocks.updateAccountStatus,
|
||||
deleteAccount: accountListMocks.deleteAccount,
|
||||
generateQRLogin: accountListMocks.generateQRLogin,
|
||||
checkQRLoginStatus: accountListMocks.checkQRLoginStatus,
|
||||
completeQRVerification: accountListMocks.completeQRVerification,
|
||||
refreshAccountProfile: accountListMocks.refreshAccountProfile,
|
||||
}));
|
||||
|
||||
vi.mock('../components/qrPolling', /* qrPollingMockFactory 提供二维码轮询器替身。 */ () => ({
|
||||
createLatestRequestGate: /* requestGateFactory 创建二维码请求代次门禁。 */ () => ({
|
||||
next: accountListMocks.requestNext,
|
||||
isCurrent: accountListMocks.requestIsCurrent,
|
||||
cancel: accountListMocks.requestCancel,
|
||||
}),
|
||||
createQRLoginPoller: /* qrPollerFactory 创建二维码轮询控制器。 */ () => ({
|
||||
start: accountListMocks.pollerStart,
|
||||
stop: accountListMocks.pollerStop,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('../app/features/accounts/components/AccountCard', /* accountCardMockFactory 提供账号卡片交互替身。 */ () => {
|
||||
// AccountCardMock 渲染页面测试需要的账号操作入口。
|
||||
const AccountCardMock: React.FC<any> = (props /* props 表示账号卡片及其事件回调。 */) => (
|
||||
<div data-testid={`account-card-${props.account.id}`}>
|
||||
<span>{props.account.nickname}</span>
|
||||
<button onClick={/* deleteAction 触发账号删除确认。 */ () => props.onDelete(props.account)}>删除</button>
|
||||
<button onClick={/* toggleAction 切换账号启用状态。 */ () => props.onToggle(props.account.id, props.account.enabled)}>切换</button>
|
||||
<button onClick={/* refreshAction 刷新账号资料。 */ () => props.onRefreshProfile(props.account)}>刷新资料</button>
|
||||
<button onClick={/* reauthorizeAction 启动账号二维码重新授权。 */ () => props.onReauthorize(props.account)}>重新授权</button>
|
||||
</div>
|
||||
);
|
||||
return { AccountCard: AccountCardMock };
|
||||
});
|
||||
|
||||
vi.mock('../app/features/accounts/components/AccountDeleteDialog', /* deleteDialogMockFactory 提供删除确认弹窗替身。 */ () => {
|
||||
// AccountDeleteDialogMock 展示删除错误并暴露确认/关闭操作。
|
||||
const AccountDeleteDialogMock: React.FC<any> = (props /* props 表示删除弹窗及其操作回调。 */) => (
|
||||
<div data-testid="delete-dialog">
|
||||
<span>{props.error}</span>
|
||||
<button onClick={/* confirmAction 确认删除当前账号。 */ () => props.onConfirm()}>确认删除</button>
|
||||
<button onClick={/* closeAction 关闭删除确认弹窗。 */ () => props.onClose()}>取消</button>
|
||||
</div>
|
||||
);
|
||||
return { AccountDeleteDialog: AccountDeleteDialogMock };
|
||||
});
|
||||
|
||||
vi.mock('../app/features/accounts/components/AccountQRCodeModal', /* qrModalMockFactory 提供二维码弹窗替身。 */ () => {
|
||||
// AccountQRCodeModalMock 展示二维码状态并暴露关闭操作。
|
||||
const AccountQRCodeModalMock: React.FC<any> = (props /* props 表示二维码弹窗及其操作回调。 */) => (
|
||||
<div data-testid="qr-modal">
|
||||
<span>{props.status}</span>
|
||||
<button onClick={/* closeAction 关闭二维码登录弹窗。 */ () => props.onClose()}>关闭二维码</button>
|
||||
</div>
|
||||
);
|
||||
return { AccountQRCodeModal: AccountQRCodeModalMock };
|
||||
});
|
||||
|
||||
vi.mock('../app/features/accounts/components/AccountEditModal', /* editModalMockFactory 提供账号编辑弹窗替身。 */ () => ({
|
||||
AccountEditModal: /* AccountEditModalMock 表示账号编辑弹窗替身。 */ () => null,
|
||||
}));
|
||||
|
||||
vi.mock('../app/features/accounts/components/AccountAISettingsModal', /* aiModalMockFactory 提供 AI 设置弹窗替身。 */ () => ({
|
||||
AccountAISettingsModal: /* AccountAISettingsModalMock 表示 AI 设置弹窗替身。 */ () => null,
|
||||
}));
|
||||
|
||||
vi.mock('../components/AccountAutomationModal', /* automationModalMockFactory 提供账号任务弹窗替身。 */ () => ({
|
||||
default: /* AccountAutomationModalMock 表示账号自动化弹窗替身。 */ () => null,
|
||||
}));
|
||||
|
||||
import AccountList from './AccountList';
|
||||
|
||||
describe('AccountList 页面组合行为', /* 当前回调验证账号列表页面的搜索、删除、资料和二维码流程。 */ () => {
|
||||
beforeEach(/* 当前回调重置账号页面 API、请求门禁和浏览器提示替身。 */ () => {
|
||||
vi.clearAllMocks();
|
||||
accountListMocks.requestNext.mockReturnValue(1);
|
||||
accountListMocks.requestIsCurrent.mockReturnValue(true);
|
||||
accountListMocks.generateQRLogin.mockResolvedValue({ success: true, qr_code_url: 'qr-url', session_id: 'session-1' });
|
||||
accountListMocks.completeQRVerification.mockResolvedValue({ success: true, account_id: 'account-1' });
|
||||
accountListMocks.updateAccountStatus.mockResolvedValue({ success: true });
|
||||
accountListMocks.deleteAccount.mockResolvedValue({ success: true });
|
||||
accountListMocks.refreshAccountProfile.mockResolvedValue({ success: true });
|
||||
vi.spyOn(window, 'alert').mockImplementation(/* alertImplementation 屏蔽页面资料刷新提示。 */ () => undefined);
|
||||
accountListMocks.pollerStart.mockImplementation(/* pollerStartImplementation 保存二维码轮询回调。 */ (_sessionId: string, _check: unknown, callbacks: Record<string, (...args: any[]) => void>) => {
|
||||
accountListMocks.pollCallbacks = callbacks;
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(/* 当前回调清理页面 DOM 和浏览器提示替身。 */ () => {
|
||||
cleanup();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
test('搜索只展示匹配账号并保留总数提示', /* 当前回调验证账号搜索过滤行为。 */ () => {
|
||||
render(<AccountList />);
|
||||
|
||||
expect(screen.getByTestId('account-card-account-1')).toBeTruthy();
|
||||
expect(screen.getByTestId('account-card-account-2')).toBeTruthy();
|
||||
fireEvent.change(screen.getByPlaceholderText('搜索昵称 / 备注 / 账号ID'), { target: { value: 'Alpha' } });
|
||||
|
||||
expect(screen.getByTestId('account-card-account-1')).toBeTruthy();
|
||||
expect(screen.queryByTestId('account-card-account-2')).toBeNull();
|
||||
expect(screen.getByText('当前显示 1 / 2 个账号')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('删除确认成功后调用 API 并移除目标账号', /* 当前回调验证删除确认和本地列表更新边界。 */ async () => {
|
||||
render(<AccountList />);
|
||||
fireEvent.click(screen.getAllByText('删除')[0]);
|
||||
expect(screen.getByTestId('delete-dialog')).toBeTruthy();
|
||||
|
||||
fireEvent.click(screen.getByText('确认删除'));
|
||||
await waitFor(/* deleteAssertion 等待删除请求完成。 */ () => expect(accountListMocks.deleteAccount).toHaveBeenCalledWith('account-1'));
|
||||
expect(accountListMocks.setAccounts).toHaveBeenCalledTimes(1);
|
||||
// nextAccounts 是页面删除逻辑提交给状态 Setter 的过滤函数结果。
|
||||
const nextAccounts = accountListMocks.setAccounts.mock.calls[0][0](accountListMocks.accounts);
|
||||
expect(nextAccounts).toEqual([accountListMocks.accounts[1]]);
|
||||
});
|
||||
|
||||
test('账号卡片操作分别触发状态切换和资料刷新', /* 当前回调验证账号卡片向页面协调器转发操作。 */ async () => {
|
||||
render(<AccountList />);
|
||||
fireEvent.click(screen.getAllByText('切换')[0]);
|
||||
await waitFor(/* toggleAssertion 等待账号状态更新请求完成。 */ () => expect(accountListMocks.updateAccountStatus).toHaveBeenCalledWith('account-1', false));
|
||||
expect(accountListMocks.loadAccounts).toHaveBeenCalled();
|
||||
|
||||
fireEvent.click(screen.getAllByText('刷新资料')[0]);
|
||||
await waitFor(/* refreshAssertion 等待资料刷新请求完成。 */ () => expect(accountListMocks.refreshAccountProfile).toHaveBeenCalledWith('account-1'));
|
||||
expect(accountListMocks.loadAccounts).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test('二维码授权启动轮询并在成功后保存会话结果', /* 当前回调验证二维码生成、轮询成功和延迟收束流程。 */ async () => {
|
||||
render(<AccountList />);
|
||||
fireEvent.click(screen.getByText('扫码添加新账号'));
|
||||
expect(await screen.findByTestId('qr-modal')).toBeTruthy();
|
||||
await waitFor(/* pollerAssertion 等待二维码轮询器启动。 */ () => expect(accountListMocks.pollerStart).toHaveBeenCalledWith('session-1', accountListMocks.checkQRLoginStatus, expect.anything()));
|
||||
expect(screen.getByText('waiting')).toBeTruthy();
|
||||
|
||||
await accountListMocks.pollCallbacks?.onSuccess();
|
||||
expect(accountListMocks.completeQRVerification).toHaveBeenCalledWith('session-1', undefined);
|
||||
await waitFor(/* successAssertion 等待二维码成功状态渲染。 */ () => expect(screen.getByText('success')).toBeTruthy());
|
||||
await waitFor(/* reloadAssertion 等待成功后的账号列表刷新。 */ () => expect(accountListMocks.loadAccounts).toHaveBeenCalled(), { timeout: 2_000 });
|
||||
});
|
||||
|
||||
test('关闭二维码弹窗会停止轮询并取消请求', /* 当前回调验证页面卸载前的二维码资源收束。 */ async () => {
|
||||
// view 是关闭二维码资源后用于卸载页面的渲染结果。
|
||||
const view = render(<AccountList />);
|
||||
fireEvent.click(screen.getByText('扫码添加新账号'));
|
||||
expect(await screen.findByTestId('qr-modal')).toBeTruthy();
|
||||
fireEvent.click(screen.getByText('关闭二维码'));
|
||||
|
||||
expect(accountListMocks.pollerStop).toHaveBeenCalled();
|
||||
expect(accountListMocks.requestCancel).toHaveBeenCalled();
|
||||
view.unmount();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user