Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmunechika committed Sep 27, 2021
1 parent bae0665 commit d42e796
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ 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'])
Expand Down Expand Up @@ -377,6 +383,28 @@ describe('src/cy/commands/actions/select', () => {
cy.get('select[name=maps]').select()
})

it('throws when called with null', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.select()` was not called with any value, index, or text.')
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()` was not called with any value, index, or text.')
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 @@ -466,6 +494,28 @@ describe('src/cy/commands/actions/select', () => {
cy.get('select[name=foods]').select('foo')
})

it('throws no matches 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 no matches error when called with empty array', (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 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
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/actions/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const newLineRe = /\n/g
export default (Commands, Cypress, cy) => {
Commands.addAll({ prevSubject: 'element' }, {
select (subject, valueOrTextOrIndex, options = {}) {
if (!valueOrTextOrIndex) {
if (!_.isNumber(valueOrTextOrIndex) && !_.isString(valueOrTextOrIndex) && !_.isArray(valueOrTextOrIndex)) {
$errUtils.throwErrByPath('select.no_argument')
}

Expand Down

0 comments on commit d42e796

Please sign in to comment.