| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078 |
- // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
- import {
- VaporTeleport,
- child,
- createComponent,
- createFor,
- createForSlots,
- createIf,
- createSlot,
- createVaporApp,
- defineVaporComponent,
- insert,
- prepend,
- remove,
- renderEffect,
- setInsertionState,
- template,
- txt,
- vaporInteropPlugin,
- withVaporCtx,
- } from '../src'
- import {
- type Ref,
- createApp,
- createSlots,
- currentInstance,
- h,
- nextTick,
- onScopeDispose,
- ref,
- renderSlot,
- shallowRef,
- toDisplayString,
- } from '@vue/runtime-dom'
- import { makeRender } from './_utils'
- import type { DynamicSlot } from '../src/componentSlots'
- import { setElementText, setText } from '../src/dom/prop'
- import { isValidBlock } from '../src/block'
- import {
- DynamicFragment,
- ForFragment,
- type SlotBoundaryContext,
- SlotFallbackController,
- SlotFragment,
- VaporFragment,
- getCurrentSlotBoundary,
- trackSlotBoundaryDirtying,
- withOwnedSlotBoundary,
- withSlotFallbackBoundary,
- } from '../src/fragment'
- const define = makeRender<any>()
- function renderWithSlots(slots: any): any {
- let instance: any
- const Comp = defineVaporComponent({
- setup() {
- const t0 = template('<div></div>')
- const n0 = t0()
- instance = currentInstance
- return n0
- },
- })
- const { render } = define({
- render() {
- return createComponent(Comp, {}, slots)
- },
- })
- render()
- return instance
- }
- describe('component: slots', () => {
- test('initSlots: instance.slots should be set correctly', () => {
- const { slots } = renderWithSlots({
- default: () => template('<span></span>')(),
- })
- expect(slots.default()).toMatchObject(document.createElement('span'))
- })
- test('updateSlots: instance.slots should be updated correctly', async () => {
- const flag1 = ref(true)
- let instance: any
- const Child = () => {
- instance = currentInstance
- return template('child')()
- }
- const { render } = define({
- render() {
- return createComponent(
- Child,
- {},
- {
- $: [
- () =>
- flag1.value
- ? { name: 'one', fn: () => template('<span></span>')() }
- : { name: 'two', fn: () => template('<div></div>')() },
- ],
- },
- )
- },
- })
- render()
- expect(instance.slots).toHaveProperty('one')
- expect(instance.slots).not.toHaveProperty('two')
- flag1.value = false
- await nextTick()
- expect(instance.slots).not.toHaveProperty('one')
- expect(instance.slots).toHaveProperty('two')
- })
- // passes but no warning for slot invocation in vapor currently
- test.todo('should not warn when mounting another app in setup', () => {
- const Comp = defineVaporComponent({
- setup(_, { slots }) {
- return slots.default!()
- },
- })
- const mountComp = () => {
- createVaporApp({
- render() {
- return createComponent(
- Comp,
- {},
- { default: () => template('msg')() },
- )!
- },
- })
- }
- const App = {
- setup() {
- mountComp()
- return []
- },
- }
- createVaporApp(App).mount(document.createElement('div'))
- expect(
- 'Slot "default" invoked outside of the render function',
- ).not.toHaveBeenWarned()
- })
- describe('slot fallback boundary', () => {
- test('slot fragment insert uses active fallback output', () => {
- const container = document.createElement('div')
- const frag = new SlotFragment()
- frag.updateSlot(undefined, () => document.createTextNode('fallback'))
- insert(frag, container)
- expect(container.innerHTML).toBe('fallback<!--slot-->')
- })
- test('slot fragment validity uses active fallback output', () => {
- const frag = new SlotFragment()
- frag.updateSlot(undefined, () => document.createTextNode('fallback'))
- expect(isValidBlock(frag)).toBe(true)
- })
- test('slot fragment remove cleans active fallback and fallback scope', () => {
- const container = document.createElement('div')
- const stop = vi.fn()
- const frag = new SlotFragment()
- frag.updateSlot(undefined, () => {
- onScopeDispose(stop)
- return document.createTextNode('fallback')
- })
- insert(frag, container)
- remove(frag, container)
- expect(Array.from(container.childNodes)).toEqual([])
- expect(stop).toHaveBeenCalledTimes(1)
- })
- test('slot fragment prefers local fallback over inherited fallback', () => {
- const localFallback = vi.fn(() =>
- document.createTextNode('local fallback'),
- )
- const inheritedFallback = vi.fn(() =>
- document.createTextNode('inherited fallback'),
- )
- const frag = new SlotFragment()
- frag.parentSlotBoundary = {
- parent: null,
- getLocalFallback: () => inheritedFallback,
- markDirty: vi.fn(),
- }
- frag.updateSlot(undefined, localFallback)
- expect(frag.fallbackBlock).toBeInstanceOf(Text)
- expect((frag.fallbackBlock as Text).textContent).toBe('local fallback')
- expect(localFallback).toHaveBeenCalled()
- expect(inheritedFallback).not.toHaveBeenCalled()
- })
- test('slot fragment local fallback renders nested slots against the parent boundary', () => {
- const parentBoundary = {
- parent: null,
- getLocalFallback: () => () => document.createTextNode('outer fallback'),
- markDirty: vi.fn(),
- }
- const frag = new SlotFragment()
- frag.parentSlotBoundary = parentBoundary
- let fallbackBoundary: any
- frag.updateSlot(undefined, () => {
- fallbackBoundary = getCurrentSlotBoundary()
- return []
- })
- // Fallback body renders under a redirected boundary whose `.parent` is
- // the owning boundary's parent — so nested slots inherit from the
- // grandparent, avoiding fallback -> <slot> -> same fallback recursion.
- expect(fallbackBoundary).not.toBe(frag.slotFallbackBoundary)
- expect(fallbackBoundary.parent).toBe(parentBoundary)
- })
- test('withSlotFallbackBoundary reuses the same redirected boundary', () => {
- const parentBoundaryA = {
- parent: null,
- getLocalFallback: () => undefined,
- markDirty: vi.fn(),
- }
- const parentBoundaryB = {
- parent: null,
- getLocalFallback: () => undefined,
- markDirty: vi.fn(),
- }
- let activeParent = parentBoundaryA
- const boundary: SlotBoundaryContext = {
- get parent() {
- return activeParent
- },
- getLocalFallback: () => () => document.createTextNode('fallback'),
- markDirty: vi.fn(),
- }
- let firstBoundary!: SlotBoundaryContext | null
- let secondBoundary!: SlotBoundaryContext | null
- withSlotFallbackBoundary(boundary, () => {
- firstBoundary = getCurrentSlotBoundary()
- })
- activeParent = parentBoundaryB
- withSlotFallbackBoundary(boundary, () => {
- secondBoundary = getCurrentSlotBoundary()
- })
- expect(firstBoundary).toBe(secondBoundary)
- expect(firstBoundary!.parent).toBe(parentBoundaryB)
- expect(firstBoundary!.getLocalFallback()).toBeUndefined()
- })
- test('slot fragment local fallback keeps itself as owner for nested fragments', () => {
- const container = document.createElement('div')
- const parentBoundary = {
- parent: null,
- getLocalFallback: () => () => document.createTextNode('outer fallback'),
- markDirty: vi.fn(),
- }
- const frag = new SlotFragment()
- const child = new DynamicFragment('if', false, false)
- let initialized = false
- frag.parentSlotBoundary = parentBoundary
- frag.updateSlot(undefined, () => {
- if (!initialized) {
- initialized = true
- trackSlotBoundaryDirtying(child)
- child.update(() => document.createTextNode('inner fallback'))
- }
- return child
- })
- insert(frag, container)
- expect(container.innerHTML).toBe('inner fallback<!--if--><!--slot-->')
- child.update(() => [])
- expect(container.innerHTML).toBe('outer fallback<!--if--><!--slot-->')
- })
- test('slot fragment local fallback ignores unrelated ancestor fallback refs', async () => {
- const ancestorText = ref('outer fallback')
- const localFallback = vi.fn(() =>
- document.createTextNode('local fallback'),
- )
- const container = document.createElement('div')
- const frag = new SlotFragment()
- const parentBoundary = {
- parent: null,
- getLocalFallback: () => () =>
- document.createTextNode(ancestorText.value),
- markDirty: vi.fn(),
- }
- frag.parentSlotBoundary = parentBoundary
- frag.updateSlot(undefined, localFallback)
- insert(frag, container)
- expect(container.innerHTML).toBe('local fallback<!--slot-->')
- expect(localFallback).toHaveBeenCalledTimes(1)
- ancestorText.value = 'updated outer fallback'
- await nextTick()
- expect(container.innerHTML).toBe('local fallback<!--slot-->')
- expect(localFallback).toHaveBeenCalledTimes(1)
- })
- test('slot fragment activates local fallback while preserving content carrier', () => {
- const container = document.createElement('div')
- const content = new DynamicFragment('if', false, false)
- const frag = new SlotFragment()
- frag.updateSlot(
- () => content,
- () => document.createTextNode('fallback'),
- )
- insert(frag, container)
- expect(container.innerHTML).toBe('fallback<!--if--><!--slot-->')
- })
- test('slot fragment activates local fallback while preserving v-for carrier', () => {
- const container = document.createElement('div')
- const content = new ForFragment([[], document.createComment('for')])
- const frag = new SlotFragment()
- frag.updateSlot(
- () => content,
- () => document.createTextNode('fallback'),
- )
- insert(frag, container)
- expect(container.innerHTML).toBe('fallback<!--for--><!--slot-->')
- })
- test('slot fragment delays fallback activation until pending child validity resolves', () => {
- const container = document.createElement('div')
- const frag = new SlotFragment()
- let child!: VaporFragment
- frag.updateSlot(
- () => {
- child = new VaporFragment([])
- child.validityPending = true
- trackSlotBoundaryDirtying(child)
- return child
- },
- () => document.createTextNode('fallback'),
- )
- insert(frag, container)
- expect(container.innerHTML).toBe('<!--slot-->')
- child.validityPending = false
- child.nodes = []
- child.onUpdated!.forEach(hook => hook())
- expect(container.innerHTML).toBe('fallback<!--slot-->')
- })
- test('slot fallback controller ignores dirty notifications after dispose', () => {
- let disposed = false
- const controller = new SlotFallbackController({
- getParentBoundary: () => null,
- getLocalFallback: () => undefined,
- getContent: () => [],
- getParentNode: () => null,
- getAnchor: () => null,
- runWithRenderCtx: fn => fn(),
- isDisposed: () => disposed,
- onValidityChange: vi.fn(),
- })
- disposed = true
- controller.boundary.markDirty()
- expect(controller.takePendingRecheck()).toBe(false)
- })
- test('slot fallback controller does not accumulate order-sync hooks', async () => {
- const fallback = new VaporFragment([
- document.createTextNode('a'),
- document.createTextNode('b'),
- ])
- const controller = new SlotFallbackController({
- getParentBoundary: () => null,
- getLocalFallback: () => () => fallback,
- getContent: () => [],
- getParentNode: () => null,
- getAnchor: () => null,
- runWithRenderCtx: fn => fn(),
- onValidityChange: vi.fn(),
- })
- controller.recheck()
- expect(fallback.onUpdated).toHaveLength(1)
- controller.syncActiveFallback()
- await nextTick()
- expect(fallback.onUpdated).toHaveLength(1)
- })
- test('slot fallback controller re-syncs the whole carrier block order', async () => {
- const container = document.createElement('div')
- const carrierA = document.createTextNode('x')
- const carrierB = document.createTextNode('y')
- const marker = document.createTextNode('!')
- const slotAnchor = document.createComment('slot')
- const fallback = new VaporFragment([
- document.createTextNode('a'),
- document.createTextNode('b'),
- ])
- const controller = new SlotFallbackController({
- getParentBoundary: () => null,
- getLocalFallback: () => () => fallback,
- getContent: () => [carrierA, carrierB],
- getParentNode: () => container,
- getAnchor: () => slotAnchor,
- runWithRenderCtx: fn => fn(),
- isContentValid: () => false,
- onValidityChange: vi.fn(),
- })
- container.append(carrierA, marker, carrierB, slotAnchor)
- controller.recheck()
- expect(container.innerHTML).toBe('abx!y<!--slot-->')
- controller.syncActiveFallback()
- await nextTick()
- expect(container.innerHTML).toBe('abxy!<!--slot-->')
- })
- test('slot fallback controller defaults to idle when isBusy is omitted', () => {
- const fallback = document.createTextNode('fallback')
- const controller = new SlotFallbackController({
- getParentBoundary: () => null,
- getLocalFallback: () => () => fallback,
- getContent: () => [],
- getParentNode: () => null,
- getAnchor: () => null,
- runWithRenderCtx: fn => fn(),
- onValidityChange: vi.fn(),
- })
- controller.boundary.markDirty()
- expect(controller.getActiveFallback()).toBe(fallback)
- })
- test('vdom slot dirties parent boundary once when content stays valid', async () => {
- const text = ref('A')
- const boundary = {
- parent: null,
- getLocalFallback: () => undefined,
- markDirty: vi.fn(),
- }
- const instance = renderWithSlots({})
- const app = createApp({ render: () => null })
- app.use(vaporInteropPlugin)
- const vapor = (app._context as any).vapor
- const slotsRef = shallowRef({
- default: () => [h('div', text.value)],
- })
- const frag = withOwnedSlotBoundary(boundary, () =>
- vapor.vdomSlot(slotsRef, 'default', {}, instance),
- )
- const host = document.createElement('div')
- insert(frag, host)
- boundary.markDirty.mockClear()
- text.value = 'B'
- await nextTick()
- expect(host.innerHTML).toContain('<div>B</div>')
- expect(boundary.markDirty).toHaveBeenCalledTimes(1)
- })
- test('vdom slot still renders vapor fallback when slot content resolves empty', () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('default', null, () => template('child fallback')())
- },
- })
- const root = document.createElement('div')
- createApp({
- render: () =>
- h(Child as any, null, {
- default: () => [],
- }),
- })
- .use(vaporInteropPlugin)
- .mount(root)
- expect(root.innerHTML).toBe('child fallback')
- })
- })
- describe('createSlot', () => {
- test('slot should be rendered correctly', () => {
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div>')()
- insert(createSlot('header'), n0 as any as ParentNode)
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: () => template('header')(),
- })
- }).render()
- expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
- })
- test('slot should be rendered correctly with slot props', async () => {
- const src = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- insert(
- createSlot('header', { title: () => src.value }),
- n0 as any as ParentNode,
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: props => {
- const el = template('<h1></h1>')()
- renderEffect(() => {
- setElementText(el, props.title)
- })
- return el
- },
- })
- }).render()
- expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
- src.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
- })
- test('slot props should be isolated per fragment in v-for', async () => {
- const items = ref([0, 1, 2])
- const Child = defineVaporComponent(() => {
- const list = createFor(
- () => items.value,
- for_item0 => {
- const n0 = template('<div></div>')()
- insert(
- createSlot('age-option', { age: () => for_item0.value }),
- n0 as any as ParentNode,
- )
- return n0
- },
- )
- return list
- })
- const { host } = define(() => {
- return createComponent(Child, null, {
- 'age-option': (props: any) => {
- const el = template('<span></span>')()
- renderEffect(() => {
- setElementText(el, toDisplayString(props.age))
- })
- return el
- },
- })
- }).render()
- expect(host.innerHTML).toBe(
- '<div><span>0</span><!--slot--></div>' +
- '<div><span>1</span><!--slot--></div>' +
- '<div><span>2</span><!--slot--></div><!--for-->',
- )
- items.value = [3, 4]
- await nextTick()
- expect(host.innerHTML).toBe(
- '<div><span>3</span><!--slot--></div>' +
- '<div><span>4</span><!--slot--></div><!--for-->',
- )
- })
- test('dynamic slot props', async () => {
- let props: any
- const bindObj = ref<Record<string, any>>({ foo: 1, baz: 'qux' })
- const Comp = defineVaporComponent(() =>
- createSlot('default', { $: [() => bindObj.value] }),
- )
- define(() =>
- createComponent(Comp, null, {
- default: (_props: any) => ((props = _props), []),
- }),
- ).render()
- expect(props).toEqual({ foo: 1, baz: 'qux' })
- bindObj.value.foo = 2
- await nextTick()
- expect(props).toEqual({ foo: 2, baz: 'qux' })
- delete bindObj.value.baz
- await nextTick()
- expect(props).toEqual({ foo: 2 })
- })
- test('dynamic slot props with static slot props', async () => {
- let props: any
- const foo = ref(0)
- const bindObj = ref<Record<string, any>>({ foo: 100, baz: 'qux' })
- const Comp = defineVaporComponent(() =>
- createSlot('default', {
- foo: () => foo.value,
- $: [() => bindObj.value],
- }),
- )
- define(() =>
- createComponent(Comp, null, {
- default: (_props: any) => ((props = _props), []),
- }),
- ).render()
- expect(props).toEqual({ foo: 100, baz: 'qux' })
- foo.value = 2
- await nextTick()
- expect(props).toEqual({ foo: 100, baz: 'qux' })
- delete bindObj.value.foo
- await nextTick()
- expect(props).toEqual({ foo: 2, baz: 'qux' })
- })
- test('dynamic slot should be rendered correctly with slot props', async () => {
- const val = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(
- n0 as any as ParentNode,
- createSlot('header', { title: () => val.value }),
- )
- return n0
- })
- const { host } = define(() => {
- // dynamic slot
- return createComponent(Comp, null, {
- $: [
- () => ({
- name: 'header',
- fn: (props: any) => {
- const el = template('<h1></h1>')()
- renderEffect(() => {
- setElementText(el, props.title)
- })
- return el
- },
- }),
- ],
- })
- }).render()
- expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
- val.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
- })
- test('dynamic slot outlet should be render correctly with slot props', async () => {
- const val = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(
- n0 as any as ParentNode,
- createSlot(
- () => val.value, // dynamic slot outlet name
- ),
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: () => template('header')(),
- footer: () => template('footer')(),
- })
- }).render()
- expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
- val.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div>footer<!--slot--></div>')
- })
- test('fallback should be render correctly', () => {
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- insert(
- createSlot('header', undefined, () => template('fallback')()),
- n0 as any as ParentNode,
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, {}, {})
- }).render()
- expect(host.innerHTML).toBe('<div>fallback<!--slot--></div>')
- })
- test('dynamic slot should be updated correctly', async () => {
- const flag1 = ref(true)
- const Child = defineVaporComponent(() => {
- const temp0 = template('<p></p>')
- const el0 = temp0()
- const el1 = temp0()
- const slot1 = createSlot('one', null, () => template('one fallback')())
- const slot2 = createSlot('two', null, () => template('two fallback')())
- insert(slot1, el0 as any as ParentNode)
- insert(slot2, el1 as any as ParentNode)
- return [el0, el1]
- })
- const { host } = define(() => {
- return createComponent(Child, null, {
- $: [
- () =>
- flag1.value
- ? {
- name: 'one',
- fn: () => template('one content')(),
- }
- : {
- name: 'two',
- fn: () => template('two content')(),
- },
- ],
- })
- }).render()
- expect(host.innerHTML).toBe(
- '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
- )
- flag1.value = false
- await nextTick()
- expect(host.innerHTML).toBe(
- '<p>one fallback<!--slot--></p><p>two content<!--slot--></p>',
- )
- flag1.value = true
- await nextTick()
- expect(host.innerHTML).toBe(
- '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
- )
- })
- test('dynamic slot outlet should be updated correctly', async () => {
- const slotOutletName = ref('one')
- const Child = defineVaporComponent(() => {
- const temp0 = template('<p>')
- const el0 = temp0()
- const slot1 = createSlot(
- () => slotOutletName.value,
- undefined,
- () => template('fallback')(),
- )
- insert(slot1, el0 as any as ParentNode)
- return el0
- })
- const { host } = define(() => {
- return createComponent(
- Child,
- {},
- {
- one: () => template('one content')(),
- two: () => template('two content')(),
- },
- )
- }).render()
- expect(host.innerHTML).toBe('<p>one content<!--slot--></p>')
- slotOutletName.value = 'two'
- await nextTick()
- expect(host.innerHTML).toBe('<p>two content<!--slot--></p>')
- slotOutletName.value = 'none'
- await nextTick()
- expect(host.innerHTML).toBe('<p>fallback<!--slot--></p>')
- })
- test('non-exist slot', async () => {
- const Child = defineVaporComponent(() => {
- const el0 = template('<p>')()
- const slot = createSlot('not-exist', undefined)
- insert(slot, el0 as any as ParentNode)
- return el0
- })
- const { host } = define(() => {
- return createComponent(Child)
- }).render()
- expect(host.innerHTML).toBe('<p><!--slot--></p>')
- })
- test('use fallback when inner content changes', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const toggle = ref(true)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createIf(
- () => toggle.value,
- () => {
- return document.createTextNode('content')
- },
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('content<!--if--><!--slot-->')
- toggle.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- toggle.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--slot-->')
- })
- test('use fallback on initial render', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const toggle = ref(false)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createIf(
- () => toggle.value,
- () => {
- return document.createTextNode('content')
- },
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- toggle.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--slot-->')
- toggle.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- })
- test('dynamic slot work with v-if', async () => {
- const val = ref('header')
- const toggle = ref(false)
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(n0 as any as ParentNode, createSlot('header', null))
- return n0
- })
- const { host } = define(() => {
- // dynamic slot
- return createComponent(Comp, null, {
- $: [
- () =>
- (toggle.value
- ? {
- name: val.value,
- fn: () => {
- return template('<h1></h1>')()
- },
- }
- : void 0) as DynamicSlot,
- ],
- })
- }).render()
- expect(host.innerHTML).toBe('<div><!--slot--></div>')
- toggle.value = true
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
- })
- test('slots proxy ownKeys trap correctly reflects dynamic slot presence', async () => {
- const val = ref('header')
- const toggle = ref(false)
- let instance: any
- const Comp = defineVaporComponent(() => {
- instance = currentInstance
- const n0 = template('<div></div>')()
- prepend(n0 as any as ParentNode, createSlot('header', null))
- return n0
- })
- define(() => {
- // dynamic slot
- return createComponent(Comp, null, {
- $: [
- () =>
- (toggle.value
- ? {
- name: val.value,
- fn: () => {
- return template('<h1></h1>')()
- },
- }
- : void 0) as DynamicSlot,
- ],
- })
- }).render()
- expect(Reflect.ownKeys(instance.slots)).not.toContain('header')
- toggle.value = true
- await nextTick()
- expect(Reflect.ownKeys(instance.slots)).toContain('header')
- toggle.value = false
- await nextTick()
- expect(Reflect.ownKeys(instance.slots)).not.toContain('header')
- })
- test('render fallback when slot content is not valid', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return template('<!--comment-->')()
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--slot-->')
- })
- test('render fallback when v-if condition is false', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const toggle = ref(false)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createIf(
- () => toggle.value,
- () => {
- return document.createTextNode('content')
- },
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- toggle.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--slot-->')
- toggle.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- })
- test('render fallback with nested v-if', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const outerShow = ref(false)
- const innerShow = ref(false)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createIf(
- () => outerShow.value,
- () => {
- return createIf(
- () => innerShow.value,
- () => {
- return document.createTextNode('content')
- },
- )
- },
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- outerShow.value = true
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
- innerShow.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
- innerShow.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
- outerShow.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- outerShow.value = true
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
- innerShow.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
- })
- test('render fallback with v-for', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const items = ref<number[]>([1])
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- const n2 = createFor(
- () => items.value,
- for_item0 => {
- const n4 = template('<span> </span>')() as any
- const x4 = child(n4) as any
- renderEffect(() =>
- setText(x4, toDisplayString(for_item0.value)),
- )
- return n4
- },
- )
- return n2
- },
- })
- },
- }).render()
- expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
- items.value.pop()
- await nextTick()
- expect(html()).toBe('fallback<!--for--><!--slot-->')
- items.value.pop()
- await nextTick()
- expect(html()).toBe('fallback<!--for--><!--slot-->')
- items.value.push(2)
- await nextTick()
- expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
- })
- test('render fallback with v-for (empty source)', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const items = ref<number[]>([])
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- const n2 = createFor(
- () => items.value,
- for_item0 => {
- const n4 = template('<span> </span>')() as any
- const x4 = child(n4) as any
- renderEffect(() =>
- setText(x4, toDisplayString(for_item0.value)),
- )
- return n4
- },
- )
- return n2
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--for--><!--slot-->')
- items.value.push(1)
- await nextTick()
- expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
- items.value.pop()
- await nextTick()
- expect(html()).toBe('fallback<!--for--><!--slot-->')
- items.value.pop()
- await nextTick()
- expect(html()).toBe('fallback<!--for--><!--slot-->')
- items.value.push(2)
- await nextTick()
- expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
- })
- test('render fallback with invalid v-for branch', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const items = ref([{ text: 'bar', show: false }])
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createFor(
- () => items.value,
- for_item0 => {
- return createIf(
- () => for_item0.value.show,
- () => {
- const n5 = template('<span> </span>')() as any
- const x5 = child(n5) as any
- renderEffect(() =>
- setText(x5, toDisplayString(for_item0.value.text)),
- )
- return n5
- },
- )
- },
- item => item.text,
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('fallback<!--if--><!--for--><!--slot-->')
- items.value[0].show = true
- await nextTick()
- expect(html()).toBe('<span>bar</span><!--if--><!--for--><!--slot-->')
- items.value[0].show = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--for--><!--slot-->')
- })
- test('should not render fallback for a single empty item in v-for', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const items = ref([
- { text: 'bar', show: true },
- { text: 'baz', show: true },
- ])
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createFor(
- () => items.value,
- for_item0 => {
- return createIf(
- () => for_item0.value.show,
- () => {
- const n5 = template('<span> </span>')() as any
- const x5 = child(n5) as any
- renderEffect(() =>
- setText(x5, toDisplayString(for_item0.value.text)),
- )
- return n5
- },
- )
- },
- item => item.text,
- )
- },
- })
- },
- }).render()
- expect(html()).toBe(
- '<span>bar</span><!--if--><span>baz</span><!--if--><!--for--><!--slot-->',
- )
- items.value[1].show = false
- await nextTick()
- expect(html()).toBe(
- '<span>bar</span><!--if--><!--if--><!--for--><!--slot-->',
- )
- })
- test('work with v-once', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot(
- 'default',
- null,
- undefined,
- undefined,
- true /* once */,
- )
- },
- })
- const count = ref(0)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- const n3 = template('<div> </div>')() as any
- const x3 = txt(n3) as any
- renderEffect(() => setText(x3, toDisplayString(count.value)))
- return n3
- },
- })
- },
- }).render()
- expect(html()).toBe('<div>0</div><!--slot-->')
- // expect no changes due to v-once
- count.value++
- await nextTick()
- expect(html()).toBe('<div>0</div><!--slot-->')
- })
- })
- describe('forwarded slot', () => {
- test('should work', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('foo', null)
- },
- })
- const Parent = defineVaporComponent({
- setup() {
- const n2 = createComponent(
- Child,
- null,
- {
- foo: withVaporCtx(() => {
- return createSlot('foo', null)
- }),
- },
- true,
- )
- return n2
- },
- })
- const foo = ref('foo')
- const { host } = define({
- setup() {
- const n2 = createComponent(
- Parent,
- null,
- {
- foo: () => {
- const n0 = template(' ')() as any
- renderEffect(() => setText(n0, foo.value))
- return n0
- },
- },
- true,
- )
- return n2
- },
- }).render()
- expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->')
- })
- test('mixed with non-forwarded slot', async () => {
- const Child = defineVaporComponent({
- setup() {
- return [createSlot('foo', null)]
- },
- })
- const Parent = defineVaporComponent({
- setup() {
- const n2 = createComponent(Child, null, {
- foo: withVaporCtx(() => {
- const n0 = createSlot('foo', null)
- return n0
- }),
- })
- const n3 = createSlot('default', null)
- return [n2, n3]
- },
- })
- const foo = ref('foo')
- const { host } = define({
- setup() {
- const n2 = createComponent(
- Parent,
- null,
- {
- foo: () => {
- const n0 = template(' ')() as any
- renderEffect(() => setText(n0, foo.value))
- return n0
- },
- default: () => {
- const n3 = template(' ')() as any
- renderEffect(() => setText(n3, foo.value))
- return n3
- },
- },
- true,
- )
- return n2
- },
- }).render()
- expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->foo<!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->bar<!--slot-->')
- })
- test('forwarded slot with fallback', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('default', null, () => template('child fallback')())
- },
- })
- const Parent = defineVaporComponent({
- setup() {
- const n2 = createComponent(Child, null, {
- default: withVaporCtx(() => {
- const n0 = createSlot('default', null, () => {
- return template('<!-- <div></div> -->')()
- })
- return n0
- }),
- })
- return n2
- },
- })
- const { html } = define({
- setup() {
- return createComponent(Parent, null, {
- default: () => template('<!-- <div>App</div> -->')(),
- })
- },
- }).render()
- expect(html()).toBe('child fallback<!--slot--><!--slot-->')
- })
- test('named forwarded slot with v-if', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('default', null)
- },
- })
- const Parent = defineVaporComponent({
- props: {
- show: Boolean,
- },
- setup(props) {
- const n6 = createComponent(
- Child,
- null,
- {
- default: withVaporCtx(() => {
- const n0 = createIf(
- () => props.show,
- () => {
- const n5 = template('<div></div>')() as any
- setInsertionState(n5, null, 0, true)
- createSlot('header', null, () => {
- const n4 = template('default header')()
- return n4
- })
- return n5
- },
- )
- return n0
- }),
- },
- true,
- )
- return n6
- },
- })
- const show = ref(false)
- const { html } = define({
- setup() {
- return createComponent(
- Parent,
- {
- show: () => show.value,
- },
- {
- header: () => template('custom header')(),
- },
- )
- },
- }).render()
- expect(html()).toBe('<!--if--><!--slot-->')
- show.value = true
- await nextTick()
- expect(html()).toBe(
- '<div>custom header<!--slot--></div><!--if--><!--slot-->',
- )
- show.value = false
- await nextTick()
- expect(html()).toBe('<!--if--><!--slot-->')
- })
- test('forwarded slot with fallback (v-if)', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('default', null, () => template('child fallback')())
- },
- })
- const show = ref(false)
- const Parent = defineVaporComponent({
- setup() {
- const n2 = createComponent(Child, null, {
- default: withVaporCtx(() => {
- const n0 = createSlot('default', null, () => {
- const n2 = createIf(
- () => show.value,
- () => {
- const n4 = template('<div>if content</div>')()
- return n4
- },
- )
- return n2
- })
- return n0
- }),
- })
- return n2
- },
- })
- const { html } = define({
- setup() {
- return createComponent(Parent, null, {
- default: () => template('<!-- <div>App</div> -->')(),
- })
- },
- }).render()
- expect(html()).toBe('child fallback<!--if--><!--slot--><!--slot-->')
- show.value = true
- await nextTick()
- expect(html()).toBe(
- '<div>if content</div><!--if--><!--slot--><!--slot-->',
- )
- })
- test('forwarded slot with fallback (v-for)', async () => {
- const Child = defineVaporComponent({
- setup() {
- return createSlot('default', null, () => template('child fallback')())
- },
- })
- const items = ref<number[]>([])
- const Parent = defineVaporComponent({
- setup() {
- const n2 = createComponent(Child, null, {
- default: withVaporCtx(() => {
- const n0 = createSlot('default', null, () => {
- const n2 = createFor(
- () => items.value,
- for_item0 => {
- const n4 = template('<span> </span>')() as any
- const x4 = child(n4) as any
- renderEffect(() =>
- setText(x4, toDisplayString(for_item0.value)),
- )
- return n4
- },
- )
- return n2
- })
- return n0
- }),
- })
- return n2
- },
- })
- const { html } = define({
- setup() {
- return createComponent(Parent, null, {
- default: () => template('<!-- <div>App</div> -->')(),
- })
- },
- }).render()
- expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
- items.value.push(1)
- await nextTick()
- expect(html()).toBe('<span>1</span><!--for--><!--slot--><!--slot-->')
- items.value.pop()
- await nextTick()
- expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
- })
- test('consecutive slots with insertion state', async () => {
- const { component: Child } = define({
- setup() {
- const n2 = template('<div><div>baz</div></div>', true)() as any
- setInsertionState(n2, 0)
- createSlot('default', null)
- setInsertionState(n2, 0)
- createSlot('foo', null)
- return n2
- },
- })
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => template('default')(),
- foo: () => template('foo')(),
- })
- },
- }).render()
- expect(html()).toBe(
- `<div>` +
- `default<!--slot-->` +
- `foo<!--slot-->` +
- `<div>baz</div>` +
- `</div>`,
- )
- })
- describe('vdom interop', () => {
- const createVaporSlot = (fallbackText = 'fallback') => {
- return defineVaporComponent({
- setup() {
- const n0 = createSlot('foo', null, () => {
- const n2 = template(`<div>${fallbackText}</div>`)()
- return n2
- })
- return n0
- },
- })
- }
- const createVdomSlot = (fallbackText = 'fallback') => {
- return {
- render(this: any) {
- return renderSlot(this.$slots, 'foo', {}, () => [
- h('div', fallbackText),
- ])
- },
- }
- }
- const createVaporForwardedSlot = (
- targetComponent: any,
- fallbackText?: string,
- ) => {
- return defineVaporComponent({
- setup() {
- const n2 = createComponent(
- targetComponent,
- null,
- {
- foo: withVaporCtx(() => {
- return fallbackText
- ? createSlot('foo', null, () => {
- const n2 = template(`<div>${fallbackText}</div>`)()
- return n2
- })
- : createSlot('foo', null)
- }),
- },
- true,
- )
- return n2
- },
- })
- }
- const createVdomForwardedSlot = (
- targetComponent: any,
- fallbackText?: string,
- ) => {
- return {
- render(this: any) {
- return h(targetComponent, null, {
- foo: () => [
- fallbackText
- ? renderSlot(this.$slots, 'foo', {}, () => [
- h('div', fallbackText),
- ])
- : renderSlot(this.$slots, 'foo'),
- ],
- _: 3 /* FORWARDED */,
- })
- },
- }
- }
- const createMultipleVaporForwardedSlots = (
- targetComponent: any,
- count: number,
- ) => {
- let current = targetComponent
- for (let i = 0; i < count; i++) {
- current = createVaporForwardedSlot(current)
- }
- return current
- }
- const createMultipleVdomForwardedSlots = (
- targetComponent: any,
- count: number,
- ) => {
- let current = targetComponent
- for (let i = 0; i < count; i++) {
- current = createVdomForwardedSlot(current)
- }
- return current
- }
- const createTestApp = (
- rootComponent: any,
- foo: Ref<string>,
- show: Ref<boolean>,
- ) => {
- return {
- setup() {
- return () =>
- h(
- rootComponent,
- null,
- createSlots({ _: 2 /* DYNAMIC */ } as any, [
- show.value
- ? {
- name: 'foo',
- fn: () => [h('span', foo.value)],
- key: '0',
- }
- : undefined,
- ]),
- )
- },
- }
- }
- const createEmptyTestApp = (rootComponent: any) => {
- return {
- setup() {
- return () => h(rootComponent)
- },
- }
- }
- test('vdom slot > vapor forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
- VaporSlot,
- 'forwarded fallback',
- )
- const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>forwarded fallback</div><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlot = createVaporForwardedSlot(VdomSlot)
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
- VdomSlot,
- 'forwarded fallback',
- )
- const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
- })
- test('vdom slot > vapor forwarded slot > vdom forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
- const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
- const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
- VdomForwardedSlot,
- 'forwarded fallback',
- )
- const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
- VaporSlot,
- 'vdom fallback',
- )
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithFallback,
- )
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vdom fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot(empty) > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
- VaporSlot,
- 'vdom fallback',
- )
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithFallback,
- )
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>vdom fallback</div>')
- })
- test('vdom forwarded fallback updates existing vapor slot block', async () => {
- const fallbackText = ref('fallback')
- const VdomForwardedSlotWithReactiveFallback = {
- render(this: any) {
- return renderSlot(this.$slots, 'foo', {}, () => [
- h('div', fallbackText.value),
- ])
- },
- }
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithReactiveFallback,
- )
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div>')
- fallbackText.value = 'updated'
- await nextTick()
- expect(root.innerHTML).toBe('<div>updated</div>')
- })
- test('vdom forwarded fallback updates when fallback function identity changes', async () => {
- const useAlt = ref(false)
- const VdomForwardedSlotWithDynamicFallback = {
- render(this: any) {
- const tag = useAlt.value ? 'p' : 'div'
- const text = useAlt.value ? 'alt fallback' : 'fallback'
- return renderSlot(this.$slots, 'foo', {}, () => [h(tag, text)])
- },
- }
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithDynamicFallback,
- )
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div>')
- useAlt.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<p>alt fallback</p>')
- })
- test('vdom forwarded fallback identity switch disposes stale fallback effects', async () => {
- const useAlt = ref(false)
- const primaryText = ref('fallback')
- const altText = ref('alt fallback')
- const VdomForwardedSlotWithDynamicFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useAlt.value
- ? () => [h('p', altText.value)]
- : () => [h('div', primaryText.value)],
- )
- },
- }
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithDynamicFallback,
- )
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div>')
- useAlt.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<p>alt fallback</p>')
- primaryText.value = 'stale fallback'
- await nextTick()
- expect(root.innerHTML).toBe('<p>alt fallback</p>')
- altText.value = 'active fallback'
- await nextTick()
- expect(root.innerHTML).toBe('<p>active fallback</p>')
- })
- test('vdom fallback updates when renderVaporSlot returns slot fragment', async () => {
- const useAlt = ref(false)
- const VdomSlotWithDynamicFallback = {
- render(this: any) {
- const tag = useAlt.value ? 'p' : 'div'
- const text = useAlt.value ? 'alt fallback' : 'fallback'
- return renderSlot(this.$slots, 'foo', {}, () => [h(tag, text)])
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithDynamicFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- useAlt.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<p>alt fallback</p><!--slot-->')
- })
- test('moving active vdom fallback keeps slot carrier order after teleport move', async () => {
- const targetA = document.createElement('div')
- targetA.id = 'component-slots-fallback-target-a'
- const targetB = document.createElement('div')
- targetB.id = 'component-slots-fallback-target-b'
- document.body.append(targetA, targetB)
- const to = ref('#component-slots-fallback-target-a')
- const fallbackText = ref('fallback')
- try {
- const VdomSlotWithReactiveFallback = {
- render(this: any) {
- return renderSlot(this.$slots, 'foo', {}, () => [
- h('div', fallbackText.value),
- ])
- },
- }
- const VaporTeleportedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VaporTeleport,
- {
- to: () => to.value,
- },
- {
- default: withVaporCtx(() =>
- createComponent(
- VdomSlotWithReactiveFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- ),
- ),
- },
- )
- },
- })
- const host = document.createElement('div')
- const app = createVaporApp(VaporTeleportedSlot)
- app.use(vaporInteropPlugin)
- app.mount(host)
- await nextTick()
- expect(targetA.innerHTML).toBe('<div>fallback</div><!--slot-->')
- expect(targetB.innerHTML).toBe('')
- to.value = '#component-slots-fallback-target-b'
- await nextTick()
- expect(targetA.innerHTML).toBe('')
- expect(targetB.innerHTML).toBe('<div>fallback</div><!--slot-->')
- fallbackText.value = 'moved fallback'
- await nextTick()
- expect(targetB.innerHTML).toBe('<div>moved fallback</div><!--slot-->')
- app.unmount()
- } finally {
- targetA.remove()
- targetB.remove()
- }
- })
- test('vdom fallback removal clears inherited vapor fallback', async () => {
- const useFallback = ref(true)
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- useFallback.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<!--slot-->')
- })
- test('vdom fallback can update to empty without crashing', async () => {
- const showFallback = ref(true)
- const VdomSlotWithReactiveFallback = {
- render(this: any) {
- return renderSlot(this.$slots, 'foo', {}, () =>
- showFallback.value ? [h('div', 'fallback')] : [],
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithReactiveFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- showFallback.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<!--slot-->')
- showFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- })
- test('vdom fallback removal clears active wrapper fallback for non-slot-fragment content', async () => {
- const useFallback = ref(true)
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() =>
- createIf(
- () => false,
- () => template('<span>content</span>')(),
- ),
- ),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div><!--if-->')
- useFallback.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<!--if-->')
- })
- test('vdom fallback toggles between local and inherited fallback for non-slot-fragment content', async () => {
- const useFallback = ref(true)
- const VaporSlot = createVaporSlot('outer fallback')
- const VdomForwardedSlotWithOptionalFallback = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [
- renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value
- ? () => [h('div', 'local fallback')]
- : undefined,
- ),
- ],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomForwardedSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() =>
- createIf(
- () => false,
- () => template('<span>content</span>')(),
- ),
- ),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>local fallback</div><!--if-->')
- useFallback.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>outer fallback</div><!--if-->')
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<div>local fallback</div><!--if-->')
- })
- test('nested interop vapor slot fallback should satisfy enclosing vapor slot validity after content becomes invalid', async () => {
- const showContent = ref(true)
- const OuterVaporSlot = createVaporSlot('outer fallback')
- const VdomSlotWithLocalFallback = {
- render(this: any) {
- return renderSlot(this.$slots, 'bar', {}, () => [
- h('div', 'local fallback'),
- ])
- },
- }
- const NestedInteropContainer = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithLocalFallback,
- null,
- {
- bar: withVaporCtx(() =>
- createIf(
- () => showContent.value,
- () => template('<span>content</span>')(),
- ),
- ),
- },
- true,
- )
- },
- })
- const App = defineVaporComponent({
- setup() {
- return createComponent(
- OuterVaporSlot,
- null,
- {
- foo: withVaporCtx(() =>
- createComponent(NestedInteropContainer, null, null),
- ),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(App).use(vaporInteropPlugin).mount(root)
- await nextTick()
- expect(root.innerHTML).toBe('<span>content</span><!--if--><!--slot-->')
- showContent.value = false
- await nextTick()
- expect(root.innerHTML).toBe(
- '<div>local fallback</div><!--if--><!--slot-->',
- )
- })
- test('vdom fallback addition activates inherited vapor fallback', async () => {
- const useFallback = ref(false)
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<!--slot-->')
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- })
- test('vdom fallback addition should not remount valid forwarded vapor content', async () => {
- const useFallback = ref(false)
- const mountSpy = vi.fn()
- const Content = defineVaporComponent({
- setup() {
- mountSpy()
- return template('<span>content</span>')()
- },
- })
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() => createComponent(Content, null, null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>content</span>')
- expect(mountSpy).toHaveBeenCalledTimes(1)
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>content</span>')
- expect(mountSpy).toHaveBeenCalledTimes(1)
- })
- test('vdom fallback added over valid forwarded vapor content should activate later when content becomes invalid', async () => {
- const useFallback = ref(false)
- const showContent = ref(true)
- const mountSpy = vi.fn()
- const Content = defineVaporComponent({
- setup() {
- mountSpy()
- return createIf(
- () => showContent.value,
- () => template('<span>content</span>')(),
- )
- },
- })
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() => createComponent(Content, null, null)),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>content</span><!--if-->')
- expect(mountSpy).toHaveBeenCalledTimes(1)
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>content</span><!--if-->')
- expect(mountSpy).toHaveBeenCalledTimes(1)
- showContent.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--if-->')
- expect(mountSpy).toHaveBeenCalledTimes(1)
- })
- test('vdom fallback added over forwarded vapor slot fragments should activate when slot content later becomes invalid', async () => {
- const useFallback = ref(false)
- const foo = ref('content')
- const showContent = ref(true)
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value ? () => [h('div', 'fallback')] : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const App = createTestApp(VaporForwardedSlot, foo, showContent)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>content</span>')
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>content</span>')
- showContent.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- })
- test('vdom fallback added later should propagate to nested slot boundaries inside still-valid content', async () => {
- const useFallback = ref(false)
- const showInner = ref(true)
- const NestedSlotContainer = defineVaporComponent({
- setup() {
- return [template('<span>stable</span>')(), createSlot('bar', null)]
- },
- })
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value
- ? () => [h('div', 'outer fallback')]
- : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() =>
- createComponent(
- NestedSlotContainer,
- null,
- {
- bar: withVaporCtx(() =>
- createIf(
- () => showInner.value,
- () => template('<i>inner</i>')(),
- ),
- ),
- },
- true,
- ),
- ),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe(
- '<span>stable</span><i>inner</i><!--if--><!--slot-->',
- )
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe(
- '<span>stable</span><i>inner</i><!--if--><!--slot-->',
- )
- showInner.value = false
- await nextTick()
- expect(root.innerHTML).toBe(
- '<span>stable</span><div>outer fallback</div><!--if--><!--slot-->',
- )
- })
- test('vdom fallback toggles should wait for the next nested invalidation inside still-valid content', async () => {
- const useFallback = ref(false)
- const showInner = ref(false)
- const NestedSlotContainer = defineVaporComponent({
- setup() {
- return [template('<span>stable</span>')(), createSlot('bar', null)]
- },
- })
- const VdomSlotWithOptionalFallback = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallback.value
- ? () => [h('div', 'outer fallback')]
- : undefined,
- )
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlotWithOptionalFallback,
- null,
- {
- foo: withVaporCtx(() =>
- createComponent(
- NestedSlotContainer,
- null,
- {
- bar: withVaporCtx(() =>
- createIf(
- () => showInner.value,
- () => template('<i>inner</i>')(),
- ),
- ),
- },
- true,
- ),
- ),
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- createVaporApp(VaporForwardedSlot).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>stable</span><!--if--><!--slot-->')
- useFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>stable</span><!--if--><!--slot-->')
- showInner.value = true
- await nextTick()
- expect(root.innerHTML).toBe(
- '<span>stable</span><i>inner</i><!--if--><!--slot-->',
- )
- showInner.value = false
- await nextTick()
- expect(root.innerHTML).toBe(
- '<span>stable</span><div>outer fallback</div><!--if--><!--slot-->',
- )
- })
- test('vdom local fallback should expose inherited fallback to nested slot boundaries', async () => {
- const VaporSlot = createVaporSlot('outer fallback')
- const NestedFallbackContainer = defineVaporComponent({
- setup() {
- return [
- template('<span>local stable</span>')(),
- createSlot('bar', null),
- ]
- },
- })
- const VdomForwardedSlotWithNestedFallback = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [
- renderSlot(this.$slots, 'foo', {}, () => [
- h(NestedFallbackContainer),
- ]),
- ],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const App = createEmptyTestApp(VdomForwardedSlotWithNestedFallback)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe(
- '<span>local stable</span><div>outer fallback</div>',
- )
- })
- test('vdom local fallback should expose inherited fallback to nested interop vapor slot outlets', async () => {
- const VaporSlot = createVaporSlot('outer fallback')
- const NestedInteropContainer = defineVaporComponent({
- setup() {
- return createSlot('bar', null)
- },
- })
- const localFallback = vi.fn(() => [
- h(NestedInteropContainer, null, {
- baz: () => [h('span', 'unused')],
- }),
- ])
- const VdomForwardedSlotWithNestedInteropFallback = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [renderSlot(this.$slots, 'foo', {}, localFallback)],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const App = createEmptyTestApp(
- VdomForwardedSlotWithNestedInteropFallback,
- )
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(localFallback).toHaveBeenCalledTimes(1)
- expect(root.textContent).toBe('outer fallback')
- })
- test('vdom local fallback should expose inherited fallback to nested interop vapor forwarded slots', async () => {
- const VaporSlot = createVaporSlot('outer fallback')
- const NestedVdomSlot = {
- render(this: any) {
- return renderSlot(this.$slots, 'bar')
- },
- }
- const NestedInteropForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- NestedVdomSlot,
- null,
- {
- bar: withVaporCtx(() => createSlot('bar', null)),
- },
- true,
- )
- },
- })
- const localFallback = vi.fn(() => [h(NestedInteropForwardedSlot)])
- const VdomForwardedSlotWithNestedInteropFallback = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [renderSlot(this.$slots, 'foo', {}, localFallback)],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const App = createEmptyTestApp(
- VdomForwardedSlotWithNestedInteropFallback,
- )
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(localFallback).toHaveBeenCalledTimes(1)
- expect(root.textContent).toBe('outer fallback')
- })
- test('vdom local fallback should keep nested inherited vapor fallback reactive after mount', async () => {
- const fallbackText = ref('outer fallback')
- const VaporSlot = defineVaporComponent({
- setup() {
- return createSlot('foo', null, () => {
- const el = template('<div></div>')()
- renderEffect(() => {
- setElementText(el, fallbackText.value)
- })
- return el
- })
- },
- })
- const NestedInteropContainer = defineVaporComponent({
- setup() {
- return createSlot('bar', null)
- },
- })
- const localFallback = vi.fn(() => [
- h(NestedInteropContainer, null, {
- baz: () => [h('span', 'unused')],
- }),
- ])
- const VdomForwardedSlotWithNestedInteropFallback = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [renderSlot(this.$slots, 'foo', {}, localFallback)],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const App = createEmptyTestApp(
- VdomForwardedSlotWithNestedInteropFallback,
- )
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(localFallback).toHaveBeenCalledTimes(1)
- expect(root.textContent).toBe('outer fallback')
- fallbackText.value = 'updated outer fallback'
- await nextTick()
- expect(root.textContent).toBe('updated outer fallback')
- })
- test('vdom forwarded inherited vapor fallback should clean up old fallback effects', async () => {
- const show = ref(false)
- const fallbackText = ref('fallback')
- const fallbackRuns = vi.fn()
- const VaporSlot = defineVaporComponent({
- setup() {
- return createSlot('foo', null, () => {
- const el = template('<div></div>')()
- renderEffect(() => {
- fallbackRuns()
- setElementText(el, fallbackText.value)
- })
- return el
- })
- },
- })
- const VdomForwardedSlot = {
- render(this: any) {
- return h(VaporSlot, null, {
- foo: () => [renderSlot(this.$slots, 'foo', {}, () => [])],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const App = {
- setup() {
- return () =>
- h(
- VdomForwardedSlot,
- null,
- createSlots({ _: 2 /* DYNAMIC */ } as any, [
- show.value
- ? {
- name: 'foo',
- fn: () => [h('span', 'content')],
- key: '0',
- }
- : undefined,
- ]),
- )
- },
- }
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>content</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- const runsBeforeUpdate = fallbackRuns.mock.calls.length
- fallbackText.value = 'updated'
- await nextTick()
- expect(root.innerHTML).toBe('<div>updated</div>')
- expect(fallbackRuns.mock.calls.length - runsBeforeUpdate).toBe(1)
- })
- test('unmounted passthrough vdom fallback should not react to inherited boundary updates', async () => {
- const showInner = ref(true)
- const useOuterFallback = ref(false)
- const localFallbackRuns = vi.fn()
- const InnerVdomSlot = {
- render(this: any) {
- return renderSlot(this.$slots, 'bar', {}, () => {
- localFallbackRuns()
- return [h('div', 'inner fallback')]
- })
- },
- }
- const OuterVdomSlot = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useOuterFallback.value
- ? () => [h('div', 'outer fallback')]
- : undefined,
- )
- },
- }
- const InnerBridge = defineVaporComponent({
- setup() {
- return createComponent(
- InnerVdomSlot,
- null,
- {
- bar: withVaporCtx(() => createSlot('bar', null)),
- },
- true,
- )
- },
- })
- const OuterBridge = defineVaporComponent({
- setup() {
- return createComponent(
- OuterVdomSlot,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const App = {
- setup() {
- return () =>
- h(
- OuterBridge,
- null,
- createSlots({ _: 2 /* DYNAMIC */ } as any, [
- showInner.value
- ? {
- name: 'foo',
- fn: () => [h(InnerBridge)],
- key: '0',
- }
- : undefined,
- ]),
- )
- },
- }
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>inner fallback</div>')
- showInner.value = false
- await nextTick()
- expect(root.innerHTML).toBe('')
- const runsBefore = localFallbackRuns.mock.calls.length
- useOuterFallback.value = true
- await nextTick()
- useOuterFallback.value = false
- await nextTick()
- expect(localFallbackRuns.mock.calls.length).toBe(runsBefore)
- })
- test('mounted passthrough vdom local fallback ignores unrelated inherited boundary updates', async () => {
- const useOuterFallback = ref(false)
- const localFallbackRuns = vi.fn()
- const InnerVdomSlot = {
- render(this: any) {
- return renderSlot(this.$slots, 'bar', {}, () => {
- localFallbackRuns()
- return [h('div', 'inner fallback')]
- })
- },
- }
- const OuterVdomSlot = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'foo',
- {},
- useOuterFallback.value
- ? () => [h('div', 'outer fallback')]
- : undefined,
- )
- },
- }
- const InnerBridge = defineVaporComponent({
- setup() {
- return createComponent(
- InnerVdomSlot,
- null,
- {
- bar: withVaporCtx(() => createSlot('bar', null)),
- },
- true,
- )
- },
- })
- const OuterBridge = defineVaporComponent({
- setup() {
- return createComponent(
- OuterVdomSlot,
- null,
- {
- foo: withVaporCtx(() => createSlot('foo', null)),
- },
- true,
- )
- },
- })
- const App = {
- setup() {
- return () =>
- h(
- OuterBridge,
- null,
- createSlots({ _: 2 /* DYNAMIC */ } as any, [
- {
- name: 'foo',
- fn: () => [h(InnerBridge)],
- key: '0',
- },
- ]),
- )
- },
- }
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>inner fallback</div>')
- const runsBefore = localFallbackRuns.mock.calls.length
- useOuterFallback.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<div>inner fallback</div>')
- expect(localFallbackRuns.mock.calls.length - runsBefore).toBe(0)
- })
- test('failed forwarded vapor slot remount should not keep stale fallback watchers', async () => {
- const values = ref([0])
- const showFallback = ref(false)
- const fallbackRuns = vi.fn()
- const handled = vi.fn()
- const VdomChild = {
- render(this: any) {
- return renderSlot(
- this.$slots,
- 'default',
- {},
- showFallback.value
- ? () => {
- fallbackRuns()
- return [h('div', 'outer fallback')]
- }
- : undefined,
- )
- },
- }
- const VaporParent = defineVaporComponent({
- setup() {
- return createComponent(
- VdomChild,
- null,
- {
- $: [
- () =>
- createForSlots(values.value, value => ({
- name: 'default',
- fn: () => {
- if (value === 1) {
- throw new Error('slot boom')
- }
- return template('<span>ok</span>')()
- },
- })),
- ],
- },
- true,
- )
- },
- })
- const root = document.createElement('div')
- const app = createApp({
- render: () => h(VaporParent as any),
- })
- app.use(vaporInteropPlugin)
- app.config.errorHandler = handled
- app.mount(root)
- expect(root.innerHTML).toBe('<span>ok</span>')
- values.value = [1]
- await nextTick()
- expect(handled).toHaveBeenCalledTimes(1)
- const runsBefore = fallbackRuns.mock.calls.length
- showFallback.value = true
- await nextTick()
- expect(fallbackRuns.mock.calls.length).toBe(runsBefore)
- })
- test('switching vdom fallback identity disposes stale fallback effects', async () => {
- const fallbackA = ref('fallback A')
- const fallbackB = ref('fallback B')
- const useFallbackA = ref(true)
- const VaporSlot = defineVaporComponent({
- setup() {
- return createSlot('foo', null) as any
- },
- })
- const VdomForwardedSlotWithDynamicFallback = {
- render(this: any) {
- return h(VaporSlot as any, null, {
- foo: () => [
- renderSlot(
- this.$slots,
- 'foo',
- {},
- useFallbackA.value
- ? () => [h('div', fallbackA.value)]
- : () => [h('div', fallbackB.value)],
- ),
- ],
- _: 3 /* FORWARDED */,
- })
- },
- }
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithDynamicFallback,
- )
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<div>fallback A</div>')
- fallbackA.value = 'fallback A updated'
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback A updated</div>')
- useFallbackA.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback B</div>')
- fallbackA.value = 'stale fallback A'
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback B</div>')
- fallbackB.value = 'fallback B updated'
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback B updated</div>')
- })
- test('vdom slot does not evaluate fallback while forwarded vapor content resolves valid output', () => {
- const fallback = vi.fn(() => [h('div', 'fallback')])
- const VdomSlot = {
- render(this: any) {
- return renderSlot(this.$slots, 'foo', {}, fallback)
- },
- }
- const VaporForwardedSlot = defineVaporComponent({
- setup() {
- return createComponent(
- VdomSlot,
- null,
- {
- foo: withVaporCtx(() =>
- createIf(
- () => true,
- () => template('<span>content</span>')(),
- ),
- ),
- },
- true,
- )
- },
- })
- const App = createEmptyTestApp(VaporForwardedSlot)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.textContent).toBe('content')
- expect(fallback).not.toHaveBeenCalled()
- })
- test('vdom slot > vapor forwarded slot > vdom forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
- const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
- const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
- VdomForwardedSlot,
- 'vapor fallback',
- )
- const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vapor fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
- VdomSlot,
- 'vdom fallback',
- )
- const VaporForwardedSlot = createVaporForwardedSlot(
- VdomForwardedSlotWithFallback,
- )
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vdom fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
- const VaporForwardedSlot = createMultipleVaporForwardedSlots(
- VdomForwardedSlot,
- 3,
- )
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
- VdomSlot,
- 'vdom fallback',
- )
- const VaporForwardedSlot = createMultipleVaporForwardedSlots(
- VdomForwardedSlotWithFallback,
- 3,
- )
- const App = createTestApp(VaporForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe(
- '<div>vdom fallback</div><!--slot--><!--slot-->',
- )
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- })
- test('vdom slot > vdom forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
- const App = createTestApp(VdomForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vdom forwarded slot > vapor forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
- const VdomForwardedSlot = createVdomForwardedSlot(VaporForwardedSlot)
- const App = createTestApp(VdomForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
- const VdomForwardedSlot = createMultipleVdomForwardedSlots(
- VaporForwardedSlot,
- 3,
- )
- const App = createTestApp(VdomForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot = createVaporForwardedSlot(
- VaporSlot,
- 'vapor fallback',
- )
- const VdomForwardedSlot = createMultipleVdomForwardedSlots(
- VaporForwardedSlot,
- 3,
- )
- const App = createTestApp(VdomForwardedSlot, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vapor fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot > vapor forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlot1 = createMultipleVaporForwardedSlots(
- VdomSlot,
- 2,
- )
- const App = createTestApp(VaporForwardedSlot1, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlot2 = createVaporForwardedSlot(VdomSlot)
- const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
- VaporForwardedSlot2,
- 'vapor1 fallback',
- )
- const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
- VdomSlot,
- 'vapor2 fallback',
- )
- const VaporForwardedSlot1 = createVaporForwardedSlot(
- VaporForwardedSlot2WithFallback,
- )
- const App = createTestApp(VaporForwardedSlot1, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vapor2 fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot > vapor forwarded slot > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot2 = createVaporForwardedSlot(VaporSlot)
- const VaporForwardedSlot1 =
- createVaporForwardedSlot(VaporForwardedSlot2)
- const App = createTestApp(VaporForwardedSlot1, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
- VdomSlot,
- 'vapor2 fallback',
- )
- const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
- VaporForwardedSlot2WithFallback,
- 'vapor1 fallback',
- )
- const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
- })
- test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
- VaporSlot,
- 'vapor2 fallback',
- )
- const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
- VaporForwardedSlot2WithFallback,
- 'vapor1 fallback',
- )
- const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe(
- '<div>vapor1 fallback</div><!--slot--><!--slot-->',
- )
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
- })
- test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
- VaporSlot,
- 'vdom2 fallback',
- )
- const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
- VdomForwardedSlot2WithFallback,
- 'vdom1 fallback',
- )
- const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vdom slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VdomSlot = createVdomSlot()
- const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
- VdomSlot,
- 'vdom2 fallback',
- )
- const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
- VdomForwardedSlot2WithFallback,
- 'vdom1 fallback',
- )
- const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) (multiple) > vapor slot', async () => {
- const foo = ref('foo')
- const show = ref(true)
- const VaporSlot = createVaporSlot()
- const VdomForwardedSlot3WithFallback = createVdomForwardedSlot(
- VaporSlot,
- 'vdom3 fallback',
- )
- const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
- VdomForwardedSlot3WithFallback,
- 'vdom2 fallback',
- )
- const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
- VdomForwardedSlot2WithFallback,
- 'vdom1 fallback',
- )
- const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
- const root = document.createElement('div')
- createApp(App).use(vaporInteropPlugin).mount(root)
- expect(root.innerHTML).toBe('<span>foo</span>')
- foo.value = 'bar'
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- show.value = false
- await nextTick()
- expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
- show.value = true
- await nextTick()
- expect(root.innerHTML).toBe('<span>bar</span>')
- })
- })
- })
- describe('createForSlots', () => {
- test('should work', async () => {
- const loop = ref([1, 2, 3])
- let instance: any
- const Child = () => {
- instance = currentInstance
- return template('child')()
- }
- const { render } = define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(loop.value, (item, i) => ({
- name: item,
- fn: () => template(item + i)(),
- })),
- ],
- })
- },
- })
- render()
- expect(instance.slots).toHaveProperty('1')
- expect(instance.slots).toHaveProperty('2')
- expect(instance.slots).toHaveProperty('3')
- loop.value.push(4)
- await nextTick()
- expect(instance.slots).toHaveProperty('4')
- loop.value.shift()
- await nextTick()
- expect(instance.slots).not.toHaveProperty('1')
- })
- test('should cache dynamic slot source result', async () => {
- const items = ref([1, 2, 3])
- let callCount = 0
- const getItems = () => {
- callCount++
- return items.value
- }
- let instance: any
- const Child = defineVaporComponent(() => {
- instance = currentInstance
- // Create multiple slots to trigger multiple getSlot calls
- const n1 = template('<div></div>')()
- const n2 = template('<div></div>')()
- const n3 = template('<div></div>')()
- insert(createSlot('slot1'), n1 as any as ParentNode)
- insert(createSlot('slot2'), n2 as any as ParentNode)
- insert(createSlot('slot3'), n3 as any as ParentNode)
- return [n1, n2, n3]
- })
- define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(getItems(), (item, i) => ({
- name: 'slot' + item,
- fn: () => template(String(item))(),
- })),
- ],
- })
- },
- }).render()
- // getItems should only be called once
- expect(callCount).toBe(1)
- expect(instance.slots).toHaveProperty('slot1')
- expect(instance.slots).toHaveProperty('slot2')
- expect(instance.slots).toHaveProperty('slot3')
- })
- test('should update when source changes', async () => {
- const items = ref([1, 2])
- let callCount = 0
- const getItems = () => {
- callCount++
- return items.value
- }
- let instance: any
- const Child = defineVaporComponent(() => {
- instance = currentInstance
- const n1 = template('<div></div>')()
- const n2 = template('<div></div>')()
- const n3 = template('<div></div>')()
- insert(createSlot('slot1'), n1 as any as ParentNode)
- insert(createSlot('slot2'), n2 as any as ParentNode)
- insert(createSlot('slot3'), n3 as any as ParentNode)
- return [n1, n2, n3]
- })
- define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(getItems(), (item, i) => ({
- name: 'slot' + item,
- fn: () => template(String(item))(),
- })),
- ],
- })
- },
- }).render()
- expect(callCount).toBe(1)
- expect(instance.slots).toHaveProperty('slot1')
- expect(instance.slots).toHaveProperty('slot2')
- expect(instance.slots).not.toHaveProperty('slot3')
- // Update items
- items.value.push(3)
- await nextTick()
- // Should be called again after source changes
- expect(callCount).toBe(2)
- expect(instance.slots).toHaveProperty('slot1')
- expect(instance.slots).toHaveProperty('slot2')
- expect(instance.slots).toHaveProperty('slot3')
- })
- test('should render slots correctly with caching', async () => {
- const items = ref([1, 2, 3, 4, 5])
- const Child = defineVaporComponent(() => {
- const containers: any[] = []
- for (let i = 1; i <= 5; i++) {
- const n = template('<div></div>')()
- insert(createSlot('slot' + i), n as any as ParentNode)
- containers.push(n)
- }
- return containers
- })
- const { host } = define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(items.value, item => ({
- name: 'slot' + item,
- fn: () => template('content' + item)(),
- })),
- ],
- })
- },
- }).render()
- expect(host.innerHTML).toBe(
- '<div>content1<!--slot--></div>' +
- '<div>content2<!--slot--></div>' +
- '<div>content3<!--slot--></div>' +
- '<div>content4<!--slot--></div>' +
- '<div>content5<!--slot--></div>',
- )
- // Update items
- items.value = [2, 4]
- await nextTick()
- expect(host.innerHTML).toBe(
- '<div><!--slot--></div>' +
- '<div>content2<!--slot--></div>' +
- '<div><!--slot--></div>' +
- '<div>content4<!--slot--></div>' +
- '<div><!--slot--></div>',
- )
- })
- // #14648
- test('should use last slot when v-for generates duplicate slot names', async () => {
- const list = ref([0, 1, 2])
- const Child = defineVaporComponent(() => {
- const n = template('<div></div>')()
- insert(createSlot('default'), n as any as ParentNode)
- return n
- })
- const { host } = define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(list.value, item => ({
- name: 'default',
- fn: () => template(String(item))(),
- })),
- ],
- })
- },
- }).render()
- // should display the last item (last wins, matching vDOM behavior)
- expect(host.innerHTML).toBe('<div>2<!--slot--></div>')
- // push: new last item should be displayed
- list.value.push(3)
- await nextTick()
- expect(host.innerHTML).toBe('<div>3<!--slot--></div>')
- // pop: should fall back to previous last item
- list.value.pop()
- await nextTick()
- expect(host.innerHTML).toBe('<div>2<!--slot--></div>')
- // splice middle: last item unchanged
- list.value.splice(1, 1)
- await nextTick()
- expect(host.innerHTML).toBe('<div>2<!--slot--></div>')
- })
- test('should work with null and undefined', async () => {
- const loop = ref<number[] | null | undefined>(undefined)
- let instance: any
- const Child = () => {
- instance = currentInstance
- return template('child')()
- }
- const { render } = define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(loop.value as any, (item, i) => ({
- name: item,
- fn: () => template(item + i)(),
- })),
- ],
- })
- },
- })
- render()
- expect(instance.slots).toEqual({})
- loop.value = [1]
- await nextTick()
- expect(instance.slots).toHaveProperty('1')
- loop.value = null
- await nextTick()
- expect(instance.slots).toEqual({})
- })
- })
- })
|