Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance .trigger() command to emit KeyboardEvent rather than the generic Event #29040

Open
toyopilgrim opened this issue Mar 1, 2024 · 0 comments
Labels
stage: proposal 💡 No work has been done of this issue type: enhancement Requested enhancement of existing feature

Comments

@toyopilgrim
Copy link

toyopilgrim commented Mar 1, 2024

What would you like?

I would like to propose an enhancement to Cypress's .trigger() command to support emitting KeyboardEvent with the key and code properties, in addition to the current keyCode.

Why is this needed?

This matter is vital because many modern web applications rely on key and code properties for keyboard event handling, and the current limitation to Event having only keyCode (which is deprecated) hampers the ability to test these applications effectively.

For instance, in my application, I have a keydown event listener that performs an action when the Space key is pressed.

document.addEventListener('keydown', keyDownGlobal);

function keyDownGlobal(event: KeyboardEvent) {
  if (event.code === 'Space') {
    doSomething();
  }

The current Cypress implementation fails to trigger this action because it does not include the code property in the event.

cy.get("[data-cy=target]").trigger("keydown", { keyCode: 32 })

I've worked around this by manually creating and dispatching a KeyboardEvent with the necessary properties.

      cy.document().then(document => {
        const spaceKeyDown = new KeyboardEvent("keydown", {
          key: " ",
          code: "Space",
          bubbles: true,
          cancelable: true,
          keyCode: 32,
          which: 32
        });
        document.dispatchEvent(spaceKeyDown);
      });

It would be more efficient and user-friendly if Cypress's .trigger() command natively supported these properties with KeyboardEvent. This enhancement would significantly improve Cypress's ability to test applications reliant on specific keyboard inputs, providing a more comprehensive and realistic testing environment.

Other

No response

@jennifer-shehane jennifer-shehane added type: enhancement Requested enhancement of existing feature stage: proposal 💡 No work has been done of this issue labels Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: proposal 💡 No work has been done of this issue type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

No branches or pull requests

2 participants