Skip to content

Commit

Permalink
fix: Error message for cy.select() with no arguments (#18234)
Browse files Browse the repository at this point in the history
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
Co-authored-by: Zach Bloomquist <github@chary.us>
Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
Co-authored-by: Chris Breiding <chrisbreiding@gmail.com>
  • Loading branch information
5 people authored Sep 27, 2021
1 parent c6758dc commit c0846f5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})
})

Expand Down Expand Up @@ -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 `<select>`. Your subject is a: `<input id="input">`')
Expand Down Expand Up @@ -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 `<option>` with value, index, or text matching: ``')
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 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]`')
expect(err.docsUrl).to.eq('https://on.cypress.io/select')

done()
})

cy.get('select[name=foods]').select([true, false])
})

it('throws when the <select> itself is disabled', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.select()` failed because this element is currently disabled:')
Expand Down
18 changes: 18 additions & 0 deletions packages/driver/src/cy/commands/actions/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ const newLineRe = /\n/g
export default (Commands, Cypress, cy) => {
Commands.addAll({ prevSubject: 'element' }, {
select (subject, valueOrTextOrIndex, options = {}) {
if (
!_.isNumber(valueOrTextOrIndex)
&& !_.isString(valueOrTextOrIndex)
&& !_.isArray(valueOrTextOrIndex)
) {
$errUtils.throwErrByPath('select.invalid_argument', { args: { value: JSON.stringify(valueOrTextOrIndex) } })
}

if (
_.isArray(valueOrTextOrIndex)
&& (
valueOrTextOrIndex.length === 0
|| !_.some(valueOrTextOrIndex, (val) => _.isNumber(val) || _.isString(val))
)
) {
$errUtils.throwErrByPath('select.invalid_array_argument', { args: { value: JSON.stringify(valueOrTextOrIndex) } })
}

const userOptions = options

options = _.defaults({}, userOptions, {
Expand Down
8 changes: 8 additions & 0 deletions packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,14 @@ export default {
},

select: {
invalid_argument: {
message: `${cmd('select')} must be passed a string, number, or array as its 1st argument. You passed: \`{{value}}\`.`,
docsUrl: 'https://on.cypress.io/select',
},
invalid_array_argument: {
message: `${cmd('select')} must be passed an array containing only strings and/or numbers. You passed: \`{{value}}\`.`,
docsUrl: 'https://on.cypress.io/select',
},
disabled: {
message: `${cmd('select')} failed because this element is currently disabled:\n\n\`{{node}}\``,
docsUrl: 'https://on.cypress.io/select',
Expand Down

4 comments on commit c0846f5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c0846f5 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.5.0/circle-develop-c0846f5d9b9fd84a7476bcbb4ee4bdccac6108b9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c0846f5 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.5.0/appveyor-develop-c0846f5d9b9fd84a7476bcbb4ee4bdccac6108b9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c0846f5 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.5.0/appveyor-develop-c0846f5d9b9fd84a7476bcbb4ee4bdccac6108b9/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on c0846f5 Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.5.0/circle-develop-c0846f5d9b9fd84a7476bcbb4ee4bdccac6108b9/cypress.tgz

Please sign in to comment.