|
1 | 1 | import { createVaporSSRApp, delegateEvents } from '../src'
|
2 |
| -import { nextTick, ref } from '@vue/runtime-dom' |
| 2 | +import { nextTick, reactive, ref } from '@vue/runtime-dom' |
3 | 3 | import { compileScript, parse } from '@vue/compiler-sfc'
|
4 | 4 | import * as runtimeVapor from '../src'
|
5 | 5 | import * as runtimeDom from '@vue/runtime-dom'
|
@@ -50,8 +50,8 @@ function compile(
|
50 | 50 | async function testHydration(
|
51 | 51 | code: string,
|
52 | 52 | components: Record<string, string> = {},
|
| 53 | + data: any = ref('foo'), |
53 | 54 | ) {
|
54 |
| - const data = ref('foo') |
55 | 55 | const ssrComponents: any = {}
|
56 | 56 | const clientComponents: any = {}
|
57 | 57 | for (const key in components) {
|
@@ -638,7 +638,100 @@ describe('Vapor Mode hydration', () => {
|
638 | 638 | )
|
639 | 639 | })
|
640 | 640 |
|
641 |
| - test.todo('if') |
| 641 | + describe('if', () => { |
| 642 | + test('basic toggle - true -> false', asy
8000
nc () => { |
| 643 | + const data = ref(true) |
| 644 | + const { container } = await testHydration( |
| 645 | + `<template> |
| 646 | + <div v-if="data">foo</div> |
| 647 | + </template>`, |
| 648 | + undefined, |
| 649 | + data, |
| 650 | + ) |
| 651 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 652 | + `"<div>foo</div><!--if-->"`, |
| 653 | + ) |
| 654 | + |
| 655 | + data.value = false |
| 656 | + await nextTick() |
| 657 | + expect(container.innerHTML).toMatchInlineSnapshot(`"<!--if-->"`) |
| 658 | + }) |
| 659 | + |
| 660 | + test('basic toggle - false -> true', async () => { |
| 661 | + const data = ref(false) |
| 662 | + const { container } = await testHydration( |
| 663 | + `<template> |
| 664 | + <div v-if="data">foo</div> |
| 665 | + </template>`, |
| 666 | + undefined, |
| 667 | + data, |
| 668 | + ) |
| 669 | + expect(container.innerHTML).toMatchInlineSnapshot(`"<!--if-->"`) |
| 670 | + |
| 671 | + data.value = true |
| 672 | + await nextTick() |
| 673 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 674 | + `"<div>foo</div><!--if-->"`, |
| 675 | + ) |
| 676 | + }) |
| 677 | + |
| 678 | + test('v-if/else-if/else chain - switch branches', async () => { |
| 679 | + const data = ref('a') |
| 680 | + const { container } = await testHydration( |
| 681 | + `<template> |
| 682 | + <div v-if="data === 'a'">foo</div> |
| 683 | + <div v-else-if="data === 'b'">bar</div> |
| 684 | + <div v-else>baz</div> |
| 685 | + </template>`, |
| 686 | + undefined, |
| 687 | + data, |
| 688 | + ) |
| 689 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 690 | + `"<div>foo</div><!--if-->"`, |
| 691 | + ) |
| 692 | + |
| 693 | + data.value = 'b' |
| 694 | + await nextTick() |
| 695 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 696 | + `"<div>bar</div><!--if--><!--if-->"`, |
| 697 | + ) |
| 698 | + |
| 699 | + data.value = 'c' |
| 700 | + await nextTick() |
| 701 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 702 | + `"<div>baz</div><!--if--><!--if-->"`, |
| 703 | + ) |
| 704 | + }) |
| 705 | + |
| 706 | + test('nested if', async () => { |
| 707 | + const data = reactive({ outer: true, inner: true }) |
| 708 | + const { container } = await testHydration( |
| 709 | + `<template> |
| 710 | + <div v-if="data.outer"> |
| 711 | + <span>outer</span> |
| 712 | + <div v-if="data.inner">inner</div> |
| 713 | + </div> |
| 714 | + </template>`, |
| 715 | + undefined, |
| 716 | + data, |
| 717 | + ) |
| 718 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 719 | + `"<div><span>outer</span><div>inner</div><!--if--></div><!--if-->"`, |
| 720 | + ) |
| 721 | + |
| 722 | + data.inner = false |
| 723 | + await nextTick() |
| 724 | + expect(container.innerHTML).toMatchInlineSnapshot( |
| 725 | + `"<div><span>outer</span><!--if--></div><!--if-->"`, |
| 726 | + ) |
| 727 | + |
| 728 | + data.outer = false |
| 729 | + await nextTick() |
| 730 | + expect(container.innerHTML).toMatchInlineSnapshot(`"<!--if-->"`) |
| 731 | + }) |
| 732 | + |
| 733 | + test.todo('on component', async () => {}) |
| 734 | + }) |
642 | 735 |
|
643 | 736 | test.todo('for')
|
644 | 737 |
|
|
0 commit comments