1

I have been trying to record URL changes on click of anchor tag. While this works on the actual page, it doesn't when I write tests in Jest.

Here's the code:

import { renderApp } from "<src>/tests/util";
import { fireEvent, wait } from "react-testing-library";

import {
  getLocationAPath,
  getLocationBPath,
  getLocationCPath
} from "<src>/paths";

let getByDataQa;
let history;

beforeEach(() => {
  ({ getByDataQa, history } = renderApp(getLocationAPath(), {}));
});

test("Validate if routes work", async () => {
  let routePath;
  fireEvent.click(getByDataQa("Container.locB"));
  //await wait();
  routePath = getLocationBPath();
  expect(history.location.pathname).toEqual(routePath);

  fireEvent.click(getByDataQa("Container.locC"));
  routePath = getLocationCPath();
  console.log(history.location.pathname);
  expect(history.location.pathname).toEqual(routePath);

});

2
  • Can you include the code of renderApp? Commented Feb 15, 2019 at 9:43
  • <li> <a href={getLocationBPath()} className={classNames(styles.bar)} data-qa="foo" title="xyz" /> </li> Commented Feb 16, 2019 at 11:01

1 Answer 1

1

I replaced anchors with href to Links. That solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.