Bird
0
0

Consider this Cypress test snippet:

medium📝 Predict Output Q4 of 15
Cypress - Component Testing
Consider this Cypress test snippet:
const onSubmit = cy.spy();
cy.mount(
); cy.get('form').submit(); expect(onSubmit).to.have.been.calledOnce();

What is the expected outcome of this test?
AThe test will pass because the onSubmit spy is called exactly once.
BThe test will fail because the onSubmit spy is never called.
CThe test will fail because the onSubmit spy is called multiple times.
DThe test will pass even if the onSubmit spy is not called.
Step-by-Step Solution
Solution:
  1. Step 1: Setup Spy

    The spy onSubmit is created to monitor calls to the submit handler.
  2. Step 2: Mount Component

    The Form component is mounted with the onSubmit prop set to the spy.
  3. Step 3: Trigger Event

    The form's submit event is triggered using cy.get('form').submit().
  4. Step 4: Assertion

    The test asserts that onSubmit was called exactly once with calledOnce().
  5. Final Answer:

    The test will pass because the onSubmit spy is called exactly once. -> Option A
  6. Quick Check:

    Spy tracks event calls correctly [OK]
Quick Trick: Use spies to verify event handler call counts [OK]
Common Mistakes:
  • Forgetting to mount the component with the spy prop
  • Using incorrect event trigger method
  • Expecting multiple calls when only one occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes