From bfba82dea2cb532c3b36a1b3dec520a744f88540 Mon Sep 17 00:00:00 2001 From: David Munechika Date: Sat, 2 Oct 2021 10:12:55 -0400 Subject: [PATCH] allow select to be called with empty array (#18329) --- .../commands/actions/select_spec.js | 19 ++++++++----------- .../driver/src/cy/commands/actions/select.ts | 8 +++----- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/driver/cypress/integration/commands/actions/select_spec.js b/packages/driver/cypress/integration/commands/actions/select_spec.js index b6737582b1..52fa2efe98 100644 --- a/packages/driver/cypress/integration/commands/actions/select_spec.js +++ b/packages/driver/cypress/integration/commands/actions/select_spec.js @@ -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]`') diff --git a/packages/driver/src/cy/commands/actions/select.ts b/packages/driver/src/cy/commands/actions/select.ts index 181b5fefed..36b2d455e4 100644 --- a/packages/driver/src/cy/commands/actions/select.ts +++ b/packages/driver/src/cy/commands/actions/select.ts @@ -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(', ') }, })