mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-08 08:10:02 -06:00
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')
})
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.
