feat(runtime-dom): allow native Set as v-model checkbox source (#1957)

This commit is contained in:
Pick
2020-09-14 23:16:50 +08:00
committed by GitHub
parent 542680e478
commit cf1b6c666f
4 changed files with 104 additions and 4 deletions

View File

@@ -433,6 +433,76 @@ describe('vModel', () => {
expect(bar.checked).toEqual(false)
})
it(`should support Set as a checkbox model`, async () => {
const component = defineComponent({
data() {
return { value: new Set() }
},
render() {
return [
withVModel(
h('input', {
type: 'checkbox',
class: 'foo',
value: 'foo',
'onUpdate:modelValue': setValue.bind(this)
}),
this.value
),
withVModel(
h('input', {
type: 'checkbox',
class: 'bar',
value: 'bar',
'onUpdate:modelValue': setValue.bind(this)
}),
this.value
)
]
}
})
render(h(component), root)
const foo = root.querySelector('.foo')
const bar = root.querySelector('.bar')
const data = root._vnode.component.data
foo.checked = true
triggerEvent('change', foo)
await nextTick()
expect(data.value).toMatchObject(new Set(['foo']))
bar.checked = true
triggerEvent('change', bar)
await nextTick()
expect(data.value).toMatchObject(new Set(['foo', 'bar']))
bar.checked = false
triggerEvent('change', bar)
await nextTick()
expect(data.value).toMatchObject(new Set(['foo']))
foo.checked = false
triggerEvent('change', foo)
await nextTick()
expect(data.value).toMatchObject(new Set())
data.value = new Set(['foo'])
await nextTick()
expect(bar.checked).toEqual(false)
expect(foo.checked).toEqual(true)
data.value = new Set(['bar'])
await nextTick()
expect(foo.checked).toEqual(false)
expect(bar.checked).toEqual(true)
data.value = new Set()
await nextTick()
expect(foo.checked).toEqual(false)
expect(bar.checked).toEqual(false)
})
it('should work with radio', async () => {
const component = defineComponent({
data() {