Created
April 22, 2021 07:52
-
-
Save ndunk28/49f7dd3c59df74bbe47d1f7bf2a512b4 to your computer and use it in GitHub Desktop.
routerTestUtil.js
This file contains hidden or 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
import React from "react"; | |
import { Router } from "react-router-dom"; | |
import { createStore } from "redux"; | |
import { Provider } from "react-redux"; | |
import { render, wait } from "@testing-library/react"; | |
import { createMemoryHistory } from "history"; | |
function renderWithRouter(ui, { route = "/", ...renderOptions } = {}) { | |
const history = createMemoryHistory({ initialEntries: [route] }); | |
const utils = render(<Router history={history}>{ui}</Router>, renderOptions); | |
const finishLoading = () => | |
wait(() => expect(utils.queryByText("Loading")).toBeNull()); | |
return { | |
...utils, | |
finishLoading, | |
history | |
}; | |
} | |
function renderWithRedux( | |
ui, | |
{ initialState, store = createStore(reducer, initialState) } = {} | |
) { | |
return { | |
...render(<Provider store={store}>{ui}</Provider>), | |
// adding `store` to the returned utilities to allow us | |
// to reference it in our tests (just try to avoid using | |
// this to test implementation details). | |
store | |
}; | |
} | |
function renderWithRouterAndRedux( | |
ui, | |
{ | |
route = "/", | |
initialState, | |
store = createStore(reducer, initialState), | |
...renderOptions | |
} = {} | |
) { | |
const history = createMemoryHistory({ initialEntries: [route] }); | |
const utils = render( | |
<Provider store={store}> | |
<Router history={history}>{ui}</Router> | |
</Provider>, | |
renderOptions | |
); | |
const finishLoading = () => | |
wait(() => expect(utils.queryByText("Loading")).toBeNull()); | |
return { | |
...utils, | |
finishLoading, | |
history, | |
store | |
}; | |
} | |
export { renderWithRouter, renderWithRedux, renderWithRouterAndRedux }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment