0
0
Cypresstesting~20 mins

cy.trigger() for custom events in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Cypress command?
Consider the following Cypress test code snippet that triggers a custom event on a button element. What will be the value of the variable eventTriggered after running this code?
Cypress
let eventTriggered = false;

cy.get('button#submit').then($btn => {
  $btn[0].addEventListener('myCustomEvent', () => {
    eventTriggered = true;
  });
});

cy.get('button#submit').trigger('myCustomEvent');
Aundefined
Bfalse
Ctrue
DThrows an error
Attempts:
2 left
💡 Hint
Remember that trigger fires the event listeners attached to the element.
📝 Syntax
intermediate
2:00remaining
Which option correctly triggers a custom event with additional data?
You want to trigger a custom event named dataReady on a div with id container and pass an object { loaded: true } as event detail. Which of the following Cypress commands is correct?
Acy.get('#container').trigger('dataReady', { detail: { loaded: true } });
Bcy.get('#container').trigger('dataReady', { eventDetail: { loaded: true } });
Ccy.get('#container').trigger('dataReady', { data: { loaded: true } });
Dcy.get('#container').trigger('dataReady', { loaded: true });
Attempts:
2 left
💡 Hint
Custom event data should be passed inside the detail property.
🔧 Debug
advanced
2:00remaining
Why does this custom event not trigger the listener?
Given this code, why does the event listener not run when cy.trigger() is called?
cy.get('#box').trigger('customEvent', { detail: { value: 42 } });

cy.get('#box').then($el => {
  $el[0].addEventListener('customEvent', () => {
    console.log('Event fired');
  });
});
AThe event listener is attached to the wrong element.
BThe event name in trigger is misspelled.
CCustom events cannot be triggered with cy.trigger().
DThe event listener is attached asynchronously and may not be ready before trigger runs.
Attempts:
2 left
💡 Hint
Think about the timing of attaching listeners and triggering events in Cypress commands.
optimization
advanced
2:00remaining
How to optimize triggering multiple custom events on the same element?
You need to trigger three different custom events start, progress, and end on a div with id progressBar. Which approach is the most efficient in Cypress?
ATrigger all events at once with <code>cy.get('#progressBar').trigger(['start', 'progress', 'end'])</code>.
BGet the element once with <code>cy.get('#progressBar')</code> and chain triggers: <code>.trigger('start').trigger('progress').trigger('end')</code>.
CUse <code>cy.get('#progressBar').then($el => { $el.trigger('start'); $el.trigger('progress'); $el.trigger('end'); })</code>.
DCall <code>cy.get('#progressBar').trigger('start')</code>, then <code>cy.get('#progressBar').trigger('progress')</code>, then <code>cy.get('#progressBar').trigger('end')</code> separately.
Attempts:
2 left
💡 Hint
Minimize DOM queries and use Cypress chaining.
🧠 Conceptual
expert
3:00remaining
What is the correct way to listen for a custom event triggered by cy.trigger() in Cypress?
You want to verify that a custom event dataLoaded is triggered on an element during a test. Which approach correctly listens and asserts the event firing?
AAttach an event listener inside <code>cy.get()</code> and set a flag variable, then assert the flag after <code>trigger()</code>.
BUse <code>cy.get().then($el => { const spy = cy.spy(); $el[0].addEventListener('dataLoaded', spy); }).trigger('dataLoaded').then(() => { expect(spy).to.have.been.called; })</code>.
CUse <code>cy.get().then($el => { cy.spy($el[0], 'dispatchEvent') }).trigger('dataLoaded').should('have.been.called')</code>.
DUse <code>cy.get().trigger('dataLoaded').should('have.been.called')</code> to assert the event was triggered.
Attempts:
2 left
💡 Hint
Attach an event listener that sets a flag before triggering and assert the flag after.