@@ -3,29 +3,32 @@ import {
3
3
serializeInner ,
4
4
render ,
5
5
h ,
6
- defineComponent ,
7
6
Portal ,
8
7
Text ,
9
8
ref ,
10
- nextTick ,
11
- TestElement ,
12
- TestNode
9
+ nextTick
13
10
} from '@vue/runtime-test'
14
- import { VNodeArrayChildren , createVNode } from '../../src/vnode'
11
+ import { createVNode } from '../../src/vnode'
15
12
16
13
describe ( 'renderer: portal' , ( ) => {
17
14
test ( 'should work' , ( ) => {
18
15
const target = nodeOps . createElement ( 'div' )
19
16
const root = nodeOps . createElement ( 'div' )
20
17
21
- const Comp = defineComponent ( ( ) => ( ) => [
22
- h ( Portal , { target } , h ( 'div' , 'teleported' ) ) ,
23
- h ( 'div' , 'root' )
24
- ] )
25
- render ( h ( Comp ) , root )
18
+ render (
19
+ h ( ( ) => [
20
+ h ( Portal , { target } , h ( 'div' , 'teleported' ) ) ,
21
+ h ( 'div' , 'root' )
22
+ ] ) ,
23
+ root
24
+ )
26
25
27
- expect ( serializeInner ( root ) ) . toMatchSnapshot ( )
28
- expect ( serializeInner ( target ) ) . toMatchSnapshot ( )
26
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
27
+ `"<!--portal--><div>root</div>"`
28
+ )
29
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
30
+ `"<div>teleported</div>"`
31
+ )
29
32
} )
30
33
31
34
test ( 'should update target' , async ( ) => {
@@ -34,63 +37,143 @@ describe('renderer: portal', () => {
34
37
const target = ref ( targetA )
35
38
const root = nodeOps . createElement ( 'div' )
36
39
37
- const Comp = defineComponent ( ( ) => ( ) => [
38
- h ( Portal , { target : target . value } , h ( 'div' , 'teleported' ) ) ,
39
- h ( 'div' , 'root' )
40
- ] )
41
- render ( h ( Comp ) , root )
40
+ render (
41
+ h ( ( ) => [
42
+ h ( Portal , { target : target . value } , h ( 'div' , 'teleported' ) ) ,
43
+ h ( 'div' , 'root' )
44
+ ] ) ,
45
+ root
46
+ )
42
47
43
- expect ( serializeInner ( root ) ) . toMatchSnapshot ( )
44
- expect ( serializeInner ( targetA ) ) . toMatchSnapshot ( )
45
- expect ( serializeInner ( targetB ) ) . toMatchSnapshot ( )
48
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
49
+ `"<!--portal--><div>root</div>"`
50
+ )
51
+ expect ( serializeInner ( targetA ) ) . toMatchInlineSnapshot (
52
+ `"<div>teleported</div>"`
53
+ )
54
+ expect ( serializeInner ( targetB ) ) . toMatchInlineSnapshot ( `""` )
46
55
47
56
target . value = targetB
48
57
await nextTick ( )
49
58
50
- expect ( serializeInner ( root ) ) . toMatchSnapshot ( )
51
- expect ( serializeInner ( targetA ) ) . toMatchSnapshot ( )
52
- expect ( serializeInner ( targetB ) ) . toMatchSnapshot ( )
59
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
60
+ `"<!--portal--><div>root</div>"`
61
+ )
62
+ expect ( serializeInner ( targetA ) ) . toMatchInlineSnapshot ( `""` )
63
+ expect ( serializeInner ( targetB ) ) . toMatchInlineSnapshot (
64
+ `"<div>teleported</div>"`
65
+ )
53
66
} )
54
67
55
68
test ( 'should update children' , async ( ) => {
56
69
const target = nodeOps . createElement ( 'div' )
57
70
const root = nodeOps . createElement ( 'div' )
58
- const children = ref < VNodeArrayChildren < TestNode , TestElement > > ( [
59
- h ( 'div' , 'teleported' )
60
- ] )
71
+ const children = ref ( [ h ( 'div' , 'teleported' ) ] )
61
72
62
- const Comp = defineComponent ( ( ) => ( ) =>
63
- h ( Portal , { target } , children . value )
73
+ render ( h ( Portal , { target } , children . value ) , root )
74
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
75
+ `"<div>teleported</div>"`
64
76
)
65
- render ( h ( Comp ) , root )
66
-
67
- expect ( serializeInner ( target ) ) . toMatchSnapshot ( )
68
77
69
78
children . value = [ ]
70
79
await nextTick ( )
71
80
72
- expect ( serializeInner ( target ) ) . toMatchSnapshot ( )
81
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
82
+ `"<div>teleported</div>"`
83
+ )
73
84
74
85
children . value = [ createVNode ( Text , null , 'teleported' ) ]
75
86
await nextTick ( )
76
87
77
- expect ( serializeInner ( target ) ) . toMatchSnapshot ( )
88
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
89
+ `"<div>teleported</div>"`
90
+ )
78
91
} )
79
92
80
93
test ( 'should remove children when unmounted' , ( ) => {
81
94
const target = nodeOps . createElement ( 'div' )
82
95
const root = nodeOps . createElement ( 'div' )
83
96
84
- const Comp = defineComponent ( ( ) => ( ) => [
85
- h ( Portal , { target } , h ( 'div' , 'teleported' ) ) ,
86
- h ( 'div' , 'root' )
87
- ] )
88
- render ( h ( Comp ) , root )
97
+ render (
98
+ h ( ( ) => [
99
+ h ( Portal , { target } , h ( 'div' , 'teleported' ) ) ,
100
+ h ( 'div' , 'root' )
101
+ ] ) ,
102
+ root
103
+ )
89
104
expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
90
105
`"<div>teleported</div>"`
91
106
)
92
107
93
108
render ( null , root )
94
109
expect ( serializeInner ( target ) ) . toBe ( '' )
95
110
} )
111
+
112
+ test ( 'multiple portal with same target' , ( ) => {
113
+ const target = nodeOps . createElement ( 'div' )
114
+ const root = nodeOps . createElement ( 'div' )
115
+
116
+ render (
117
+ h ( 'div' , [
118
+ h ( Portal , { target } , h ( 'div' , 'one' ) ) ,
119
+ h ( Portal , { target } , 'two' )
120
+ ] ) ,
121
+ root
122
+ )
123
+
124
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
125
+ `"<div><!--portal--><!--portal--></div>"`
126
+ )
127
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot ( `"<div>one</div>two"` )
128
+
129
+ // update existing content
130
+ render (
131
+ h ( 'div' , [
132
+ h ( Portal , { target } , [ h ( 'div' , 'one' ) , h ( 'div' , 'two' ) ] ) ,
133
+ h ( Portal , { target } , 'three' )
134
+ ] ) ,
135
+ root
136
+ )
137
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
138
+ `"<div>one</div><div>two</div>three"`
139
+ )
140
+
141
+ // toggling
142
+ render ( h ( 'div' , [ null , h ( Portal , { target } , 'three' ) ] ) , root )
143
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
144
+ `"<div><!----><!--portal--></div>"`
145
+ )
146
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot ( `"three"` )
147
+
148
+ // toggle back
149
+ render (
150
+ h ( 'div' , [
151
+ h ( Portal , { target } , [ h ( 'div' , 'one' ) , h ( 'div' , 'two' ) ] ) ,
152
+ h ( Portal , { target } , 'three' )
153
+ ] ) ,
154
+ root
155
+ )
156
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
157
+ `"<div><!--portal--><!--portal--></div>"`
158
+ )
159
+ // should append
160
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
161
+ `"three<div>one</div><div>two</div>"`
162
+ )
163
+
164
+ // toggle the other portal
165
+ render (
166
+ h ( 'div' , [
167
+ h ( Portal , { target } , [ h ( 'div' , 'one' ) , h ( 'div' , 'two' ) ] ) ,
168
+ null
169
+ ] ) ,
170
+ root
171
+ )
172
+ expect ( serializeInner ( root ) ) . toMatchInlineSnapshot (
173
+ `"<div><!--portal--><!----></div>"`
174
+ )
175
+ expect ( serializeInner ( target ) ) . toMatchInlineSnapshot (
176
+ `"<div>one</div><div>two</div>"`
177
+ )
178
+ } )
96
179
} )
0 commit comments