javascript - Jest/react-testing-library:单击按钮未更新测试中的值
问题描述
最初的问题
我有一个简单的增量/减量组件。
它显示一个数字计数和 2 个按 1 递增或递减的按钮。
还有一个输入;如果在输入中提供了数值,则按钮使用输入值而不是 1 来增加/减少。
我有这个测试:
it(`should increment by input value, if value > 0`, () => {
const { input } = setup();
// Change input value to 4
fireEvent.change(input, {target: { value: 4}});
// Find and click +n button
const btn = screen.getByText(`+n`);
fireEvent.click(btn);
// Find and check updated count
const updatedCount = parseInt(screen.getByTestId(`count`).textContent, 10)
expect(updatedCount).toBe(4);
})
问题:此处更新的计数返回 1(默认为useState
),我预计为 4。
expect(parseInt(input.value, 10)).toBe(4)
通过并且输入onChange
已连接。
问题:为什么没有使用更新的输入值?
附加信息:虽然我不确定,但我认为它可能没有更新useState
on 更改,所以我还添加了一个键盘事件以在我更改其值后点击输入上的 enter 键。我希望进一步模拟用户并更新状态,但我没有运气。
非常感谢任何帮助!为任何不合适的地方道歉,我最近几天才看 Jest/RTL。
回复信息
测试组件:
import React, { useState } from "react";
const MyTest = () => {
const [count, setCount] = useState(0);
const [value, setValue] = useState(1);
const incCount = (n) => {
if (n !== 0) {
count + n <= 10 && setCount((currCount) => currCount + n);
return;
}
if (count < 10) setCount((currCount) => currCount + 1);
};
const decCount = (n) => {
if (n !== 0) {
count - n >= 0 && setCount((currCount) => currCount - n);
return;
}
if (count > 0) {
setCount((currCount) => currCount - 1);
}
};
return (
<>
<div>value: {value}</div>
<div data-testid="count">{count}</div>
<br />
<label htmlFor="inp">N Input</label>
<br />
<input
type="texts"
placeholder="inc or dec by num"
id="inp"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
<br />
<button type="button" onClick={() => decCount(parseInt(value))}>
-n
</button>
<button type="button" onClick={() => incCount(parseInt(value))}>
+n
</button>
</>
);
};
export default MyTest;
测试:
import React from 'react';
import {screen, fireEvent, render, cleanup, waitFor} from '@testing-library/react';
import "@testing-library/jest-dom/extend-expect";
import MyTest from './myTest';
describe(`Increment and decrement the count when relevant button is pressed`, () => {
// Get component
const setup = () => {
const utils = render(<MyTest />)
const initCount = parseInt(screen.getByTestId(`count`).textContent, 10)
const input = screen.getByLabelText(`N Input`);
return {
initCount,
input,
...utils
}
};
// ...other tests here
it(`should increment by input value, if value > 0`, () => {
const { input } = setup();
// Change input value to 4
fireEvent.change(input, {target: { value: 4}});
// Find and click +n button
const btn = screen.getByText(`+n`);
fireEvent.click(btn);
// Find and check updated count
const updatedCount = parseInt(screen.getByTestId(`count`).textContent, 10)
expect(updatedCount).toBe(4);
})
})
解决方案
您的类型属性中有错字。改变这个
<input type="texts" {...} />
对此
<input type="text" {...} />
并且测试将再次起作用。我的猜测是,通过指定<input>
无效type
属性,库不知道您是哪个角色input
,因此它无法onchange
正确处理事件。
我不确定为什么要更新到新版本,react-scripts
并且@testing-library/react
即使您将错字留在那里,也会解决问题。
推荐阅读
- android - 为 arm/arm64 构建 android sdk 工具(aapt、adb 等)
- sql - 按表 b 日期连接和分组表 a 行
- c - 这个函数是可重入的吗?
- c# - 如何使用 C# 中的复选框将文本框的内容更改为任何一个特殊字符?
- apache-kafka - 如何从微服务调用使用 Kafka 事务生产者
- javascript - 让画布透明
- leaflet - 传单自定义图标标记旋转一个角度,变换样式冲突
- angular - 如何在 Angular 2+ 项目中调用 circleCI 环境变量?
- android - PageView.builder 不断重新缓存静态图像
- macos - OSX上“stat”输出的每个字段的含义是什么?