我正在尝试模拟一个反应选择组件,以便在测试触发事件中使用它。下面是我使用 jest 创建的反应选择。
jest.mock("react-select", () => ({ options, onChange }) => {
function handleChange(event) {
const option = [];
option.push(options.find(option => option === event.currentTarget.value));
onChange(option);
console.log(option);............................................................//line 1
}
return (
<select
data-testid="test-select"
name="users"
label="users"
options={[
"a",
"b",
"c",
"d",
"e"
]}
onChange={handleChange}
>
{options.map(value => (
<option key={value} value={value}>
{value}
</option>
))}
</select>
);
});
问题是控制台在控制台中line 1
打印未定义。下面是我测试的火灾事件模型:
const chooseInput = getByTestId("test-select");
fireEvent.change(chooseInput, {
target: {
value: "b"
}
});
测试失败为:
expect(received).toBeInTheDocument()
received value must be an HTMLElement or an SVGElement.
Received has value: null
为什么在火灾事件发生时该选项没有更新?