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

In a select element, the onChange event fires even when selecting the currently chosen option #19494

Closed
YishaiBerg opened this issue Dec 29, 2021 · 3 comments · Fixed by #19623
Assignees
Labels
pkg/driver This is due to an issue in the packages/driver directory

Comments

@YishaiBerg
Copy link

Current behavior

In a select element, the onChange event fires even when clicking on the option which is already selected. This behavior is not consistent with what actually happens in browsers (I tested this only on Chrome).

Desired behavior

In a select element, the onChange event should only fire when the value of the select element actually changes. When clicking on an option that is already selected, it should not fire.

Test code to reproduce

Here is a repo with a failing test. The code for the AUT can be found here.

Cypress Version

9.2.0

Other

No response

@sainthkh sainthkh added stage: ready for work The issue is reproducible and in scope pkg/driver This is due to an issue in the packages/driver directory labels Jan 3, 2022
@sainthkh
Copy link
Contributor

sainthkh commented Jan 3, 2022

Confirmed with the code below:

<html>
<head>
  <meta charset='utf-8'>
  <meta name='robots' content='noindex, nofollow'>

  <title>Cypress onChange event bug</title>
</head>
<label>Choose an ice cream flavor:
  <select class='ice-cream' name='ice-cream'>
    <option value=''>Select One …</option>
    <option value='chocolate'>Chocolate</option>
    <option value='sardine'>Sardine</option>
    <option value='vanilla'>Vanilla</option>
  </select>
</label>

<div class='result'></div>

<script>
  const selectElement = document.querySelector('.ice-cream');
  let numOfTimesOnChangeFired = 0;
  selectElement.addEventListener('change', (event) => {
    const result = document.querySelector('.result');
    result.textContent = `Number of times onChange event fired: ${++numOfTimesOnChangeFired}`;
  });

</script>
</html> 
it('does not fire an onChange event when there is no change in a select element', () => {
  cy.visit('fixtures/a.html')
  cy.get('.ice-cream').select('Chocolate')
  cy.get('.result').should('have.text', 'Number of times onChange event fired: 1')
  // Select the option that is already selected - onChange event should not fire.
  cy.get('.ice-cream').select('Chocolate')
  cy.get('.result').should('have.text', 'Number of times onChange event fired: 1')
})

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: work in progress and removed stage: ready for work The issue is reproducible and in scope stage: needs review The PR code is done & tested, needs review stage: work in progress labels Jan 10, 2022
@cypress-bot cypress-bot bot added stage: backlog and removed stage: needs review The PR code is done & tested, needs review labels Jan 19, 2022
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: backlog labels Feb 3, 2022
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Feb 9, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 9, 2022

The code for this is done in cypress-io/cypress#19623, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Feb 15, 2022

Released in 9.5.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v9.5.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/driver This is due to an issue in the packages/driver directory
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants