Files
eip4-web/src/pages/DesignCenterPage.entrances.test.tsx
zhengwr 768ffa8551 refactor(web): isAdmin 收口到 isAdminProfile(B4)
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>
2026-06-24 11:20:16 +08:00

49 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import '@testing-library/jest-dom';
import { render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { DesignCenterPage } from './DesignCenterPage';
// menu 走真实 http → mock 掉,避免网络
vi.mock('../api/menu', () => ({
fetchMenu: vi.fn().mockResolvedValue({ roots: [] }),
collectLeaves: vi.fn().mockReturnValue([]),
}));
// 受控 profile:默认 admin
const profileMock = vi.fn();
vi.mock('../auth/token', async (importOriginal) => ({
...(await importOriginal<typeof import('../auth/token')>()),
getProfile: () => profileMock(),
}));
function renderPage() {
return render(
<MemoryRouter>
<DesignCenterPage />
</MemoryRouter>,
);
}
describe('DesignCenterPage 入口按钮', () => {
beforeEach(() => {
profileMock.mockReset();
});
it('admin 看到 [+ 新建功能] 与 [⚡ AI 建功能]', async () => {
profileMock.mockReturnValue({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] });
renderPage();
await waitFor(() => {
expect(screen.getByTestId('dc-new-feature')).toBeInTheDocument();
expect(screen.getByTestId('dc-ai-feature')).toBeInTheDocument();
});
});
it('非 admin 不渲染入口(整页降级)', () => {
profileMock.mockReturnValue({ userId: 'U1', account: 'zhangsan', name: 'z', roles: [] });
renderPage();
expect(screen.queryByTestId('dc-new-feature')).not.toBeInTheDocument();
expect(screen.queryByTestId('dc-ai-feature')).not.toBeInTheDocument();
});
});