Verify component renders with correct props and handles click event
Preconditions (2)
✅ Expected Result: The button displays the label 'Submit' and the click event handler is triggered exactly once when clicked
import React from 'react'; import { mount } from '@cypress/react'; import Button from '../../src/components/Button'; describe('Button component props and event testing', () => { it('renders with correct label and handles click event', () => { const onClickSpy = cy.spy().as('onClickSpy'); mount(<Button label="Submit" onClick={onClickSpy} />); cy.get('button').should('have.text', 'Submit'); cy.get('button').click(); cy.get('@onClickSpy').should('have.been.calledOnce'); }); });
The test imports React and the mount function from Cypress React plugin to render the Button component.
We create a spy function onClickSpy to track clicks on the button.
The Button is mounted with the label prop set to 'Submit' and the onClick prop set to the spy.
We assert the button's text matches the label prop.
Then we simulate a click on the button and verify the spy was called exactly once.
This ensures the component renders props correctly and handles events as expected.
Now add data-driven testing with 3 different label props and verify each renders correctly and triggers click event
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