【发布时间】:2023-03-12 00:38:01
【问题描述】:
我有一个正常的选择列表。当我选择一个选项时,我需要测试 handleChoice 是否被调用。如何使用 React 测试库做到这一点?
<select
onChange={handleChoice}
data-testid="select"
>
<option value="default">Make your choice</option>
{attributes.map(item => {
return (
<option key={item.key} value={item.key}>
{item.label}
</option>
);
})}
</select>
getByDisplayValue 的值为item.label 不返回任何内容,可能是因为它在页面上不可见?
【问题讨论】:
-
你试过
fireEvent.change(getByTestId("select"), { target: { value: '<item label>' } }); -
好像不像getByTestId("select"),报错:TypeError: container.querySelectorAll is not a function