allow select to be called with empty array (#18329)

This commit is contained in:
David Munechika
2021-10-02 10:12:55 -04:00
committed by GitHub
parent 4b3c49af58
commit bfba82dea2
2 changed files with 11 additions and 16 deletions

View File

@@ -112,6 +112,14 @@ describe('src/cy/commands/actions/select', () => {
})
})
it('unselects all options if called with empty array', () => {
cy.get('select[name=movies]').select(['apoc', 'br'])
cy.get('select[name=movies]').select([]).then(($select) => {
expect($select.val()).to.deep.eq([])
})
})
// readonly should only be limited to inputs, not checkboxes
it('can select a readonly select', () => {
cy.get('select[name=hunter]').select('gon').then(($select) => {
@@ -505,17 +513,6 @@ describe('src/cy/commands/actions/select', () => {
cy.get('select[name=foods]').select('')
})
it('throws invalid array argument error when called with empty array', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.select()` must be passed an array containing only strings and/or numbers. You passed: `[]`')
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
done()
})
cy.get('select[name=foods]').select([])
})
it('throws invalid array argument error when called with invalid array', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.select()` must be passed an array containing only strings and/or numbers. You passed: `[true,false]`')

View File

@@ -21,10 +21,8 @@ export default (Commands, Cypress, cy) => {
if (
_.isArray(valueOrTextOrIndex)
&& (
valueOrTextOrIndex.length === 0
|| !_.some(valueOrTextOrIndex, (val) => _.isNumber(val) || _.isString(val))
)
&& valueOrTextOrIndex.length > 0
&& !_.some(valueOrTextOrIndex, (val) => _.isNumber(val) || _.isString(val))
) {
$errUtils.throwErrByPath('select.invalid_array_argument', { args: { value: JSON.stringify(valueOrTextOrIndex) } })
}
@@ -163,7 +161,7 @@ export default (Commands, Cypress, cy) => {
})
}
if (!values.length) {
if (!values.length && !(_.isArray(valueOrTextOrIndex) && valueOrTextOrIndex.length === 0)) {
$errUtils.throwErrByPath('select.no_matches', {
args: { value: valueOrTextOrIndex.join(', ') },
})