0
0
Cypresstesting~10 mins

Props and event testing in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a React component receives the correct props and if clicking a button triggers the expected event handler.

Test Code - Cypress
Cypress
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');
  });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner is ready to execute the test-PASS
2Mount the MyButton component with label prop 'Click me' and onClick event handler spyComponent is rendered in the test DOM with a button showing text 'Click me'-PASS
3Find the button elementButton element with text 'Click me' is found in the DOMCheck button text equals 'Click me'PASS
4Click the buttonButton is clicked by the test-PASS
5Verify the onClick event handler spy was called onceSpy function recorded one call from the button clickSpy was called exactly oncePASS
Failure Scenario
Failing Condition: The button text does not match the 'label' prop or the onClick handler is not called on click
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the button's label?
AThe button has a default label regardless of props
BThe button displays the text passed as the 'label' prop
CThe button label changes after clicking
DThe button label is empty
Key Result
Always verify that components receive the correct props and that event handlers are triggered as expected to ensure UI behaves correctly.