Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I test for a react router route param in react-testing-library?

I am using redux and react router.

I want to test that a route param shows up on the page.

test('route param is displayed', async () => {
  const { queryByText } = customRender(
    <EditCard />,
    {
      route: '/card/1554990887217',
    }
  );
const cardId = queryByText(/1554990887217/);
await waitForElement(() => cardId);
})

my custom render wraps router and redux like so:

export const customRender = (
  ui,
  {
    route = '/',
    history = createMemoryHistory({ initialEntries: [route] }),
    initialState,
    store = createStore(rootReducer, initialState),
    ...options
  } = {}
) => ({
  ...rtl(
    <Provider store={store}>
      <Router history={history}>{ui}</Router>
    </Provider>,
    options
  ),
  history,
});

The route param just doesn't show up in the test. It just errors with a time out.

It works on the page though, as in everything works as expected if I start the app up and test it manually.

like image 246
Josh Pittman Avatar asked Feb 04 '23 19:02

Josh Pittman


1 Answers

You need to pass the route in as well.

const { queryByText } = render(
    <Route path="/card/:cardId">
      <EditCard />
    </Route>,
    {
      route: '/card/1554990887217',
    }
  );
like image 52
Josh Pittman Avatar answered Feb 06 '23 07:02

Josh Pittman