Bird
0
0

You want to test a Vue component that emits an event when a button is clicked. Which approach correctly mounts the component and listens for the event in Cypress?

hard📝 framework Q8 of 15
Cypress - Component Testing
You want to test a Vue component that emits an event when a button is clicked. Which approach correctly mounts the component and listens for the event in Cypress?
Acy.mount(MyComponent, { on: { submit: cy.stub().as('submitSpy') } })
Bcy.mount(MyComponent).then(({ component }) => { component.$on('submit', cy.stub().as('submitSpy')) })
Ccy.mount(MyComponent, { listeners: { submit: cy.stub().as('submitSpy') } })
Dcy.mount(MyComponent).invoke('on', 'submit', cy.stub().as('submitSpy'))
Step-by-Step Solution
Solution:
  1. Step 1: Understand event listening in mounted Vue components

    After mounting, you can access the Vue component instance and attach event listeners using $on.
  2. Step 2: Evaluate options

    Only cy.mount(MyComponent).then(({ component }) => { component.$on('submit', cy.stub().as('submitSpy')) }) correctly uses then to get the component instance and attach a stub listener with $on. Other options use invalid keys or methods.
  3. Final Answer:

    cy.mount(MyComponent).then(({ component }) => { component.$on('submit', cy.stub().as('submitSpy')) }) -> Option B
  4. Quick Check:

    Use component.$on after mount to listen events [OK]
Quick Trick: Access component instance with then() and use $on for events [OK]
Common Mistakes:
  • Passing event listeners as mount options
  • Using invalid keys like 'on' or 'listeners'
  • Trying to invoke 'on' directly on cy.mount()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes