Skip to content

userEvent

一句话解释

userEvent simulates 真实用户 interactions (点击, 类型, etc.) 更多 realistically than fireEvent, 使用 proper 事件序列.

用大白话说

userEvent 是部分的 测试库. 它 simulates 用户 interactions 如点击, 输入, and hovering — 但更多 realistically than older fireEvent utility.

The difference: 当真实用户类型 'hello' in 文本 box, 它's 不 one 事件 — 它's 序列: focus, keydown 'h', keyup 'h', 输入 'h', keydown 'e', keyup 'e', 输入 'e', and 所以 on. fireEvent.更改 fires 只是 one 更改事件. userEvent.类型 fires 完整 realistic 序列.

This matters 因为一些组件 respond 到特定事件 (如 keydown for shortcuts). Using userEvent ensures 你的测试触发相同事件真实用户会, 制作它们更多 realistic and 更少 likely 到 miss bugs.

Always prefer userEvent over fireEvent 当可能.

示例

javascript
// Prefer userEvent (realistic)
await userEvent.type(input, 'hello');
await userEvent.click(button);

// Over fireEvent (simplified)
fireEvent.change(input, { target: { value: 'hello' } });
fireEvent.click(button);

userEvent fires 完整事件序列; fireEvent fires 单个 simplified 事件.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表