【问题标题】:React Jest/React-testing-Library route change not working properlyReact Jest/React-testing-Library 路由更改无法正常工作
【发布时间】:2019-07-08 06:00:24
【问题描述】:

我一直在尝试记录单击锚标记时的 URL 更改。虽然这在实际页面上有效,但在我用 Jest 编写测试时却不行。

代码如下:

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);

});

【问题讨论】:

  • 能把renderApp的代码加进去吗?
  • 跨度>

标签: reactjs jestjs react-testing-library


【解决方案1】:

我将anchors 替换为hrefLinks。这样就解决了问题。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签