Test Overview
This test checks if a React component receives the correct props and if clicking a button triggers the expected event handler.
This test checks if a React component receives the correct props and if clicking a button triggers the expected event handler.
describe('Props and event testing', () => { it('should render with correct props and handle click event', () => { const onClickSpy = cy.spy().as('clickHandler'); cy.mount(<MyButton label="Click me" onClick={onClickSpy} />); cy.get('button').should('have.text', 'Click me'); cy.get('button').click(); cy.get('@clickHandler').should('have.been.calledOnce'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner is ready to execute the test | — | PASS |
| 2 | Mount the MyButton component with label prop 'Click me' and onClick event handler spy | Component is rendered in the test DOM with a button showing text 'Click me' | — | PASS |
| 3 | Find the button element | Button element with text 'Click me' is found in the DOM | Check button text equals 'Click me' | PASS |
| 4 | Click the button | Button is clicked by the test | — | PASS |
| 5 | Verify the onClick event handler spy was called once | Spy function recorded one call from the button click | Spy was called exactly once | PASS |
props testing in Cypress component tests?const onClick = cy.spy();
cy.mount();
cy.get('button').click();
expect(onClick).to.have.been.calledOnce();const onClick = cy.spy();
cy.mount();
cy.get('button').click();
expect(onClick).to.have.been.called();disabled prop and an onSubmit event handler prop. Which Cypress test approach correctly verifies that clicking the submit button calls onSubmit only when disabled is false?Pass the test and this pattern moves unknown