Skip to content

Instantly share code, notes, and snippets.

@ndunk28
Created April 22, 2021 07:52
Show Gist options
  • Save ndunk28/49f7dd3c59df74bbe47d1f7bf2a512b4 to your computer and use it in GitHub Desktop.
Save ndunk28/49f7dd3c59df74bbe47d1f7bf2a512b4 to your computer and use it in GitHub Desktop.
routerTestUtil.js
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