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?
cy.get('button#submit').click().then(() => { cy.get('@clickHandler').should('have.been.calledOnce') })
Think about what should('have.been.calledOnce') checks after the click.
The test checks that the click event handler alias @clickHandler was called exactly once after clicking the button. If the alias is set correctly, the test passes.
You want to select a React component in Cypress by a custom prop data-test-id="login-button". Which locator is best practice?
Use attributes designed for testing to avoid brittle selectors.
Using a custom attribute like data-test-id is best practice because it is stable and does not depend on styling or text content.
You want to check that a Vue component has a prop visible set to true. Which assertion is correct?
cy.get('my-component').should( /* assertion here */ )
Props are JavaScript properties, not HTML attributes or classes.
The have.prop assertion checks the JavaScript property visible on the component instance, which is the correct way to verify a prop value.
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?
Check when the spy is attached relative to the event trigger.
If the spy is attached after the event is triggered, it will never detect the event. The spy must be set up before triggering the event.
You want to test that changing a prop isActive on a component triggers an event statusChanged. Which approach is best?
Think about how to simulate prop changes and listen for events in component tests.
Mounting the component with initial props, attaching an event spy, then updating the prop programmatically allows testing if the event is emitted on prop change.