jestjs - 如何使用 Jest 从另一个元素的输入测试一个元素的输出?
问题描述
我的测试试图模拟用户在文本区域中输入“abcdef”作为输入,然后验证输出中是否写入了相同的字符串(“abcdef”):
it("should write in input and correspond in output", async () => {
render(App);
const input = document.getElementById("input");
fireEvent.change(input, { target: { value: 'abcdef' }})
let output = document.getElementById("output");
expect(output).toHaveTextContent("abcdef"); // undefined
});
这是输出元素:
<h1 id="output">{output}</h1>
如果我在 {output} 标记旁边硬编码“abcdef”,那么我的测试通过,但fireEvent.change似乎无法正常工作。也许我误解了 Jest 的功能 - 也许它只进行单元测试,而不是用户测试/端到端测试?
解决方案
fireEvent.change(input, { target: { value: 'abcdef' }})
应该有一个await
:
await fireEvent.change(input, { target: { value: 'abcdef' }})
它有效!
it("should write in input and correspond in output", async () => {
render(App);
const input = document.getElementById("input");
fireEvent.change(input, { target: { value: 'abcdef' }})
let output = document.getElementById("output");
expect(output).toHaveTextContent("abcdef");
});
结果:✓ 应写入输入并对应输出(3 ms)
推荐阅读
- android - 如何在 xml 中逻辑地构造 Android 视图
- r - 绘制因子(分类数据)的相关矩阵的等价物?和混合类型?
- git - 用于 dist/production 的 Git 分支
- mongodb - 我正在研究 MongoDB 模式设计。有一个混乱
- java - 使用三个堆栈的回文(Java)
- json - Terraform 语法和格式有什么区别?
- java - 在 Spock 中将 Lambda 函数定义为参数约束
- javascript - 创建 HTML 边栏时使用存储的属性
- jquery - Jquery - 模糊和keyup多次触发
- android - 如何将字符串数组返回到java JNI