补齐 React 路由行为与分片预算门禁
This commit is contained in:
1 parent
010e77be91
commit
d3271e357a
3 files changed
+165
No files matched your search
@@ -0,0 +1,134 @@
|
||||
// @vitest-environment jsdom
|
||||
import React from 'react';
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { Item } from '../../types';
|
||||
import type { DeliveryRuleTarget } from './AuthenticatedShell';
|
||||
|
||||
vi.mock('../../components/Dashboard', /* dashboardMockFactory 提供仪表盘页面的轻量替身。 */ () => {
|
||||
// DashboardMock 渲染仪表盘页面标识,避免行为测试加载真实图表依赖。
|
||||
const DashboardMock: React.FC = () => <div data-testid="dashboard-page">仪表盘</div>;
|
||||
return { default: DashboardMock };
|
||||
});
|
||||
|
||||
vi.mock('../../components/Settings', /* settingsMockFactory 提供设置页面的轻量替身。 */ () => {
|
||||
// SettingsMock 渲染设置页面标识,验证管理员权限分支。
|
||||
const SettingsMock: React.FC = () => <div data-testid="settings-page">设置</div>;
|
||||
return { default: SettingsMock };
|
||||
});
|
||||
|
||||
vi.mock('../../components/ItemList', /* itemListMockFactory 提供商品页面的联动替身。 */ () => {
|
||||
// MockItemListProps 描述商品页面替身接收的规则配置回调。
|
||||
interface MockItemListProps {
|
||||
// onConfigureDelivery 负责通知父级打开商品规则配置。
|
||||
onConfigureDelivery: (item: Item) => void;
|
||||
}
|
||||
// ItemListMock 模拟用户点击商品规则配置入口。
|
||||
const ItemListMock: React.FC<MockItemListProps> = ({ onConfigureDelivery }) => {
|
||||
// itemFixture 是商品→规则联动测试使用的最小商品对象。
|
||||
const itemFixture = { cookie_id: 'cookie-1', item_id: 'item-1' } as Item;
|
||||
return <button data-testid="configure-delivery" onClick={/* configureAction 触发规则配置联动。 */ () => onConfigureDelivery(itemFixture)}>配置发货规则</button>;
|
||||
};
|
||||
return { default: ItemListMock };
|
||||
});
|
||||
|
||||
vi.mock('../../components/Rules', /* rulesMockFactory 提供规则页面的轻量替身。 */ () => {
|
||||
// MockRulesProps 描述规则页面替身接收的联动参数。
|
||||
interface MockRulesProps {
|
||||
// initialDeliveryTarget 保存商品页面传来的规则目标。
|
||||
initialDeliveryTarget?: DeliveryRuleTarget;
|
||||
// onDeliveryTargetHandled 表示规则页面已消费联动目标。
|
||||
onDeliveryTargetHandled?: () => void;
|
||||
}
|
||||
// RulesMock 展示规则目标,验证商品→规则页参数保持不变。
|
||||
const RulesMock: React.FC<MockRulesProps> = ({ initialDeliveryTarget, onDeliveryTargetHandled }) => (
|
||||
<div data-testid="rules-page">
|
||||
<span>{initialDeliveryTarget?.cookieId}:{initialDeliveryTarget?.itemId}</span>
|
||||
<button onClick={/* handledAction 确认规则页面已消费目标。 */ () => onDeliveryTargetHandled?.()}>完成</button>
|
||||
</div>
|
||||
);
|
||||
return { default: RulesMock };
|
||||
});
|
||||
|
||||
// loadAppContent 动态导入认证后页面组合器,等待懒加载模块完成解析。
|
||||
const loadAppContent = async () => import('./AuthenticatedShell');
|
||||
|
||||
describe('AuthenticatedShell 页面组合行为', /* 当前回调验证权限回退和跨页面联动边界。 */ () => {
|
||||
afterEach(/* 当前回调清理每个页面组合测试挂载的 DOM。 */ () => cleanup());
|
||||
|
||||
test('非管理员访问设置页时回退到仪表盘', /* 当前回调验证非管理员的设置页权限回退。 */ async () => {
|
||||
// appContentModule 表示动态加载的页面组合模块。
|
||||
const appContentModule = await loadAppContent();
|
||||
// AppContentComponent 表示动态模块导出的页面组合组件。
|
||||
const AppContentComponent = appContentModule.AppContent;
|
||||
render(
|
||||
<AppContentComponent
|
||||
activeTab="settings"
|
||||
isAdmin={false}
|
||||
onConfigureDelivery={/* configureTargetAction 接收商品规则目标。 */ () => undefined}
|
||||
onDeliveryTargetHandled={/* handledAction 表示规则目标已消费。 */ () => undefined}
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(/* dashboardAssertion 等待仪表盘懒加载完成。 */ () => expect(screen.getByTestId('dashboard-page')).toBeTruthy());
|
||||
expect(screen.queryByTestId('settings-page')).toBeNull();
|
||||
});
|
||||
|
||||
test('管理员访问设置页时加载设置页面', /* 当前回调验证管理员设置页面加载。 */ async () => {
|
||||
// appContentModule 表示动态加载的页面组合模块。
|
||||
const appContentModule = await loadAppContent();
|
||||
// AppContentComponent 表示动态模块导出的页面组合组件。
|
||||
const AppContentComponent = appContentModule.AppContent;
|
||||
render(
|
||||
<AppContentComponent
|
||||
activeTab="settings"
|
||||
isAdmin
|
||||
onConfigureDelivery={/* configureTargetAction 接收商品规则目标。 */ () => undefined}
|
||||
onDeliveryTargetHandled={/* handledAction 表示规则目标已消费。 */ () => undefined}
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(/* settingsAssertion 等待设置页面懒加载完成。 */ () => expect(screen.getByTestId('settings-page')).toBeTruthy());
|
||||
});
|
||||
|
||||
test('商品配置入口把目标传递到规则页面并支持消费确认', /* 当前回调验证商品到规则页面的联动参数和消费确认。 */ async () => {
|
||||
// appContentModule 表示动态加载的页面组合模块。
|
||||
const appContentModule = await loadAppContent();
|
||||
// AppContentComponent 表示动态模块导出的页面组合组件。
|
||||
const AppContentComponent = appContentModule.AppContent;
|
||||
// configureTarget 保存商品页面发出的规则目标。
|
||||
const configureTarget = vi.fn<(target: DeliveryRuleTarget) => void>();
|
||||
render(
|
||||
<AppContentComponent
|
||||
activeTab="items"
|
||||
isAdmin={false}
|
||||
onConfigureDelivery={configureTarget}
|
||||
onDeliveryTargetHandled={/* handledAction 表示规则目标已消费。 */ () => undefined}
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(/* itemAssertion 等待商品页面懒加载完成。 */ () => expect(screen.getByTestId('configure-delivery')).toBeTruthy());
|
||||
fireEvent.click(screen.getByTestId('configure-delivery'));
|
||||
expect(configureTarget).toHaveBeenCalledWith(expect.objectContaining({ cookieId: 'cookie-1', itemId: 'item-1' }));
|
||||
// target 是商品页面刚刚发出的完整规则配置目标。
|
||||
const target = configureTarget.mock.calls[0][0];
|
||||
expect(target.requestId).toEqual(expect.any(Number));
|
||||
|
||||
cleanup();
|
||||
// handledTarget 记录规则页面消费联动目标的回调。
|
||||
const handledTarget = vi.fn();
|
||||
render(
|
||||
<AppContentComponent
|
||||
activeTab="rules"
|
||||
isAdmin={false}
|
||||
deliveryRuleTarget={target}
|
||||
onConfigureDelivery={/* configureTargetAction 接收商品规则目标。 */ () => undefined}
|
||||
onDeliveryTargetHandled={handledTarget}
|
||||
/>,
|
||||
);
|
||||
await waitFor(/* rulesAssertion 等待规则页面懒加载完成。 */ () => expect(screen.getByTestId('rules-page')).toBeTruthy());
|
||||
expect(screen.getByText('cookie-1:item-1')).toBeTruthy();
|
||||
fireEvent.click(screen.getByText('完成'));
|
||||
expect(handledTarget).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,18 @@ import { describe, expect, test } from 'vitest';
|
||||
const staticRoot = resolve(__dirname, '../internal/webui/static');
|
||||
// indexHtml 是生产构建生成的入口 HTML。
|
||||
const indexHtml = readFileSync(resolve(staticRoot, 'index.html'), 'utf8');
|
||||
// PAGE_CHUNK_BUDGETS 定义每个业务页面动态分片允许的原始字节上限。
|
||||
const PAGE_CHUNK_BUDGETS: Record<string, number> = {
|
||||
Dashboard: 30 * 1024,
|
||||
AccountList: 70 * 1024,
|
||||
OrderList: 40 * 1024,
|
||||
CardList: 45 * 1024,
|
||||
ItemList: 65 * 1024,
|
||||
Settings: 30 * 1024,
|
||||
Rules: 65 * 1024,
|
||||
Notifications: 45 * 1024,
|
||||
Chat: 50 * 1024,
|
||||
};
|
||||
|
||||
describe('frontend production bundle boundary', /* 当前回调验证生产入口包体和页面分片。 */ () => {
|
||||
test('入口脚本保持轻量且业务页面不被预加载', /* 当前回调验证首屏入口不同步载入所有页面。 */ () => {
|
||||
@@ -26,4 +38,21 @@ describe('frontend production bundle boundary', /* 当前回调验证生产入
|
||||
const pageChunkNames = readdirSync(resolve(staticRoot, 'assets')).filter(/* 当前回调筛选业务页面分片文件。 */ fileName => /^(Dashboard|AccountList|OrderList|CardList|ItemList|Settings|Rules|Notifications|Chat)-.+\.js$/.test(fileName));
|
||||
expect(pageChunkNames).toHaveLength(9);
|
||||
});
|
||||
|
||||
test('每个业务页面 chunk 都保持在独立预算内', /* 当前回调验证单个页面不会重新膨胀首屏后的按需下载。 */ () => {
|
||||
// assetNames 保存当前生产构建输出的所有静态资源文件名。
|
||||
const assetNames = readdirSync(resolve(staticRoot, 'assets'));
|
||||
for (const budget /* budget 表示一个页面分片的预算条目。 */ of Object.entries(PAGE_CHUNK_BUDGETS)) {
|
||||
// pageName 表示预算对应的页面名称。
|
||||
const pageName = budget[0];
|
||||
// maxBytes 表示该页面分片允许的最大原始字节数。
|
||||
const maxBytes = budget[1];
|
||||
// pageChunkNames 保存该页面匹配到的生产分片文件。
|
||||
const pageChunkNames = assetNames.filter(/* 当前回调筛选当前页面的动态分片。 */ fileName => fileName.startsWith(`${pageName}-`) && fileName.endsWith('.js'));
|
||||
expect(pageChunkNames, `${pageName} 应只有一个动态分片`).toHaveLength(1);
|
||||
// actualBytes 保存当前页面分片的实际原始字节数。
|
||||
const actualBytes = statSync(resolve(staticRoot, 'assets', pageChunkNames[0])).size;
|
||||
expect(actualBytes, `${pageName} 分片超过 ${maxBytes} 字节预算`).toBeLessThanOrEqual(maxBytes);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user