Skip to content

Commit c0846f5

Browse files
davidmunechikajennifer-shehaneflotwigchrisbreiding
authored
fix: Error message for cy.select() with no arguments (#18234)
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>
1 parent c6758dc commit c0846f5

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

packages/driver/cypress/integration/commands/actions/select_spec.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ describe('src/cy/commands/actions/select', () => {
5454
})
5555
})
5656

57+
it('can handle valid index 0', () => {
58+
cy.get('select[name=maps]').select(0).then(($select) => {
59+
expect($select).to.have.value('de_dust2')
60+
})
61+
})
62+
5763
it('can select an array of values', () => {
58-
cy.get('select[name=movies]').select(['apoc', 'br']).then(($select) => {
59-
expect($select.val()).to.deep.eq(['apoc', 'br'])
64+
cy.get('select[name=movies]').select(['apoc', 'br', 'co']).then(($select) => {
65+
expect($select.val()).to.deep.eq(['apoc', 'br', 'co'])
6066
})
6167
})
6268

@@ -366,6 +372,39 @@ describe('src/cy/commands/actions/select', () => {
366372
cy.get('select').select('foo')
367373
})
368374

375+
it('throws when called with no arguments', (done) => {
376+
cy.on('fail', (err) => {
377+
expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `undefined`.')
378+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
379+
380+
done()
381+
})
382+
383+
cy.get('select[name=maps]').select()
384+
})
385+
386+
it('throws when called with null', (done) => {
387+
cy.on('fail', (err) => {
388+
expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `null`.')
389+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
390+
391+
done()
392+
})
393+
394+
cy.get('select[name=maps]').select(null)
395+
})
396+
397+
it('throws when called with invalid type', (done) => {
398+
cy.on('fail', (err) => {
399+
expect(err.message).to.include('`cy.select()` must be passed a string, number, or array as its 1st argument. You passed: `true`.')
400+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
401+
402+
done()
403+
})
404+
405+
cy.get('select[name=foods]').select(true)
406+
})
407+
369408
it('throws on anything other than a select', (done) => {
370409
cy.on('fail', (err) => {
371410
expect(err.message).to.include('`cy.select()` can only be called on a `<select>`. Your subject is a: `<input id="input">`')
@@ -455,6 +494,39 @@ describe('src/cy/commands/actions/select', () => {
455494
cy.get('select[name=foods]').select('foo')
456495
})
457496

497+
it('throws invalid argument error when called with empty string', (done) => {
498+
cy.on('fail', (err) => {
499+
expect(err.message).to.include('`cy.select()` failed because it could not find a single `<option>` with value, index, or text matching: ``')
500+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
501+
502+
done()
503+
})
504+
505+
cy.get('select[name=foods]').select('')
506+
})
507+
508+
it('throws invalid array argument error when called with empty array', (done) => {
509+
cy.on('fail', (err) => {
510+
expect(err.message).to.include('`cy.select()` must be passed an array containing only strings and/or numbers. You passed: `[]`')
511+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
512+
513+
done()
514+
})
515+
516+
cy.get('select[name=foods]').select([])
517+
})
518+
519+
it('throws invalid array argument error when called with invalid array', (done) => {
520+
cy.on('fail', (err) => {
521+
expect(err.message).to.include('`cy.select()` must be passed an array containing only strings and/or numbers. You passed: `[true,false]`')
522+
expect(err.docsUrl).to.eq('https://on.cypress.io/select')
523+
524+
done()
525+
})
526+
527+
cy.get('select[name=foods]').select([true, false])
528+
})
529+
458530
it('throws when the <select> itself is disabled', (done) => {
459531
cy.on('fail', (err) => {
460532
expect(err.message).to.include('`cy.select()` failed because this element is currently disabled:')

packages/driver/src/cy/commands/actions/select.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ const newLineRe = /\n/g
1111
export default (Commands, Cypress, cy) => {
1212
Commands.addAll({ prevSubject: 'element' }, {
1313
select (subject, valueOrTextOrIndex, options = {}) {
14+
if (
15+
!_.isNumber(valueOrTextOrIndex)
16+
&& !_.isString(valueOrTextOrIndex)
17+
&& !_.isArray(valueOrTextOrIndex)
18+
) {
19+
$errUtils.throwErrByPath('select.invalid_argument', { args: { value: JSON.stringify(valueOrTextOrIndex) } })
20+
}
21+
22+
if (
23+
_.isArray(valueOrTextOrIndex)
24+
&& (
25+
valueOrTextOrIndex.length === 0
26+
|| !_.some(valueOrTextOrIndex, (val) => _.isNumber(val) || _.isString(val))
27+
)
28+
) {
29+
$errUtils.throwErrByPath('select.invalid_array_argument', { args: { value: JSON.stringify(valueOrTextOrIndex) } })
30+
}
31+
1432
const userOptions = options
1533

1634
options = _.defaults({}, userOptions, {

packages/driver/src/cypress/error_messages.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,14 @@ export default {
13811381
},
13821382

13831383
select: {
1384+
invalid_argument: {
1385+
message: `${cmd('select')} must be passed a string, number, or array as its 1st argument. You passed: \`{{value}}\`.`,
1386+
docsUrl: 'https://on.cypress.io/select',
1387+
},
1388+
invalid_array_argument: {
1389+
message: `${cmd('select')} must be passed an array containing only strings and/or numbers. You passed: \`{{value}}\`.`,
1390+
docsUrl: 'https://on.cypress.io/select',
1391+
},
13841392
disabled: {
13851393
message: `${cmd('select')} failed because this element is currently disabled:\n\n\`{{node}}\``,
13861394
docsUrl: 'https://on.cypress.io/select',

0 commit comments

Comments
 (0)