Skip to content

Commit 1ee03ac

Browse files
38elementseddyerburgh
authored andcommitted
refactor(types): fix the type of wrapper.setChecked() (#788)
1 parent 5bb6a0b commit 1ee03ac

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

Diff for: flow/wrapper.flow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ declare interface BaseWrapper {
3737
setComputed(computed: Object): void;
3838
setMethods(methods: Object): void;
3939
setValue(value: any): void;
40-
setChecked(checked: boolean): void;
40+
setChecked(checked?: boolean): void;
4141
setSelected(): void;
4242
setProps(data: Object): void;
4343
trigger(type: string, options: Object): void;

Diff for: packages/test-utils/src/wrapper-array.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export default class WrapperArray implements BaseWrapper {
225225
this.wrappers.forEach(wrapper => wrapper.setValue(value))
226226
}
227227

228-
setChecked (checked: boolean): void {
228+
setChecked (checked: boolean = true): void {
229229
this.throwErrorIfWrappersIsEmpty('setChecked')
230230

231231
this.wrappers.forEach(wrapper => wrapper.setChecked(checked))

Diff for: packages/test-utils/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ interface BaseWrapper {
7474
setProps (props: object): void
7575

7676
setValue (value: any): void
77-
setChecked (checked: boolean): void
77+
setChecked (checked?: boolean): void
7878
setSelected (): void
7979

8080
trigger (eventName: string, options?: object): void

Diff for: packages/test-utils/types/test/wrapper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ array = wrapper.findAll(ClassComponent)
6363
array = wrapper.findAll({ ref: 'myButton' })
6464
array = wrapper.findAll({ name: 'my-button' })
6565

66+
wrapper.setChecked()
6667
wrapper.setChecked(true)
6768
wrapper.setValue('some string')
6869
wrapper.setSelected()

Diff for: test/specs/error-wrapper.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ describeWithShallowAndMount('ErrorWrapper', mountingMethod => {
2828
'setMethods',
2929
'setData',
3030
'setProps',
31+
'setChecked',
32+
'setSelected',
33+
'setValue',
3134
'trigger',
3235
'destroy'
3336
]

Diff for: test/specs/wrapper-array/setChecked.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describeWithShallowAndMount } from '~resources/utils'
2+
3+
describeWithShallowAndMount('setChecked', mountingMethod => {
4+
it('sets value to the input elements of type checkbox or radio', () => {
5+
const wrapper = mountingMethod({
6+
data () {
7+
return {
8+
t1: false,
9+
t2: ''
10+
}
11+
},
12+
template: `
13+
<div>
14+
<input type="checkbox" name="t1" class="foo" v-model="t1" />
15+
<input type="radio" name="t2" class="foo" value="foo" v-model="t2"/>
16+
<input type="radio" name="t2" class="bar" value="bar" v-model="t2"/>
17+
</div>`
18+
})
19+
const wrapperArray = wrapper.findAll('.foo')
20+
expect(wrapper.vm.t1).to.equal(false)
21+
expect(wrapper.vm.t2).to.equal('')
22+
wrapperArray.setChecked()
23+
expect(wrapper.vm.t1).to.equal(true)
24+
expect(wrapper.vm.t2).to.equal('foo')
25+
expect(wrapperArray.at(0).element.checked).to.equal(true)
26+
expect(wrapperArray.at(1).element.checked).to.equal(true)
27+
})
28+
})

Diff for: test/specs/wrapper-array/setSelected.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describeWithShallowAndMount } from '~resources/utils'
2+
3+
describeWithShallowAndMount('setSelected', mountingMethod => {
4+
it('sets value to the option elements', () => {
5+
const wrapperArray = mountingMethod({
6+
template: `
7+
<div>
8+
<select>
9+
<option/></option>
10+
<option class="foo"/>a</option>
11+
</select>
12+
<select>
13+
<option/></option>
14+
<option class="foo"/>b</option>
15+
</select>
16+
</div>`
17+
}).findAll('.foo')
18+
const fn = () => wrapperArray.setSelected()
19+
const message = '[vue-test-utils]: setSelected must be called on a single wrapper, use at(i) to access a wrapper'
20+
expect(fn)
21+
.to.throw()
22+
.with.property('message', message)
23+
})
24+
})

Diff for: test/specs/wrapper-array/setValue.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describeWithShallowAndMount } from '~resources/utils'
2+
3+
describeWithShallowAndMount('setValue', mountingMethod => {
4+
it('sets value to the text-control input elements', () => {
5+
const wrapper = mountingMethod({
6+
data () {
7+
return {
8+
t1: '',
9+
t2: ''
10+
}
11+
},
12+
template: `
13+
<div>
14+
<input type="text" name="t1" class="foo" v-model="t1" />
15+
<input type="text" name="t2" class="foo" v-model="t2"/>
16+
</div>`
17+
})
18+
const wrapperArray = wrapper.findAll('.foo')
19+
expect(wrapper.vm.t1).to.equal('')
20+
expect(wrapper.vm.t2).to.equal('')
21+
wrapperArray.setValue('foo')
22+
expect(wrapper.vm.t1).to.equal('foo')
23+
expect(wrapper.vm.t2).to.equal('foo')
24+
expect(wrapperArray.at(0).element.value).to.equal('foo')
25+
expect(wrapperArray.at(1).element.value).to.equal('foo')
26+
})
27+
})

0 commit comments

Comments
 (0)