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                                  76x           76x                                     76x             76x        
// 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';
 
// ============================================================================
// Store Setup
// ============================================================================
 
export type AppStore = EnhancedStore;
 
export function setupStore(): AppStore {
	return configureStore({
		reducer: {
			[api.reducerPath]: api.reducer,
			auth: authReducer
		},
		middleware: (getDefaultMiddleware) =>
			getDefaultMiddleware({
				serializableCheck: false
			}).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})
	};
}