vdomInterop.spec.ts 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  1. import {
  2. KeepAlive,
  3. type ShallowRef,
  4. Suspense,
  5. Teleport,
  6. createApp,
  7. createCommentVNode,
  8. createVNode,
  9. defineComponent,
  10. h,
  11. inject,
  12. nextTick,
  13. onActivated,
  14. onBeforeMount,
  15. onBeforeUpdate,
  16. onDeactivated,
  17. onMounted,
  18. onUnmounted,
  19. onUpdated,
  20. provide,
  21. ref,
  22. renderSlot,
  23. resolveDynamicComponent,
  24. shallowRef,
  25. toDisplayString,
  26. useModel,
  27. useTemplateRef,
  28. vShow,
  29. withDirectives,
  30. } from '@vue/runtime-dom'
  31. import { makeInteropRender } from './_utils'
  32. import {
  33. VaporKeepAlive,
  34. VaporTeleport,
  35. applyTextModel,
  36. applyVShow,
  37. child,
  38. createComponent,
  39. createDynamicComponent,
  40. createForSlots,
  41. createIf,
  42. createSlot,
  43. createTemplateRefSetter,
  44. defineVaporAsyncComponent,
  45. defineVaporComponent,
  46. insert,
  47. renderEffect,
  48. setText,
  49. template,
  50. txt,
  51. vaporInteropPlugin,
  52. withVaporCtx,
  53. } from '../src'
  54. const define = makeInteropRender()
  55. describe('vdomInterop', () => {
  56. describe('key', () => {
  57. test('preserves vnode key on blocks passed from vdom to vapor', () => {
  58. const VDomChild = defineComponent({
  59. setup() {
  60. return () => h('div', 'vdom child')
  61. },
  62. })
  63. const app = createApp({ render: () => null })
  64. app.use(vaporInteropPlugin)
  65. const vapor = (app._context as any).vapor
  66. const vnodeBlock = vapor.vdomMountVNode(
  67. h(VDomChild, { key: 'foo' }),
  68. null,
  69. )
  70. expect(vnodeBlock.$key).toBe('foo')
  71. expect(vnodeBlock.vnode.key).toBe('foo')
  72. const componentBlock = vapor.vdomMount(VDomChild, null, { key: 'bar' })
  73. expect(componentBlock.$key).toBe('bar')
  74. expect(componentBlock.vnode.key).toBe('bar')
  75. })
  76. test('preserves single slot vnode key on interop fragments', async () => {
  77. const key = ref('foo')
  78. const CompA = defineComponent({
  79. setup() {
  80. return () => h('div', 'A')
  81. },
  82. })
  83. const CompB = defineComponent({
  84. setup() {
  85. return () => h('div', 'B')
  86. },
  87. })
  88. const current = shallowRef<any>(CompA)
  89. const VaporChild = defineVaporComponent({
  90. setup() {
  91. return createSlot('default') as any
  92. },
  93. })
  94. const Parent = defineComponent({
  95. setup() {
  96. return () =>
  97. h(VaporChild as any, null, {
  98. default: () => [h(current.value, { key: key.value })],
  99. })
  100. },
  101. })
  102. const app = createApp(Parent)
  103. app.use(vaporInteropPlugin)
  104. const vapor = (app._context as any).vapor
  105. const originalVdomSlot = vapor.vdomSlot
  106. let frag: any
  107. vapor.vdomSlot = (...args: any[]) => (frag = originalVdomSlot(...args))
  108. const host = document.createElement('div')
  109. app.mount(host)
  110. expect(frag.$key).toBe('_defaultfoo')
  111. key.value = 'bar'
  112. current.value = CompB
  113. await nextTick()
  114. expect(frag.$key).toBe('_defaultbar')
  115. })
  116. })
  117. describe('fragment nodes', () => {
  118. test('refreshes interop fragment nodes after component root updates', async () => {
  119. const show = ref(false)
  120. const VDomChild = defineComponent({
  121. setup() {
  122. return () =>
  123. show.value ? h('div', 'child') : createCommentVNode('v-if', true)
  124. },
  125. })
  126. const app = createApp({ render: () => null })
  127. app.use(vaporInteropPlugin)
  128. const vapor = (app._context as any).vapor
  129. const host = document.createElement('div')
  130. const frag = vapor.vdomMount(VDomChild, null)
  131. insert(frag, host)
  132. expect(host.innerHTML).toBe('<!--v-if-->')
  133. expect(frag.nodes).toBeInstanceOf(Comment)
  134. show.value = true
  135. await nextTick()
  136. expect(host.innerHTML).toBe('<div>child</div>')
  137. expect(frag.nodes).toBeInstanceOf(HTMLDivElement)
  138. })
  139. test('refreshes vdom slot fragment nodes after child root updates', async () => {
  140. const show = ref(false)
  141. const VDomChild = defineComponent({
  142. setup() {
  143. return () =>
  144. show.value ? h('div', 'child') : createCommentVNode('v-if', true)
  145. },
  146. })
  147. const VaporChild = defineVaporComponent({
  148. setup() {
  149. return createSlot('default') as any
  150. },
  151. })
  152. const Parent = defineComponent({
  153. setup() {
  154. return () =>
  155. h(VaporChild as any, null, {
  156. default: () => [h(VDomChild)],
  157. })
  158. },
  159. })
  160. const app = createApp(Parent)
  161. app.use(vaporInteropPlugin)
  162. const vapor = (app._context as any).vapor
  163. const originalVdomSlot = vapor.vdomSlot
  164. let frag: any
  165. vapor.vdomSlot = (...args: any[]) => (frag = originalVdomSlot(...args))
  166. const host = document.createElement('div')
  167. app.mount(host)
  168. const onUpdated = vi.fn()
  169. frag.onUpdated = [onUpdated]
  170. const getNodes = () =>
  171. (Array.isArray(frag.nodes) ? frag.nodes : [frag.nodes]).filter(Boolean)
  172. expect(host.innerHTML).toBe('<!--v-if-->')
  173. expect(getNodes().some((n: Node) => n instanceof HTMLDivElement)).toBe(
  174. false,
  175. )
  176. show.value = true
  177. await nextTick()
  178. expect(host.innerHTML).toContain('<div>child</div>')
  179. expect(getNodes().some((n: Node) => n instanceof HTMLDivElement)).toBe(
  180. true,
  181. )
  182. expect(onUpdated).toHaveBeenCalled()
  183. })
  184. })
  185. describe('props', () => {
  186. test('should work if props are not provided', () => {
  187. const VaporChild = defineVaporComponent({
  188. props: {
  189. msg: String,
  190. },
  191. setup(_, { attrs }) {
  192. return [document.createTextNode(attrs.class || 'foo')]
  193. },
  194. })
  195. const { html } = define({
  196. setup() {
  197. return () => h(VaporChild as any)
  198. },
  199. }).render()
  200. expect(html()).toBe('foo')
  201. })
  202. test('should handle class prop when vapor renders vdom component', () => {
  203. const VDomChild = defineComponent({
  204. setup() {
  205. return () => h('div', { class: 'foo' })
  206. },
  207. })
  208. const VaporChild = defineVaporComponent({
  209. setup() {
  210. return createComponent(VDomChild as any, { class: () => 'bar' })
  211. },
  212. })
  213. const { html } = define({
  214. setup() {
  215. return () => h(VaporChild as any)
  216. },
  217. }).render()
  218. expect(html()).toBe('<div class="foo bar"></div>')
  219. })
  220. test('should not pass reserved props into vapor attrs on update', async () => {
  221. const msg = ref('foo')
  222. const onVnodeMounted = vi.fn()
  223. const VaporChild = defineVaporComponent({
  224. setup(_, { attrs }) {
  225. const n0 = template(' ')() as any
  226. renderEffect(() => {
  227. setText(
  228. n0,
  229. `${String(attrs.msg)}|${String('onVnodeMounted' in attrs)}`,
  230. )
  231. })
  232. return n0
  233. },
  234. })
  235. const { html } = define({
  236. setup() {
  237. return () =>
  238. h(VaporChild as any, {
  239. msg: msg.value,
  240. onVnodeMounted,
  241. })
  242. },
  243. }).render()
  244. expect(html()).toBe('foo|false')
  245. msg.value = 'bar'
  246. await nextTick()
  247. expect(html()).toBe('bar|false')
  248. })
  249. test('should invoke onVnodeMounted and onVnodeUnmounted', async () => {
  250. const VaporChild = defineVaporComponent({
  251. setup() {
  252. return template('<div>vapor</div>')()
  253. },
  254. })
  255. const show = ref(true)
  256. const vnodeMounted = vi.fn()
  257. const vnodeUnmounted = vi.fn()
  258. const { html } = define({
  259. setup() {
  260. return () =>
  261. show.value
  262. ? h(VaporChild as any, {
  263. onVnodeMounted: vnodeMounted,
  264. onVnodeUnmounted: vnodeUnmounted,
  265. })
  266. : null
  267. },
  268. }).render()
  269. await nextTick()
  270. expect(html()).toBe('<div>vapor</div>')
  271. expect(vnodeMounted).toHaveBeenCalledTimes(1)
  272. expect(vnodeUnmounted).toHaveBeenCalledTimes(0)
  273. show.value = false
  274. await nextTick()
  275. expect(vnodeUnmounted).toHaveBeenCalledTimes(1)
  276. })
  277. test('should invoke vnode and directive mount hooks in VDOM order', async () => {
  278. const calls: string[] = []
  279. const vCustom = {
  280. created: vi.fn(() => calls.push('directive created')),
  281. beforeMount: vi.fn(() => calls.push('directive beforeMount')),
  282. mounted: vi.fn(() => calls.push('directive mounted')),
  283. }
  284. const VaporChild = defineVaporComponent({
  285. setup() {
  286. return template('<div>vapor</div>')()
  287. },
  288. })
  289. const App = defineComponent({
  290. setup() {
  291. return () =>
  292. withDirectives(
  293. h(VaporChild as any, {
  294. onVnodeBeforeMount: () => calls.push('vnode beforeMount'),
  295. onVnodeMounted: () => calls.push('vnode mounted'),
  296. }),
  297. [[vCustom]],
  298. )
  299. },
  300. })
  301. const root = document.createElement('div')
  302. const app = createApp(App)
  303. app.use(vaporInteropPlugin)
  304. app.mount(root)
  305. await nextTick()
  306. expect(calls).toEqual([
  307. 'vnode beforeMount',
  308. 'directive created',
  309. 'directive beforeMount',
  310. 'directive mounted',
  311. 'vnode mounted',
  312. ])
  313. })
  314. test('should invoke vnode and directive unmount hooks in VDOM order', async () => {
  315. const calls: string[] = []
  316. const vCustom = {
  317. beforeUnmount: vi.fn(() => calls.push('directive beforeUnmount')),
  318. unmounted: vi.fn(() => calls.push('directive unmounted')),
  319. }
  320. const VaporChild = defineVaporComponent({
  321. setup() {
  322. return template('<div>vapor</div>')()
  323. },
  324. })
  325. const show = ref(true)
  326. const { html } = define({
  327. setup() {
  328. return () =>
  329. show.value
  330. ? withDirectives(
  331. h(VaporChild as any, {
  332. onVnodeBeforeUnmount: () =>
  333. calls.push('vnode beforeUnmount'),
  334. onVnodeUnmounted: () => calls.push('vnode unmounted'),
  335. }),
  336. [[vCustom]],
  337. )
  338. : null
  339. },
  340. }).render()
  341. expect(html()).toBe('<div>vapor</div>')
  342. show.value = false
  343. await nextTick()
  344. expect(calls).toEqual([
  345. 'vnode beforeUnmount',
  346. 'directive beforeUnmount',
  347. 'directive unmounted',
  348. 'vnode unmounted',
  349. ])
  350. })
  351. test('should invoke update hooks in VDOM order on normal updates', async () => {
  352. const msg = ref('foo')
  353. const calls: string[] = []
  354. const vCustom = {
  355. beforeUpdate: vi.fn(() => calls.push('directive beforeUpdate')),
  356. updated: vi.fn(() => calls.push('directive updated')),
  357. }
  358. const VaporChild = defineVaporComponent({
  359. props: {
  360. msg: String,
  361. },
  362. setup(props: any) {
  363. const n0 = template('<div> </div>', true)() as any
  364. const x0 = child(n0) as any
  365. renderEffect(() => {
  366. setText(x0, props.msg)
  367. })
  368. return n0
  369. },
  370. })
  371. const App = defineComponent({
  372. setup() {
  373. return () =>
  374. withDirectives(
  375. h(VaporChild as any, {
  376. msg: msg.value,
  377. onVnodeBeforeUpdate: () => calls.push('vnode beforeUpdate'),
  378. onVnodeUpdated: () => calls.push('vnode updated'),
  379. }),
  380. [[vCustom]],
  381. )
  382. },
  383. })
  384. const root = document.createElement('div')
  385. const app = createApp(App)
  386. app.use(vaporInteropPlugin)
  387. app.mount(root)
  388. msg.value = 'bar'
  389. await nextTick()
  390. expect(calls).toEqual([
  391. 'vnode beforeUpdate',
  392. 'directive beforeUpdate',
  393. 'directive updated',
  394. 'vnode updated',
  395. ])
  396. })
  397. })
  398. describe('v-model', () => {
  399. test('basic work', async () => {
  400. const VaporChild = defineVaporComponent({
  401. props: {
  402. modelValue: {},
  403. modelModifiers: {},
  404. },
  405. emits: ['update:modelValue'],
  406. setup(__props) {
  407. const modelValue = useModel(__props, 'modelValue')
  408. const n0 = template('<h1> </h1>')() as any
  409. const n1 = template('<input>')() as any
  410. const x0 = child(n0) as any
  411. applyTextModel(
  412. n1,
  413. () => modelValue.value,
  414. _value => (modelValue.value = _value),
  415. )
  416. renderEffect(() => setText(x0, toDisplayString(modelValue.value)))
  417. return [n0, n1]
  418. },
  419. })
  420. const { html, host } = define({
  421. setup() {
  422. const msg = ref('foo')
  423. return () =>
  424. h(VaporChild as any, {
  425. modelValue: msg.value,
  426. 'onUpdate:modelValue': (value: string) => {
  427. msg.value = value
  428. },
  429. })
  430. },
  431. }).render()
  432. expect(html()).toBe('<h1>foo</h1><input>')
  433. const inputEl = host.querySelector('input')!
  434. inputEl.value = 'bar'
  435. inputEl.dispatchEvent(new Event('input'))
  436. await nextTick()
  437. expect(html()).toBe('<h1>bar</h1><input>')
  438. })
  439. test('slot v-model should persist when switching vapor/vdom child', async () => {
  440. const VaporComp1 = defineVaporComponent({
  441. name: 'VaporComp1',
  442. setup() {
  443. return [document.createTextNode('comp1: '), createSlot('default')]
  444. },
  445. })
  446. const VDomComp2 = defineComponent({
  447. name: 'VDomComp2',
  448. setup(_, { slots }) {
  449. return () =>
  450. h('div', [
  451. 'comp2: ',
  452. // vdom <slot/>
  453. renderSlot(slots, 'default'),
  454. ])
  455. },
  456. })
  457. const VaporParent = defineVaporComponent({
  458. name: 'VaporParent',
  459. props: {
  460. show: Boolean,
  461. modelValue: {},
  462. modelModifiers: {},
  463. },
  464. emits: ['update:modelValue'],
  465. setup(__props) {
  466. const modelValue = useModel(__props, 'modelValue')
  467. return createDynamicComponent(
  468. () => (__props.show ? VaporComp1 : VDomComp2),
  469. null,
  470. {
  471. default: () => {
  472. const input = template('<input>')() as any
  473. applyTextModel(
  474. input,
  475. () => modelValue.value,
  476. _value => (modelValue.value = _value),
  477. )
  478. return input
  479. },
  480. },
  481. true,
  482. )
  483. },
  484. })
  485. const show = ref(true)
  486. const msg = ref('')
  487. const { host } = define({
  488. setup() {
  489. return () =>
  490. h(VaporParent as any, {
  491. show: show.value,
  492. modelValue: msg.value,
  493. 'onUpdate:modelValue': (value: string) => {
  494. msg.value = value
  495. },
  496. })
  497. },
  498. }).render()
  499. const input1 = host.querySelector('input')!
  500. input1.value = 'hello'
  501. input1.dispatchEvent(new Event('input'))
  502. await nextTick()
  503. expect(msg.value).toBe('hello')
  504. show.value = false
  505. await nextTick()
  506. const input2 = host.querySelector('input')!
  507. expect(input2.value).toBe('hello')
  508. })
  509. })
  510. describe('emit', () => {
  511. test('emit from vapor child to vdom parent', () => {
  512. const VaporChild = defineVaporComponent({
  513. emits: ['click'],
  514. setup(_, { emit }) {
  515. emit('click')
  516. return []
  517. },
  518. })
  519. const fn = vi.fn()
  520. define({
  521. setup() {
  522. return () => h(VaporChild as any, { onClick: fn })
  523. },
  524. }).render()
  525. // fn should be called once
  526. expect(fn).toHaveBeenCalledTimes(1)
  527. })
  528. })
  529. describe('directives', () => {
  530. test('apply v-show to vdom child', async () => {
  531. const VDomChild = {
  532. setup() {
  533. return () => h('div')
  534. },
  535. }
  536. const show = ref(false)
  537. const VaporChild = defineVaporComponent({
  538. setup() {
  539. const n1 = createComponent(VDomChild as any)
  540. applyVShow(n1, () => show.value)
  541. return n1
  542. },
  543. })
  544. const { html } = define({
  545. setup() {
  546. return () => h(VaporChild as any)
  547. },
  548. }).render()
  549. expect(html()).toBe('<div style="display: none;"></div>')
  550. show.value = true
  551. await nextTick()
  552. expect(html()).toBe('<div style=""></div>')
  553. })
  554. test('apply v-show to vapor child', async () => {
  555. const VaporChild = defineVaporComponent({
  556. setup() {
  557. return template('<div></div>', true)()
  558. },
  559. })
  560. const show = ref(false)
  561. const App = defineComponent({
  562. setup() {
  563. return () =>
  564. h('div', null, [
  565. withDirectives(h(VaporChild as any), [[vShow, show.value]]),
  566. ])
  567. },
  568. })
  569. const root = document.createElement('div')
  570. const app = createApp(App)
  571. app.use(vaporInteropPlugin)
  572. app.mount(root)
  573. expect(root.innerHTML).toBe(
  574. '<div><div style="display: none;"></div></div>',
  575. )
  576. show.value = true
  577. await nextTick()
  578. expect(root.innerHTML).toBe('<div><div style=""></div></div>')
  579. })
  580. test('apply custom directive to vapor child', async () => {
  581. const vCustom = {
  582. created: vi.fn(),
  583. beforeMount: vi.fn(),
  584. mounted: vi.fn(),
  585. beforeUpdate: vi.fn(),
  586. updated: vi.fn(),
  587. beforeUnmount: vi.fn(),
  588. unmounted: vi.fn(),
  589. }
  590. const VaporChild = defineVaporComponent({
  591. setup() {
  592. return template('<div></div>', true)()
  593. },
  594. })
  595. const count = ref(0)
  596. const App = defineComponent({
  597. setup() {
  598. return () =>
  599. h('div', null, [
  600. withDirectives(h(VaporChild as any), [[vCustom, count.value]]),
  601. ])
  602. },
  603. })
  604. const root = document.createElement('div')
  605. const app = createApp(App)
  606. app.use(vaporInteropPlugin)
  607. app.mount(root)
  608. // root > div (App root) > div (VaporChild root)
  609. const el = root.querySelector('div')!.querySelector('div')!
  610. expect(vCustom.created).toHaveBeenCalledTimes(1)
  611. expect(vCustom.beforeMount).toHaveBeenCalledTimes(1)
  612. expect(vCustom.mounted).toHaveBeenCalledTimes(1)
  613. expect(vCustom.beforeUpdate).toHaveBeenCalledTimes(0)
  614. expect(vCustom.updated).toHaveBeenCalledTimes(0)
  615. expect(vCustom.created).toHaveBeenCalledWith(
  616. el,
  617. expect.objectContaining({ value: 0, oldValue: undefined }),
  618. expect.any(Object),
  619. null,
  620. )
  621. expect(vCustom.beforeMount).toHaveBeenCalledWith(
  622. el,
  623. expect.objectContaining({ value: 0, oldValue: undefined }),
  624. expect.any(Object),
  625. null,
  626. )
  627. expect(vCustom.mounted).toHaveBeenCalledWith(
  628. el,
  629. expect.objectContaining({ value: 0, oldValue: undefined }),
  630. expect.any(Object),
  631. null,
  632. )
  633. count.value++
  634. await nextTick()
  635. expect(vCustom.beforeUpdate).toHaveBeenCalledTimes(1)
  636. expect(vCustom.updated).toHaveBeenCalledTimes(1)
  637. expect(vCustom.beforeUpdate).toHaveBeenCalledWith(
  638. el,
  639. expect.objectContaining({ value: 1, oldValue: 0 }),
  640. expect.any(Object),
  641. expect.any(Object),
  642. )
  643. expect(vCustom.updated).toHaveBeenCalledWith(
  644. el,
  645. expect.objectContaining({ value: 1, oldValue: 0 }),
  646. expect.any(Object),
  647. expect.any(Object),
  648. )
  649. app.unmount()
  650. expect(vCustom.beforeUnmount).toHaveBeenCalledTimes(1)
  651. expect(vCustom.unmounted).toHaveBeenCalledTimes(1)
  652. expect(vCustom.beforeUnmount).toHaveBeenCalledWith(
  653. el,
  654. expect.objectContaining({ value: 1, oldValue: 0 }),
  655. expect.any(Object),
  656. null,
  657. )
  658. expect(vCustom.unmounted).toHaveBeenCalledWith(
  659. el,
  660. expect.objectContaining({ value: 1, oldValue: 0 }),
  661. expect.any(Object),
  662. null,
  663. )
  664. })
  665. test('warn on directive with non-element root vapor child', () => {
  666. const calls: string[] = []
  667. const vCustom = {
  668. created: () => calls.push('created'),
  669. beforeMount: () => calls.push('beforeMount'),
  670. mounted: () => calls.push('mounted'),
  671. beforeUpdate: () => calls.push('beforeUpdate'),
  672. updated: () => calls.push('updated'),
  673. beforeUnmount: () => calls.push('beforeUnmount'),
  674. unmounted: () => calls.push('unmounted'),
  675. }
  676. const VaporChild = defineVaporComponent({
  677. setup() {
  678. return [template('<div></div>')(), template('<div></div>')()]
  679. },
  680. })
  681. const App = defineComponent({
  682. setup() {
  683. return () =>
  684. h('div', null, [withDirectives(h(VaporChild as any), [[vCustom]])])
  685. },
  686. })
  687. const root = document.createElement('div')
  688. const app = createApp(App)
  689. app.use(vaporInteropPlugin)
  690. app.mount(root)
  691. if (__DEV__) {
  692. expect(
  693. `Runtime directive used on component with non-element root node.`,
  694. ).toHaveBeenWarned()
  695. }
  696. expect(calls.length).toBe(0)
  697. app.unmount()
  698. })
  699. test('should expose the latest vapor root element in updated hooks', async () => {
  700. const useAltRoot = ref(false)
  701. const updatedSpy = vi.fn((vnode: any) => {
  702. expect((vnode.el as Element).tagName).toBe('P')
  703. })
  704. const VaporChild = defineVaporComponent({
  705. props: {
  706. alt: Boolean,
  707. },
  708. setup(props: any) {
  709. return createIf(
  710. () => props.alt,
  711. () => template('<p>alt</p>')(),
  712. () => template('<div>base</div>')(),
  713. )
  714. },
  715. })
  716. const App = defineComponent({
  717. setup() {
  718. return () =>
  719. h(VaporChild as any, {
  720. alt: useAltRoot.value,
  721. onVnodeUpdated: updatedSpy,
  722. })
  723. },
  724. })
  725. const root = document.createElement('div')
  726. const app = createApp(App)
  727. app.use(vaporInteropPlugin)
  728. app.mount(root)
  729. useAltRoot.value = true
  730. await nextTick()
  731. expect(root.querySelector('p')).not.toBeNull()
  732. expect(updatedSpy).toHaveBeenCalledTimes(1)
  733. })
  734. })
  735. describe('slots', () => {
  736. test('basic', () => {
  737. const VDomChild = defineComponent({
  738. setup(_, { slots }) {
  739. return () => renderSlot(slots, 'default')
  740. },
  741. })
  742. const VaporChild = defineVaporComponent({
  743. setup() {
  744. return createComponent(
  745. VDomChild as any,
  746. null,
  747. {
  748. default: () => document.createTextNode('default slot'),
  749. },
  750. true,
  751. )
  752. },
  753. })
  754. const { html } = define({
  755. setup() {
  756. return () => h(VaporChild as any)
  757. },
  758. }).render()
  759. expect(html()).toBe('default slot')
  760. })
  761. test('functional slot', () => {
  762. const VDomChild = defineComponent({
  763. setup(_, { slots }) {
  764. return () => createVNode(slots.default!)
  765. },
  766. })
  767. const VaporChild = defineVaporComponent({
  768. setup() {
  769. return createComponent(
  770. VDomChild as any,
  771. null,
  772. {
  773. default: () => document.createTextNode('default slot'),
  774. },
  775. true,
  776. )
  777. },
  778. })
  779. const { html } = define({
  780. setup() {
  781. return () => h(VaporChild as any)
  782. },
  783. }).render()
  784. expect(html()).toBe('default slot')
  785. })
  786. test('slots.default() direct invocation', () => {
  787. const VDomChild = defineComponent({
  788. setup(_, { slots }) {
  789. return () => h('div', null, slots.default!())
  790. },
  791. })
  792. const VaporChild = defineVaporComponent({
  793. setup() {
  794. return createComponent(
  795. VDomChild as any,
  796. null,
  797. {
  798. default: () => template('direct call slot')(),
  799. },
  800. true,
  801. )
  802. },
  803. })
  804. const { html } = define({
  805. setup() {
  806. return () => h(VaporChild as any)
  807. },
  808. }).render()
  809. expect(html()).toBe('<div>direct call slot</div>')
  810. })
  811. test('slots.default() access should return a stable wrapper', () => {
  812. const VDomChild = defineComponent({
  813. setup(_, { slots }) {
  814. const first = slots.default
  815. const second = slots.default
  816. return () => h('div', String(first === second))
  817. },
  818. })
  819. const VaporChild = defineVaporComponent({
  820. setup() {
  821. return createComponent(
  822. VDomChild as any,
  823. null,
  824. {
  825. default: () => template('stable slot wrapper')(),
  826. },
  827. true,
  828. )
  829. },
  830. })
  831. const { html } = define({
  832. setup() {
  833. return () => h(VaporChild as any)
  834. },
  835. }).render()
  836. expect(html()).toBe('<div>true</div>')
  837. })
  838. test('slots.default() with slot props', () => {
  839. const VDomChild = defineComponent({
  840. setup(_, { slots }) {
  841. return () => h('div', null, slots.default!({ msg: 'hello' }))
  842. },
  843. })
  844. const VaporChild = defineVaporComponent({
  845. setup() {
  846. return createComponent(
  847. VDomChild as any,
  848. null,
  849. {
  850. default: (props: { msg: string }) => {
  851. const n0 = template('<span></span>')()
  852. n0.textContent = props.msg
  853. return [n0]
  854. },
  855. },
  856. true,
  857. )
  858. },
  859. })
  860. const { html } = define({
  861. setup() {
  862. return () => h(VaporChild as any)
  863. },
  864. }).render()
  865. expect(html()).toBe('<div><span>hello</span></div>')
  866. })
  867. test('slots.default() with falsy slot props should keep has/ownKeys semantics', () => {
  868. const VDomChild = defineComponent({
  869. setup(_, { slots }) {
  870. return () =>
  871. h('div', null, slots.default!({ flag: false, count: 0, text: '' }))
  872. },
  873. })
  874. const VaporChild = defineVaporComponent({
  875. setup() {
  876. return createComponent(
  877. VDomChild as any,
  878. null,
  879. {
  880. default: (props: Record<string, any>) => {
  881. const n0 = document.createTextNode(
  882. `${'flag' in props}/${'count' in props}/${'text' in props}|` +
  883. `${Object.keys(props).join(',')}|` +
  884. `${String(props.flag)},${String(props.count)},${String(props.text)}`,
  885. )
  886. return [n0]
  887. },
  888. },
  889. true,
  890. )
  891. },
  892. })
  893. const { html } = define({
  894. setup() {
  895. return () => h(VaporChild as any)
  896. },
  897. }).render()
  898. expect(html()).toBe('<div>true/true/true|flag,count,text|false,0,</div>')
  899. })
  900. test('named slot with slots[name]() invocation', () => {
  901. const VDomChild = defineComponent({
  902. setup(_, { slots }) {
  903. return () =>
  904. h('div', null, [
  905. h('header', null, slots.header!()),
  906. h('main', null, slots.default!()),
  907. h('footer', null, slots.footer!()),
  908. ])
  909. },
  910. })
  911. const VaporChild = defineVaporComponent({
  912. setup() {
  913. return createComponent(
  914. VDomChild as any,
  915. null,
  916. {
  917. header: () => template('Header')(),
  918. default: () => template('Main')(),
  919. footer: () => template('Footer')(),
  920. },
  921. true,
  922. )
  923. },
  924. })
  925. const { html } = define({
  926. setup() {
  927. return () => h(VaporChild as any)
  928. },
  929. }).render()
  930. expect(html()).toBe(
  931. '<div><header>Header</header><main>Main</main><footer>Footer</footer></div>',
  932. )
  933. })
  934. test('slots.default() return directly', () => {
  935. const VDomChild = defineComponent({
  936. setup(_, { slots }) {
  937. return () => slots.default!()
  938. },
  939. })
  940. const VaporChild = defineVaporComponent({
  941. setup() {
  942. return createComponent(
  943. VDomChild as any,
  944. null,
  945. {
  946. default: () => template('direct return slot')(),
  947. },
  948. true,
  949. )
  950. },
  951. })
  952. const { html } = define({
  953. setup() {
  954. return () => h(VaporChild as any)
  955. },
  956. }).render()
  957. expect(html()).toBe('direct return slot')
  958. })
  959. test('rendering forwarding vapor slot', () => {
  960. const VDomChild = defineComponent({
  961. setup(_, { slots }) {
  962. return () => h('div', null, { default: slots.default })
  963. },
  964. })
  965. const VaporChild = defineVaporComponent({
  966. setup() {
  967. return createComponent(
  968. VDomChild as any,
  969. null,
  970. {
  971. default: () => template('forwarded slot')(),
  972. },
  973. true,
  974. )
  975. },
  976. })
  977. const { html } = define({
  978. setup() {
  979. return () => h(VaporChild as any)
  980. },
  981. }).render()
  982. expect(html()).toBe('<div>forwarded slot</div>')
  983. })
  984. test('dynamic slots via createForSlots should update in vdom child', async () => {
  985. const list = ref([0, 1, 2])
  986. const VDomChild = defineComponent({
  987. setup(_, { slots }) {
  988. return () => h('div', null, [renderSlot(slots, 'default')])
  989. },
  990. })
  991. const VaporParent = defineVaporComponent({
  992. setup() {
  993. return createComponent(VDomChild as any, null, {
  994. $: [
  995. () =>
  996. createForSlots(list.value, value => ({
  997. name: 'default',
  998. fn: () => {
  999. const n = template('<span> </span>')() as Element
  1000. const t = txt(n) as Text
  1001. renderEffect(() => setText(t, toDisplayString(value)))
  1002. return n
  1003. },
  1004. })),
  1005. ],
  1006. })
  1007. },
  1008. })
  1009. const { html } = define({
  1010. setup() {
  1011. return () => h(VaporParent as any)
  1012. },
  1013. }).render()
  1014. // last-wins: shows last item
  1015. expect(html()).toBe('<div><span>2</span></div>')
  1016. list.value.push(3)
  1017. await nextTick()
  1018. expect(html()).toBe('<div><span>3</span></div>')
  1019. list.value.pop()
  1020. list.value.pop()
  1021. await nextTick()
  1022. expect(html()).toBe('<div><span>1</span></div>')
  1023. })
  1024. test('dynamic slots via createForSlots should re-mount fragment slot in vdom child', async () => {
  1025. const list = ref([0, 1, 2])
  1026. const VDomChild = defineComponent({
  1027. setup(_, { slots }) {
  1028. return () => h('div', null, [renderSlot(slots, 'default')])
  1029. },
  1030. })
  1031. const VaporParent = defineVaporComponent({
  1032. setup() {
  1033. return createComponent(VDomChild as any, null, {
  1034. $: [
  1035. () =>
  1036. createForSlots(list.value, value => ({
  1037. name: 'default',
  1038. fn: () =>
  1039. createIf(
  1040. () => true,
  1041. () => {
  1042. const n = template('<span> </span>')() as Element
  1043. const t = txt(n) as Text
  1044. renderEffect(() => setText(t, toDisplayString(value)))
  1045. return n
  1046. },
  1047. ),
  1048. })),
  1049. ],
  1050. })
  1051. },
  1052. })
  1053. const { html } = define({
  1054. setup() {
  1055. return () => h(VaporParent as any)
  1056. },
  1057. }).render()
  1058. expect(html()).toBe('<div><span>2</span><!--if--></div>')
  1059. list.value.push(3)
  1060. await nextTick()
  1061. expect(html()).toBe('<div><span>3</span><!--if--></div>')
  1062. list.value.pop()
  1063. list.value.pop()
  1064. await nextTick()
  1065. expect(html()).toBe('<div><span>1</span><!--if--></div>')
  1066. })
  1067. test('dynamic slot re-mount should stop stale effects from previous slot function', async () => {
  1068. const list = ref([0, 1])
  1069. const slotStates = new Map([
  1070. [0, { text: ref('zero'), runs: vi.fn() }],
  1071. [1, { text: ref('one'), runs: vi.fn() }],
  1072. [2, { text: ref('two'), runs: vi.fn() }],
  1073. ])
  1074. const VDomChild = defineComponent({
  1075. setup(_, { slots }) {
  1076. return () => h('div', null, [renderSlot(slots, 'default')])
  1077. },
  1078. })
  1079. const VaporParent = defineVaporComponent({
  1080. setup() {
  1081. return createComponent(VDomChild as any, null, {
  1082. $: [
  1083. () =>
  1084. createForSlots(list.value, value => ({
  1085. name: 'default',
  1086. fn: () => {
  1087. const state = slotStates.get(value)!
  1088. const n = template('<span> </span>')() as Element
  1089. const t = txt(n) as Text
  1090. renderEffect(() => {
  1091. state.runs()
  1092. setText(t, state.text.value)
  1093. })
  1094. return n
  1095. },
  1096. })),
  1097. ],
  1098. })
  1099. },
  1100. })
  1101. const { html } = define({
  1102. setup() {
  1103. return () => h(VaporParent as any)
  1104. },
  1105. }).render()
  1106. const firstState = slotStates.get(1)!
  1107. expect(html()).toBe('<div><span>one</span></div>')
  1108. expect(firstState.runs).toHaveBeenCalledTimes(1)
  1109. list.value.push(2)
  1110. await nextTick()
  1111. expect(html()).toBe('<div><span>two</span></div>')
  1112. expect(firstState.runs).toHaveBeenCalledTimes(1)
  1113. firstState.text.value = 'stale-one'
  1114. await nextTick()
  1115. expect(html()).toBe('<div><span>two</span></div>')
  1116. expect(firstState.runs).toHaveBeenCalledTimes(1)
  1117. })
  1118. test('should stop vdom slot outlet effects after outlet unmount', async () => {
  1119. const showOutlet = ref(true)
  1120. const msg = ref('one')
  1121. const track = vi.fn()
  1122. const VaporChild = defineVaporComponent({
  1123. setup() {
  1124. return createIf(
  1125. () => showOutlet.value,
  1126. () => createSlot('default'),
  1127. )
  1128. },
  1129. })
  1130. const { html } = define({
  1131. setup() {
  1132. return () =>
  1133. h(VaporChild as any, null, {
  1134. default: () => {
  1135. track()
  1136. return [h('span', msg.value)]
  1137. },
  1138. })
  1139. },
  1140. }).render()
  1141. expect(html()).toBe('<span>one</span><!--if-->')
  1142. expect(track).toHaveBeenCalledTimes(1)
  1143. showOutlet.value = false
  1144. await nextTick()
  1145. expect(html()).toBe('<!--if-->')
  1146. msg.value = 'two'
  1147. await nextTick()
  1148. expect(track).toHaveBeenCalledTimes(1)
  1149. expect(html()).toBe('<!--if-->')
  1150. })
  1151. })
  1152. describe('provide / inject', () => {
  1153. it('should inject value from vdom parent', async () => {
  1154. const VaporChild = defineVaporComponent({
  1155. setup() {
  1156. const foo = inject('foo')
  1157. const n0 = template(' ')() as any
  1158. renderEffect(() => setText(n0, toDisplayString(foo)))
  1159. return n0
  1160. },
  1161. })
  1162. const value = ref('foo')
  1163. const { html } = define({
  1164. setup() {
  1165. provide('foo', value)
  1166. return () => h(VaporChild as any)
  1167. },
  1168. }).render()
  1169. expect(html()).toBe('foo')
  1170. value.value = 'bar'
  1171. await nextTick()
  1172. expect(html()).toBe('bar')
  1173. })
  1174. })
  1175. describe('template ref', () => {
  1176. it('useTemplateRef with vapor child', async () => {
  1177. const VaporChild = defineVaporComponent({
  1178. setup(_, { expose }) {
  1179. const foo = ref('foo')
  1180. expose({ foo })
  1181. const n0 = template(' ')() as any
  1182. renderEffect(() => setText(n0, toDisplayString(foo)))
  1183. return n0
  1184. },
  1185. })
  1186. let elRef: ShallowRef
  1187. const { html } = define({
  1188. setup() {
  1189. elRef = useTemplateRef('el')
  1190. return () => h(VaporChild as any, { ref: 'el' })
  1191. },
  1192. }).render()
  1193. expect(html()).toBe('foo')
  1194. elRef!.value.foo = 'bar'
  1195. await nextTick()
  1196. expect(html()).toBe('bar')
  1197. })
  1198. it('static ref with vapor child', async () => {
  1199. const VaporChild = defineVaporComponent({
  1200. setup(_, { expose }) {
  1201. const foo = ref('foo')
  1202. expose({ foo })
  1203. const n0 = template(' ')() as any
  1204. renderEffect(() => setText(n0, toDisplayString(foo)))
  1205. return n0
  1206. },
  1207. })
  1208. let elRef: ShallowRef
  1209. const { html } = define({
  1210. setup() {
  1211. elRef = shallowRef()
  1212. return { elRef }
  1213. },
  1214. render() {
  1215. return h(VaporChild as any, { ref: 'elRef' })
  1216. },
  1217. }).render()
  1218. expect(html()).toBe('foo')
  1219. elRef!.value.foo = 'bar'
  1220. await nextTick()
  1221. expect(html()).toBe('bar')
  1222. })
  1223. it('dynamic component includes vdom component', async () => {
  1224. const vdomRef = ref<any>(null)
  1225. const VdomChild = defineComponent({
  1226. setup(_, { expose }) {
  1227. expose({ name: 'vdomChild' })
  1228. return () => h('div', 'vdom child')
  1229. },
  1230. })
  1231. const VaporChild = defineVaporComponent({
  1232. setup() {
  1233. return { vdomRef }
  1234. },
  1235. render() {
  1236. const setRef = createTemplateRefSetter()
  1237. const n0 = createDynamicComponent(() => VdomChild)
  1238. setRef(n0, vdomRef, false, 'vdomRef')
  1239. return n0
  1240. },
  1241. })
  1242. define({
  1243. setup() {
  1244. return () => h(VaporChild as any)
  1245. },
  1246. }).render()
  1247. await nextTick()
  1248. expect(vdomRef.value).toBeDefined()
  1249. expect(vdomRef.value.name).toBe('vdomChild')
  1250. })
  1251. it('dynamic component includes vdom component should cleanup old ref', async () => {
  1252. const VdomChild = defineComponent({
  1253. setup(_, { expose }) {
  1254. expose({ name: 'vdomChild' })
  1255. return () => h('div', 'vdom child')
  1256. },
  1257. })
  1258. const useA = ref(true)
  1259. const refA = ref<any>(null)
  1260. const refB = ref<any>(null)
  1261. const VaporChild = defineVaporComponent({
  1262. setup() {
  1263. const setRef = createTemplateRefSetter()
  1264. const n0 = createDynamicComponent(() => VdomChild)
  1265. renderEffect(() => {
  1266. setRef(n0, useA.value ? refA : refB, false, 'vdomRef')
  1267. })
  1268. return n0
  1269. },
  1270. })
  1271. define({
  1272. setup() {
  1273. return () => h(VaporChild as any)
  1274. },
  1275. }).render()
  1276. await nextTick()
  1277. expect(refA.value).toBeDefined()
  1278. expect(refA.value.name).toBe('vdomChild')
  1279. expect(refB.value).toBe(null)
  1280. useA.value = false
  1281. await nextTick()
  1282. expect(refA.value).toBe(null)
  1283. expect(refB.value).toBeDefined()
  1284. expect(refB.value.name).toBe('vdomChild')
  1285. })
  1286. })
  1287. describe('dynamic component', () => {
  1288. it('dynamic component with vapor child', async () => {
  1289. const VaporChild = defineVaporComponent({
  1290. setup() {
  1291. return template('<div>vapor child</div>')() as any
  1292. },
  1293. })
  1294. const VdomChild = defineComponent({
  1295. setup() {
  1296. return () => h('div', 'vdom child')
  1297. },
  1298. })
  1299. const view = shallowRef<any>(VaporChild)
  1300. const { html } = define({
  1301. setup() {
  1302. return () => h(resolveDynamicComponent(view.value) as any)
  1303. },
  1304. }).render()
  1305. expect(html()).toBe('<div>vapor child</div>')
  1306. view.value = VdomChild
  1307. await nextTick()
  1308. expect(html()).toBe('<div>vdom child</div>')
  1309. view.value = VaporChild
  1310. await nextTick()
  1311. expect(html()).toBe('<div>vapor child</div>')
  1312. })
  1313. describe('render VNodes', () => {
  1314. it('should render VNode containing vapor component from VDOM slot', async () => {
  1315. const VaporComp = defineVaporComponent({
  1316. setup() {
  1317. return template('<div>vapor comp</div>')() as any
  1318. },
  1319. })
  1320. const RouterView = defineComponent({
  1321. setup(_, { slots }) {
  1322. return () => {
  1323. const component = h(VaporComp as any)
  1324. return slots.default!({ Component: component })
  1325. }
  1326. },
  1327. })
  1328. const App = defineVaporComponent({
  1329. setup() {
  1330. return createComponent(
  1331. RouterView as any,
  1332. null,
  1333. {
  1334. default: (slotProps: { Component: any }) => {
  1335. return createDynamicComponent(() => slotProps.Component)
  1336. },
  1337. },
  1338. true,
  1339. )
  1340. },
  1341. })
  1342. const { html } = define({
  1343. setup() {
  1344. return () => h(App as any)
  1345. },
  1346. }).render()
  1347. expect(html()).toBe('<div>vapor comp</div><!--dynamic-component-->')
  1348. })
  1349. it('should render VNode containing vdom component from VDOM slot', async () => {
  1350. const VdomComp = defineComponent({
  1351. setup() {
  1352. return () => h('div', 'vdom comp')
  1353. },
  1354. })
  1355. const RouterView = defineComponent({
  1356. setup(_, { slots }) {
  1357. return () => {
  1358. const component = h(VdomComp)
  1359. return slots.default!({ Component: component })
  1360. }
  1361. },
  1362. })
  1363. const App = defineVaporComponent({
  1364. setup() {
  1365. return createComponent(
  1366. RouterView as any,
  1367. null,
  1368. {
  1369. default: (slotProps: { Component: any }) => {
  1370. return createDynamicComponent(() => slotProps.Component)
  1371. },
  1372. },
  1373. true,
  1374. )
  1375. },
  1376. })
  1377. const { html } = define({
  1378. setup() {
  1379. return () => h(App as any)
  1380. },
  1381. }).render()
  1382. expect(html()).toBe('<div>vdom comp</div><!--dynamic-component-->')
  1383. })
  1384. it('should update when VNode changes', async () => {
  1385. const VaporCompA = defineVaporComponent({
  1386. setup() {
  1387. return template('<div>vapor A</div>')() as any
  1388. },
  1389. })
  1390. const VaporCompB = defineVaporComponent({
  1391. setup() {
  1392. return template('<div>vapor B</div>')() as any
  1393. },
  1394. })
  1395. const current = shallowRef<any>(VaporCompA)
  1396. const RouterView = defineComponent({
  1397. setup(_, { slots }) {
  1398. return () => {
  1399. const component = h(current.value as any)
  1400. return slots.default!({ Component: component })
  1401. }
  1402. },
  1403. })
  1404. const App = defineVaporComponent({
  1405. setup() {
  1406. return createComponent(
  1407. RouterView as any,
  1408. null,
  1409. {
  1410. default: (slotProps: { Component: any }) => {
  1411. return createDynamicComponent(() => slotProps.Component)
  1412. },
  1413. },
  1414. true,
  1415. )
  1416. },
  1417. })
  1418. const { html } = define({
  1419. setup() {
  1420. return () => h(App as any)
  1421. },
  1422. }).render()
  1423. expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
  1424. current.value = VaporCompB
  1425. await nextTick()
  1426. expect(html()).toBe('<div>vapor B</div><!--dynamic-component-->')
  1427. })
  1428. describe('with VaporKeepAlive', () => {
  1429. it('switch VNode with inner vapor components', async () => {
  1430. const hooksA = {
  1431. mounted: vi.fn(),
  1432. activated: vi.fn(),
  1433. deactivated: vi.fn(),
  1434. unmounted: vi.fn(),
  1435. }
  1436. const hooksB = {
  1437. mounted: vi.fn(),
  1438. activated: vi.fn(),
  1439. deactivated: vi.fn(),
  1440. unmounted: vi.fn(),
  1441. }
  1442. const VaporCompA = defineVaporComponent({
  1443. setup() {
  1444. onMounted(() => hooksA.mounted())
  1445. onActivated(() => hooksA.activated())
  1446. onDeactivated(() => hooksA.deactivated())
  1447. onUnmounted(() => hooksA.unmounted())
  1448. return template('<div>vapor A</div>')() as any
  1449. },
  1450. })
  1451. const VaporCompB = defineVaporComponent({
  1452. setup() {
  1453. onMounted(() => hooksB.mounted())
  1454. onActivated(() => hooksB.activated())
  1455. onDeactivated(() => hooksB.deactivated())
  1456. onUnmounted(() => hooksB.unmounted())
  1457. return template('<div>vapor B</div>')() as any
  1458. },
  1459. })
  1460. const current = shallowRef<any>(VaporCompA)
  1461. const RouterView = defineComponent({
  1462. setup(_, { slots }) {
  1463. return () => {
  1464. const component = h(current.value as any)
  1465. return slots.default!({ Component: component })
  1466. }
  1467. },
  1468. })
  1469. const App = defineVaporComponent({
  1470. setup() {
  1471. return createComponent(
  1472. RouterView as any,
  1473. null,
  1474. {
  1475. default: (slotProps: { Component: any }) => {
  1476. return createComponent(VaporKeepAlive, null, {
  1477. default: () =>
  1478. createDynamicComponent(() => slotProps.Component),
  1479. })
  1480. },
  1481. },
  1482. true,
  1483. )
  1484. },
  1485. })
  1486. const { html } = define({
  1487. setup() {
  1488. return () => h(App as any)
  1489. },
  1490. }).render()
  1491. expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
  1492. // A: mounted + activated
  1493. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1494. expect(hooksA.activated).toHaveBeenCalledTimes(1)
  1495. expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
  1496. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1497. current.value = VaporCompB
  1498. await nextTick()
  1499. expect(html()).toBe('<div>vapor B</div><!--dynamic-component-->')
  1500. // A: deactivated (cached)
  1501. expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
  1502. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1503. // B: mounted + activated
  1504. expect(hooksB.mounted).toHaveBeenCalledTimes(1)
  1505. expect(hooksB.activated).toHaveBeenCalledTimes(1)
  1506. current.value = VaporCompA
  1507. await nextTick()
  1508. expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
  1509. // B: deactivated (cached)
  1510. expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
  1511. expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
  1512. // A: re-activated (not re-mounted)
  1513. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1514. expect(hooksA.activated).toHaveBeenCalledTimes(2)
  1515. })
  1516. it('switch VNode with inner VDOM components', async () => {
  1517. const hooksA = {
  1518. mounted: vi.fn(),
  1519. activated: vi.fn(),
  1520. deactivated: vi.fn(),
  1521. unmounted: vi.fn(),
  1522. }
  1523. const hooksB = {
  1524. mounted: vi.fn(),
  1525. activated: vi.fn(),
  1526. deactivated: vi.fn(),
  1527. unmounted: vi.fn(),
  1528. }
  1529. const VDOMCompA = defineComponent({
  1530. setup() {
  1531. onMounted(() => hooksA.mounted())
  1532. onActivated(() => hooksA.activated())
  1533. onDeactivated(() => hooksA.deactivated())
  1534. onUnmounted(() => hooksA.unmounted())
  1535. return () => h('div', 'vdom A')
  1536. },
  1537. })
  1538. const VDOMCompB = defineComponent({
  1539. setup() {
  1540. onMounted(() => hooksB.mounted())
  1541. onActivated(() => hooksB.activated())
  1542. onDeactivated(() => hooksB.deactivated())
  1543. onUnmounted(() => hooksB.unmounted())
  1544. return () => h('div', 'vdom B')
  1545. },
  1546. })
  1547. const current = shallowRef<any>(VDOMCompA)
  1548. const RouterView = defineComponent({
  1549. setup(_, { slots }) {
  1550. return () => {
  1551. const component = h(current.value as any)
  1552. return slots.default!({ Component: component })
  1553. }
  1554. },
  1555. })
  1556. const App = defineVaporComponent({
  1557. setup() {
  1558. return createComponent(
  1559. RouterView as any,
  1560. null,
  1561. {
  1562. default: (slotProps: { Component: any }) => {
  1563. return createComponent(VaporKeepAlive, null, {
  1564. default: () =>
  1565. createDynamicComponent(() => slotProps.Component),
  1566. })
  1567. },
  1568. },
  1569. true,
  1570. )
  1571. },
  1572. })
  1573. const { html } = define({
  1574. setup() {
  1575. return () => h(App as any)
  1576. },
  1577. }).render()
  1578. expect(html()).toBe('<div>vdom A</div><!--dynamic-component-->')
  1579. // A: mounted + activated
  1580. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1581. expect(hooksA.activated).toHaveBeenCalledTimes(1)
  1582. expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
  1583. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1584. current.value = VDOMCompB
  1585. await nextTick()
  1586. expect(html()).toBe('<div>vdom B</div><!--dynamic-component-->')
  1587. // A: deactivated (cached)
  1588. expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
  1589. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1590. // B: mounted + activated
  1591. expect(hooksB.mounted).toHaveBeenCalledTimes(1)
  1592. expect(hooksB.activated).toHaveBeenCalledTimes(1)
  1593. current.value = VDOMCompA
  1594. await nextTick()
  1595. expect(html()).toBe('<div>vdom A</div><!--dynamic-component-->')
  1596. // B: deactivated (cached)
  1597. expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
  1598. expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
  1599. // A: re-activated (not re-mounted)
  1600. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1601. expect(hooksA.activated).toHaveBeenCalledTimes(2)
  1602. })
  1603. it('switch VNode with inner mixed vapor/VDOM components', async () => {
  1604. const hooksA = {
  1605. mounted: vi.fn(),
  1606. activated: vi.fn(),
  1607. deactivated: vi.fn(),
  1608. unmounted: vi.fn(),
  1609. }
  1610. const hooksB = {
  1611. mounted: vi.fn(),
  1612. activated: vi.fn(),
  1613. deactivated: vi.fn(),
  1614. unmounted: vi.fn(),
  1615. }
  1616. const VaporCompA = defineVaporComponent({
  1617. setup() {
  1618. onMounted(() => hooksA.mounted())
  1619. onActivated(() => hooksA.activated())
  1620. onDeactivated(() => hooksA.deactivated())
  1621. onUnmounted(() => hooksA.unmounted())
  1622. return template('<div>vapor A</div>')()
  1623. },
  1624. })
  1625. const VDOMCompB = defineComponent({
  1626. setup() {
  1627. onMounted(() => hooksB.mounted())
  1628. onActivated(() => hooksB.activated())
  1629. onDeactivated(() => hooksB.deactivated())
  1630. onUnmounted(() => hooksB.unmounted())
  1631. return () => h('div', 'vdom B')
  1632. },
  1633. })
  1634. const current = shallowRef<any>(VaporCompA)
  1635. const RouterView = defineComponent({
  1636. setup(_, { slots }) {
  1637. return () => {
  1638. const component = h(current.value as any)
  1639. return slots.default!({ Component: component })
  1640. }
  1641. },
  1642. })
  1643. const App = defineVaporComponent({
  1644. setup() {
  1645. return createComponent(
  1646. RouterView as any,
  1647. null,
  1648. {
  1649. default: (slotProps: { Component: any }) => {
  1650. return createComponent(VaporKeepAlive, null, {
  1651. default: () =>
  1652. createDynamicComponent(() => slotProps.Component),
  1653. })
  1654. },
  1655. },
  1656. true,
  1657. )
  1658. },
  1659. })
  1660. const { html } = define({
  1661. setup() {
  1662. return () => h(App as any)
  1663. },
  1664. }).render()
  1665. expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
  1666. // A (vapor): mounted + activated
  1667. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1668. expect(hooksA.activated).toHaveBeenCalledTimes(1)
  1669. expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
  1670. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1671. current.value = VDOMCompB
  1672. await nextTick()
  1673. expect(html()).toBe('<div>vdom B</div><!--dynamic-component-->')
  1674. // A (vapor): deactivated (cached)
  1675. expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
  1676. expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
  1677. // B (vdom): mounted + activated
  1678. expect(hooksB.mounted).toHaveBeenCalledTimes(1)
  1679. expect(hooksB.activated).toHaveBeenCalledTimes(1)
  1680. current.value = VaporCompA
  1681. await nextTick()
  1682. expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
  1683. // B (vdom): deactivated (cached)
  1684. expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
  1685. expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
  1686. // A (vapor): re-activated (not re-mounted)
  1687. expect(hooksA.mounted).toHaveBeenCalledTimes(1)
  1688. expect(hooksA.activated).toHaveBeenCalledTimes(2)
  1689. })
  1690. })
  1691. })
  1692. })
  1693. describe('attribute fallthrough', () => {
  1694. it('should fallthrough attrs to vdom child', () => {
  1695. const VDomChild = defineComponent({
  1696. setup() {
  1697. return () => h('div')
  1698. },
  1699. })
  1700. const VaporChild = defineVaporComponent({
  1701. setup() {
  1702. return createComponent(
  1703. VDomChild as any,
  1704. { foo: () => 'vapor foo' },
  1705. null,
  1706. true,
  1707. )
  1708. },
  1709. })
  1710. const { html } = define({
  1711. setup() {
  1712. return () => h(VaporChild as any, { foo: 'foo', bar: 'bar' })
  1713. },
  1714. }).render()
  1715. expect(html()).toBe('<div foo="foo" bar="bar"></div>')
  1716. })
  1717. it('should not fallthrough emit handlers to vdom child', () => {
  1718. const VDomChild = defineComponent({
  1719. emits: ['click'],
  1720. setup(_, { emit }) {
  1721. return () => h('button', { onClick: () => emit('click') }, 'click me')
  1722. },
  1723. })
  1724. const fn = vi.fn()
  1725. const VaporChild = defineVaporComponent({
  1726. emits: ['click'],
  1727. setup() {
  1728. return createComponent(
  1729. VDomChild as any,
  1730. { onClick: () => fn },
  1731. null,
  1732. true,
  1733. )
  1734. },
  1735. })
  1736. const { host, html } = define({
  1737. setup() {
  1738. return () => h(VaporChild as any)
  1739. },
  1740. }).render()
  1741. expect(html()).toBe('<button>click me</button>')
  1742. const button = host.querySelector('button')!
  1743. button.dispatchEvent(new Event('click'))
  1744. // fn should be called once
  1745. expect(fn).toHaveBeenCalledTimes(1)
  1746. })
  1747. it('should update attrs passed from vapor parent to vdom child', async () => {
  1748. const msg = ref('foo')
  1749. const VDomChild = defineComponent({
  1750. setup(_, { attrs }) {
  1751. return () =>
  1752. h(
  1753. 'div',
  1754. {
  1755. 'data-msg': attrs['data-msg'] as string,
  1756. },
  1757. attrs['data-msg'] as string,
  1758. )
  1759. },
  1760. })
  1761. const VaporChild = defineVaporComponent({
  1762. setup() {
  1763. return createComponent(
  1764. VDomChild as any,
  1765. {
  1766. 'data-msg': () => msg.value,
  1767. },
  1768. null,
  1769. true,
  1770. )
  1771. },
  1772. })
  1773. const { html } = define({
  1774. setup() {
  1775. return () => h(VaporChild as any)
  1776. },
  1777. }).render()
  1778. expect(html()).toBe('<div data-msg="foo">foo</div>')
  1779. msg.value = 'bar'
  1780. await nextTick()
  1781. expect(html()).toBe('<div data-msg="bar">bar</div>')
  1782. })
  1783. })
  1784. describe('async component', () => {
  1785. const duration = 5
  1786. test('render vapor async component', async () => {
  1787. const VdomChild = {
  1788. setup() {
  1789. return () => h('div', 'foo')
  1790. },
  1791. }
  1792. const VaporAsyncChild = defineVaporAsyncComponent({
  1793. loader: () => {
  1794. return new Promise(r => {
  1795. setTimeout(() => {
  1796. r(VdomChild as any)
  1797. }, duration)
  1798. })
  1799. },
  1800. loadingComponent: () => h('span', 'loading...'),
  1801. })
  1802. const { html } = define({
  1803. setup() {
  1804. return () => h(VaporAsyncChild as any)
  1805. },
  1806. }).render()
  1807. expect(html()).toBe('<span>loading...</span><!--async component-->')
  1808. await new Promise(r => setTimeout(r, duration))
  1809. await nextTick()
  1810. expect(html()).toBe('<div>foo</div><!--async component-->')
  1811. })
  1812. })
  1813. describe('KeepAlive', () => {
  1814. function assertHookCalls(
  1815. hooks: {
  1816. beforeMount: any
  1817. mounted: any
  1818. activated: any
  1819. deactivated: any
  1820. unmounted: any
  1821. },
  1822. callCounts: number[],
  1823. ) {
  1824. expect([
  1825. hooks.beforeMount.mock.calls.length,
  1826. hooks.mounted.mock.calls.length,
  1827. hooks.activated.mock.calls.length,
  1828. hooks.deactivated.mock.calls.length,
  1829. hooks.unmounted.mock.calls.length,
  1830. ]).toEqual(callCounts)
  1831. }
  1832. let hooks: any
  1833. beforeEach(() => {
  1834. hooks = {
  1835. beforeMount: vi.fn(),
  1836. mounted: vi.fn(),
  1837. activated: vi.fn(),
  1838. deactivated: vi.fn(),
  1839. unmounted: vi.fn(),
  1840. }
  1841. })
  1842. test('render vapor component', async () => {
  1843. const VaporChild = defineVaporComponent({
  1844. setup() {
  1845. const msg = ref('vapor')
  1846. onBeforeMount(() => hooks.beforeMount())
  1847. onMounted(() => hooks.mounted())
  1848. onActivated(() => hooks.activated())
  1849. onDeactivated(() => hooks.deactivated())
  1850. onUnmounted(() => hooks.unmounted())
  1851. const n0 = template('<input type="text">', true)() as any
  1852. applyTextModel(
  1853. n0,
  1854. () => msg.value,
  1855. _value => (msg.value = _value),
  1856. )
  1857. return n0
  1858. },
  1859. })
  1860. const show = ref(true)
  1861. const toggle = ref(true)
  1862. const { html, host } = define({
  1863. setup() {
  1864. return () =>
  1865. show.value
  1866. ? h(KeepAlive, null, {
  1867. default: () => (toggle.value ? h(VaporChild as any) : null),
  1868. })
  1869. : null
  1870. },
  1871. }).render()
  1872. expect(html()).toBe('<input type="text">')
  1873. let inputEl = host.firstChild as HTMLInputElement
  1874. expect(inputEl.value).toBe('vapor')
  1875. assertHookCalls(hooks, [1, 1, 1, 0, 0])
  1876. // change input value
  1877. inputEl.value = 'changed'
  1878. inputEl.dispatchEvent(new Event('input'))
  1879. await nextTick()
  1880. // deactivate
  1881. toggle.value = false
  1882. await nextTick()
  1883. expect(html()).toBe('<!---->')
  1884. assertHookCalls(hooks, [1, 1, 1, 1, 0])
  1885. // activate
  1886. toggle.value = true
  1887. await nextTick()
  1888. expect(html()).toBe('<input type="text">')
  1889. inputEl = host.firstChild as HTMLInputElement
  1890. expect(inputEl.value).toBe('changed')
  1891. assertHookCalls(hooks, [1, 1, 2, 1, 0])
  1892. // unmount keepalive
  1893. show.value = false
  1894. await nextTick()
  1895. expect(html()).toBe('<!---->')
  1896. assertHookCalls(hooks, [1, 1, 2, 2, 1])
  1897. // mount keepalive
  1898. show.value = true
  1899. await nextTick()
  1900. inputEl = host.firstChild as HTMLInputElement
  1901. expect(inputEl.value).toBe('vapor')
  1902. assertHookCalls(hooks, [2, 2, 3, 2, 1])
  1903. })
  1904. test('render vapor slot', async () => {
  1905. const show = ref(true)
  1906. const VDomComp = defineComponent({
  1907. setup(_, { slots }) {
  1908. return () => renderSlot(slots, 'default')
  1909. },
  1910. })
  1911. const App = defineVaporComponent({
  1912. setup() {
  1913. const n5 = createComponent(VaporKeepAlive, null, {
  1914. default: () =>
  1915. createIf(
  1916. () => show.value,
  1917. () =>
  1918. createComponent(VDomComp as any, null, {
  1919. default: () => template('slot text')(),
  1920. }),
  1921. ),
  1922. })
  1923. return n5
  1924. },
  1925. })
  1926. const { html } = define({
  1927. setup() {
  1928. return () => h(App)
  1929. },
  1930. }).render()
  1931. expect(html()).toBe('slot text<!--if-->')
  1932. show.value = false
  1933. await nextTick()
  1934. expect(html()).toBe('<!--if-->')
  1935. show.value = true
  1936. await nextTick()
  1937. expect(html()).toBe('slot text<!--if-->')
  1938. })
  1939. test('vdom slot fallback inside VaporKeepAlive should preserve render context', async () => {
  1940. const show = ref(true)
  1941. const VDomComp = defineComponent({
  1942. setup(_, { slots }) {
  1943. return () => renderSlot(slots, 'default')
  1944. },
  1945. })
  1946. const VaporFallback = defineVaporComponent({
  1947. setup() {
  1948. onBeforeMount(() => hooks.beforeMount())
  1949. onMounted(() => hooks.mounted())
  1950. onActivated(() => hooks.activated())
  1951. onDeactivated(() => hooks.deactivated())
  1952. onUnmounted(() => hooks.unmounted())
  1953. return template('<div>fallback</div>')() as any
  1954. },
  1955. })
  1956. const App = defineVaporComponent({
  1957. setup() {
  1958. return createComponent(VaporKeepAlive, null, {
  1959. default: withVaporCtx(() =>
  1960. createIf(
  1961. () => show.value,
  1962. () =>
  1963. createComponent(
  1964. VDomComp as any,
  1965. null,
  1966. {
  1967. default: withVaporCtx(() =>
  1968. createSlot('default', null, () =>
  1969. createComponent(VaporFallback as any),
  1970. ),
  1971. ),
  1972. },
  1973. true,
  1974. ),
  1975. ),
  1976. ),
  1977. })
  1978. },
  1979. })
  1980. const { html } = define({
  1981. setup() {
  1982. return () => h(App)
  1983. },
  1984. }).render()
  1985. expect(html()).toBe('<div>fallback</div><!--if-->')
  1986. assertHookCalls(hooks, [1, 1, 1, 0, 0])
  1987. show.value = false
  1988. await nextTick()
  1989. expect(html()).toBe('<!--if-->')
  1990. assertHookCalls(hooks, [1, 1, 1, 1, 0])
  1991. show.value = true
  1992. await nextTick()
  1993. expect(html()).toBe('<div>fallback</div><!--if-->')
  1994. assertHookCalls(hooks, [1, 1, 2, 1, 0])
  1995. })
  1996. test('unmounting vapor slot should remove vnode slot content', async () => {
  1997. const show = ref(true)
  1998. const VaporSlotOutlet = defineVaporComponent({
  1999. setup() {
  2000. return createSlot('default')
  2001. },
  2002. })
  2003. const { html } = define({
  2004. setup() {
  2005. return () =>
  2006. h('div', null, [
  2007. show.value
  2008. ? h(VaporSlotOutlet as any, null, {
  2009. default: () => [h('span', 'slot vnode')],
  2010. })
  2011. : null,
  2012. ])
  2013. },
  2014. }).render()
  2015. expect(html()).toBe('<div><span>slot vnode</span></div>')
  2016. show.value = false
  2017. await nextTick()
  2018. expect(html()).toBe('<div><!----></div>')
  2019. })
  2020. test('should update props on reactivation of vapor child in vdom KeepAlive', async () => {
  2021. const VaporChild = defineVaporComponent({
  2022. props: { msg: String },
  2023. setup(props: any) {
  2024. const n0 = template('<div> </div>')() as any
  2025. const x0 = child(n0) as any
  2026. renderEffect(() => setText(x0, props.msg))
  2027. return n0
  2028. },
  2029. })
  2030. const VdomChild = defineComponent({
  2031. setup() {
  2032. return () => h('span', 'vdom')
  2033. },
  2034. })
  2035. const current = shallowRef<any>(VaporChild)
  2036. const msg = ref('hello')
  2037. const App = defineComponent({
  2038. setup() {
  2039. return () =>
  2040. h(KeepAlive, null, {
  2041. default: () =>
  2042. h(
  2043. resolveDynamicComponent(current.value) as any,
  2044. current.value === VaporChild ? { msg: msg.value } : null,
  2045. ),
  2046. })
  2047. },
  2048. })
  2049. const root = document.createElement('div')
  2050. const app = createApp(App)
  2051. app.use(vaporInteropPlugin)
  2052. app.mount(root)
  2053. expect(root.innerHTML).toBe('<div>hello</div>')
  2054. // Switch to vdom child (deactivates vapor child)
  2055. current.value = VdomChild
  2056. await nextTick()
  2057. expect(root.innerHTML).toBe('<span>vdom</span>')
  2058. // Change props while vapor child is deactivated
  2059. msg.value = 'updated'
  2060. await nextTick()
  2061. expect(root.innerHTML).toBe('<span>vdom</span>')
  2062. // Reactivate vapor child — should reflect new props
  2063. current.value = VaporChild
  2064. await nextTick()
  2065. expect(root.innerHTML).toBe('<div>updated</div>')
  2066. })
  2067. test('should invoke vnode hooks on activate/deactivate', async () => {
  2068. const VaporChild = defineVaporComponent({
  2069. setup() {
  2070. return template('<div>vapor</div>')()
  2071. },
  2072. })
  2073. const VdomChild = defineComponent({
  2074. setup() {
  2075. return () => h('span', 'vdom')
  2076. },
  2077. })
  2078. const current = shallowRef<any>(VaporChild)
  2079. const vnodeMounted = vi.fn()
  2080. const vnodeUnmounted = vi.fn()
  2081. const App = defineComponent({
  2082. setup() {
  2083. return () =>
  2084. h(KeepAlive, null, {
  2085. default: () =>
  2086. h(
  2087. resolveDynamicComponent(current.value) as any,
  2088. current.value === VaporChild
  2089. ? {
  2090. onVnodeMounted: vnodeMounted,
  2091. onVnodeUnmounted: vnodeUnmounted,
  2092. }
  2093. : null,
  2094. ),
  2095. })
  2096. },
  2097. })
  2098. const root = document.createElement('div')
  2099. const app = createApp(App)
  2100. app.use(vaporInteropPlugin)
  2101. app.mount(root)
  2102. await nextTick()
  2103. expect(vnodeMounted).toHaveBeenCalledTimes(1)
  2104. expect(vnodeUnmounted).toHaveBeenCalledTimes(0)
  2105. // Deactivate vapor child
  2106. current.value = VdomChild
  2107. await nextTick()
  2108. expect(vnodeUnmounted).toHaveBeenCalledTimes(1)
  2109. // Reactivate vapor child
  2110. current.value = VaporChild
  2111. await nextTick()
  2112. expect(vnodeMounted).toHaveBeenCalledTimes(2)
  2113. })
  2114. test('should invoke update hooks in VDOM order on reactivation', async () => {
  2115. const VaporChild = defineVaporComponent({
  2116. props: ['msg'],
  2117. setup(props: any) {
  2118. return template('<div></div>')()
  2119. },
  2120. })
  2121. const VdomChild = defineComponent({
  2122. setup() {
  2123. return () => h('span', 'vdom')
  2124. },
  2125. })
  2126. const current = shallowRef<any>(VaporChild)
  2127. const msg = ref('hello')
  2128. const calls: string[] = []
  2129. const vDir = {
  2130. beforeUpdate: vi.fn(() => calls.push('directive beforeUpdate')),
  2131. updated: vi.fn(() => calls.push('directive updated')),
  2132. }
  2133. const App = defineComponent({
  2134. setup() {
  2135. return () =>
  2136. h(KeepAlive, null, {
  2137. default: () =>
  2138. current.value === VaporChild
  2139. ? withDirectives(
  2140. h(VaporChild as any, {
  2141. msg: msg.value,
  2142. onVnodeBeforeUpdate: () =>
  2143. calls.push('vnode beforeUpdate'),
  2144. onVnodeUpdated: () => calls.push('vnode updated'),
  2145. }),
  2146. [[vDir]],
  2147. )
  2148. : h(VdomChild),
  2149. })
  2150. },
  2151. })
  2152. const root = document.createElement('div')
  2153. const app = createApp(App)
  2154. app.use(vaporInteropPlugin)
  2155. app.mount(root)
  2156. await nextTick()
  2157. // Deactivate vapor child
  2158. current.value = VdomChild
  2159. await nextTick()
  2160. // Change props while deactivated
  2161. msg.value = 'world'
  2162. // Reactivate — should trigger update hooks
  2163. current.value = VaporChild
  2164. await nextTick()
  2165. expect(calls).toEqual([
  2166. 'vnode beforeUpdate',
  2167. 'directive beforeUpdate',
  2168. 'directive updated',
  2169. 'vnode updated',
  2170. ])
  2171. })
  2172. test('should invoke updated before activated on reactivation', async () => {
  2173. const calls: string[] = []
  2174. const VaporChild = defineVaporComponent({
  2175. props: ['msg'],
  2176. setup(props: any) {
  2177. onActivated(() => calls.push('activated'))
  2178. return template('<div></div>')()
  2179. },
  2180. })
  2181. const VdomChild = defineComponent({
  2182. setup() {
  2183. return () => h('span', 'vdom')
  2184. },
  2185. })
  2186. const current = shallowRef<any>(VaporChild)
  2187. const msg = ref('one')
  2188. const App = defineComponent({
  2189. setup() {
  2190. return () =>
  2191. h(KeepAlive, null, {
  2192. default: () =>
  2193. current.value === VaporChild
  2194. ? h(VaporChild as any, {
  2195. msg: msg.value,
  2196. onVnodeUpdated: () => calls.push('vnode updated'),
  2197. onVnodeMounted: () => calls.push('vnode mounted'),
  2198. })
  2199. : h(VdomChild),
  2200. })
  2201. },
  2202. })
  2203. const root = document.createElement('div')
  2204. const app = createApp(App)
  2205. app.use(vaporInteropPlugin)
  2206. app.mount(root)
  2207. await nextTick()
  2208. calls.length = 0
  2209. current.value = VdomChild
  2210. await nextTick()
  2211. msg.value = 'two'
  2212. current.value = VaporChild
  2213. await nextTick()
  2214. expect(calls).toEqual(['vnode updated', 'activated', 'vnode mounted'])
  2215. })
  2216. test('should expose the latest vapor root element in updated hooks on reactivation', async () => {
  2217. const VaporChild = defineVaporComponent({
  2218. props: {
  2219. alt: Boolean,
  2220. },
  2221. setup(props: any) {
  2222. return createIf(
  2223. () => props.alt,
  2224. () => template('<p>alt</p>')(),
  2225. () => template('<div>base</div>')(),
  2226. )
  2227. },
  2228. })
  2229. const VdomChild = defineComponent({
  2230. setup() {
  2231. return () => h('span', 'vdom')
  2232. },
  2233. })
  2234. const current = shallowRef<any>(VaporChild)
  2235. const useAltRoot = ref(false)
  2236. const updatedSpy = vi.fn((vnode: any) => {
  2237. expect((vnode.el as Element).tagName).toBe('P')
  2238. })
  2239. const App = defineComponent({
  2240. setup() {
  2241. return () =>
  2242. h(KeepAlive, null, {
  2243. default: () =>
  2244. current.value === VaporChild
  2245. ? h(VaporChild as any, {
  2246. alt: useAltRoot.value,
  2247. onVnodeUpdated: updatedSpy,
  2248. })
  2249. : h(VdomChild),
  2250. })
  2251. },
  2252. })
  2253. const root = document.createElement('div')
  2254. const app = createApp(App)
  2255. app.use(vaporInteropPlugin)
  2256. app.mount(root)
  2257. await nextTick()
  2258. current.value = VdomChild
  2259. await nextTick()
  2260. useAltRoot.value = true
  2261. current.value = VaporChild
  2262. await nextTick()
  2263. expect(root.querySelector('p')).not.toBeNull()
  2264. expect(updatedSpy).toHaveBeenCalledTimes(1)
  2265. })
  2266. test('should bail out directive beforeUpdate/updated on reactivation for non-element root vapor child', async () => {
  2267. const beforeUpdateSpy = vi.fn()
  2268. const updatedSpy = vi.fn()
  2269. const vDir = {
  2270. beforeUpdate: beforeUpdateSpy,
  2271. updated: updatedSpy,
  2272. }
  2273. const VaporChild = defineVaporComponent({
  2274. props: ['msg'],
  2275. setup() {
  2276. return [template('<div></div>')(), template('<div></div>')()]
  2277. },
  2278. })
  2279. const VdomChild = defineComponent({
  2280. setup() {
  2281. return () => h('span', 'vdom')
  2282. },
  2283. })
  2284. const current = shallowRef<any>(VaporChild)
  2285. const msg = ref('hello')
  2286. const App = defineComponent({
  2287. setup() {
  2288. return () =>
  2289. h(KeepAlive, null, {
  2290. default: () =>
  2291. current.value === VaporChild
  2292. ? withDirectives(h(VaporChild as any, { msg: msg.value }), [
  2293. [vDir],
  2294. ])
  2295. : h(VdomChild),
  2296. })
  2297. },
  2298. })
  2299. const root = document.createElement('div')
  2300. const app = createApp(App)
  2301. app.use(vaporInteropPlugin)
  2302. app.mount(root)
  2303. await nextTick()
  2304. if (__DEV__) {
  2305. expect(
  2306. `Runtime directive used on component with non-element root node.`,
  2307. ).toHaveBeenWarnedTimes(1)
  2308. }
  2309. expect(beforeUpdateSpy).toHaveBeenCalledTimes(0)
  2310. expect(updatedSpy).toHaveBeenCalledTimes(0)
  2311. current.value = VdomChild
  2312. await nextTick()
  2313. msg.value = 'world'
  2314. current.value = VaporChild
  2315. await nextTick()
  2316. expect(
  2317. `Runtime directive used on component with non-element root node.`,
  2318. ).toHaveBeenWarnedTimes(2)
  2319. expect(beforeUpdateSpy).toHaveBeenCalledTimes(0)
  2320. expect(updatedSpy).toHaveBeenCalledTimes(0)
  2321. })
  2322. })
  2323. describe('Teleport', () => {
  2324. test('mounts VDOM Teleport from createDynamicComponent', async () => {
  2325. const target = document.createElement('div')
  2326. target.id = 'interop-teleport-target'
  2327. document.body.appendChild(target)
  2328. try {
  2329. const VaporChild = defineVaporComponent({
  2330. setup() {
  2331. return createDynamicComponent(
  2332. () => Teleport,
  2333. { to: () => '#interop-teleport-target' },
  2334. {
  2335. default: () => template('<span>teleported</span>')(),
  2336. },
  2337. true,
  2338. )
  2339. },
  2340. })
  2341. define({
  2342. setup() {
  2343. return () => h(VaporChild as any)
  2344. },
  2345. }).render()
  2346. await nextTick()
  2347. expect(target.innerHTML).toContain('<span>teleported</span>')
  2348. } finally {
  2349. target.remove()
  2350. }
  2351. })
  2352. test('should patch vdom slot content in the new teleport target after move', async () => {
  2353. const targetA = document.createElement('div')
  2354. targetA.id = 'interop-slot-target-a'
  2355. const targetB = document.createElement('div')
  2356. targetB.id = 'interop-slot-target-b'
  2357. document.body.append(targetA, targetB)
  2358. const to = ref('#interop-slot-target-a')
  2359. const useAltRoot = ref(false)
  2360. try {
  2361. const VaporChild = defineVaporComponent({
  2362. setup() {
  2363. return createComponent(
  2364. VaporTeleport,
  2365. {
  2366. to: () => to.value,
  2367. },
  2368. {
  2369. default: () => createSlot('default'),
  2370. },
  2371. )
  2372. },
  2373. })
  2374. const App = defineComponent({
  2375. setup() {
  2376. return () =>
  2377. h(VaporChild as any, null, {
  2378. default: () => [
  2379. useAltRoot.value ? h('p', 'moved') : h('div', 'initial'),
  2380. ],
  2381. })
  2382. },
  2383. })
  2384. const host = document.createElement('div')
  2385. const app = createApp(App)
  2386. app.use(vaporInteropPlugin)
  2387. app.mount(host)
  2388. await nextTick()
  2389. expect(targetA.innerHTML).toBe('<div>initial</div>')
  2390. expect(targetB.innerHTML).toBe('')
  2391. to.value = '#interop-slot-target-b'
  2392. await nextTick()
  2393. expect(targetA.innerHTML).toBe('')
  2394. expect(targetB.innerHTML).toBe('<div>initial</div>')
  2395. useAltRoot.value = true
  2396. await nextTick()
  2397. expect(targetA.innerHTML).toBe('')
  2398. expect(targetB.innerHTML).toBe('<p>moved</p>')
  2399. } finally {
  2400. targetA.remove()
  2401. targetB.remove()
  2402. }
  2403. })
  2404. })
  2405. describe('Suspense', () => {
  2406. test('renders vapor async wrapper inside VDOM Suspense', async () => {
  2407. const duration = 5
  2408. const VaporAsyncChild = defineVaporAsyncComponent({
  2409. loader: () =>
  2410. new Promise(resolve => {
  2411. setTimeout(() => {
  2412. resolve(
  2413. defineVaporComponent({
  2414. setup() {
  2415. return template('<div><button>click</button></div>')()
  2416. },
  2417. }) as any,
  2418. )
  2419. }, duration)
  2420. }),
  2421. })
  2422. const VaporParent = defineVaporComponent({
  2423. setup() {
  2424. return createComponent(
  2425. Suspense as any,
  2426. null,
  2427. {
  2428. default: () => createComponent(VaporAsyncChild, null, null, true),
  2429. fallback: () => template('loading')(),
  2430. },
  2431. true,
  2432. )
  2433. },
  2434. })
  2435. const { html } = define({
  2436. setup() {
  2437. return () => h(VaporParent as any)
  2438. },
  2439. }).render()
  2440. expect(html()).toContain('loading')
  2441. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2442. await nextTick()
  2443. expect(html()).toContain('<div><button>click</button></div>')
  2444. })
  2445. test('does not suspend vapor async wrapper with suspensible false inside VDOM Suspense', async () => {
  2446. const duration = 5
  2447. const VaporAsyncChild = defineVaporAsyncComponent({
  2448. loader: () =>
  2449. new Promise(resolve => {
  2450. setTimeout(() => {
  2451. resolve(
  2452. defineVaporComponent({
  2453. setup() {
  2454. return template('<div><button>click</button></div>')()
  2455. },
  2456. }) as any,
  2457. )
  2458. }, duration)
  2459. }),
  2460. suspensible: false,
  2461. })
  2462. const VaporParent = defineVaporComponent({
  2463. setup() {
  2464. return createComponent(
  2465. Suspense as any,
  2466. null,
  2467. {
  2468. default: () => createComponent(VaporAsyncChild, null, null, true),
  2469. fallback: () => template('loading')(),
  2470. },
  2471. true,
  2472. )
  2473. },
  2474. })
  2475. const { html } = define({
  2476. setup() {
  2477. return () => h(VaporParent as any)
  2478. },
  2479. }).render()
  2480. expect(html()).not.toContain('loading')
  2481. expect(html()).toContain('<!--async component-->')
  2482. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2483. await nextTick()
  2484. expect(html()).toContain('<div><button>click</button></div>')
  2485. })
  2486. test('renders error component for vapor async wrapper inside VDOM Suspense', async () => {
  2487. const tick = () => new Promise(resolve => setTimeout(resolve))
  2488. let reject!: (error: Error) => void
  2489. const VaporAsyncChild = defineVaporAsyncComponent({
  2490. loader: () =>
  2491. new Promise((_resolve, _reject) => {
  2492. reject = _reject as (error: Error) => void
  2493. }),
  2494. errorComponent: defineVaporComponent({
  2495. props: ['error'],
  2496. setup(props: { error: Error }) {
  2497. return template(props.error.message)()
  2498. },
  2499. }),
  2500. })
  2501. const VaporParent = defineVaporComponent({
  2502. setup() {
  2503. return createComponent(
  2504. Suspense as any,
  2505. null,
  2506. {
  2507. default: () => createComponent(VaporAsyncChild, null, null, true),
  2508. fallback: () => template('loading')(),
  2509. },
  2510. true,
  2511. )
  2512. },
  2513. })
  2514. const host = document.createElement('div')
  2515. const app = createApp({
  2516. render: () => h(VaporParent as any),
  2517. })
  2518. const errorHandler = vi.fn()
  2519. app.use(vaporInteropPlugin)
  2520. app.config.errorHandler = errorHandler
  2521. try {
  2522. app.mount(host)
  2523. expect(host.innerHTML).toContain('loading')
  2524. reject(new Error('errored out'))
  2525. await tick()
  2526. await nextTick()
  2527. expect(errorHandler).toHaveBeenCalled()
  2528. expect(host.innerHTML).toContain('errored out')
  2529. } finally {
  2530. app.unmount()
  2531. host.remove()
  2532. }
  2533. })
  2534. test('does not fall through slots to error component for vapor async wrapper inside VDOM Suspense', async () => {
  2535. const tick = () => new Promise(resolve => setTimeout(resolve))
  2536. let reject!: (error: Error) => void
  2537. const VaporAsyncChild = defineVaporAsyncComponent({
  2538. loader: () =>
  2539. new Promise((_resolve, _reject) => {
  2540. reject = _reject as (error: Error) => void
  2541. }),
  2542. errorComponent: defineVaporComponent({
  2543. setup() {
  2544. const n0 = template('<div>error</div>')()
  2545. insert(createSlot('default'), n0 as any as ParentNode)
  2546. return n0
  2547. },
  2548. }),
  2549. })
  2550. const VaporParent = defineVaporComponent({
  2551. setup() {
  2552. return createComponent(
  2553. Suspense as any,
  2554. null,
  2555. {
  2556. default: () =>
  2557. createComponent(
  2558. VaporAsyncChild,
  2559. null,
  2560. {
  2561. default: withVaporCtx(() =>
  2562. template('<span>slot content</span>')(),
  2563. ),
  2564. },
  2565. true,
  2566. ),
  2567. fallback: () => template('loading')(),
  2568. },
  2569. true,
  2570. )
  2571. },
  2572. })
  2573. const host = document.createElement('div')
  2574. const app = createApp({
  2575. render: () => h(VaporParent as any),
  2576. })
  2577. app.use(vaporInteropPlugin)
  2578. const errorHandler = vi.fn()
  2579. app.config.errorHandler = errorHandler
  2580. try {
  2581. app.mount(host)
  2582. expect(host.innerHTML).toContain('loading')
  2583. reject(new Error('errored out'))
  2584. await tick()
  2585. await nextTick()
  2586. expect(errorHandler).toHaveBeenCalled()
  2587. expect(host.innerHTML).toContain('error')
  2588. expect(host.innerHTML).not.toContain('slot content')
  2589. } finally {
  2590. app.unmount()
  2591. host.remove()
  2592. }
  2593. })
  2594. test('renders async setup vapor component inside VDOM Suspense', async () => {
  2595. const duration = 5
  2596. const VaporAsyncChild = defineVaporComponent({
  2597. async setup() {
  2598. await new Promise(resolve => setTimeout(resolve, duration))
  2599. return template('<div><button>click</button></div>')()
  2600. },
  2601. })
  2602. const VaporParent = defineVaporComponent({
  2603. setup() {
  2604. return createComponent(
  2605. Suspense as any,
  2606. null,
  2607. {
  2608. default: () => createComponent(VaporAsyncChild, null, null, true),
  2609. fallback: () => template('loading')(),
  2610. },
  2611. true,
  2612. )
  2613. },
  2614. })
  2615. const { html } = define({
  2616. setup() {
  2617. return () => h(VaporParent as any)
  2618. },
  2619. }).render()
  2620. expect(html()).toContain('loading')
  2621. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2622. await nextTick()
  2623. expect(html()).toContain('<div><button>click</button></div>')
  2624. })
  2625. test('renders async VDOM child inside VDOM Suspense', async () => {
  2626. const duration = 5
  2627. const VDomAsyncChild = defineComponent({
  2628. async setup() {
  2629. await new Promise(resolve => setTimeout(resolve, duration))
  2630. return () => h('div', [h('button', 'click')])
  2631. },
  2632. })
  2633. const VaporParent = defineVaporComponent({
  2634. setup() {
  2635. return createComponent(
  2636. Suspense as any,
  2637. null,
  2638. {
  2639. default: () =>
  2640. createComponent(VDomAsyncChild as any, null, null, true),
  2641. fallback: () => template('loading')(),
  2642. },
  2643. true,
  2644. )
  2645. },
  2646. })
  2647. const { html } = define({
  2648. setup() {
  2649. return () => h(VaporParent as any)
  2650. },
  2651. }).render()
  2652. expect(html()).toContain('loading')
  2653. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2654. await nextTick()
  2655. expect(html()).toContain('<div><button>click</button></div>')
  2656. })
  2657. test('renders async VDOM child from vapor slot outlet inside VDOM Suspense', async () => {
  2658. const duration = 5
  2659. const VaporSlotOutlet = defineVaporComponent({
  2660. setup() {
  2661. return createSlot('default')
  2662. },
  2663. })
  2664. const VDomAsyncChild = defineComponent({
  2665. async setup() {
  2666. await new Promise(resolve => setTimeout(resolve, duration))
  2667. return () => h('div', 'slot async')
  2668. },
  2669. })
  2670. const App = defineComponent({
  2671. setup() {
  2672. return () =>
  2673. h(Suspense, null, {
  2674. default: () =>
  2675. h(VaporSlotOutlet as any, null, {
  2676. default: () => [h(VDomAsyncChild as any)],
  2677. }),
  2678. fallback: () => h('div', 'loading'),
  2679. })
  2680. },
  2681. })
  2682. const { html } = define(App).render()
  2683. expect(html()).toContain('loading')
  2684. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2685. await nextTick()
  2686. expect(html()).toContain('<div>slot async</div>')
  2687. })
  2688. test('renders async VDOM vnode via createDynamicComponent inside VDOM Suspense', async () => {
  2689. const duration = 5
  2690. const VDomAsyncChild = defineComponent({
  2691. async setup() {
  2692. await new Promise(resolve => setTimeout(resolve, duration))
  2693. return () => h('button', 'vnode async')
  2694. },
  2695. })
  2696. const VaporParent = defineVaporComponent({
  2697. setup() {
  2698. return createComponent(
  2699. Suspense as any,
  2700. null,
  2701. {
  2702. default: () =>
  2703. createDynamicComponent(
  2704. () => h(VDomAsyncChild as any),
  2705. null,
  2706. null,
  2707. true,
  2708. ),
  2709. fallback: () => template('loading')(),
  2710. },
  2711. true,
  2712. )
  2713. },
  2714. })
  2715. const { html } = define({
  2716. setup() {
  2717. return () => h(VaporParent as any)
  2718. },
  2719. }).render()
  2720. expect(html()).toContain('loading')
  2721. await new Promise(resolve => setTimeout(resolve, duration + 1))
  2722. await nextTick()
  2723. expect(html()).toContain('<button>vnode async</button>')
  2724. })
  2725. test('mounts VDOM Suspense from createDynamicComponent', async () => {
  2726. const VaporChild = defineVaporComponent({
  2727. setup() {
  2728. return createDynamicComponent(
  2729. () => Suspense,
  2730. null,
  2731. {
  2732. default: () => template('<span>resolved</span>')(),
  2733. fallback: () => template('<span>fallback</span>')(),
  2734. },
  2735. true,
  2736. )
  2737. },
  2738. })
  2739. const { html } = define({
  2740. setup() {
  2741. return () => h(VaporChild as any)
  2742. },
  2743. }).render()
  2744. await nextTick()
  2745. expect(html()).toContain('<span>resolved</span>')
  2746. })
  2747. })
  2748. describe('vnode hooks', () => {
  2749. test('should invoke onVnodeBeforeMount/onVnodeBeforeUnmount on vapor child', async () => {
  2750. const beforeMountSpy = vi.fn()
  2751. const beforeUnmountSpy = vi.fn()
  2752. const VaporChild = defineVaporComponent({
  2753. setup() {
  2754. return template('<div>vapor</div>')()
  2755. },
  2756. })
  2757. const show = ref(true)
  2758. const App = defineComponent({
  2759. setup() {
  2760. return () =>
  2761. show.value
  2762. ? h(VaporChild as any, {
  2763. onVnodeBeforeMount: beforeMountSpy,
  2764. onVnodeBeforeUnmount: beforeUnmountSpy,
  2765. })
  2766. : null
  2767. },
  2768. })
  2769. const root = document.createElement('div')
  2770. const app = createApp(App)
  2771. app.use(vaporInteropPlugin)
  2772. app.mount(root)
  2773. await nextTick()
  2774. expect(beforeMountSpy).toHaveBeenCalledTimes(1)
  2775. // unmount
  2776. show.value = false
  2777. await nextTick()
  2778. expect(beforeUnmountSpy).toHaveBeenCalledTimes(1)
  2779. })
  2780. test('should invoke update hooks in VDOM order on vapor child self-update', async () => {
  2781. const calls: string[] = []
  2782. let flip!: () => void
  2783. const VaporChild = defineVaporComponent({
  2784. setup() {
  2785. const alt = ref(false)
  2786. onBeforeUpdate(() => calls.push('component beforeUpdate'))
  2787. onUpdated(() => calls.push('component updated'))
  2788. flip = () => {
  2789. alt.value = true
  2790. }
  2791. return createIf(
  2792. () => alt.value,
  2793. () => template('<p>alt</p>')(),
  2794. () => template('<div>base</div>')(),
  2795. )
  2796. },
  2797. })
  2798. const App = defineComponent({
  2799. setup() {
  2800. return () =>
  2801. h(VaporChild as any, {
  2802. onVnodeBeforeUpdate: (vnode: any, prevVNode: any) => {
  2803. expect((vnode.el as Element).tagName).toBe('DIV')
  2804. expect((prevVNode.el as Element).tagName).toBe('DIV')
  2805. calls.push('vnode beforeUpdate')
  2806. },
  2807. onVnodeUpdated: (vnode: any) => {
  2808. expect((vnode.el as Element).tagName).toBe('P')
  2809. calls.push('vnode updated')
  2810. },
  2811. })
  2812. },
  2813. })
  2814. const root = document.createElement('div')
  2815. const app = createApp(App)
  2816. app.use(vaporInteropPlugin)
  2817. app.mount(root)
  2818. expect(root.querySelector('div')!.textContent).toBe('base')
  2819. flip()
  2820. await nextTick()
  2821. expect(root.querySelector('p')!.textContent).toBe('alt')
  2822. expect(calls).toEqual([
  2823. 'component beforeUpdate',
  2824. 'vnode beforeUpdate',
  2825. 'component updated',
  2826. 'vnode updated',
  2827. ])
  2828. })
  2829. })
  2830. })