• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

159
Views
Why can't @testing-library/user-event interact with input elements?

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?

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error