最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked Apr 12, 2019 at 3:39 Josh PittmanJosh Pittman 7,3248 gold badges43 silver badges67 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

You need to pass the route in as well.

const { queryByText } = render(
    <Route path="/card/:cardId">
      <EditCard />
    </Route>,
    {
      route: '/card/1554990887217',
    }
  );
发布评论

评论列表(0)

  1. 暂无评论