| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211 |
- import {
- KeepAlive,
- type ShallowRef,
- Suspense,
- Teleport,
- createApp,
- createCommentVNode,
- createVNode,
- defineComponent,
- h,
- inject,
- nextTick,
- onActivated,
- onBeforeMount,
- onDeactivated,
- onMounted,
- onUnmounted,
- provide,
- ref,
- renderSlot,
- resolveDynamicComponent,
- shallowRef,
- toDisplayString,
- useModel,
- useTemplateRef,
- vShow,
- withDirectives,
- } from '@vue/runtime-dom'
- import { makeInteropRender } from './_utils'
- import {
- VaporKeepAlive,
- VaporTeleport,
- applyTextModel,
- applyVShow,
- child,
- createComponent,
- createDynamicComponent,
- createForSlots,
- createIf,
- createSlot,
- createTemplateRefSetter,
- defineVaporAsyncComponent,
- defineVaporComponent,
- insert,
- renderEffect,
- setText,
- template,
- txt,
- vaporInteropPlugin,
- withVaporCtx,
- } from '../src'
- const define = makeInteropRender()
- describe('vdomInterop', () => {
- describe('key', () => {
- test('preserves vnode key on blocks passed from vdom to vapor', () => {
- const VDomChild = defineComponent({
- setup() {
- return () => h('div', 'vdom child')
- },
- })
- const app = createApp({ render: () => null })
- app.use(vaporInteropPlugin)
- const vapor = (app._context as any).vapor
- const vnodeBlock = vapor.vdomMountVNode(
- h(VDomChild, { key: 'foo' }),
- null,
- )
- expect(vnodeBlock.$key).toBe('foo')
- expect(vnodeBlock.vnode.key).toBe('foo')
- const componentBlock = vapor.vdomMount(VDomChild, null, { key: 'bar' })
- expect(componentBlock.$key).toBe('bar')
- expect(componentBlock.vnode.key).toBe('bar')
- })
- test('preserves single slot vnode key on interop fragments', async () => {
- const key = ref('foo')
- const CompA = defineComponent({
- setup() {
- return () => h('div', 'A')
- },
- })
- const CompB = defineComponent({
- setup() {
- return () => h('div', 'B')
- },
- })
- const current = shallowRef<any>(CompA)
- const VaporChild = defineVaporComponent({
- setup() {
- return createSlot('default') as any
- },
- })
- const Parent = defineComponent({
- setup() {
- return () =>
- h(VaporChild as any, null, {
- default: () => [h(current.value, { key: key.value })],
- })
- },
- })
- const app = createApp(Parent)
- app.use(vaporInteropPlugin)
- const vapor = (app._context as any).vapor
- const originalVdomSlot = vapor.vdomSlot
- let frag: any
- vapor.vdomSlot = (...args: any[]) => (frag = originalVdomSlot(...args))
- const host = document.createElement('div')
- app.mount(host)
- expect(frag.$key).toBe('_defaultfoo')
- key.value = 'bar'
- current.value = CompB
- await nextTick()
- expect(frag.$key).toBe('_defaultbar')
- })
- })
- describe('fragment nodes', () => {
- test('refreshes interop fragment nodes after component root updates', async () => {
- const show = ref(false)
- const VDomChild = defineComponent({
- setup() {
- return () =>
- show.value ? h('div', 'child') : createCommentVNode('v-if', true)
- },
- })
- const app = createApp({ render: () => null })
- app.use(vaporInteropPlugin)
- const vapor = (app._context as any).vapor
- const host = document.createElement('div')
- const frag = vapor.vdomMount(VDomChild, null)
- insert(frag, host)
- expect(host.innerHTML).toBe('<!--v-if-->')
- expect(frag.nodes).toBeInstanceOf(Comment)
- show.value = true
- await nextTick()
- expect(host.innerHTML).toBe('<div>child</div>')
- expect(frag.nodes).toBeInstanceOf(HTMLDivElement)
- })
- test('refreshes vdom slot fragment nodes after child root updates', async () => {
- const show = ref(false)
- const VDomChild = defineComponent({
- setup() {
- return () =>
- show.value ? h('div', 'child') : createCommentVNode('v-if', true)
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createSlot('default') as any
- },
- })
- const Parent = defineComponent({
- setup() {
- return () =>
- h(VaporChild as any, null, {
- default: () => [h(VDomChild)],
- })
- },
- })
- const app = createApp(Parent)
- app.use(vaporInteropPlugin)
- const vapor = (app._context as any).vapor
- const originalVdomSlot = vapor.vdomSlot
- let frag: any
- vapor.vdomSlot = (...args: any[]) => (frag = originalVdomSlot(...args))
- const host = document.createElement('div')
- app.mount(host)
- const onUpdated = vi.fn()
- frag.onUpdated = [onUpdated]
- const getNodes = () =>
- (Array.isArray(frag.nodes) ? frag.nodes : [frag.nodes]).filter(Boolean)
- expect(host.innerHTML).toBe('<!--v-if-->')
- expect(getNodes().some((n: Node) => n instanceof HTMLDivElement)).toBe(
- false,
- )
- show.value = true
- await nextTick()
- expect(host.innerHTML).toContain('<div>child</div>')
- expect(getNodes().some((n: Node) => n instanceof HTMLDivElement)).toBe(
- true,
- )
- expect(onUpdated).toHaveBeenCalled()
- })
- })
- describe('props', () => {
- test('should work if props are not provided', () => {
- const VaporChild = defineVaporComponent({
- props: {
- msg: String,
- },
- setup(_, { attrs }) {
- return [document.createTextNode(attrs.class || 'foo')]
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('foo')
- })
- test('should handle class prop when vapor renders vdom component', () => {
- const VDomChild = defineComponent({
- setup() {
- return () => h('div', { class: 'foo' })
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(VDomChild as any, { class: () => 'bar' })
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div class="foo bar"></div>')
- })
- test('should not pass reserved props into vapor attrs on update', async () => {
- const msg = ref('foo')
- const onVnodeMounted = vi.fn()
- const VaporChild = defineVaporComponent({
- setup(_, { attrs }) {
- const n0 = template(' ')() as any
- renderEffect(() => {
- setText(
- n0,
- `${String(attrs.msg)}|${String('onVnodeMounted' in attrs)}`,
- )
- })
- return n0
- },
- })
- const { html } = define({
- setup() {
- return () =>
- h(VaporChild as any, {
- msg: msg.value,
- onVnodeMounted,
- })
- },
- }).render()
- expect(html()).toBe('foo|false')
- msg.value = 'bar'
- await nextTick()
- expect(html()).toBe('bar|false')
- })
- test('should invoke onVnodeMounted and onVnodeUnmounted', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor</div>')()
- },
- })
- const show = ref(true)
- const vnodeMounted = vi.fn()
- const vnodeUnmounted = vi.fn()
- const { html } = define({
- setup() {
- return () =>
- show.value
- ? h(VaporChild as any, {
- onVnodeMounted: vnodeMounted,
- onVnodeUnmounted: vnodeUnmounted,
- })
- : null
- },
- }).render()
- await nextTick()
- expect(html()).toBe('<div>vapor</div>')
- expect(vnodeMounted).toHaveBeenCalledTimes(1)
- expect(vnodeUnmounted).toHaveBeenCalledTimes(0)
- show.value = false
- await nextTick()
- expect(vnodeUnmounted).toHaveBeenCalledTimes(1)
- })
- test('should invoke vnode and directive mount hooks in VDOM order', async () => {
- const calls: string[] = []
- const vCustom = {
- created: vi.fn(() => calls.push('directive created')),
- beforeMount: vi.fn(() => calls.push('directive beforeMount')),
- mounted: vi.fn(() => calls.push('directive mounted')),
- }
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor</div>')()
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- withDirectives(
- h(VaporChild as any, {
- onVnodeBeforeMount: () => calls.push('vnode beforeMount'),
- onVnodeMounted: () => calls.push('vnode mounted'),
- }),
- [[vCustom]],
- )
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- expect(calls).toEqual([
- 'vnode beforeMount',
- 'directive created',
- 'directive beforeMount',
- 'directive mounted',
- 'vnode mounted',
- ])
- })
- test('should invoke vnode and directive unmount hooks in VDOM order', async () => {
- const calls: string[] = []
- const vCustom = {
- beforeUnmount: vi.fn(() => calls.push('directive beforeUnmount')),
- unmounted: vi.fn(() => calls.push('directive unmounted')),
- }
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor</div>')()
- },
- })
- const show = ref(true)
- const { html } = define({
- setup() {
- return () =>
- show.value
- ? withDirectives(
- h(VaporChild as any, {
- onVnodeBeforeUnmount: () =>
- calls.push('vnode beforeUnmount'),
- onVnodeUnmounted: () => calls.push('vnode unmounted'),
- }),
- [[vCustom]],
- )
- : null
- },
- }).render()
- expect(html()).toBe('<div>vapor</div>')
- show.value = false
- await nextTick()
- expect(calls).toEqual([
- 'vnode beforeUnmount',
- 'directive beforeUnmount',
- 'directive unmounted',
- 'vnode unmounted',
- ])
- })
- test('should invoke update hooks in VDOM order on normal updates', async () => {
- const msg = ref('foo')
- const calls: string[] = []
- const vCustom = {
- beforeUpdate: vi.fn(() => calls.push('directive beforeUpdate')),
- updated: vi.fn(() => calls.push('directive updated')),
- }
- const VaporChild = defineVaporComponent({
- props: {
- msg: String,
- },
- setup(props: any) {
- const n0 = template('<div> </div>', true)() as any
- const x0 = child(n0) as any
- renderEffect(() => {
- setText(x0, props.msg)
- })
- return n0
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- withDirectives(
- h(VaporChild as any, {
- msg: msg.value,
- onVnodeBeforeUpdate: () => calls.push('vnode beforeUpdate'),
- onVnodeUpdated: () => calls.push('vnode updated'),
- }),
- [[vCustom]],
- )
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- msg.value = 'bar'
- await nextTick()
- expect(calls).toEqual([
- 'vnode beforeUpdate',
- 'directive beforeUpdate',
- 'directive updated',
- 'vnode updated',
- ])
- })
- })
- describe('v-model', () => {
- test('basic work', async () => {
- const VaporChild = defineVaporComponent({
- props: {
- modelValue: {},
- modelModifiers: {},
- },
- emits: ['update:modelValue'],
- setup(__props) {
- const modelValue = useModel(__props, 'modelValue')
- const n0 = template('<h1> </h1>')() as any
- const n1 = template('<input>')() as any
- const x0 = child(n0) as any
- applyTextModel(
- n1,
- () => modelValue.value,
- _value => (modelValue.value = _value),
- )
- renderEffect(() => setText(x0, toDisplayString(modelValue.value)))
- return [n0, n1]
- },
- })
- const { html, host } = define({
- setup() {
- const msg = ref('foo')
- return () =>
- h(VaporChild as any, {
- modelValue: msg.value,
- 'onUpdate:modelValue': (value: string) => {
- msg.value = value
- },
- })
- },
- }).render()
- expect(html()).toBe('<h1>foo</h1><input>')
- const inputEl = host.querySelector('input')!
- inputEl.value = 'bar'
- inputEl.dispatchEvent(new Event('input'))
- await nextTick()
- expect(html()).toBe('<h1>bar</h1><input>')
- })
- test('slot v-model should persist when switching vapor/vdom child', async () => {
- const VaporComp1 = defineVaporComponent({
- name: 'VaporComp1',
- setup() {
- return [document.createTextNode('comp1: '), createSlot('default')]
- },
- })
- const VDomComp2 = defineComponent({
- name: 'VDomComp2',
- setup(_, { slots }) {
- return () =>
- h('div', [
- 'comp2: ',
- // vdom <slot/>
- renderSlot(slots, 'default'),
- ])
- },
- })
- const VaporParent = defineVaporComponent({
- name: 'VaporParent',
- props: {
- show: Boolean,
- modelValue: {},
- modelModifiers: {},
- },
- emits: ['update:modelValue'],
- setup(__props) {
- const modelValue = useModel(__props, 'modelValue')
- return createDynamicComponent(
- () => (__props.show ? VaporComp1 : VDomComp2),
- null,
- {
- default: () => {
- const input = template('<input>')() as any
- applyTextModel(
- input,
- () => modelValue.value,
- _value => (modelValue.value = _value),
- )
- return input
- },
- },
- true,
- )
- },
- })
- const show = ref(true)
- const msg = ref('')
- const { host } = define({
- setup() {
- return () =>
- h(VaporParent as any, {
- show: show.value,
- modelValue: msg.value,
- 'onUpdate:modelValue': (value: string) => {
- msg.value = value
- },
- })
- },
- }).render()
- const input1 = host.querySelector('input')!
- input1.value = 'hello'
- input1.dispatchEvent(new Event('input'))
- await nextTick()
- expect(msg.value).toBe('hello')
- show.value = false
- await nextTick()
- const input2 = host.querySelector('input')!
- expect(input2.value).toBe('hello')
- })
- })
- describe('emit', () => {
- test('emit from vapor child to vdom parent', () => {
- const VaporChild = defineVaporComponent({
- emits: ['click'],
- setup(_, { emit }) {
- emit('click')
- return []
- },
- })
- const fn = vi.fn()
- define({
- setup() {
- return () => h(VaporChild as any, { onClick: fn })
- },
- }).render()
- // fn should be called once
- expect(fn).toHaveBeenCalledTimes(1)
- })
- })
- describe('directives', () => {
- test('apply v-show to vdom child', async () => {
- const VDomChild = {
- setup() {
- return () => h('div')
- },
- }
- const show = ref(false)
- const VaporChild = defineVaporComponent({
- setup() {
- const n1 = createComponent(VDomChild as any)
- applyVShow(n1, () => show.value)
- return n1
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div style="display: none;"></div>')
- show.value = true
- await nextTick()
- expect(html()).toBe('<div style=""></div>')
- })
- test('apply v-show to vapor child', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div></div>', true)()
- },
- })
- const show = ref(false)
- const App = defineComponent({
- setup() {
- return () =>
- h('div', null, [
- withDirectives(h(VaporChild as any), [[vShow, show.value]]),
- ])
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- expect(root.innerHTML).toBe(
- '<div><div style="display: none;"></div></div>',
- )
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<div><div style=""></div></div>')
- })
- test('apply custom directive to vapor child', async () => {
- const vCustom = {
- created: vi.fn(),
- beforeMount: vi.fn(),
- mounted: vi.fn(),
- beforeUpdate: vi.fn(),
- updated: vi.fn(),
- beforeUnmount: vi.fn(),
- unmounted: vi.fn(),
- }
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div></div>', true)()
- },
- })
- const count = ref(0)
- const App = defineComponent({
- setup() {
- return () =>
- h('div', null, [
- withDirectives(h(VaporChild as any), [[vCustom, count.value]]),
- ])
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- // root > div (App root) > div (VaporChild root)
- const el = root.querySelector('div')!.querySelector('div')!
- expect(vCustom.created).toHaveBeenCalledTimes(1)
- expect(vCustom.beforeMount).toHaveBeenCalledTimes(1)
- expect(vCustom.mounted).toHaveBeenCalledTimes(1)
- expect(vCustom.beforeUpdate).toHaveBeenCalledTimes(0)
- expect(vCustom.updated).toHaveBeenCalledTimes(0)
- expect(vCustom.created).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 0, oldValue: undefined }),
- expect.any(Object),
- null,
- )
- expect(vCustom.beforeMount).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 0, oldValue: undefined }),
- expect.any(Object),
- null,
- )
- expect(vCustom.mounted).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 0, oldValue: undefined }),
- expect.any(Object),
- null,
- )
- count.value++
- await nextTick()
- expect(vCustom.beforeUpdate).toHaveBeenCalledTimes(1)
- expect(vCustom.updated).toHaveBeenCalledTimes(1)
- expect(vCustom.beforeUpdate).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 1, oldValue: 0 }),
- expect.any(Object),
- expect.any(Object),
- )
- expect(vCustom.updated).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 1, oldValue: 0 }),
- expect.any(Object),
- expect.any(Object),
- )
- app.unmount()
- expect(vCustom.beforeUnmount).toHaveBeenCalledTimes(1)
- expect(vCustom.unmounted).toHaveBeenCalledTimes(1)
- expect(vCustom.beforeUnmount).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 1, oldValue: 0 }),
- expect.any(Object),
- null,
- )
- expect(vCustom.unmounted).toHaveBeenCalledWith(
- el,
- expect.objectContaining({ value: 1, oldValue: 0 }),
- expect.any(Object),
- null,
- )
- })
- test('warn on directive with non-element root vapor child', () => {
- const calls: string[] = []
- const vCustom = {
- created: () => calls.push('created'),
- beforeMount: () => calls.push('beforeMount'),
- mounted: () => calls.push('mounted'),
- beforeUpdate: () => calls.push('beforeUpdate'),
- updated: () => calls.push('updated'),
- beforeUnmount: () => calls.push('beforeUnmount'),
- unmounted: () => calls.push('unmounted'),
- }
- const VaporChild = defineVaporComponent({
- setup() {
- return [template('<div></div>')(), template('<div></div>')()]
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- h('div', null, [withDirectives(h(VaporChild as any), [[vCustom]])])
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- if (__DEV__) {
- expect(
- `Runtime directive used on component with non-element root node.`,
- ).toHaveBeenWarned()
- }
- expect(calls.length).toBe(0)
- app.unmount()
- })
- test('should expose the latest vapor root element in updated hooks', async () => {
- const useAltRoot = ref(false)
- const updatedSpy = vi.fn((vnode: any) => {
- expect((vnode.el as Element).tagName).toBe('P')
- })
- const VaporChild = defineVaporComponent({
- props: {
- alt: Boolean,
- },
- setup(props: any) {
- return createIf(
- () => props.alt,
- () => template('<p>alt</p>')(),
- () => template('<div>base</div>')(),
- )
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- h(VaporChild as any, {
- alt: useAltRoot.value,
- onVnodeUpdated: updatedSpy,
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- useAltRoot.value = true
- await nextTick()
- expect(root.querySelector('p')).not.toBeNull()
- expect(updatedSpy).toHaveBeenCalledTimes(1)
- })
- })
- describe('slots', () => {
- test('basic', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => renderSlot(slots, 'default')
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => document.createTextNode('default slot'),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('default slot')
- })
- test('functional slot', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => createVNode(slots.default!)
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => document.createTextNode('default slot'),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('default slot')
- })
- test('slots.default() direct invocation', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, slots.default!())
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => template('direct call slot')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div>direct call slot</div>')
- })
- test('slots.default() access should return a stable wrapper', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- const first = slots.default
- const second = slots.default
- return () => h('div', String(first === second))
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => template('stable slot wrapper')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div>true</div>')
- })
- test('slots.default() with slot props', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, slots.default!({ msg: 'hello' }))
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: (props: { msg: string }) => {
- const n0 = template('<span></span>')()
- n0.textContent = props.msg
- return [n0]
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div><span>hello</span></div>')
- })
- test('slots.default() with falsy slot props should keep has/ownKeys semantics', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () =>
- h('div', null, slots.default!({ flag: false, count: 0, text: '' }))
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: (props: Record<string, any>) => {
- const n0 = document.createTextNode(
- `${'flag' in props}/${'count' in props}/${'text' in props}|` +
- `${Object.keys(props).join(',')}|` +
- `${String(props.flag)},${String(props.count)},${String(props.text)}`,
- )
- return [n0]
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div>true/true/true|flag,count,text|false,0,</div>')
- })
- test('named slot with slots[name]() invocation', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () =>
- h('div', null, [
- h('header', null, slots.header!()),
- h('main', null, slots.default!()),
- h('footer', null, slots.footer!()),
- ])
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- header: () => template('Header')(),
- default: () => template('Main')(),
- footer: () => template('Footer')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe(
- '<div><header>Header</header><main>Main</main><footer>Footer</footer></div>',
- )
- })
- test('slots.default() return directly', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => slots.default!()
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => template('direct return slot')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('direct return slot')
- })
- test('rendering forwarding vapor slot', () => {
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, { default: slots.default })
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- null,
- {
- default: () => template('forwarded slot')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div>forwarded slot</div>')
- })
- test('dynamic slots via createForSlots should update in vdom child', async () => {
- const list = ref([0, 1, 2])
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, [renderSlot(slots, 'default')])
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(VDomChild as any, null, {
- $: [
- () =>
- createForSlots(list.value, value => ({
- name: 'default',
- fn: () => {
- const n = template('<span> </span>')() as Element
- const t = txt(n) as Text
- renderEffect(() => setText(t, toDisplayString(value)))
- return n
- },
- })),
- ],
- })
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- // last-wins: shows last item
- expect(html()).toBe('<div><span>2</span></div>')
- list.value.push(3)
- await nextTick()
- expect(html()).toBe('<div><span>3</span></div>')
- list.value.pop()
- list.value.pop()
- await nextTick()
- expect(html()).toBe('<div><span>1</span></div>')
- })
- test('dynamic slots via createForSlots should re-mount fragment slot in vdom child', async () => {
- const list = ref([0, 1, 2])
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, [renderSlot(slots, 'default')])
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(VDomChild as any, null, {
- $: [
- () =>
- createForSlots(list.value, value => ({
- name: 'default',
- fn: () =>
- createIf(
- () => true,
- () => {
- const n = template('<span> </span>')() as Element
- const t = txt(n) as Text
- renderEffect(() => setText(t, toDisplayString(value)))
- return n
- },
- ),
- })),
- ],
- })
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).toBe('<div><span>2</span><!--if--></div>')
- list.value.push(3)
- await nextTick()
- expect(html()).toBe('<div><span>3</span><!--if--></div>')
- list.value.pop()
- list.value.pop()
- await nextTick()
- expect(html()).toBe('<div><span>1</span><!--if--></div>')
- })
- test('dynamic slot re-mount should stop stale effects from previous slot function', async () => {
- const list = ref([0, 1])
- const slotStates = new Map([
- [0, { text: ref('zero'), runs: vi.fn() }],
- [1, { text: ref('one'), runs: vi.fn() }],
- [2, { text: ref('two'), runs: vi.fn() }],
- ])
- const VDomChild = defineComponent({
- setup(_, { slots }) {
- return () => h('div', null, [renderSlot(slots, 'default')])
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(VDomChild as any, null, {
- $: [
- () =>
- createForSlots(list.value, value => ({
- name: 'default',
- fn: () => {
- const state = slotStates.get(value)!
- const n = template('<span> </span>')() as Element
- const t = txt(n) as Text
- renderEffect(() => {
- state.runs()
- setText(t, state.text.value)
- })
- return n
- },
- })),
- ],
- })
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- const firstState = slotStates.get(1)!
- expect(html()).toBe('<div><span>one</span></div>')
- expect(firstState.runs).toHaveBeenCalledTimes(1)
- list.value.push(2)
- await nextTick()
- expect(html()).toBe('<div><span>two</span></div>')
- expect(firstState.runs).toHaveBeenCalledTimes(1)
- firstState.text.value = 'stale-one'
- await nextTick()
- expect(html()).toBe('<div><span>two</span></div>')
- expect(firstState.runs).toHaveBeenCalledTimes(1)
- })
- test('should stop vdom slot outlet effects after outlet unmount', async () => {
- const showOutlet = ref(true)
- const msg = ref('one')
- const track = vi.fn()
- const VaporChild = defineVaporComponent({
- setup() {
- return createIf(
- () => showOutlet.value,
- () => createSlot('default'),
- )
- },
- })
- const { html } = define({
- setup() {
- return () =>
- h(VaporChild as any, null, {
- default: () => {
- track()
- return [h('span', msg.value)]
- },
- })
- },
- }).render()
- expect(html()).toBe('<span>one</span><!--if-->')
- expect(track).toHaveBeenCalledTimes(1)
- showOutlet.value = false
- await nextTick()
- expect(html()).toBe('<!--if-->')
- msg.value = 'two'
- await nextTick()
- expect(track).toHaveBeenCalledTimes(1)
- expect(html()).toBe('<!--if-->')
- })
- })
- describe('provide / inject', () => {
- it('should inject value from vdom parent', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- const foo = inject('foo')
- const n0 = template(' ')() as any
- renderEffect(() => setText(n0, toDisplayString(foo)))
- return n0
- },
- })
- const value = ref('foo')
- const { html } = define({
- setup() {
- provide('foo', value)
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('foo')
- value.value = 'bar'
- await nextTick()
- expect(html()).toBe('bar')
- })
- })
- describe('template ref', () => {
- it('useTemplateRef with vapor child', async () => {
- const VaporChild = defineVaporComponent({
- setup(_, { expose }) {
- const foo = ref('foo')
- expose({ foo })
- const n0 = template(' ')() as any
- renderEffect(() => setText(n0, toDisplayString(foo)))
- return n0
- },
- })
- let elRef: ShallowRef
- const { html } = define({
- setup() {
- elRef = useTemplateRef('el')
- return () => h(VaporChild as any, { ref: 'el' })
- },
- }).render()
- expect(html()).toBe('foo')
- elRef!.value.foo = 'bar'
- await nextTick()
- expect(html()).toBe('bar')
- })
- it('static ref with vapor child', async () => {
- const VaporChild = defineVaporComponent({
- setup(_, { expose }) {
- const foo = ref('foo')
- expose({ foo })
- const n0 = template(' ')() as any
- renderEffect(() => setText(n0, toDisplayString(foo)))
- return n0
- },
- })
- let elRef: ShallowRef
- const { html } = define({
- setup() {
- elRef = shallowRef()
- return { elRef }
- },
- render() {
- return h(VaporChild as any, { ref: 'elRef' })
- },
- }).render()
- expect(html()).toBe('foo')
- elRef!.value.foo = 'bar'
- await nextTick()
- expect(html()).toBe('bar')
- })
- it('dynamic component includes vdom component', async () => {
- const vdomRef = ref<any>(null)
- const VdomChild = defineComponent({
- setup(_, { expose }) {
- expose({ name: 'vdomChild' })
- return () => h('div', 'vdom child')
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return { vdomRef }
- },
- render() {
- const setRef = createTemplateRefSetter()
- const n0 = createDynamicComponent(() => VdomChild)
- setRef(n0, vdomRef, false, 'vdomRef')
- return n0
- },
- })
- define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- await nextTick()
- expect(vdomRef.value).toBeDefined()
- expect(vdomRef.value.name).toBe('vdomChild')
- })
- it('dynamic component includes vdom component should cleanup old ref', async () => {
- const VdomChild = defineComponent({
- setup(_, { expose }) {
- expose({ name: 'vdomChild' })
- return () => h('div', 'vdom child')
- },
- })
- const useA = ref(true)
- const refA = ref<any>(null)
- const refB = ref<any>(null)
- const VaporChild = defineVaporComponent({
- setup() {
- const setRef = createTemplateRefSetter()
- const n0 = createDynamicComponent(() => VdomChild)
- renderEffect(() => {
- setRef(n0, useA.value ? refA : refB, false, 'vdomRef')
- })
- return n0
- },
- })
- define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- await nextTick()
- expect(refA.value).toBeDefined()
- expect(refA.value.name).toBe('vdomChild')
- expect(refB.value).toBe(null)
- useA.value = false
- await nextTick()
- expect(refA.value).toBe(null)
- expect(refB.value).toBeDefined()
- expect(refB.value.name).toBe('vdomChild')
- })
- })
- describe('dynamic component', () => {
- it('dynamic component with vapor child', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor child</div>')() as any
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('div', 'vdom child')
- },
- })
- const view = shallowRef<any>(VaporChild)
- const { html } = define({
- setup() {
- return () => h(resolveDynamicComponent(view.value) as any)
- },
- }).render()
- expect(html()).toBe('<div>vapor child</div>')
- view.value = VdomChild
- await nextTick()
- expect(html()).toBe('<div>vdom child</div>')
- view.value = VaporChild
- await nextTick()
- expect(html()).toBe('<div>vapor child</div>')
- })
- describe('render VNodes', () => {
- it('should render VNode containing vapor component from VDOM slot', async () => {
- const VaporComp = defineVaporComponent({
- setup() {
- return template('<div>vapor comp</div>')() as any
- },
- })
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(VaporComp as any)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createDynamicComponent(() => slotProps.Component)
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vapor comp</div><!--dynamic-component-->')
- })
- it('should render VNode containing vdom component from VDOM slot', async () => {
- const VdomComp = defineComponent({
- setup() {
- return () => h('div', 'vdom comp')
- },
- })
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(VdomComp)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createDynamicComponent(() => slotProps.Component)
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vdom comp</div><!--dynamic-component-->')
- })
- it('should update when VNode changes', async () => {
- const VaporCompA = defineVaporComponent({
- setup() {
- return template('<div>vapor A</div>')() as any
- },
- })
- const VaporCompB = defineVaporComponent({
- setup() {
- return template('<div>vapor B</div>')() as any
- },
- })
- const current = shallowRef<any>(VaporCompA)
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(current.value as any)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createDynamicComponent(() => slotProps.Component)
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
- current.value = VaporCompB
- await nextTick()
- expect(html()).toBe('<div>vapor B</div><!--dynamic-component-->')
- })
- describe('with VaporKeepAlive', () => {
- it('switch VNode with inner vapor components', async () => {
- const hooksA = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const hooksB = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const VaporCompA = defineVaporComponent({
- setup() {
- onMounted(() => hooksA.mounted())
- onActivated(() => hooksA.activated())
- onDeactivated(() => hooksA.deactivated())
- onUnmounted(() => hooksA.unmounted())
- return template('<div>vapor A</div>')() as any
- },
- })
- const VaporCompB = defineVaporComponent({
- setup() {
- onMounted(() => hooksB.mounted())
- onActivated(() => hooksB.activated())
- onDeactivated(() => hooksB.deactivated())
- onUnmounted(() => hooksB.unmounted())
- return template('<div>vapor B</div>')() as any
- },
- })
- const current = shallowRef<any>(VaporCompA)
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(current.value as any)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createComponent(VaporKeepAlive, null, {
- default: () =>
- createDynamicComponent(() => slotProps.Component),
- })
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
- // A: mounted + activated
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(1)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- current.value = VaporCompB
- await nextTick()
- expect(html()).toBe('<div>vapor B</div><!--dynamic-component-->')
- // A: deactivated (cached)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- // B: mounted + activated
- expect(hooksB.mounted).toHaveBeenCalledTimes(1)
- expect(hooksB.activated).toHaveBeenCalledTimes(1)
- current.value = VaporCompA
- await nextTick()
- expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
- // B: deactivated (cached)
- expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
- // A: re-activated (not re-mounted)
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(2)
- })
- it('switch VNode with inner VDOM components', async () => {
- const hooksA = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const hooksB = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const VDOMCompA = defineComponent({
- setup() {
- onMounted(() => hooksA.mounted())
- onActivated(() => hooksA.activated())
- onDeactivated(() => hooksA.deactivated())
- onUnmounted(() => hooksA.unmounted())
- return () => h('div', 'vdom A')
- },
- })
- const VDOMCompB = defineComponent({
- setup() {
- onMounted(() => hooksB.mounted())
- onActivated(() => hooksB.activated())
- onDeactivated(() => hooksB.deactivated())
- onUnmounted(() => hooksB.unmounted())
- return () => h('div', 'vdom B')
- },
- })
- const current = shallowRef<any>(VDOMCompA)
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(current.value as any)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createComponent(VaporKeepAlive, null, {
- default: () =>
- createDynamicComponent(() => slotProps.Component),
- })
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vdom A</div><!--dynamic-component-->')
- // A: mounted + activated
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(1)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- current.value = VDOMCompB
- await nextTick()
- expect(html()).toBe('<div>vdom B</div><!--dynamic-component-->')
- // A: deactivated (cached)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- // B: mounted + activated
- expect(hooksB.mounted).toHaveBeenCalledTimes(1)
- expect(hooksB.activated).toHaveBeenCalledTimes(1)
- current.value = VDOMCompA
- await nextTick()
- expect(html()).toBe('<div>vdom A</div><!--dynamic-component-->')
- // B: deactivated (cached)
- expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
- // A: re-activated (not re-mounted)
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(2)
- })
- it('switch VNode with inner mixed vapor/VDOM components', async () => {
- const hooksA = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const hooksB = {
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- const VaporCompA = defineVaporComponent({
- setup() {
- onMounted(() => hooksA.mounted())
- onActivated(() => hooksA.activated())
- onDeactivated(() => hooksA.deactivated())
- onUnmounted(() => hooksA.unmounted())
- return template('<div>vapor A</div>')()
- },
- })
- const VDOMCompB = defineComponent({
- setup() {
- onMounted(() => hooksB.mounted())
- onActivated(() => hooksB.activated())
- onDeactivated(() => hooksB.deactivated())
- onUnmounted(() => hooksB.unmounted())
- return () => h('div', 'vdom B')
- },
- })
- const current = shallowRef<any>(VaporCompA)
- const RouterView = defineComponent({
- setup(_, { slots }) {
- return () => {
- const component = h(current.value as any)
- return slots.default!({ Component: component })
- }
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- RouterView as any,
- null,
- {
- default: (slotProps: { Component: any }) => {
- return createComponent(VaporKeepAlive, null, {
- default: () =>
- createDynamicComponent(() => slotProps.Component),
- })
- },
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(App as any)
- },
- }).render()
- expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
- // A (vapor): mounted + activated
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(1)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(0)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- current.value = VDOMCompB
- await nextTick()
- expect(html()).toBe('<div>vdom B</div><!--dynamic-component-->')
- // A (vapor): deactivated (cached)
- expect(hooksA.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksA.unmounted).toHaveBeenCalledTimes(0)
- // B (vdom): mounted + activated
- expect(hooksB.mounted).toHaveBeenCalledTimes(1)
- expect(hooksB.activated).toHaveBeenCalledTimes(1)
- current.value = VaporCompA
- await nextTick()
- expect(html()).toBe('<div>vapor A</div><!--dynamic-component-->')
- // B (vdom): deactivated (cached)
- expect(hooksB.deactivated).toHaveBeenCalledTimes(1)
- expect(hooksB.unmounted).toHaveBeenCalledTimes(0)
- // A (vapor): re-activated (not re-mounted)
- expect(hooksA.mounted).toHaveBeenCalledTimes(1)
- expect(hooksA.activated).toHaveBeenCalledTimes(2)
- })
- })
- })
- })
- describe('attribute fallthrough', () => {
- it('should fallthrough attrs to vdom child', () => {
- const VDomChild = defineComponent({
- setup() {
- return () => h('div')
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- { foo: () => 'vapor foo' },
- null,
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any, { foo: 'foo', bar: 'bar' })
- },
- }).render()
- expect(html()).toBe('<div foo="foo" bar="bar"></div>')
- })
- it('should not fallthrough emit handlers to vdom child', () => {
- const VDomChild = defineComponent({
- emits: ['click'],
- setup(_, { emit }) {
- return () => h('button', { onClick: () => emit('click') }, 'click me')
- },
- })
- const fn = vi.fn()
- const VaporChild = defineVaporComponent({
- emits: ['click'],
- setup() {
- return createComponent(
- VDomChild as any,
- { onClick: () => fn },
- null,
- true,
- )
- },
- })
- const { host, html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<button>click me</button>')
- const button = host.querySelector('button')!
- button.dispatchEvent(new Event('click'))
- // fn should be called once
- expect(fn).toHaveBeenCalledTimes(1)
- })
- it('should update attrs passed from vapor parent to vdom child', async () => {
- const msg = ref('foo')
- const VDomChild = defineComponent({
- setup(_, { attrs }) {
- return () =>
- h(
- 'div',
- {
- 'data-msg': attrs['data-msg'] as string,
- },
- attrs['data-msg'] as string,
- )
- },
- })
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VDomChild as any,
- {
- 'data-msg': () => msg.value,
- },
- null,
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- expect(html()).toBe('<div data-msg="foo">foo</div>')
- msg.value = 'bar'
- await nextTick()
- expect(html()).toBe('<div data-msg="bar">bar</div>')
- })
- })
- describe('async component', () => {
- const duration = 5
- test('render vapor async component', async () => {
- const VdomChild = {
- setup() {
- return () => h('div', 'foo')
- },
- }
- const VaporAsyncChild = defineVaporAsyncComponent({
- loader: () => {
- return new Promise(r => {
- setTimeout(() => {
- r(VdomChild as any)
- }, duration)
- })
- },
- loadingComponent: () => h('span', 'loading...'),
- })
- const { html } = define({
- setup() {
- return () => h(VaporAsyncChild as any)
- },
- }).render()
- expect(html()).toBe('<span>loading...</span><!--async component-->')
- await new Promise(r => setTimeout(r, duration))
- await nextTick()
- expect(html()).toBe('<div>foo</div><!--async component-->')
- })
- })
- describe('KeepAlive', () => {
- function assertHookCalls(
- hooks: {
- beforeMount: any
- mounted: any
- activated: any
- deactivated: any
- unmounted: any
- },
- callCounts: number[],
- ) {
- expect([
- hooks.beforeMount.mock.calls.length,
- hooks.mounted.mock.calls.length,
- hooks.activated.mock.calls.length,
- hooks.deactivated.mock.calls.length,
- hooks.unmounted.mock.calls.length,
- ]).toEqual(callCounts)
- }
- let hooks: any
- beforeEach(() => {
- hooks = {
- beforeMount: vi.fn(),
- mounted: vi.fn(),
- activated: vi.fn(),
- deactivated: vi.fn(),
- unmounted: vi.fn(),
- }
- })
- test('render vapor component', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- const msg = ref('vapor')
- onBeforeMount(() => hooks.beforeMount())
- onMounted(() => hooks.mounted())
- onActivated(() => hooks.activated())
- onDeactivated(() => hooks.deactivated())
- onUnmounted(() => hooks.unmounted())
- const n0 = template('<input type="text">', true)() as any
- applyTextModel(
- n0,
- () => msg.value,
- _value => (msg.value = _value),
- )
- return n0
- },
- })
- const show = ref(true)
- const toggle = ref(true)
- const { html, host } = define({
- setup() {
- return () =>
- show.value
- ? h(KeepAlive, null, {
- default: () => (toggle.value ? h(VaporChild as any) : null),
- })
- : null
- },
- }).render()
- expect(html()).toBe('<input type="text">')
- let inputEl = host.firstChild as HTMLInputElement
- expect(inputEl.value).toBe('vapor')
- assertHookCalls(hooks, [1, 1, 1, 0, 0])
- // change input value
- inputEl.value = 'changed'
- inputEl.dispatchEvent(new Event('input'))
- await nextTick()
- // deactivate
- toggle.value = false
- await nextTick()
- expect(html()).toBe('<!---->')
- assertHookCalls(hooks, [1, 1, 1, 1, 0])
- // activate
- toggle.value = true
- await nextTick()
- expect(html()).toBe('<input type="text">')
- inputEl = host.firstChild as HTMLInputElement
- expect(inputEl.value).toBe('changed')
- assertHookCalls(hooks, [1, 1, 2, 1, 0])
- // unmount keepalive
- show.value = false
- await nextTick()
- expect(html()).toBe('<!---->')
- assertHookCalls(hooks, [1, 1, 2, 2, 1])
- // mount keepalive
- show.value = true
- await nextTick()
- inputEl = host.firstChild as HTMLInputElement
- expect(inputEl.value).toBe('vapor')
- assertHookCalls(hooks, [2, 2, 3, 2, 1])
- })
- test('render vapor slot', async () => {
- const show = ref(true)
- const VDomComp = defineComponent({
- setup(_, { slots }) {
- return () => renderSlot(slots, 'default')
- },
- })
- const App = defineVaporComponent({
- setup() {
- const n5 = createComponent(VaporKeepAlive, null, {
- default: () =>
- createIf(
- () => show.value,
- () =>
- createComponent(VDomComp as any, null, {
- default: () => template('slot text')(),
- }),
- ),
- })
- return n5
- },
- })
- const { html } = define({
- setup() {
- return () => h(App)
- },
- }).render()
- expect(html()).toBe('slot text<!--if-->')
- show.value = false
- await nextTick()
- expect(html()).toBe('<!--if-->')
- show.value = true
- await nextTick()
- expect(html()).toBe('slot text<!--if-->')
- })
- test('vdom slot fallback inside VaporKeepAlive should preserve render context', async () => {
- const show = ref(true)
- const VDomComp = defineComponent({
- setup(_, { slots }) {
- return () => renderSlot(slots, 'default')
- },
- })
- const VaporFallback = defineVaporComponent({
- setup() {
- onBeforeMount(() => hooks.beforeMount())
- onMounted(() => hooks.mounted())
- onActivated(() => hooks.activated())
- onDeactivated(() => hooks.deactivated())
- onUnmounted(() => hooks.unmounted())
- return template('<div>fallback</div>')() as any
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(VaporKeepAlive, null, {
- default: withVaporCtx(() =>
- createIf(
- () => show.value,
- () =>
- createComponent(
- VDomComp as any,
- null,
- {
- default: withVaporCtx(() =>
- createSlot('default', null, () =>
- createComponent(VaporFallback as any),
- ),
- ),
- },
- true,
- ),
- ),
- ),
- })
- },
- })
- const { html } = define({
- setup() {
- return () => h(App)
- },
- }).render()
- expect(html()).toBe('<div>fallback</div><!--if-->')
- assertHookCalls(hooks, [1, 1, 1, 0, 0])
- show.value = false
- await nextTick()
- expect(html()).toBe('<!--if-->')
- assertHookCalls(hooks, [1, 1, 1, 1, 0])
- show.value = true
- await nextTick()
- expect(html()).toBe('<div>fallback</div><!--if-->')
- assertHookCalls(hooks, [1, 1, 2, 1, 0])
- })
- test('unmounting vapor slot should remove vnode slot content', async () => {
- const show = ref(true)
- const VaporSlotOutlet = defineVaporComponent({
- setup() {
- return createSlot('default')
- },
- })
- const { html } = define({
- setup() {
- return () =>
- h('div', null, [
- show.value
- ? h(VaporSlotOutlet as any, null, {
- default: () => [h('span', 'slot vnode')],
- })
- : null,
- ])
- },
- }).render()
- expect(html()).toBe('<div><span>slot vnode</span></div>')
- show.value = false
- await nextTick()
- expect(html()).toBe('<div><!----></div>')
- })
- test('should update props on reactivation of vapor child in vdom KeepAlive', async () => {
- const VaporChild = defineVaporComponent({
- props: { msg: String },
- setup(props: any) {
- const n0 = template('<div> </div>')() as any
- const x0 = child(n0) as any
- renderEffect(() => setText(x0, props.msg))
- return n0
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const msg = ref('hello')
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- h(
- resolveDynamicComponent(current.value) as any,
- current.value === VaporChild ? { msg: msg.value } : null,
- ),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- expect(root.innerHTML).toBe('<div>hello</div>')
- // Switch to vdom child (deactivates vapor child)
- current.value = VdomChild
- await nextTick()
- expect(root.innerHTML).toBe('<span>vdom</span>')
- // Change props while vapor child is deactivated
- msg.value = 'updated'
- await nextTick()
- expect(root.innerHTML).toBe('<span>vdom</span>')
- // Reactivate vapor child — should reflect new props
- current.value = VaporChild
- await nextTick()
- expect(root.innerHTML).toBe('<div>updated</div>')
- })
- test('should invoke vnode hooks on activate/deactivate', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor</div>')()
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const vnodeMounted = vi.fn()
- const vnodeUnmounted = vi.fn()
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- h(
- resolveDynamicComponent(current.value) as any,
- current.value === VaporChild
- ? {
- onVnodeMounted: vnodeMounted,
- onVnodeUnmounted: vnodeUnmounted,
- }
- : null,
- ),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- expect(vnodeMounted).toHaveBeenCalledTimes(1)
- expect(vnodeUnmounted).toHaveBeenCalledTimes(0)
- // Deactivate vapor child
- current.value = VdomChild
- await nextTick()
- expect(vnodeUnmounted).toHaveBeenCalledTimes(1)
- // Reactivate vapor child
- current.value = VaporChild
- await nextTick()
- expect(vnodeMounted).toHaveBeenCalledTimes(2)
- })
- test('should invoke update hooks in VDOM order on reactivation', async () => {
- const VaporChild = defineVaporComponent({
- props: ['msg'],
- setup(props: any) {
- return template('<div></div>')()
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const msg = ref('hello')
- const calls: string[] = []
- const vDir = {
- beforeUpdate: vi.fn(() => calls.push('directive beforeUpdate')),
- updated: vi.fn(() => calls.push('directive updated')),
- }
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- current.value === VaporChild
- ? withDirectives(
- h(VaporChild as any, {
- msg: msg.value,
- onVnodeBeforeUpdate: () =>
- calls.push('vnode beforeUpdate'),
- onVnodeUpdated: () => calls.push('vnode updated'),
- }),
- [[vDir]],
- )
- : h(VdomChild),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- // Deactivate vapor child
- current.value = VdomChild
- await nextTick()
- // Change props while deactivated
- msg.value = 'world'
- // Reactivate — should trigger update hooks
- current.value = VaporChild
- await nextTick()
- expect(calls).toEqual([
- 'vnode beforeUpdate',
- 'directive beforeUpdate',
- 'directive updated',
- 'vnode updated',
- ])
- })
- test('should invoke updated before activated on reactivation', async () => {
- const calls: string[] = []
- const VaporChild = defineVaporComponent({
- props: ['msg'],
- setup(props: any) {
- onActivated(() => calls.push('activated'))
- return template('<div></div>')()
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const msg = ref('one')
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- current.value === VaporChild
- ? h(VaporChild as any, {
- msg: msg.value,
- onVnodeUpdated: () => calls.push('vnode updated'),
- onVnodeMounted: () => calls.push('vnode mounted'),
- })
- : h(VdomChild),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- calls.length = 0
- current.value = VdomChild
- await nextTick()
- msg.value = 'two'
- current.value = VaporChild
- await nextTick()
- expect(calls).toEqual(['vnode updated', 'activated', 'vnode mounted'])
- })
- test('should expose the latest vapor root element in updated hooks on reactivation', async () => {
- const VaporChild = defineVaporComponent({
- props: {
- alt: Boolean,
- },
- setup(props: any) {
- return createIf(
- () => props.alt,
- () => template('<p>alt</p>')(),
- () => template('<div>base</div>')(),
- )
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const useAltRoot = ref(false)
- const updatedSpy = vi.fn((vnode: any) => {
- expect((vnode.el as Element).tagName).toBe('P')
- })
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- current.value === VaporChild
- ? h(VaporChild as any, {
- alt: useAltRoot.value,
- onVnodeUpdated: updatedSpy,
- })
- : h(VdomChild),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- current.value = VdomChild
- await nextTick()
- useAltRoot.value = true
- current.value = VaporChild
- await nextTick()
- expect(root.querySelector('p')).not.toBeNull()
- expect(updatedSpy).toHaveBeenCalledTimes(1)
- })
- test('should bail out directive beforeUpdate/updated on reactivation for non-element root vapor child', async () => {
- const beforeUpdateSpy = vi.fn()
- const updatedSpy = vi.fn()
- const vDir = {
- beforeUpdate: beforeUpdateSpy,
- updated: updatedSpy,
- }
- const VaporChild = defineVaporComponent({
- props: ['msg'],
- setup() {
- return [template('<div></div>')(), template('<div></div>')()]
- },
- })
- const VdomChild = defineComponent({
- setup() {
- return () => h('span', 'vdom')
- },
- })
- const current = shallowRef<any>(VaporChild)
- const msg = ref('hello')
- const App = defineComponent({
- setup() {
- return () =>
- h(KeepAlive, null, {
- default: () =>
- current.value === VaporChild
- ? withDirectives(h(VaporChild as any, { msg: msg.value }), [
- [vDir],
- ])
- : h(VdomChild),
- })
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- if (__DEV__) {
- expect(
- `Runtime directive used on component with non-element root node.`,
- ).toHaveBeenWarnedTimes(1)
- }
- expect(beforeUpdateSpy).toHaveBeenCalledTimes(0)
- expect(updatedSpy).toHaveBeenCalledTimes(0)
- current.value = VdomChild
- await nextTick()
- msg.value = 'world'
- current.value = VaporChild
- await nextTick()
- expect(
- `Runtime directive used on component with non-element root node.`,
- ).toHaveBeenWarnedTimes(2)
- expect(beforeUpdateSpy).toHaveBeenCalledTimes(0)
- expect(updatedSpy).toHaveBeenCalledTimes(0)
- })
- })
- describe('Teleport', () => {
- test('mounts VDOM Teleport from createDynamicComponent', async () => {
- const target = document.createElement('div')
- target.id = 'interop-teleport-target'
- document.body.appendChild(target)
- try {
- const VaporChild = defineVaporComponent({
- setup() {
- return createDynamicComponent(
- () => Teleport,
- { to: () => '#interop-teleport-target' },
- {
- default: () => template('<span>teleported</span>')(),
- },
- true,
- )
- },
- })
- define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- await nextTick()
- expect(target.innerHTML).toContain('<span>teleported</span>')
- } finally {
- target.remove()
- }
- })
- test('should patch vdom slot content in the new teleport target after move', async () => {
- const targetA = document.createElement('div')
- targetA.id = 'interop-slot-target-a'
- const targetB = document.createElement('div')
- targetB.id = 'interop-slot-target-b'
- document.body.append(targetA, targetB)
- const to = ref('#interop-slot-target-a')
- const useAltRoot = ref(false)
- try {
- const VaporChild = defineVaporComponent({
- setup() {
- return createComponent(
- VaporTeleport,
- {
- to: () => to.value,
- },
- {
- default: () => createSlot('default'),
- },
- )
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- h(VaporChild as any, null, {
- default: () => [
- useAltRoot.value ? h('p', 'moved') : h('div', 'initial'),
- ],
- })
- },
- })
- const host = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(host)
- await nextTick()
- expect(targetA.innerHTML).toBe('<div>initial</div>')
- expect(targetB.innerHTML).toBe('')
- to.value = '#interop-slot-target-b'
- await nextTick()
- expect(targetA.innerHTML).toBe('')
- expect(targetB.innerHTML).toBe('<div>initial</div>')
- useAltRoot.value = true
- await nextTick()
- expect(targetA.innerHTML).toBe('')
- expect(targetB.innerHTML).toBe('<p>moved</p>')
- } finally {
- targetA.remove()
- targetB.remove()
- }
- })
- })
- describe('Suspense', () => {
- test('renders vapor async wrapper inside VDOM Suspense', async () => {
- const duration = 5
- const VaporAsyncChild = defineVaporAsyncComponent({
- loader: () =>
- new Promise(resolve => {
- setTimeout(() => {
- resolve(
- defineVaporComponent({
- setup() {
- return template('<div><button>click</button></div>')()
- },
- }) as any,
- )
- }, duration)
- }),
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () => createComponent(VaporAsyncChild, null, null, true),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).toContain('loading')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<div><button>click</button></div>')
- })
- test('does not suspend vapor async wrapper with suspensible false inside VDOM Suspense', async () => {
- const duration = 5
- const VaporAsyncChild = defineVaporAsyncComponent({
- loader: () =>
- new Promise(resolve => {
- setTimeout(() => {
- resolve(
- defineVaporComponent({
- setup() {
- return template('<div><button>click</button></div>')()
- },
- }) as any,
- )
- }, duration)
- }),
- suspensible: false,
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () => createComponent(VaporAsyncChild, null, null, true),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).not.toContain('loading')
- expect(html()).toContain('<!--async component-->')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<div><button>click</button></div>')
- })
- test('renders error component for vapor async wrapper inside VDOM Suspense', async () => {
- const tick = () => new Promise(resolve => setTimeout(resolve))
- let reject!: (error: Error) => void
- const VaporAsyncChild = defineVaporAsyncComponent({
- loader: () =>
- new Promise((_resolve, _reject) => {
- reject = _reject as (error: Error) => void
- }),
- errorComponent: defineVaporComponent({
- props: ['error'],
- setup(props: { error: Error }) {
- return template(props.error.message)()
- },
- }),
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () => createComponent(VaporAsyncChild, null, null, true),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const host = document.createElement('div')
- const app = createApp({
- render: () => h(VaporParent as any),
- })
- const errorHandler = vi.fn()
- app.use(vaporInteropPlugin)
- app.config.errorHandler = errorHandler
- try {
- app.mount(host)
- expect(host.innerHTML).toContain('loading')
- reject(new Error('errored out'))
- await tick()
- await nextTick()
- expect(errorHandler).toHaveBeenCalled()
- expect(host.innerHTML).toContain('errored out')
- } finally {
- app.unmount()
- host.remove()
- }
- })
- test('does not fall through slots to error component for vapor async wrapper inside VDOM Suspense', async () => {
- const tick = () => new Promise(resolve => setTimeout(resolve))
- let reject!: (error: Error) => void
- const VaporAsyncChild = defineVaporAsyncComponent({
- loader: () =>
- new Promise((_resolve, _reject) => {
- reject = _reject as (error: Error) => void
- }),
- errorComponent: defineVaporComponent({
- setup() {
- const n0 = template('<div>error</div>')()
- insert(createSlot('default'), n0 as any as ParentNode)
- return n0
- },
- }),
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () =>
- createComponent(
- VaporAsyncChild,
- null,
- {
- default: withVaporCtx(() =>
- template('<span>slot content</span>')(),
- ),
- },
- true,
- ),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const host = document.createElement('div')
- const app = createApp({
- render: () => h(VaporParent as any),
- })
- app.use(vaporInteropPlugin)
- const errorHandler = vi.fn()
- app.config.errorHandler = errorHandler
- try {
- app.mount(host)
- expect(host.innerHTML).toContain('loading')
- reject(new Error('errored out'))
- await tick()
- await nextTick()
- expect(errorHandler).toHaveBeenCalled()
- expect(host.innerHTML).toContain('error')
- expect(host.innerHTML).not.toContain('slot content')
- } finally {
- app.unmount()
- host.remove()
- }
- })
- test('renders async setup vapor component inside VDOM Suspense', async () => {
- const duration = 5
- const VaporAsyncChild = defineVaporComponent({
- async setup() {
- await new Promise(resolve => setTimeout(resolve, duration))
- return template('<div><button>click</button></div>')()
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () => createComponent(VaporAsyncChild, null, null, true),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).toContain('loading')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<div><button>click</button></div>')
- })
- test('renders async VDOM child inside VDOM Suspense', async () => {
- const duration = 5
- const VDomAsyncChild = defineComponent({
- async setup() {
- await new Promise(resolve => setTimeout(resolve, duration))
- return () => h('div', [h('button', 'click')])
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () =>
- createComponent(VDomAsyncChild as any, null, null, true),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).toContain('loading')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<div><button>click</button></div>')
- })
- test('renders async VDOM child from vapor slot outlet inside VDOM Suspense', async () => {
- const duration = 5
- const VaporSlotOutlet = defineVaporComponent({
- setup() {
- return createSlot('default')
- },
- })
- const VDomAsyncChild = defineComponent({
- async setup() {
- await new Promise(resolve => setTimeout(resolve, duration))
- return () => h('div', 'slot async')
- },
- })
- const App = defineComponent({
- setup() {
- return () =>
- h(Suspense, null, {
- default: () =>
- h(VaporSlotOutlet as any, null, {
- default: () => [h(VDomAsyncChild as any)],
- }),
- fallback: () => h('div', 'loading'),
- })
- },
- })
- const { html } = define(App).render()
- expect(html()).toContain('loading')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<div>slot async</div>')
- })
- test('renders async VDOM vnode via createDynamicComponent inside VDOM Suspense', async () => {
- const duration = 5
- const VDomAsyncChild = defineComponent({
- async setup() {
- await new Promise(resolve => setTimeout(resolve, duration))
- return () => h('button', 'vnode async')
- },
- })
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- Suspense as any,
- null,
- {
- default: () =>
- createDynamicComponent(
- () => h(VDomAsyncChild as any),
- null,
- null,
- true,
- ),
- fallback: () => template('loading')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporParent as any)
- },
- }).render()
- expect(html()).toContain('loading')
- await new Promise(resolve => setTimeout(resolve, duration + 1))
- await nextTick()
- expect(html()).toContain('<button>vnode async</button>')
- })
- test('mounts VDOM Suspense from createDynamicComponent', async () => {
- const VaporChild = defineVaporComponent({
- setup() {
- return createDynamicComponent(
- () => Suspense,
- null,
- {
- default: () => template('<span>resolved</span>')(),
- fallback: () => template('<span>fallback</span>')(),
- },
- true,
- )
- },
- })
- const { html } = define({
- setup() {
- return () => h(VaporChild as any)
- },
- }).render()
- await nextTick()
- expect(html()).toContain('<span>resolved</span>')
- })
- })
- describe('vnode hooks', () => {
- test('should invoke onVnodeBeforeMount/onVnodeBeforeUnmount on vapor child', async () => {
- const beforeMountSpy = vi.fn()
- const beforeUnmountSpy = vi.fn()
- const VaporChild = defineVaporComponent({
- setup() {
- return template('<div>vapor</div>')()
- },
- })
- const show = ref(true)
- const App = defineComponent({
- setup() {
- return () =>
- show.value
- ? h(VaporChild as any, {
- onVnodeBeforeMount: beforeMountSpy,
- onVnodeBeforeUnmount: beforeUnmountSpy,
- })
- : null
- },
- })
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- await nextTick()
- expect(beforeMountSpy).toHaveBeenCalledTimes(1)
- // unmount
- show.value = false
- await nextTick()
- expect(beforeUnmountSpy).toHaveBeenCalledTimes(1)
- })
- })
- })
|