All files / test test-utils.tsx

100% Statements 4/4
100% Branches 2/2
100% Functions 4/4
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                170x           170x                                         170x             170x        
// src/test/test-utils.tsx
import {type ReactElement, type ReactNode} from 'react';
import {render, type RenderOptions} from '@testing-library/react';
import {Provider} from 'react-redux';
import {BrowserRouter} from 'react-router-dom';
import {configureStore, type EnhancedStore} from '@reduxjs/toolkit';
 
import {api} from '../services/api.ts';
import authReducer from '../features/auth/authSlice';
import {listenerMiddleware} from '../store/listenerMiddleware';
 
// ============================================================================
// Store Setup
// ============================================================================
 
export type AppStore = EnhancedStore;
 
/**
 * Mirrors the real store's middleware chain (see store.tsx), including the
 * listener middleware. Without it, behaviour that only shows up once the
 * listeners run — such as the API-cache reset on logout — is invisible to
 * tests, which is how FSC-162 shipped with a passing LoginPage suite.
 */
export function setupStore(): AppStore {
	return configureStore({
		reducer: {
			[api.reducerPath]: api.reducer,
			auth: authReducer
		},
		middleware: (getDefaultMiddleware) =>
			getDefaultMiddleware({
				serializableCheck: false
			})
				.prepend(listenerMiddleware.middleware)
				.concat(api.middleware)
	});
}
 
// ============================================================================
// Test Render
// ============================================================================
 
interface ExtendedRenderOptions extends Omit<RenderOptions, 'queries'> {
	store?: AppStore;
}
 
export function renderWithProviders(
	ui: ReactElement,
	{store = setupStore(), ...renderOptions}: ExtendedRenderOptions = {}
) {
	function Wrapper({children}: { children: ReactNode }): ReactElement {
		return (
			<Provider store={store}>
				<BrowserRouter>{children}</BrowserRouter>
			</Provider>
		);
	}
 
	return {
		store,
		...render(ui, {wrapper: Wrapper, ...renderOptions})
	};
}