0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.