Skip to content

Commit 0628a7c

Browse files
committed
refactor: remove checks from selected
1 parent 8ec7f92 commit 0628a7c

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -521,47 +521,32 @@ export default class Wrapper implements BaseWrapper {
521521
*/
522522
setSelected (): void {
523523
const tagName = this.element.tagName
524-
// $FlowIgnore
525-
const type = this.attributes().type
524+
525+
if (tagName === 'SELECT') {
526+
throwError(
527+
`wrapper.setSelected() cannot be called on select. ` +
528+
`Call it on one of its options`
529+
)
530+
}
526531

527532
if (tagName === 'OPTION') {
528533
// $FlowIgnore
529534
this.element.selected = true
530535
// $FlowIgnore
531-
if (this.element.parentElement.tagName === 'OPTGROUP') {
532-
// $FlowIgnore
533-
createWrapper(this.element.parentElement.parentElement, this.options)
534-
.trigger('change')
535-
} else {
536+
let parentElement = this.element.parentElement
537+
538+
// $FlowIgnore
539+
if (parentElement.tagName === 'OPTGROUP') {
536540
// $FlowIgnore
537-
createWrapper(this.element.parentElement, this.options)
538-
.trigger('change')
541+
parentElement = parentElement.parentElement
539542
}
540-
} else if (tagName === 'SELECT') {
541-
throwError(
542-
`wrapper.setSelected() cannot be called on select. ` +
543-
`Call it on one of its options`
544-
)
545-
} else if (tagName === 'INPUT' && type === 'checkbox') {
546-
throwError(
547-
`wrapper.setSelected() cannot be called on a <input ` +
548-
`type="checkbox" /> element. Use ` +
549-
`wrapper.setChecked() instead`
550-
)
551-
} else if (tagName === 'INPUT' && type === 'radio') {
552-
throwError(
553-
`wrapper.setSelected() cannot be called on a <input ` +
554-
`type="radio" /> element. Use wrapper.setChecked() ` +
555-
`instead`
556-
)
557-
} else if (tagName === 'INPUT' || tagName === 'TEXTAREA') {
558-
throwError(
559-
`wrapper.setSelected() cannot be called on "text" ` +
560-
`inputs. Use wrapper.setValue() instead`
561-
)
562-
} else {
563-
throwError(`wrapper.setSelected() cannot be called on this element`)
543+
544+
// $FlowIgnore
545+
createWrapper(parentElement, this.options).trigger('change')
546+
return
564547
}
548+
549+
throwError(`wrapper.setSelected() cannot be called on this element`)
565550
}
566551

567552
/**
Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ComponentWithInput from '~resources/components/component-with-input.vue'
22
import { describeWithShallowAndMount } from '~resources/utils'
33

4-
describeWithShallowAndMount('setSelected', mountingMethod => {
4+
describeWithShallowAndMount.only('setSelected', mountingMethod => {
55
it('sets element selected true', () => {
66
const wrapper = mountingMethod(ComponentWithInput)
77
const options = wrapper.find('select').findAll('option')
@@ -33,37 +33,15 @@ describeWithShallowAndMount('setSelected', mountingMethod => {
3333
expect(wrapper.text()).to.contain('selectA')
3434
})
3535

36-
it('throws error if element is radio', () => {
37-
const message =
38-
'wrapper.setSelected() cannot be called on a <input type="radio" /> element. Use wrapper.setChecked() instead'
39-
shouldThrowErrorOnElement('input[type="radio"]', message)
40-
})
41-
42-
it('throws error if element is radio', () => {
43-
const message =
44-
'wrapper.setSelected() cannot be called on a <input type="checkbox" /> element. Use wrapper.setChecked() instead'
45-
shouldThrowErrorOnElement('input[type="checkbox"]', message)
46-
})
47-
48-
it('throws error if element is text like', () => {
49-
const message =
50-
'wrapper.setSelected() cannot be called on "text" inputs. Use wrapper.setValue() instead'
51-
shouldThrowErrorOnElement('input[type="text"]', message)
52-
shouldThrowErrorOnElement('textarea', message)
53-
})
54-
5536
it('throws error if element is not valid', () => {
5637
const message = 'wrapper.setSelected() cannot be called on this element'
57-
shouldThrowErrorOnElement('#label-el', message)
58-
})
5938

60-
function shouldThrowErrorOnElement (selector, message, value) {
6139
const wrapper = mountingMethod(ComponentWithInput)
62-
const input = wrapper.find(selector)
40+
const input = wrapper.find('#label-el')
6341

64-
const fn = () => input.setSelected(value)
42+
const fn = () => input.setSelected('value')
6543
expect(fn)
6644
.to.throw()
6745
.with.property('message', '[vue-test-utils]: ' + message)
68-
}
46+
})
6947
})

0 commit comments

Comments
 (0)