From c0846f5d9b9fd84a7476bcbb4ee4bdccac6108b9 Mon Sep 17 00:00:00 2001 From: David Munechika Date: Mon, 27 Sep 2021 15:11:13 -0400 Subject: [PATCH] fix: Error message for cy.select() with no arguments (#18234) Co-authored-by: Jennifer Shehane Co-authored-by: Zach Bloomquist Co-authored-by: Chris Breiding Co-authored-by: Chris Breiding --- .../commands/actions/select_spec.js | 76 ++++++++++++++++++- .../driver/src/cy/commands/actions/select.ts | 18 +++++ packages/driver/src/cypress/error_messages.ts | 8 ++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/packages/driver/cypress/integration/commands/actions/select_spec.js b/packages/driver/cypress/integration/commands/actions/select_spec.js index 98a03d3f0d0c..b6737582b133 100644 --- a/packages/driver/cypress/integration/commands/actions/select_spec.js +++ b/packages/driver/cypress/integration/commands/actions/select_spec.js @@ -54,9 +54,15 @@ describe('src/cy/commands/actions/select', () => { }) }) + it('can handle valid index 0', () => { + cy.get('select[name=maps]').select(0).then(($select) => { + expect($select).to.have.value('de_dust2') + }) + }) + it('can select an array of values', () => { - cy.get('select[name=movies]').select(['apoc', 'br']).then(($select) => { - expect($select.val()).to.deep.eq(['apoc', 'br']) + cy.get('select[name=movies]').select(['apoc', 'br', 'co']).then(($select) => { + expect($select.val()).to.deep.eq(['apoc', 'br', 'co']) }) }) @@ -366,6 +372,39 @@ describe('src/cy/commands/actions/select', () => { cy.get('select').select('foo') }) + it('throws when called with no arguments', (done) => { + cy.on('fail', (err) => { + expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `undefined`.') + expect(err.docsUrl).to.eq('https://on.cypress.io/select') + + done() + }) + + cy.get('select[name=maps]').select() + }) + + it('throws when called with null', (done) => { + cy.on('fail', (err) => { + expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `null`.') + expect(err.docsUrl).to.eq('https://on.cypress.io/select') + + done() + }) + + cy.get('select[name=maps]').select(null) + }) + + it('throws when called with invalid type', (done) => { + cy.on('fail', (err) => { + expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `true`.') + expect(err.docsUrl).to.eq('https://on.cypress.io/select') + + done() + }) + + cy.get('select[name=foods]').select(true) + }) + it('throws on anything other than a select', (done) => { cy.on('fail', (err) => { expect(err.message).to.include('`cy.select()` can only be called on a ``') @@ -455,6 +494,39 @@ describe('src/cy/commands/actions/select', () => { cy.get('select[name=foods]').select('foo') }) + it('throws invalid argument error when called with empty string', (done) => { + cy.on('fail', (err) => { + expect(err.message).to.include('`cy.select()` failed because it could not find a single `