@@ -6,9 +6,13 @@ import {
66import {
77 type VaporDirective ,
88 VaporTeleport ,
9+ child ,
910 createIf ,
1011 createTemplateRefSetter ,
12+ defineVaporComponent ,
13+ renderEffect ,
1114 setInsertionState ,
15+ setText ,
1216 template ,
1317 withVaporDirectives ,
1418} from '@vue/runtime-vapor'
@@ -35,6 +39,86 @@ describe('renderer: VaporTeleport', () => {
3539
3640 describe ( 'defer mode' , ( ) => {
3741 runSharedTests ( true )
42+
43+ test ( 'should be able to target content appearing later than the teleport with defer' , ( ) => {
44+ const root = document . createElement ( 'div' )
45+ document . body . appendChild ( root )
46+
47+ const { mount } = define ( {
48+ setup ( ) {
49+ const n1 = createComp (
50+ VaporTeleport ,
51+ {
52+ to : ( ) => '#target' ,
53+ defer : ( ) => true ,
54+ } ,
55+ {
56+ default : ( ) => template ( '<div>teleported</div>' ) ( ) ,
57+ } ,
58+ )
59+ const n2 = template ( '<div id=target></div>' ) ( )
60+ return [ n1 , n2 ]
61+ } ,
62+ } ) . create ( )
63+ mount ( root )
64+
65+ expect ( root . innerHTML ) . toBe (
66+ '<!--teleport--><div id="target"><div>teleported</div></div>' ,
67+ )
68+ } )
69+
70+ test . todo ( 'defer mode should work inside suspense' , ( ) => { } )
71+
72+ test ( 'update before mounted with defer' , async ( ) => {
73+ const root = document . createElement ( 'div' )
74+ document . body . appendChild ( root )
75+
76+ const show = ref ( false )
77+ const foo = ref ( 'foo' )
78+ const Header = defineVaporComponent ( {
79+ props : { foo : String } ,
80+ setup ( props ) {
81+ const n0 = template ( `<div> </div>` ) ( )
82+ const x0 = child ( n0 as any )
83+ renderEffect ( ( ) => setText ( x0 as any , props . foo ) )
84+ return [ n0 ]
85+ } ,
86+ } )
87+ const Footer = defineVaporComponent ( {
88+ setup ( ) {
89+ foo . value = 'bar'
90+ return template ( '<div>Footer</div>' ) ( )
91+ } ,
92+ } )
93+
94+ const { mount } = define ( {
95+ setup ( ) {
96+ return createIf (
97+ ( ) => show . value ,
98+ ( ) => {
99+ const n1 = createComp (
100+ VaporTeleport ,
101+ { to : ( ) => '#targetId' , defer : ( ) => true } ,
102+ { default : ( ) => createComp ( Header , { foo : ( ) => foo . value } ) } ,
103+ )
104+ const n2 = createComp ( Footer )
105+ const n3 = template ( '<div id="targetId"></div>' ) ( )
106+ return [ n1 , n2 , n3 ]
107+ } ,
108+ ( ) => template ( '<div></div>' ) ( ) ,
109+ )
110+ } ,
111+ } ) . create ( )
112+ mount ( root )
113+
114+ expect ( root . innerHTML ) . toBe ( '<div></div><!--if-->' )
115+
116+ show . value = true
117+ await nextTick ( )
118+ expect ( root . innerHTML ) . toBe (
119+ `<!--teleport--><div>Footer</div><div id="targetId"><div>bar</div></div><!--if-->` ,
120+ )
121+ } )
38122 } )
39123
40124 describe ( 'HMR' , ( ) => {
0 commit comments