React Jest/React-testing-Library 路由更改无法正常工作
React Jest/React-testing-Library route change not working properly
我一直在尝试记录单击锚标记时的 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);
});
我用 href
替换了 anchors
到 Link
s。那解决了问题。
我一直在尝试记录单击锚标记时的 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);
});
我用 href
替换了 anchors
到 Link
s。那解决了问题。