Skip to content
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

ClipboardEvent is not defined #310

Closed
lulidd01 opened this issue Jul 9, 2019 · 3 comments
Closed

ClipboardEvent is not defined #310

lulidd01 opened this issue Jul 9, 2019 · 3 comments

Comments

@lulidd01
Copy link

lulidd01 commented Jul 9, 2019

when i use ClipboardEvent, it not defined.

const pasteFn = new ClipboardEvent('paste', {
      dataType: 'text/plain',
      data: 'paste text'
    })

but i can use other events, like: MouseEvent, KeybordEvent

@afontcu
Copy link
Member

afontcu commented Jul 12, 2019

Hi @lulidd01! ClipboardEvent appears to be defined here. Can you please provide a reproduction link? I'm not quite sure what's your use case given the snippet you pasted above.

Thanks!

@Davetherave2010
Copy link

To build on @afontcu's answer you could use createEvent.paste

test('Pasting also fires onPaste event which returns clipboard data', () => {
  const pasted = jest.fn(() => null);
  const changed = jest.fn(() => null);

  render(
    <PasteComponent paste={pasted} changeEvent={changed} data-testid='paste-input' />);

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

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

  fireEvent(PhoneNumberElement, paste);

  expect(pasted).toHaveBeenCalled();
  expect(pasted).toHaveBeenCalledWith('123456');
});

This might help - https://medium.davidendersby.me/2-ways-to-trigger-the-onpaste-event-with-testing-library-1502c5fdb9e

@osheral
Copy link

osheral commented Sep 11, 2024

fireEvent.paste(screen.getByTestId('add-images-modal-body'), {
  clipboardData: {
    items: [
      {
        kind: 'file',
        getAsFile: () => new File(['(⌐□_□)'], imageName, { type: 'image/png' }),
      },
    ],
  },
});

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

No branches or pull requests

5 participants