Commit fd4d42edaca7bd3b19358730acc01b011c6b7ff5
1 parent
490a6b1b
test(fe-shell): 外壳/路由测试共享渲染工具 REQ-USR-003
Showing
2 changed files
with
63 additions
and
0 deletions
frontend/tests/unit/renderShell.smoke.test.tsx
0 → 100644
| 1 | +// REQ-USR-003: renderShell 共享渲染工具自带冒烟用例(T0) | |
| 2 | +import { describe, it, expect } from 'vitest'; | |
| 3 | +import { screen } from '@testing-library/react'; | |
| 4 | +import { renderShell } from './renderShell'; | |
| 5 | + | |
| 6 | +describe('renderShell', () => { | |
| 7 | + it('renderShell mounts a route element', () => { | |
| 8 | + renderShell(<div>shell-ok</div>, { | |
| 9 | + initialEntries: ['/'], | |
| 10 | + preloadedAuth: { | |
| 11 | + token: 't', | |
| 12 | + user: { id: 1, sUserName: '朱子纯', sUserType: '超级管理员', sLanguage: '中文' }, | |
| 13 | + }, | |
| 14 | + }); | |
| 15 | + expect(screen.getByText('shell-ok')).toBeInTheDocument(); | |
| 16 | + }); | |
| 17 | +}); | ... | ... |
frontend/tests/unit/renderShell.tsx
0 → 100644
| 1 | +// REQ-USR-003: 外壳 / 路由测试共享渲染工具(Provider + 真实 store + MemoryRouter + AntD App) | |
| 2 | +// 复用 FE-01 renderLogin.tsx 模式,扩展为可注入 auth 预置态(token/user)。 | |
| 3 | +import type { ReactElement } from 'react'; | |
| 4 | +import { render } from '@testing-library/react'; | |
| 5 | +import { Provider } from 'react-redux'; | |
| 6 | +import { MemoryRouter } from 'react-router-dom'; | |
| 7 | +import { App as AntdApp, ConfigProvider } from 'antd'; | |
| 8 | +import { configureStore } from '@reduxjs/toolkit'; | |
| 9 | +import authReducer, { type AuthState } from '../../src/store/slices/authSlice'; | |
| 10 | +import type { RootState } from '../../src/store/store'; | |
| 11 | + | |
| 12 | +/** 构建带 auth slice 的测试 store,可注入 token/user 预置态 */ | |
| 13 | +export function makeShellStore(preloadedAuth?: Partial<AuthState>) { | |
| 14 | + return configureStore({ | |
| 15 | + reducer: { auth: authReducer }, | |
| 16 | + preloadedState: preloadedAuth | |
| 17 | + ? { auth: { token: null, user: null, ...preloadedAuth } } | |
| 18 | + : undefined, | |
| 19 | + }); | |
| 20 | +} | |
| 21 | + | |
| 22 | +export interface RenderShellOptions { | |
| 23 | + initialEntries?: string[]; | |
| 24 | + preloadedAuth?: Partial<AuthState>; | |
| 25 | + store?: ReturnType<typeof makeShellStore>; | |
| 26 | +} | |
| 27 | + | |
| 28 | +export function renderShell(ui: ReactElement, options?: RenderShellOptions) { | |
| 29 | + const store = options?.store ?? makeShellStore(options?.preloadedAuth); | |
| 30 | + const result = render( | |
| 31 | + <Provider store={store}> | |
| 32 | + <ConfigProvider> | |
| 33 | + <AntdApp> | |
| 34 | + <MemoryRouter initialEntries={options?.initialEntries ?? ['/']}> | |
| 35 | + {ui} | |
| 36 | + </MemoryRouter> | |
| 37 | + </AntdApp> | |
| 38 | + </ConfigProvider> | |
| 39 | + </Provider>, | |
| 40 | + ); | |
| 41 | + return { | |
| 42 | + ...result, | |
| 43 | + store, | |
| 44 | + getState: () => store.getState() as RootState, | |
| 45 | + }; | |
| 46 | +} | ... | ... |