fix: .select() by index works if <options> have same value (run ci) (#25016)

This commit is contained in:
Blue F
2022-12-08 09:41:12 -08:00
committed by GitHub
parent 24f65d8b30
commit b32a8afe51
2 changed files with 14 additions and 3 deletions

View File

@@ -51,6 +51,14 @@ describe('src/cy/commands/actions/select', () => {
})
})
it('can handle index when all values are identical', () => {
cy.$$('select[name=maps] option').attr('value', 'foo')
cy.get('select[name=maps]').select(2).then(($select) => {
expect($select[0].selectedOptions[0].text).to.eq('nuke')
})
})
it('can select an array of values', () => {
cy.get('select[name=movies]').select(['apoc', 'br', 'co']).then(($select) => {
expect($select.val()).to.deep.eq(['apoc', 'br', 'co'])

View File

@@ -127,6 +127,11 @@ export default (Commands, Cypress, cy) => {
if (valueOrTextOrIndex.includes(value) || valueOrTextOrIndex.includes(index)) {
optionEls.push(optEl)
values.push(value)
// https://github.com/cypress-io/cypress/issues/24739
if (options.$el.find(`option[value="${value}"]`).length > 1) {
notAllUniqueValues = true
}
}
// replace new line chars, then trim spaces
@@ -183,9 +188,7 @@ export default (Commands, Cypress, cy) => {
args: { node },
})
}
})
_.each(optionEls, ($el) => {
if ($el.closest('optgroup').prop('disabled')) {
node = $dom.stringify($el)
@@ -271,7 +274,7 @@ export default (Commands, Cypress, cy) => {
if (notAllUniqueValues) {
// if all the values are the same and the user is trying to
// select based on the text, setting the val() will just
// select based on the text or index, setting the val() will just
// select the first one
let selectedIndex = 0