Skip to content

Preventing paste works on browser but not during the test #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ematipico opened this issue Mar 6, 2019 · 6 comments
Closed

Preventing paste works on browser but not during the test #316

ematipico opened this issue Mar 6, 2019 · 6 comments
Labels
jsdom Issue with JSDOM environment

Comments

@ematipico
Copy link

  • react-testing-library version: 5.2.0
  • react version: 16.7.0
  • node version: 16.7.0
  • npm (or yarn) version: npm

Relevant code or config:

Sandbox: https://codesandbox.io/s/40nwnrv650

What you did:

Preventing the paste functionality on a single input. Used the evt.preventDefault().

What happened:

The input sets the value

Reproduction:

Sandbox: https://codesandbox.io/s/40nwnrv650

Problem description:

When pasting on the input, the value should not be set

Suggested solution:

N/A

@weyert
Copy link
Contributor

weyert commented Mar 7, 2019

I would assume the event handling in jsdom is off?

@ematipico
Copy link
Author

Hey @weyert, could you please be more specific? In my environment I just run

jest --env=jsdom

I don't do anything fancy with jsdom. Should I add some particular configuration?

@weyert
Copy link
Contributor

weyert commented Mar 7, 2019

I am assuming that jsdom doesn't have proper support for paste-event or it's doesn't taking your event.preventDefault() into account

@alexkrolick alexkrolick added the jsdom Issue with JSDOM environment label Mar 31, 2019
@kentcdodds
Copy link
Member

Closing this for inactivity. If someone wants to work on it in the future feel free to open a new issue. Thanks!

@alexblack
Copy link

I'm hitting this too. When I try to use fireEvent then the input never gets the pasted value applied. When I try to use userEvent.paste, e.preventDefault() doesn't prevent the input from getting the pasted value applied.

@alexblack
Copy link

I think this is a working solution:

const paste = (input: HTMLElement, value: string) => {
  const unpreventedDefault = fireEvent.paste(input, {
    clipboardData: {
      getData: () => value,
    },
  });
  if (unpreventedDefault) {
    fireEvent.change(input, { target: { value } });
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jsdom Issue with JSDOM environment
Projects
None yet
Development

No branches or pull requests

5 participants