Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
How do I mock a DataTransfer item for testing-library

I am trying to use Testing-library user-event paste to test an onPaste event handler for a react component.

The event handler:

pasteHandler = (event: any) => {
    const text = event.clipboardData.getData("text");
    parseAndUpdateState(text);
}

If I try const clipboardData = new DataTransfer(); or a global mock as mentioned here I get: Error: Uncaught [ReferenceError: DataTransfer is not defined].

No matter how I try to define either a mock jest object or create my own definition, I get errors for either missing dataTransfer object or missing fields.

Is there a something I am missing for how to mock out a DataTransfer object where I just need to get clipboard data?

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

0

Found Article from Question

I missed the documentation for createEvent and after using that along with

  const PhoneNumberElement = screen.queryByTestId('paste-input');

  const paste = createEvent.paste(PhoneNumberElement, {
    clipboardData: {
      getData: () => '123456',
    },
  });

  fireEvent(PhoneNumberElement, paste);

as mentioned in the article I was able to paste and trigger the onPaste handler.

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!