Bird
Raised Fist0
Cypresstesting~20 mins

Props and event testing in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Props and Event Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Cypress test for a button click event?

Consider this Cypress test code that checks if a button click triggers an event handler:

cy.get('button#submit').click().then(() => {
  cy.get('@clickHandler').should('have.been.calledOnce')
})

Assuming the alias @clickHandler is correctly set for the click event, what will be the test result?

Cypress
cy.get('button#submit').click().then(() => {
  cy.get('@clickHandler').should('have.been.calledOnce')
})
ATest fails because the click event handler was never called
BTest passes because the click event handler was called exactly once
CTest fails with a syntax error due to incorrect alias usage
DTest passes but the event handler was called multiple times
Attempts:
2 left
💡 Hint

Think about what should('have.been.calledOnce') checks after the click.

❓ locator
intermediate
1:30remaining
Which locator is best to select a React component by its prop for testing?

You want to select a React component in Cypress by a custom prop data-test-id="login-button". Which locator is best practice?

Acy.get('button[data-test-id="login-button"]')
Bcy.get('button#login-button')
Ccy.get('button.login-button')
Dcy.get('button').contains('Login')
Attempts:
2 left
💡 Hint

Use attributes designed for testing to avoid brittle selectors.

❓ assertion
advanced
2:00remaining
Which assertion correctly verifies a prop value in a Vue component using Cypress?

You want to check that a Vue component has a prop visible set to true. Which assertion is correct?

Cypress
cy.get('my-component').should( /* assertion here */ )
Ahave.attr('visible', 'true')
Bhave.class('visible')
Chave.prop('visible', true)
Dcontain('visible: true')
Attempts:
2 left
💡 Hint

Props are JavaScript properties, not HTML attributes or classes.

🔧 Debug
advanced
2:30remaining
Why does this Cypress test fail to detect the emitted event?

Given this test code:

cy.get('child-component').invoke('trigger', 'customEvent')
cy.get('@eventSpy').should('have.been.called')

The test fails because @eventSpy is never called. What is the likely cause?

AThe event spy was not attached to the component before triggering the event
BThe event name <code>customEvent</code> is misspelled in the trigger
CThe component does not emit events in Cypress tests
DThe trigger method cannot simulate custom events
Attempts:
2 left
💡 Hint

Check when the spy is attached relative to the event trigger.

❓ framework
expert
3:00remaining
In Cypress, how do you best test a component's prop change triggers an event?

You want to test that changing a prop isActive on a component triggers an event statusChanged. Which approach is best?

AUse <code>cy.get</code> to find the component, then call <code>.trigger('statusChanged')</code> manually
BMount the component and assert the event was emitted without changing the prop
CMount the component, update the prop directly in the DOM, then check for the event
DMount the component with <code>isActive</code>, attach an event spy, then update the prop using <code>cy.wrap</code> and check the spy
Attempts:
2 left
💡 Hint

Think about how to simulate prop changes and listen for events in component tests.

Practice

(1/5)
1. What is the main purpose of props testing in Cypress component tests?
easy
A. To verify that components receive and display data correctly
B. To check if the component's CSS styles are applied
C. To test if the component renders without errors
D. To measure the performance of component rendering

Solution

  1. Step 1: Understand what props are

    Props are data passed to components to control their behavior or display.
  2. Step 2: Identify the goal of props testing

    Props testing ensures the component uses and shows this data correctly.
  3. Final Answer:

    To verify that components receive and display data correctly -> Option A
  4. Quick Check:

    Props testing = verify data usage [OK]
Hint: Props testing checks data passed to components [OK]
Common Mistakes:
  • Confusing props testing with style or performance testing
  • Thinking props testing checks event handlers
  • Assuming props testing is about rendering errors
2. Which Cypress command is used to spy on an event handler function passed as a prop?
easy
A. cy.spy()
B. cy.stub()
C. cy.mount()
D. cy.visit()

Solution

  1. Step 1: Recall Cypress commands for spying

    cy.spy() creates a spy to watch function calls without changing behavior.
  2. Step 2: Match command to event handler spying

    Spying on event handlers uses cy.spy() to check if they were called.
  3. Final Answer:

    cy.spy() -> Option A
  4. Quick Check:

    Spy event handler = cy.spy() [OK]
Hint: Use cy.spy() to watch function calls [OK]
Common Mistakes:
  • Using cy.stub() which replaces function behavior
  • Confusing cy.mount() with spying
  • Using cy.visit() which is for page navigation
3. Given this Cypress test snippet:
const onClick = cy.spy();
cy.mount();
cy.get('button').click();
expect(onClick).to.have.been.calledOnce();

What will be the test result?
medium
A. Test passes because the click triggers onClick once
B. Test passes but onClick is called multiple times
C. Test fails because onClick is never called
D. Test fails due to syntax error in expect statement

Solution

  1. Step 1: Analyze the spy and mount setup

    onClick is a spy passed as prop; Button is mounted with it.
  2. Step 2: Check the assertion syntax

    The assertion uses calledOnce() which is incorrect syntax; it should be calledOnce without parentheses.
  3. Final Answer:

    Test fails due to syntax error in expect statement -> Option D
  4. Quick Check:

    calledOnce() is invalid syntax [OK]
Hint: Use calledOnce without () in assertions [OK]
Common Mistakes:
  • Adding parentheses after calledOnce
  • Assuming spy is not called when it is
  • Ignoring syntax errors in assertions
4. You wrote this Cypress test to check a button click event:
const onClick = cy.spy();
cy.mount();
cy.get('button').click();
expect(onClick).to.have.been.called();

The test fails. What is the likely problem?
medium
A. The spy was not created correctly with cy.spy()
B. The event handler prop name is wrong
C. The assertion incorrectly uses parentheses after called
D. The button selector is incorrect

Solution

  1. Step 1: Check spy creation and usage

    Spy is created correctly with cy.spy() and passed as prop.
  2. Step 2: Review assertion syntax

    called is a property, not a function, so no parentheses should be used; parentheses cause failure.
  3. Final Answer:

    The assertion incorrectly uses parentheses after called -> Option C
  4. Quick Check:

    called is property, no () [OK]
Hint: Don't add () after called in assertions [OK]
Common Mistakes:
  • Using called() instead of called
  • Incorrect selector causing no click
  • Wrong prop name like onClickHandler
5. You want to test a component that receives a 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?
hard
A. Mount component with disabled=true and spy on onSubmit; click button and assert spy called once; then mount with disabled=false and assert spy not called
B. Mount component with disabled=false and spy on onSubmit; click button and assert spy called once; then mount with disabled=true and assert spy not called on click
C. Mount component once with disabled=false and spy on onSubmit; click button twice and assert spy called twice regardless of disabled prop
D. Mount component with disabled=false without spy; click button and check UI changes only

Solution

  1. Step 1: Test enabled state with spy

    Mount with disabled=false, spy on onSubmit, click button, expect spy called once.
  2. Step 2: Test disabled state with spy

    Mount with disabled=true, click button, expect spy not called to confirm no event triggered.
  3. Final Answer:

    Mount component with disabled=false and spy on onSubmit; click button and assert spy called once; then mount with disabled=true and assert spy not called on click -> Option B
  4. Quick Check:

    Test both states with spy to confirm event firing [OK]
Hint: Test enabled and disabled states separately with spy [OK]
Common Mistakes:
  • Testing only one disabled state
  • Not using spy to check event calls
  • Assuming event fires regardless of disabled prop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard