Files
crypto_trader/frontend/src/test/utils.tsx

81 lines
1.9 KiB
TypeScript

import { ReactElement } from 'react'
import { render, RenderOptions } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { BrowserRouter } from 'react-router-dom'
import { vi } from 'vitest'
/**
* Creates a fresh QueryClient for testing with retry disabled
*/
export function createTestQueryClient() {
return new QueryClient({
defaultOptions: {
queries: {
retry: false,
gcTime: 0,
},
mutations: {
retry: false,
},
},
})
}
interface WrapperProps {
children: React.ReactNode
}
/**
* Custom render function that wraps components with necessary providers
*/
export function renderWithProviders(
ui: ReactElement,
options?: Omit<RenderOptions, 'wrapper'> & {
queryClient?: QueryClient
route?: string
}
) {
const { queryClient = createTestQueryClient(), route = '/', ...renderOptions } = options || {}
// Set the route
window.history.pushState({}, 'Test page', route)
function Wrapper({ children }: WrapperProps) {
return (
<QueryClientProvider client={queryClient}>
<BrowserRouter>{children}</BrowserRouter>
</QueryClientProvider>
)
}
return {
...render(ui, { wrapper: Wrapper, ...renderOptions }),
queryClient,
}
}
/**
* Mock snackbar context for testing
*/
export const mockSnackbar = {
showError: vi.fn(),
showSuccess: vi.fn(),
showWarning: vi.fn(),
showInfo: vi.fn(),
}
/**
* Mock WebSocket context for testing
*/
export const mockWebSocketContext = {
isConnected: true,
lastMessage: null,
messageHistory: [],
sendMessage: vi.fn(),
subscribe: vi.fn(),
}
// Re-export everything from testing-library
export * from '@testing-library/react'
export { default as userEvent } from '@testing-library/user-event'