Last active
March 6, 2022 07:18
-
-
Save jozsefDevs/5f838481696057b34442727725504411 to your computer and use it in GitHub Desktop.
[TypeScript] Set up React Testing Library with Redux Toolkit + Routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------- | |
// 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