https://cypress.io logo
Cypress update a text input's value, whilst preser...
# i-need-help
m
I have a simple text input, where instead of using .type() to enter characters 1 at a time to get text into an input, i want to insert a bunch of characters all at once, essentially imitating what happens when a user pastes text into an input. i've tried doing this:
Copy code
ts
import { mount } from 'cypress/react18';
const onChange = cy
      .stub()
      .as('onChange')
      .callsFake(moo => {
        console.log('onChange called', moo);
      });
    mount(
      <input type="text" data-testid="moo" onChange={onChange} />,
    );
    cy.get('[data-testid="moo"]').invoke('val', 'how now brown cow');
This test succeeds in updating the input's current value, but does not trigger React's onChange event. 😦 Is there a way to update the input's value, and also trigger React's onChange event at the same time?
g
Search for "onChange" https://cypress.tips/search since React overwrites it an you need to call the original method