Files
eip4-web/src/pages/DesignCenterPage.entrances.test.tsx
2026-06-20 09:51:26 +08:00

48 lines
1.5 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', () => ({
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();
});
});