react-select-event 不会触发 onChange 函数在测试期间设置值
react-select-event doesn't trigger the onChange function to set the value during testing
这几天我一直在努力编写一个测试,我认为这是一个寻求一些提示的好地方。我正在尝试填写一个使用 react-select
下拉列表的表单,这些表单位于呈现其中 4 个的地图内。但是,当尝试 运行 下面的代码在每个表单的 select 或循环中时,在第一次迭代后 运行 很好,在表单中找到的输入元素将执行onChange 没有 onChange 处理程序,也不会触发 select 我需要的选项的更改功能。根据文档 here,我用于 selection 的软件包是 react-select-event
。我仍然是 react-testing-library
的初学者,并且总体上正在测试,所以我很可能错过了一些东西。欢迎任何提示或想法!谢谢
const selectOptionAndAssertValue = async (indicator, field, element, option, inputForm) => {
await selectEvent.select(element, option.label, {
container: document.body,
});
await waitFor(
async () => {
expect(inputForm).toHaveFormValues({
[${indicator.short_alias}-${field}]: option.value,
});
},
{
onTimeout: error => console.log(error),
}
);
};
注意:为了查找 inputForm,我使用的是 findByLabelText,它确实找到了元素。
编辑:此回购协议中重复的问题https://github.com/ildaballiu/select-testing-demo
问题已通过将容器更改为
解决
container: () => document.body.querySelector('[class$=-menu]')
这几天我一直在努力编写一个测试,我认为这是一个寻求一些提示的好地方。我正在尝试填写一个使用 react-select
下拉列表的表单,这些表单位于呈现其中 4 个的地图内。但是,当尝试 运行 下面的代码在每个表单的 select 或循环中时,在第一次迭代后 运行 很好,在表单中找到的输入元素将执行onChange 没有 onChange 处理程序,也不会触发 select 我需要的选项的更改功能。根据文档 here,我用于 selection 的软件包是 react-select-event
。我仍然是 react-testing-library
的初学者,并且总体上正在测试,所以我很可能错过了一些东西。欢迎任何提示或想法!谢谢
const selectOptionAndAssertValue = async (indicator, field, element, option, inputForm) => {
await selectEvent.select(element, option.label, {
container: document.body,
});
await waitFor(
async () => {
expect(inputForm).toHaveFormValues({
[${indicator.short_alias}-${field}]: option.value,
});
},
{
onTimeout: error => console.log(error),
}
);
};
注意:为了查找 inputForm,我使用的是 findByLabelText,它确实找到了元素。
编辑:此回购协议中重复的问题https://github.com/ildaballiu/select-testing-demo
问题已通过将容器更改为
解决 container: () => document.body.querySelector('[class$=-menu]')