Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Error message for cy.select() with no arguments #18234

Merged
merged 20 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0d6b5dc
feat(driver): add select by index
davidmunechika Sep 21, 2021
c371363
Merge branch 'develop' into issue-757-select-by-index
flotwig Sep 22, 2021
17f6a0a
feat(driver): update types definitions
davidmunechika Sep 22, 2021
de643f8
Merge branch 'issue-757-select-by-index' of https://github.com/cypres…
davidmunechika Sep 22, 2021
2012b86
feat(driver): add indexes array test
davidmunechika Sep 22, 2021
72bbc13
Merge branch 'develop' into issue-757-select-by-index
davidmunechika Sep 22, 2021
d7bce00
feat(driver): update renamed variable in comment
davidmunechika Sep 24, 2021
2af7172
feat(driver): lint fix
davidmunechika Sep 24, 2021
6487357
feat(driver): update error message text to include index
davidmunechika Sep 24, 2021
e0a67ec
Merge branch 'develop' into issue-757-select-by-index
davidmunechika Sep 24, 2021
64424a3
Merge branch 'develop' into issue-757-select-by-index
davidmunechika Sep 24, 2021
f879003
fix: add error message for no argument select
davidmunechika Sep 24, 2021
d6a2cc2
merge branch develop
davidmunechika Sep 27, 2021
bae0665
fix error messages
davidmunechika Sep 27, 2021
d42e796
add test cases
davidmunechika Sep 27, 2021
0313373
update tests
davidmunechika Sep 27, 2021
b03fddb
minor fix
chrisbreiding Sep 27, 2021
05aa5bf
Merge branch 'develop' into no-argument-select-error-message
davidmunechika Sep 27, 2021
9d125cf
use JSON.stringify on error message args, fix tests
chrisbreiding Sep 27, 2021
6dcaf29
fix select array validation
chrisbreiding Sep 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) } })
}

davidmunechika marked this conversation as resolved.
Show resolved Hide resolved
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