为什么 @testing-library/user-event 不能与输入元素交互?

发布于 2025-01-19 09:20:27 字数 535 浏览 1 评论 0原文

我做了一个小仓库来演示我的问题: https://github.com/ msawatzky75/user-event-input-test

我在这里有一些不同的场景:

  1. 复选框的html 按钮
  2. 带有点击处理程序
  3. div 以及带有数据绑定的数据绑定,该
  4. 输入会在每次输入

每个触发器 时触发由 msw 处理的获取请求。我还设置了 2 个不同的 DOM 环境来运行这些测试:jsdomhappy-dom

只有与输入元素(复选框和文本输入)交互的测试会失败,并且结果在 DOM 环境中保持一致。

这里有什么问题呢?这是 @testing-library/user-event 的错误吗?如果没有,如何解决这个问题?

i've made a small repo to demontstrate my issue: https://github.com/msawatzky75/user-event-input-test

i have a few different scenarios here:

  1. html button
  2. div with click handler
  3. checkbox with data-binding that is watched
  4. input that triggers every time its typed into

each of these triggers a fetch request that is handled by msw. i have also setup 2 different DOM environments to run these tests in: jsdom and happy-dom.

only the tests that interact with input elements (checkbox and the text input) fail and the results are consistant across DOM environments.

What is the issue here? is it a bug with @testing-library/user-event? if not, how could one go about fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与之呼应 2025-01-26 09:20:27

这里的问题是如何渲染组件。更具体地说,如何将其连接到文档主体上。

the original render is as follows:

const root = document.createElement("div");
const { queryByText, getByTestId, debug } = render(BaseApp, {
    container: root,
    attachTo: document.body,
});

the attachTo property seems to misbehave, but is resovled by doing this instead:

const root = document.createElement("div");
document.body.appendChild(root);
const { queryByText, getByTestId } = render(BaseApp, {
    container: root,
});

this brings the number of passing tests in this example from 4/8 to 7/8, with only Happy-Dom复选框测试失败。

The issue here is how the component is rendered. More specifically, how it is attached to the document body.

the original render is as follows:

const root = document.createElement("div");
const { queryByText, getByTestId, debug } = render(BaseApp, {
    container: root,
    attachTo: document.body,
});

the attachTo property seems to misbehave, but is resovled by doing this instead:

const root = document.createElement("div");
document.body.appendChild(root);
const { queryByText, getByTestId } = render(BaseApp, {
    container: root,
});

this brings the number of passing tests in this example from 4/8 to 7/8, with only the happy-dom checkbox test failing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文