【发布时间】:2016-12-28 03:32:39
【问题描述】:
考虑一下 React 组件中的以下输入元素:
<input onChange={() => console.log('onChange')} ... />
在测试 React 组件时,我正在模拟用户更改输入值:
input.value = newValue;
TestUtils.Simulate.change(input);
这会按预期导致'onChange' 被记录。
但是,当'change' 事件被直接调度时(我使用的是jsdom):
input.value = newValue;
input.dispatchEvent(new Event('change'));
onChange 处理程序未被调用。
为什么?
我使用dispatchEvent 而不是TestUtils.Simulate 的动机是因为TestUtils.Simulate 不支持事件冒泡,而我的组件的行为依赖于此。我想知道是否有办法在没有TestUtils.Simulate 的情况下测试事件?
【问题讨论】: