最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Paste test in cypress in reactjs project - Stack Overflow

programmeradmin0浏览0评论

Any ideas how to simulate Paste action in Cypress in a bundle with React?

My test should check value right after paste action.

I found few solutions based on DOM manipulation because as the authors say it pastes changes directly to DOM input field and then envoke change event.

My tried cy.get(selector).invoke('val', 'copy-pasted text').trigger('change');

These solutions dont work as expected because React manipulates the DOM it-self hence the "pasting" by suggested ways pushes changes directly to input values and violates React workflow.

Thanks

Any ideas how to simulate Paste action in Cypress in a bundle with React?

My test should check value right after paste action.

I found few solutions based on DOM manipulation because as the authors say it pastes changes directly to DOM input field and then envoke change event.

My tried cy.get(selector).invoke('val', 'copy-pasted text').trigger('change');

These solutions dont work as expected because React manipulates the DOM it-self hence the "pasting" by suggested ways pushes changes directly to input values and violates React workflow.

Thanks

Share Improve this question edited Feb 8, 2020 at 0:31 Rick Smith 9,25116 gold badges83 silver badges86 bronze badges asked Feb 7, 2020 at 10:40 Oleksiï NikonovOleksiï Nikonov 5,5786 gold badges47 silver badges74 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

I think this code could help you:

add to commands:

Cypress.Commands.add("paste", { prevSubject: true }, (selector, pastePayload) => {
  // https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event
  cy.wrap(selector).then($destination => {
    const pasteEvent = Object.assign(new Event("paste", { bubbles: true, cancelable: true }), {
      clipboardData: {
        getData: () => pastePayload
      }
    });
    $destination[0].dispatchEvent(pasteEvent);
  });
});

usage:

cy.getByTestId("some-input-test-id").paste('some text');

The main difference between this and other examples from the internet is that your plugin could have problems catching what was "really" paste. So ie callback onItemsPaste in blueprint would not catch anything. Original version.

The React Testing Library will fire the change event in a React app.

Ref How do I trigger a change event on radio buttons in react-testing-library

I wrapped it in a Cypress custom command for convenience.
Seems the element wants to be focused first.

import { fireEvent } from "@testing-library/react";

Cypress.Commands.add('fireEvent', {prevSubject: true}, (element, event, value) => {
  element.focus()
  fireEvent[event](element[0], { target: { value } });
})
...

it('fires change event', () => {
  cy.get(selector).fireEvent('change', 'copy-pasted text');
  /* 
    Test react re-render effects here with re-tryable commands,
    e.g should(), not expect()
  */
});

Is this a Paste test?

There is a paste event, so perhaps 'change' is not really testing the scenario. Is there a difference between typing and pasting? Will circle back to this.

BTW I use

cy.get(selector).focus().clear().type('copy-pasted text');

without a problem.

The only caveat, as above, subsequent commands must be retryable to give React time to process (or add cy.wait(100)).

cy.window().its('navigator.clipboard')
    .invoke('readText').then((data) => {
                console.log(data);
            })

Should do the trick.

Based on Richard Matsen answer above, utilizing the fireEvent object.

import { fireEvent } from "@testing-library/react";

Cypress.Commands.add("typeInstant", { prevSubject: true }, (element, value) => {
  element.focus();
  fireEvent.change(element[0], { target: { value } });
});

Usage:

cy.get(inputSelector).typeInstant(VERY_LONG_TEXT);
发布评论

评论列表(0)

  1. 暂无评论