Skip to content

Instantly share code, notes, and snippets.

@jozsefDevs
Last active March 6, 2022 07:18
Show Gist options
  • Save jozsefDevs/5f838481696057b34442727725504411 to your computer and use it in GitHub Desktop.
Save jozsefDevs/5f838481696057b34442727725504411 to your computer and use it in GitHub Desktop.
[TypeScript] Set up React Testing Library with Redux Toolkit + Routing
// ---------------------------------
// USING
// ---------------------------------
// "@reduxjs/toolkit": "1.5.0",
// "@testing-library/react": "11.2.5",
// "typescript": "3.7.2"
// "react": "16.13.1"
// "react-redux": "7.2.0",
// "react-router-dom": "5.2.0",
import { configureStore } from '@reduxjs/toolkit';
import { render as rtlRender } from '@testing-library/react';
import React from 'react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import rootReducer from 'state/rootReducer';
interface WrapperProps {
children?: React.ReactNode;
}
function render(ui: any, { route = '/', initialState = {} } = {}) {
window.history.pushState({}, 'Test page', route);
const store = configureStore({ reducer: rootReducer, preloadedState: initialState });
const Wrapper = ({ children }: WrapperProps) => {
return (
<Provider store={store}>
<BrowserRouter>{children}</BrowserRouter>
</Provider>
);
};
return rtlRender(ui, { wrapper: Wrapper });
}
// re-export everything
export * from '@testing-library/react';
// override render method
export { render };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment