jeudi 30 juillet 2020

Whats the best way to write test for testing Routes?

I wanted to write a test for main.tsx. Below is the code for the same. I haven't written any test for routes before. How do I write test for these routes?

const init = async () => {
  const history = createBrowserHistory({ basename: `${HISTORY_BASENAME}` });

  // Catch link clicks and push them to the history object.
  handleNavigation(history);
  const renderCreateOldSliModal = () => <OldSliCreateModal />;
  const renderCreateSRModal = (forceProblemType?: SupportType) => (
    <CreateSRModal forceProblemType={forceProblemType} interPluginContext= 
      {pluginListener.getContext()} />
  );
  
  ReactDOM.render(
    <Configuration value=>
    <Provider store={store}>
        <ConnectedRouter history={history}>
            <NavMenuContextProvider navigation={navTree}>
              <Switch>
                <Route
                  path={`/${ResourceNames.serviceRequest}/${ResourceNames.create}`}
                  render={renderCreateOldSliModal}
                />
                <Route
                  path={`/${ResourceNames.create}`}
                  render={() => renderCreateSRModal()}
                  exact={true}
                />
            </Switch>
         </NavMenuContextProvider>
       </ConnectedRouter>
    </Provider>
    </Configuration>,
    document.getElementById(CONTAINER_ID)
};
init();

I wrote a test like below, but I get an error

TypeError: Cannot read property 'createElement' of undefined

Below is my test

it("check routes", () => {
    const history = createBrowserHistory();
    const { container } = render(
      <ConnectedRouter history={history}>
        <NavMenuContextProvider navigation={undefined}>
          <Switch>
            <Route history={history}>
              path="/support/create"
            </Route>
          </Switch>
        </NavMenuContextProvider>
      </ConnectedRouter>,
    );
    expect(container.innerHTML).toMatch("Open a Support Request");
  });

Aucun commentaire:

Enregistrer un commentaire