1
1
import type { NodeRef } from '../../src/apiTemplateRef'
2
2
import {
3
+ child ,
3
4
createComponent ,
5
+ createDynamicComponent ,
4
6
createFor ,
5
7
createIf ,
6
8
createSlot ,
7
9
createTemplateRefSetter ,
10
+ defineVaporComponent ,
8
11
insert ,
9
12
renderEffect ,
10
13
template ,
@@ -19,7 +22,8 @@ import {
19
22
useTemplateRef ,
20
23
watchEffect ,
21
24
} from '@vue/runtime-dom'
22
- import { setElementText } from '../../src/dom/prop'
25
+ import { setElementText , setText } from '../../src/dom/prop'
26
+ import type { VaporComponent } from '../../src/component'
23
27
24
28
const define = makeRender ( )
25
29
@@ -676,6 +680,39 @@ describe('api: template ref', () => {
676
680
expect ( r ! . value ) . toBe ( n )
677
681
} )
678
682
683
+ test ( 'work with dynamic component' , async ( ) => {
684
+ const Child = defineVaporComponent ( {
685
+ setup ( _ , { expose } ) {
686
+ const msg = ref ( 'one' )
687
+ expose ( { setMsg : ( m : string ) => ( msg . value = m ) } )
688
+ const n0 = template ( `<div> </div>` ) ( ) as any
689
+ const x0 = child ( n0 ) as any
690
+ renderEffect ( ( ) => setText ( x0 , msg . value ) )
691
+ return n0
692
+ } ,
693
+ } )
694
+
695
+ const views : Record < string , VaporComponent > = { child : Child }
696
+ const view = ref ( 'child' )
697
+ const refKey = ref < any > ( null )
698
+
699
+ const { html } = define ( {
700
+ setup ( ) {
701
+ const setRef = createTemplateRefSetter ( )
702
+ const n0 = createDynamicComponent ( ( ) => views [ view . value ] ) as any
703
+ setRef ( n0 , refKey )
704
+ return n0
705
+ } ,
706
+ } ) . render ( )
707
+
708
+ expect ( html ( ) ) . toBe ( '<div>one</div><!--dynamic-component-->' )
709
+ expect ( refKey . value ) . toBeDefined ( )
710
+
711
+ refKey . value . setMsg ( 'changed' )
712
+ await nextTick ( )
713
+ expect ( html ( ) ) . toBe ( '<div>changed</div><!--dynamic-component-->' )
714
+ } )
715
+
679
716
// TODO: can not reproduce in Vapor
680
717
// // #2078
681
718
// test('handling multiple merged refs', async () => {
0 commit comments