reactjs - React Jest/React-testing-Library 路由更改无法正常工作
问题描述
我一直在尝试记录单击锚标记时的 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);
});
解决方案
我替换anchors
为href
to Link
s。这解决了这个问题。
推荐阅读
- php - 无法从其他选项卡访问 Cookie
- apache-kafka-streams - 检查 StateStore 是否已完全填充
- django - 为什么 __init__() 和 get_context_data() 中对象的属性不同?
- r - 将 dplyr 管道转换为导致“错误:结果的长度必须为 n,而不是 N”的函数。
- linux - TCP 窗口更新被忽略
- rest - 如何通过休息请求获取 Google OAuth 2.0 令牌
- c++ - 使用指向字符的指针并编写函数来分隔文本单词
- charts - 在 Google Charts 中,如何在 arrayToDataTable 中使用标题行,并且还可以为同一个图表提供选项?
- java - 在Java中从pair中获取值到HashMap
- python - 如何修复 ModuleNotFoundError :没有命名的模块