|
6 | 6 | vModelDynamic,
|
7 | 7 | withDirectives,
|
8 | 8 | VNode,
|
9 |
| - ref |
| 9 | + ref, |
| 10 | + reactive |
10 | 11 | } from '@vue/runtime-dom'
|
11 | 12 |
|
12 | 13 | const triggerEvent = (type: string, el: Element) => {
|
@@ -433,6 +434,78 @@ describe('vModel', () => {
|
433 | 434 | expect(bar.checked).toEqual(false)
|
434 | 435 | })
|
435 | 436 |
|
| 437 | + it(`should support the reactive array in setup as a checkbox model`, async () => { |
| 438 | + const value = reactive<string[]>([]) |
| 439 | + |
| 440 | + const component = defineComponent({ |
| 441 | + setup() { |
| 442 | + return () => { |
| 443 | + return [ |
| 444 | + withVModel( |
| 445 | + h('input', { |
| 446 | + type: 'checkbox', |
| 447 | + class: 'foo', |
| 448 | + value: 'foo', |
| 449 | + 'onUpdate:modelValue': setValue.bind(this) |
| 450 | + }), |
| 451 | + value |
| 452 | + ), |
| 453 | + withVModel( |
| 454 | + h('input', { |
| 455 | + type: 'checkbox', |
| 456 | + class: 'bar', |
| 457 | + value: 'bar', |
| 458 | + 'onUpdate:modelValue': setValue.bind(this) |
| 459 | + }), |
| 460 | + value |
| 461 | + ) |
| 462 | + ] |
| 463 | + } |
| 464 | + } |
| 465 | + }) |
| 466 | + render(h(component), root) |
| 467 | + |
| 468 | + const foo = root.querySelector('.foo') |
| 469 | + const bar = root.querySelector('.bar') |
| 470 | + |
| 471 | + foo.checked = true |
| 472 | + triggerEvent('change', foo) |
| 473 | + await nextTick() |
| 474 | + expect(value).toMatchObject(['foo']) |
| 475 | + |
| 476 | + bar.checked = true |
| 477 | + triggerEvent('change', bar) |
| 478 | + await nextTick() |
| 479 | + expect(value).toMatchObject(['foo', 'bar']) |
| 480 | + |
| 481 | + bar.checked = false |
| 482 | + triggerEvent('change', bar) |
| 483 | + await nextTick() |
| 484 | + expect(value).toMatchObject(['foo']) |
| 485 | + |
| 486 | + foo.checked = false |
| 487 | + triggerEvent('change', foo) |
| 488 | + await nextTick() |
| 489 | + expect(value).toMatchObject([]) |
| 490 | + |
| 491 | + value.length = 0 |
| 492 | + value.push('foo') |
| 493 | + await nextTick() |
| 494 | + expect(bar.checked).toEqual(false) |
| 495 | + expect(foo.checked).toEqual(true) |
| 496 | + |
| 497 | + value.length = 0 |
| 498 | + value.push('bar') |
| 499 | + await nextTick() |
| 500 | + expect(foo.checked).toEqual(false) |
| 501 | + expect(bar.checked).toEqual(true) |
| 502 | + |
| 503 | + value.length = 0 |
| 504 | + await nextTick() |
| 505 | + expect(foo.checked).toEqual(false) |
| 506 | + expect(bar.checked).toEqual(false) |
| 507 | + }) |
| 508 | + |
436 | 509 | it(`should support Set as a checkbox model`, async () => {
|
437 | 510 | const component = defineComponent({
|
438 | 511 | data() {
|
|
0 commit comments