11import {
2+ h ,
23 nextTick ,
34 onActivated ,
45 onBeforeMount ,
89 reactive ,
910 ref ,
1011 shallowRef ,
12+ vModelText ,
13+ withDirectives ,
1114} from 'vue'
1215import type { LooseRawProps , VaporComponent } from '../../src/component'
1316import { makeRender } from '../_utils'
@@ -18,10 +21,12 @@ import {
1821 createDynamicComponent ,
1922 createIf ,
2023 createTemplateRefSetter ,
24+ createVaporApp ,
2125 defineVaporComponent ,
2226 renderEffect ,
2327 setText ,
2428 template ,
29+ vaporInteropPlugin ,
2530} from '../../src'
2631
2732const define = makeRender ( )
@@ -1186,4 +1191,107 @@ describe('VaporKeepAlive', () => {
11861191 expect ( deactivatedHome ) . toHaveBeenCalledTimes ( 0 )
11871192 expect ( unmountedHome ) . toHaveBeenCalledTimes ( 1 )
11881193 } )
1194+
1195+ describe ( 'vdom interop' , ( ) => {
1196+ test ( 'render vdom component' , async ( ) => {
1197+ const VdomComp = {
1198+ setup ( ) {
1199+ const msg = ref ( 'vdom' )
1200+ onBeforeMount ( ( ) => oneHooks . beforeMount ( ) )
1201+ onMounted ( ( ) => oneHooks . mounted ( ) )
1202+ onActivated ( ( ) => oneHooks . activated ( ) )
1203+ onDeactivated ( ( ) => oneHooks . deactivated ( ) )
1204+ onUnmounted ( ( ) => oneHooks . unmounted ( ) )
1205+ return ( ) => {
1206+ return withDirectives (
1207+ h (
1208+ 'input' ,
1209+ {
1210+ type : 'text' ,
1211+ 'onUpdate:modelValue' : ( $event : any ) => ( msg . value = $event ) ,
1212+ } ,
1213+ [ ] ,
1214+ ) ,
1215+ [ [ vModelText , msg . value ] ] ,
1216+ )
1217+ }
1218+ } ,
1219+ }
1220+
1221+ const show = ref ( true )
1222+ const toggle = ref ( true )
1223+
1224+ const App = defineVaporComponent ( {
1225+ setup ( ) {
1226+ const n0 = createIf (
1227+ ( ) => show . value ,
1228+ ( ) => {
1229+ const n5 = createComponent (
1230+ VaporKeepAlive ,
1231+ null ,
1232+ {
1233+ default : ( ) => {
1234+ const n2 = createIf (
1235+ ( ) => toggle . value ,
1236+ ( ) => {
1237+ const n4 = createComponent ( VdomComp )
1238+ return n4
1239+ } ,
1240+ )
1241+ return n2
1242+ } ,
1243+ } ,
1244+ true ,
1245+ )
1246+ return n5
1247+ } ,
1248+ )
1249+ return n0
1250+ } ,
1251+ } )
1252+
1253+ const container = document . createElement ( 'div' )
1254+ document . body . appendChild ( container )
1255+ const app = createVaporApp ( App )
1256+ app . use ( vaporInteropPlugin )
1257+ app . mount ( container )
1258+
1259+ expect ( container . innerHTML ) . toBe ( `<input type="text"><!--if--><!--if-->` )
1260+ assertHookCalls ( oneHooks , [ 1 , 1 , 1 , 0 , 0 ] )
1261+
1262+ let inputEl = container . firstChild as HTMLInputElement
1263+ expect ( inputEl . value ) . toBe ( 'vdom' )
1264+
1265+ inputEl . value = 'changed'
1266+ inputEl . dispatchEvent ( new Event ( 'input' ) )
1267+ await nextTick ( )
1268+
1269+ // deactivate
1270+ toggle . value = false
1271+ await nextTick ( )
1272+ expect ( container . innerHTML ) . toBe ( `<!--if--><!--if-->` )
1273+ assertHookCalls ( oneHooks , [ 1 , 1 , 1 , 1 , 0 ] )
1274+
1275+ // activate
1276+ toggle . value = true
1277+ await nextTick ( )
1278+ expect ( container . innerHTML ) . toBe ( `<input type="text"><!--if--><!--if-->` )
1279+ assertHookCalls ( oneHooks , [ 1 , 1 , 2 , 1 , 0 ] )
1280+ expect ( inputEl . value ) . toBe ( 'changed' )
1281+
1282+ // unmount keepalive
1283+ show . value = false
1284+ await nextTick ( )
1285+ expect ( container . innerHTML ) . toBe ( `<!--if-->` )
1286+ assertHookCalls ( oneHooks , [ 1 , 1 , 2 , 2 , 1 ] )
1287+
1288+ // mount keepalive
1289+ show . value = true
1290+ await nextTick ( )
1291+ expect ( container . innerHTML ) . toBe ( `<input type="text"><!--if--><!--if-->` )
1292+ assertHookCalls ( oneHooks , [ 2 , 2 , 3 , 2 , 1 ] )
1293+ inputEl = container . firstChild as HTMLInputElement
1294+ expect ( inputEl . value ) . toBe ( 'vdom' )
1295+ } )
1296+ } )
11891297} )
0 commit comments