Files
cypress/npm/react/cypress/component/basic/stub-example

Stub examples

You can pass a cy.stub as a property to the mounted component and then assert it was invoked.

it('calls the click prop', () => {
  const onClick = cy.stub().as('clicker')
  mount(<Clicker click={onClick} />)
  cy.get('button')
    .click()
    .click()
  cy.get('@clicker').should('have.been.calledTwice')
})

Stub

Tests

  • clicker-spec.js passes a stub to the component and asserts it was called
  • clicker-with-delay-spec.js passes a stub to the component that is called on click, but only after a delay. Shows the difference between .then and .should. Shows my favorite method of querying stubs via an alias
const onClick = cy.stub().as('clicker')
mount(<Clicker click={onClick} />)
...
cy.get('@clicker').should('have.been.calledTwice')

More info

Watch Assert that the stub was called twice showing these specs.

Read Stubs, spies and clocks and Retry-ability.