mirror of
https://github.com/vernu/textbee.git
synced 2026-07-30 17:07:46 -04:00
Establish the testing safety net for the web dashboard before refactoring. - Vitest + React Testing Library + jsdom for unit/component tests - MSW node server mocks the API; unhandled requests throw so tests can never reach a real backend - Playwright e2e boots the real Next app but intercepts every /api/v1 call with shared fixtures, and mints a NextAuth session cookie to run authed - Shared fixtures in test/fixtures.ts back both layers - Seed tests: Button unit tests, MSW sanity checks, and a dashboard e2e happy path plus an auth-guard redirect Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import { ReactElement, ReactNode } from 'react'
|
|
import { render, RenderOptions } from '@testing-library/react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { SessionProvider } from 'next-auth/react'
|
|
import type { Session } from 'next-auth'
|
|
import { TEST_ACCESS_TOKEN, mockUser } from './fixtures'
|
|
|
|
export const mockSession: Session = {
|
|
user: {
|
|
id: mockUser.id,
|
|
name: mockUser.name,
|
|
email: mockUser.email,
|
|
phone: mockUser.phone,
|
|
role: mockUser.role,
|
|
avatar: mockUser.avatar ?? undefined,
|
|
accessToken: TEST_ACCESS_TOKEN,
|
|
},
|
|
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
|
|
}
|
|
|
|
// A fresh QueryClient per render with retries disabled so failing queries
|
|
// surface immediately instead of retrying during tests.
|
|
export function makeTestQueryClient() {
|
|
return new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false, gcTime: 0 },
|
|
mutations: { retry: false },
|
|
},
|
|
})
|
|
}
|
|
|
|
type ProvidersProps = {
|
|
children: ReactNode
|
|
session?: Session | null
|
|
queryClient?: QueryClient
|
|
}
|
|
|
|
export function TestProviders({
|
|
children,
|
|
session = mockSession,
|
|
queryClient,
|
|
}: ProvidersProps) {
|
|
const client = queryClient ?? makeTestQueryClient()
|
|
return (
|
|
<SessionProvider session={session}>
|
|
<QueryClientProvider client={client}>{children}</QueryClientProvider>
|
|
</SessionProvider>
|
|
)
|
|
}
|
|
|
|
type CustomRenderOptions = Omit<RenderOptions, 'wrapper'> & {
|
|
session?: Session | null
|
|
queryClient?: QueryClient
|
|
}
|
|
|
|
// Custom render that wraps a UI in the app's providers. Use this instead of
|
|
// RTL's render for any component that reads the session or react-query.
|
|
export function renderWithProviders(
|
|
ui: ReactElement,
|
|
{ session, queryClient, ...options }: CustomRenderOptions = {}
|
|
) {
|
|
return render(ui, {
|
|
wrapper: ({ children }) => (
|
|
<TestProviders session={session} queryClient={queryClient}>
|
|
{children}
|
|
</TestProviders>
|
|
),
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export * from '@testing-library/react'
|
|
export { default as userEvent } from '@testing-library/user-event'
|