5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。
顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。
验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import '@testing-library/jest-dom';
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
import { MemoryRouter, Routes, Route } from 'react-router-dom';
|
|
import { vi, describe, it, expect } from 'vitest';
|
|
import { NewFeatureShell } from './pages/NewFeatureShell';
|
|
import { AiFeatureForm } from './pages/AiFeatureForm';
|
|
|
|
vi.mock('./auth/token', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('./auth/token')>()),
|
|
getProfile: () => ({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] }),
|
|
}));
|
|
vi.mock('./api/meta', () => ({ listEntities: vi.fn().mockResolvedValue([]), createEntity: vi.fn() }));
|
|
vi.mock('./api/recipe', () => ({
|
|
listRecipes: () => [{ id: 'maintenance-crud-v1', label: '维护型 CRUD' }],
|
|
compileRecipe: vi.fn(),
|
|
}));
|
|
vi.mock('./api/publish', () => ({ publishFeature: vi.fn() }));
|
|
vi.mock('./api/menu', () => ({ fetchMenu: vi.fn().mockResolvedValue({ roots: [] }), collectLeaves: vi.fn() }));
|
|
vi.mock('./api/ai', async (io) => {
|
|
const actual = await io<typeof import('./api/ai')>();
|
|
return { ...actual, autoDevelop: vi.fn() };
|
|
});
|
|
|
|
describe('新路由可达', () => {
|
|
it('/new-feature 渲染 NewFeatureShell', async () => {
|
|
render(
|
|
<MemoryRouter initialEntries={['/new-feature']}>
|
|
<Routes>
|
|
<Route path="/new-feature" element={<NewFeatureShell />} />
|
|
<Route path="/ai-feature" element={<AiFeatureForm />} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
await waitFor(() => expect(screen.getByTestId('new-feature-shell')).toBeInTheDocument());
|
|
});
|
|
|
|
it('/ai-feature 渲染 AiFeatureForm', () => {
|
|
render(
|
|
<MemoryRouter initialEntries={['/ai-feature']}>
|
|
<Routes>
|
|
<Route path="/new-feature" element={<NewFeatureShell />} />
|
|
<Route path="/ai-feature" element={<AiFeatureForm />} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
expect(screen.getByTestId('ai-feature-form')).toBeInTheDocument();
|
|
});
|
|
});
|