test(web): 添加 renderWithProviders — MemoryRouter + AntD ConfigProvider 包裹器
This commit is contained in:
26
apps/web/src/test/utils/renderWithProviders.test.tsx
Normal file
26
apps/web/src/test/utils/renderWithProviders.test.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import { renderWithProviders } from './renderWithProviders';
|
||||
|
||||
function DummyPage() {
|
||||
return <div data-testid="dummy">Hello Test</div>;
|
||||
}
|
||||
|
||||
describe('renderWithProviders', () => {
|
||||
it('renders child component', () => {
|
||||
renderWithProviders(<DummyPage />);
|
||||
expect(screen.getByTestId('dummy')).toHaveTextContent('Hello Test');
|
||||
});
|
||||
|
||||
it('wraps with MemoryRouter — Link renders without error', () => {
|
||||
function PageWithLink() {
|
||||
return (
|
||||
<div>
|
||||
<a href="/test">Go</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
renderWithProviders(<PageWithLink />);
|
||||
expect(screen.getByText('Go')).toHaveAttribute('href', '/test');
|
||||
});
|
||||
});
|
||||
25
apps/web/src/test/utils/renderWithProviders.tsx
Normal file
25
apps/web/src/test/utils/renderWithProviders.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import { ReactElement } from 'react';
|
||||
import { render, RenderOptions } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { ConfigProvider } from 'antd';
|
||||
import zhCN from 'antd/locale/zh_CN';
|
||||
|
||||
interface CustomRenderOptions extends Omit<RenderOptions, 'wrapper'> {
|
||||
route?: string;
|
||||
}
|
||||
|
||||
export function renderWithProviders(ui: ReactElement, options: CustomRenderOptions = {}) {
|
||||
const { route = '/', ...renderOptions } = options;
|
||||
|
||||
function Wrapper({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<MemoryRouter initialEntries={[route]}>
|
||||
<ConfigProvider locale={zhCN}>{children}</ConfigProvider>
|
||||
</MemoryRouter>
|
||||
);
|
||||
}
|
||||
|
||||
return render(ui, { wrapper: Wrapper, ...renderOptions });
|
||||
}
|
||||
|
||||
export * from '@testing-library/react';
|
||||
Reference in New Issue
Block a user