| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463 |
- import {
- VaporTeleport,
- child,
- createComponent,
- createPlainElement,
- createVaporSSRApp,
- defineVaporAsyncComponent,
- defineVaporComponent,
- delegateEvents,
- renderEffect,
- setStyle,
- template,
- useVaporCssVars,
- } from '../src'
- import { defineAsyncComponent, nextTick, reactive, ref } from '@vue/runtime-dom'
- import { isString } from '@vue/shared'
- import type { VaporComponentInstance } from '../src/component'
- import type { TeleportFragment } from '../src/components/Teleport'
- import { VueServerRenderer, compile, runtimeDom, runtimeVapor } from './_utils'
- const formatHtml = (raw: string) => {
- return raw
- .replace(/<!--\[/g, '\n<!--[')
- .replace(/]-->/g, ']-->\n')
- .replace(/\n{2,}/g, '\n')
- }
- async function testWithVaporApp(
- code: string,
- components?: Record<string, string | { code: string; vapor: boolean }>,
- data?: any,
- ) {
- return testHydration(code, components, data, {
- isVaporApp: true,
- interop: true,
- })
- }
- async function testWithVDOMApp(
- code: string,
- components?: Record<string, string | { code: string; vapor: boolean }>,
- data?: any,
- ) {
- return testHydration(code, components, data, {
- isVaporApp: false,
- interop: true,
- })
- }
- function compileVaporComponent(
- code: string,
- data: runtimeDom.Ref<any> = ref({}),
- components?: Record<string, any>,
- ssr = false,
- ) {
- if (!code.includes(`<script`)) {
- code = `<template>${code}</template>`
- }
- return compile(code, data, components, {
- vapor: true,
- ssr,
- })
- }
- async function mountWithHydration(
- html: string,
- code: string,
- data: runtimeDom.Ref<any> = ref({}),
- components?: Record<string, any>,
- ) {
- const container = document.createElement('div')
- container.innerHTML = html
- document.body.appendChild(container)
- const clientComp = compileVaporComponent(code, data, components)
- const app = createVaporSSRApp(clientComp)
- app.mount(container)
- return {
- block: (app._instance! as VaporComponentInstance).block,
- container,
- }
- }
- async function testHydration(
- code: string,
- components: Record<string, string | { code: string; vapor: boolean }> = {},
- data: any = ref('foo'),
- { isVaporApp = true, interop = false } = {},
- ) {
- const ssrComponents: any = {}
- const clientComponents: any = {}
- for (const key in components) {
- const comp = components[key]
- const code = isString(comp) ? comp : comp.code
- const isVaporComp = isString(comp) || !!comp.vapor
- clientComponents[key] = compile(code, data, clientComponents, {
- vapor: isVaporComp,
- ssr: false,
- })
- ssrComponents[key] = compile(code, data, ssrComponents, {
- vapor: isVaporComp,
- ssr: true,
- })
- }
- const serverComp = compile(code, data, ssrComponents, {
- vapor: isVaporApp,
- ssr: true,
- })
- const html = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(serverComp),
- )
- const container = document.createElement('div')
- document.body.appendChild(container)
- container.innerHTML = html
- const clientComp = compile(code, data, clientComponents, {
- vapor: isVaporApp,
- ssr: false,
- })
- let app
- if (isVaporApp) {
- app = createVaporSSRApp(clientComp)
- } else {
- app = runtimeDom.createSSRApp(clientComp)
- }
- if (interop) {
- app.use(runtimeVapor.vaporInteropPlugin)
- }
- app.mount(container)
- return { data, container }
- }
- const triggerEvent = (type: string, el: Element) => {
- const event = new Event(type, { bubbles: true })
- el.dispatchEvent(event)
- }
- delegateEvents('click')
- beforeEach(() => {
- document.body.innerHTML = ''
- })
- describe('Vapor Mode hydration', () => {
- describe('text', () => {
- test('root text', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foo"`)
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"bar"`)
- })
- test('consecutive text nodes', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foofoo"`)
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"barbar"`)
- })
- test('consecutive text nodes with insertion anchor', async () => {
- const { data, container } = await testHydration(`
- <template><span/>{{ data }}{{ data }}<span/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>foofoo<span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>barbar<span></span><!--]-->
- "
- `,
- )
- })
- test('mixed text nodes', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}A{{ data }}B{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"fooAfooBfoo"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"barAbarBbar"`,
- )
- })
- test('mixed text nodes with insertion anchor', async () => {
- const { data, container } = await testHydration(`
- <template><span/>{{ data }}A{{ data }}B{{ data }}<span/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>fooAfooBfoo<span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>barAbarBbar<span></span><!--]-->
- "
- `,
- )
- })
- test('empty text node', async () => {
- const data = reactive({ txt: '' })
- const { container } = await testHydration(
- `<template><div>{{ data.txt }}</div></template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div></div>"`,
- )
- data.txt = 'foo'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div>"`,
- )
- })
- test('empty text node in slot', async () => {
- const data = reactive({ txt: '' })
- const { container } = await testHydration(
- `<template><components.Child>{{data.txt}}</components.Child></template>`,
- {
- Child: `<template><slot/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.txt = 'foo'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo<!--]-->
- "
- `,
- )
- })
- })
- describe('element', () => {
- test('root comment', async () => {
- const { container } = await testHydration(`
- <template><!----></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"<!---->"`)
- expect(`mismatch in <div>`).not.toHaveBeenWarned()
- })
- test('root with mixed element and text', async () => {
- const { container, data } = await testHydration(`
- <template> A<span>{{ data }}</span>{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--> A<span>foo</span>foo<!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--> A<span>bar</span>bar<!--]-->
- "
- `,
- )
- })
- test('empty element', async () => {
- const { container } = await testHydration(`
- <template><div/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div></div>"`,
- )
- expect(`mismatch in <div>`).not.toHaveBeenWarned()
- })
- test('element with binding and text children', async () => {
- const { container, data } = await testHydration(`
- <template><div :class="data">{{ data }}</div></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="foo">foo</div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="bar">bar</div>"`,
- )
- })
- test('element with elements children', async () => {
- const { container } = await testHydration(`
- <template>
- <div>
- <span>{{ data }}</span>
- <span :class="data" @click="data = 'bar'"/>
- </div>
- </template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span class="foo"></span></div>"`,
- )
- // event handler
- triggerEvent('click', container.querySelector('.foo')!)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>bar</span><span class="bar"></span></div>"`,
- )
- })
- test('element with ref', async () => {
- const { data, container } = await testHydration(
- `<template>
- <div ref="data">hi</div>
- </template>
- `,
- {},
- ref(null),
- )
- expect(data.value).toBe(container.firstChild)
- })
- })
- describe('component', () => {
- test('basic component', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Child/></div></template>
- `,
- { Child: `<template>{{ data }}</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo</div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar</div>"`,
- )
- })
- test('fragment component', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Child/></div></template>
- `,
- { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- </div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- </div>"
- `,
- )
- })
- test('fragment component with prepend', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><components.Child/><span></span></div></template>
- `,
- { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment components', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div/><components.Child/><div/></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div></div>
- <!--[--><div>foo</div>-foo-<!--]-->
- <div></div><!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div></div>
- <!--[--><div>bar</div>-bar-<!--]-->
- <div></div><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template>{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar<span></span></div>"`,
- )
- })
- test('nested components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><span></span></div>"`,
- )
- })
- test('nested components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>foo</div><span></span></div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>bar</div><span></span></div><span></span></div>"`,
- )
- })
- test('consecutive components with insertion parent', async () => {
- const data = reactive({ foo: 'foo', bar: 'bar' })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child1/>
- <components.Child2/>
- </div>
- </template>
- `,
- {
- Child1: `<template><span>{{ data.foo }}</span></template>`,
- Child2: `<template><span>{{ data.bar }}</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div>"`,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo1</span><span>bar1</span></div>"`,
- )
- })
- test('nested consecutive components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><div>foo</div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><div>bar</div><span></span></div>"`,
- )
- })
- test('nested consecutive components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>foo</div><div>foo</div><span></span></div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>bar</div><div>bar</div><span></span></div><span></span></div>"`,
- )
- })
- test('mixed component and element with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template>{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<span></span>foo<span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar<span></span>bar<span></span></div>"`,
- )
- })
- test('fragment component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment component with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span/><components.Parent/><span/></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- })
- test('consecutive fragment components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with root level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><components.Child/><components.Child/></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--]-->
- <span></span></div>"
- `,
- )
- })
- test('mixed fragment component and element with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('mixed fragment component and text with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- {{ data }}
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- foo
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- bar
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- })
- describe('dynamic component', () => {
- test('basic dynamic component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <component :is="components[data]"/>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--dynamic-component-->"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>bar</div><!--dynamic-component-->"`,
- )
- })
- test('dynamic component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components[data]"/>
- <span/>
- </div>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><!--dynamic-component--><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><!--dynamic-component--><span></span></div>"`,
- )
- })
- test('consecutive dynamic components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components[data]"/>
- <component :is="components[data]"/>
- <span/>
- </div>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><!--dynamic-component--><div>foo</div><!--dynamic-component--><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><!--dynamic-component--><div>bar</div><!--dynamic-component--><span></span></div>"`,
- )
- })
- test('dynamic component fallback', async () => {
- const { container, data } = await testHydration(
- `<template>
- <component :is="'button'">
- <span>{{ data }}</span>
- </component>
- </template>`,
- {},
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button><span>foo</span></button><!--dynamic-component-->"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button><span>bar</span></button><!--dynamic-component-->"`,
- )
- })
- test('in ssr slot vnode fallback', async () => {
- const { container, data } = await testHydration(
- `<template>
- <components.Child>
- <span>{{ data }}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <component :is="'div'">
- <slot />
- </component>
- </template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- </div><!--dynamic-component-->"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>bar</span><!--]-->
- </div><!--dynamic-component-->"
- `,
- )
- })
- test('dynamic component fallback with dynamic slots', async () => {
- const data = ref({
- name: 'default',
- msg: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <component :is="'div'">
- <template v-slot:[data.name]>
- <span>{{ data.msg }}</span>
- </template>
- </component>
- </template>`,
- {},
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><!----></div><!--dynamic-component-->"`,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>bar</span><!----></div><!--dynamic-component-->"`,
- )
- })
- })
- describe('if', () => {
- test('basic toggle - true -> false', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">foo</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('basic toggle - false -> true', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">foo</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('v-if on insertion parent', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">
- <components.Child/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('v-if/else-if/else chain - switch branches', async () => {
- const data = ref('a')
- const { container } = await testHydration(
- `<template>
- <div v-if="data === 'a'">foo</div>
- <div v-else-if="data === 'b'">bar</div>
- <div v-else>baz</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = 'b'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>bar</div><!--if--><!--if-->"`,
- )
- data.value = 'c'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>baz</div><!--if--><!--if-->"`,
- )
- data.value = 'a'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('v-if/else with sibling components and elements', async () => {
- const data = ref('a')
- const { container } = await testHydration(
- `<script setup>
- const msg = _data
- const { Comp } = _components
- </script>
- <template>
- <div>
- <Comp/>
- <div>11</div>
- <div v-if="msg === 'a'">foo</div>
- <div v-else>baz</div>
- <div>11</div>
- <Comp/>
- </div>
- </template>`,
- {
- Comp: `<template><span>comp</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>comp</span><div>11</div><div>foo</div><!--if--><div>11</div><span>comp</span></div>"`,
- )
- data.value = 'b'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>comp</span><div>11</div><div>baz</div><!--if--><div>11</div><span>comp</span></div>"`,
- )
- })
- test('nested if', async () => {
- const data = reactive({ outer: true, inner: true })
- const { container } = await testHydration(
- `<template>
- <div v-if="data.outer">
- <span>outer</span>
- <div v-if="data.inner">inner</div>
- </div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>outer</span><div>inner</div><!--if--></div><!--if-->"`,
- )
- data.inner = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>outer</span><!--if--></div><!--if-->"`,
- )
- data.outer = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('on component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <components.Child v-if="data"/>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"foo<!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('consecutive if node', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <components.Child v-if="data"/>
- </template>`,
- { Child: `<template><div v-if="data">foo</div></template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if--><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if--><!--if-->"`,
- )
- })
- test('mixed prepend and insertion anchor', async () => {
- const data = reactive({
- show: true,
- foo: 'foo',
- bar: 'bar',
- qux: 'qux',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child/>
- </template>`,
- {
- Child: `<template>
- <span v-if="data.show">
- <span v-if="data.show">{{data.foo}}</span>
- <span v-if="data.show">{{data.bar}}</span>
- <span>baz</span>
- <span v-if="data.show">{{data.qux}}</span>
- <span>quux</span>
- </span>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- data.qux = 'qux1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- data.foo = 'foo1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo1</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- })
- test('v-if/else-if/else chain on component - switch branches', async () => {
- const data = ref('a')
- const { container } = await testHydration(
- `<template>
- <components.Child1 v-if="data === 'a'"/>
- <components.Child2 v-else-if="data === 'b'"/>
- <components.Child3 v-else/>
- </template>`,
- {
- Child1: `<template><span>{{data}} child1</span></template>`,
- Child2: `<template><span>{{data}} child2</span></template>`,
- Child3: `<template><span>{{data}} child3</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>a child1</span><!--if-->"`,
- )
- data.value = 'b'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>b child2</span><!--if--><!--if-->"`,
- )
- data.value = 'c'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>c child3</span><!--if--><!--if-->"`,
- )
- data.value = 'a'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>a child1</span><!--if-->"`,
- )
- })
- test('on component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--if--><span></span></div>"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><!--if--><span></span></div>"`,
- )
- })
- test('consecutive component with insertion parent', async () => {
- const data = reactive({
- show: true,
- foo: 'foo',
- bar: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <div v-if="data.show">
- <components.Child/>
- <components.Child2/>
- </div>
- </template>`,
- {
- Child: `<template><span>{{data.foo}}</span></template>`,
- Child2: `<template><span>{{data.bar}}</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo1</span><span>bar1</span></div><!--if-->"`,
- )
- })
- test('on fragment component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child v-if="data"/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>true</div>-true-<!--]-->
- <!--if--></div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <!--if--></div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <div>true</div>-true-<!--if--></div>"
- `,
- )
- })
- test('on fragment component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--]-->
- <!--if--><span></span></div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><!--]-->
- <!--if--><span></span></div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div><span></span>
- <!--[--><!--]-->
- <div>true</div>-true-<!--if--><span></span></div>"
- `)
- })
- test('consecutive v-if on fragment component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--]-->
- <!--if-->
- <!--[--><div>true</div>-true-<!--]-->
- <!--if--><span></span></div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><!--]-->
- <!--if-->
- <!--[--><!--]-->
- <!--if--><span></span></div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div><span></span>
- <!--[--><!--]-->
- <div>true</div>-true-<!--if-->
- <!--[--><!--]-->
- <div>true</div>-true-<!--if--><span></span></div>"
- `)
- })
- test('on dynamic component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components.Child" v-if="data"/>
- <span/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><!--if--><span></span></div>"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
- )
- })
- test('v-if with insertion parent + sibling component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span v-if="data">hello</span>
- </div>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>child</div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><span>hello</span><!--if--></div><div>child</div><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><!--if--></div><div>child</div><!--]-->
- "
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><span>hello</span><!--if--></div><div>child</div><!--]-->
- "
- `,
- )
- })
- test('v-if with static sibling + root sibling component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span v-if="data">hello</span>
- <div>1</div>
- </div>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>child</div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><span>hello</span><!--if--><div>1</div></div><div>child</div><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><!--if--><div>1</div></div><div>child</div><!--]-->
- "
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div><span>hello</span><!--if--><div>1</div></div><div>child</div><!--]-->
- "
- `,
- )
- })
- test('v-if + static sibling + root sibling component (flat)', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <span v-if="data">hello</span>
- <span></span>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>child</div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>hello</span><!--if--><span></span><div>child</div><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--if--><span></span><div>child</div><!--]-->
- "
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>hello</span><!--if--><span></span><div>child</div><!--]-->
- "
- `,
- )
- })
- })
- describe('for', () => {
- test('basic v-for', async () => {
- const { container, data } = await testHydration(
- `<template>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- "
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- "
- `,
- )
- })
- test('empty v-for', async () => {
- const { container, data } = await testHydration(
- `<template>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </template>`,
- undefined,
- ref([]),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.value.push('a')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><!--]-->
- "
- `,
- )
- })
- test('v-for with insertion parent + sibling component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </div>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>{{data.length}}</div></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- </div><div>3</div><!--]-->
- "
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- </div><div>4</div><!--]-->
- "
- `,
- )
- })
- test('v-for with static sibling + root sibling component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <div>1</div>
- </div>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>{{data.length}}</div></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <div>1</div></div><div>3</div><!--]-->
- "
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <div>1</div></div><div>4</div><!--]-->
- "
- `,
- )
- })
- test('v-for with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span/>
- </div>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.splice(0, 1)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('consecutive v-for with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span/>
- </div>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.splice(0, 2)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>c</span><span>d</span><!--]-->
- <!--[--><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('v-for on component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template><div>comp</div></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>comp</div><div>comp</div><div>comp</div><!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>comp</div><div>comp</div><div>comp</div><div>comp</div><!--]-->
- </div>"
- `,
- )
- })
- test('v-for on component with slots', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item">
- <span>{{ item }}</span>
- </components.Child>
- </div>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><span>a</span><!--]-->
- <!--[--><span>b</span><!--]-->
- <!--[--><span>c</span><!--]-->
- <!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><span>a</span><!--]-->
- <!--[--><span>b</span><!--]-->
- <!--[--><span>c</span><!--]-->
- <span>d</span><!--slot--><!--]-->
- </div>"
- `,
- )
- })
- test('on fragment component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template><div>foo</div>-bar-</template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <div>foo</div>-bar-<!--]-->
- </div>"
- `,
- )
- })
- test('on component with non-hydration node', async () => {
- const data = ref({ show: true, msg: 'foo' })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in 2" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template>
- <div>
- <div>
- <div v-if="data.show">{{ data.msg }}</div>
- </div>
- <span>non-hydration node</span>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- data.value.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- })
- test('with non-hydration node', async () => {
- const data = ref({ show: true, msg: 'foo' })
- const { container } = await testHydration(
- `<template>
- <div>
- <div v-for="item in 2">
- <div>
- <div v-if="data.show">{{ data.msg }}</div>
- </div>
- <span>non-hydration node</span>
- </div>
- </div>
- </template>`,
- {},
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- data.value.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- })
- })
- describe('slots', () => {
- test('basic slot', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>bar</span><!--]-->
- "
- `,
- )
- })
- test('named slot', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo>
- <span>{{data}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/><slot name="foo"/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- <!--[--><span>bar</span><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('named slot with v-if', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo v-if="data">
- <span>{{data}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot name="foo"/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "
- <!--[--><span>true</span><!--]-->
- "
- `)
- })
- test('named slot with v-if and v-for', async () => {
- const data = reactive({
- show: true,
- items: ['a', 'b', 'c'],
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo v-if="data.show">
- <span v-for="item in data.items" :key="item">{{item}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot name="foo"/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- "
- `,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--for--><!--]-->
- "
- `,
- )
- })
- test('with insertion anchor', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span/>
- <span>{{data}}</span>
- <span/>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span><span>foo</span><span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span><span>bar</span><span></span><!--]-->
- "
- `,
- )
- })
- test('with multi level anchor insertion', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span/>
- <span>{{data}}</span>
- <span/>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div/>
- <div/>
- <slot/>
- <div/>
- </div>
- </template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div></div><div></div>
- <!--[--><span></span><span>foo</span><span></span><!--]-->
- <div></div><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div></div><div></div>
- <!--[--><span></span><span>bar</span><span></span><!--]-->
- <div></div><!--]-->
- "
- `,
- )
- })
- test('mixed slot and text node', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot/>{{data.msg}}</div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- hi</div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- bar</div>"
- `,
- )
- })
- test('mixed root slot and text node', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template>{{data.text}}<slot/>{{data.msg}}</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo
- <!--[--><span>foo</span><!--]-->
- hi<!--]-->
- "
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo
- <!--[--><span>foo</span><!--]-->
- bar<!--]-->
- "
- `,
- )
- })
- test('mixed consecutive slot and element', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo><span>{{data.text}}</span></template>
- <template #bar><span>bar</span></template>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot name="foo"/><slot name="bar"/><div>{{data.msg}}</div></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>hi</div></div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- test('mixed slot and element', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot/><div>{{data.msg}}</div></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <div>hi</div></div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- test('mixed slot and component', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div>
- <components.Child2/>
- <slot/>
- <components.Child2/>
- </div>
- </template>`,
- Child2: `
- <template>
- <div>{{data.msg2}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div>bar</div>
- <!--[--><span>foo</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- data.msg2 = 'hello'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div>hello</div>
- <!--[--><span>foo</span><!--]-->
- <div>hello</div></div>"
- `,
- )
- })
- test('mixed slot and fragment component', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div>
- <components.Child2/>
- <slot/>
- <components.Child2/>
- </div>
- </template>`,
- Child2: `
- <template>
- <div>{{data.msg1}}</div> {{data.msg2}}
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>foo</div> bar<!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>foo</div> bar<!--]-->
- </div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>hello</div> vapor<!--]-->
- <!--[--><span>hello</span><!--]-->
- <!--[--><div>hello</div> vapor<!--]-->
- </div>"
- `,
- )
- })
- test('mixed slot and v-if', async () => {
- const data = reactive({
- show: true,
- msg: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div v-if="data.show">{{data.msg}}</div>
- <slot/>
- <div v-if="data.show">{{data.msg}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>foo</div><!--if-->
- <!--[--><span>foo</span><!--]-->
- <div>foo</div><!--if--><!--]-->
- "
- `,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--if-->
- <!--[--><span>foo</span><!--]-->
- <!--if--><!--]-->
- "
- `,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "
- <!--[--><div>foo</div><!--if-->
- <!--[--><span>foo</span><!--]-->
- <div>foo</div><!--if--><!--]-->
- "
- `)
- })
- test('mixed slot and v-for', async () => {
- const data = reactive({
- items: ['a', 'b', 'c'],
- msg: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div v-for="item in data.items" :key="item">{{item}}</div>
- <slot/>
- <div v-for="item in data.items" :key="item">{{item}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
- <!--]-->
- "
- `,
- )
- data.items.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('consecutive slots', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/><slot name="bar"/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('consecutive slots with insertion anchor', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template>
- <div>
- <span/>
- <slot/>
- <slot name="bar"/>
- <span/>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <span></span></div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('consecutive slots prepend', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- msg3: 'baz',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo>
- <span>{{data.msg1}}</span>
- </template>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template>
- <div>
- <slot name="foo"/>
- <slot name="bar"/>
- <div>{{data.msg3}}</div>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>baz</div></div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <div>baz</div></div>"
- `,
- )
- })
- test('slot fallback', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot><span>{{data.foo}}</span></slot></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.foo = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>bar</span><!--]-->
- "
- `,
- )
- })
- test('forwarded slot', async () => {
- const data = reactive({
- foo: 'foo',
- bar: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Parent>
- <span>{{data.foo}}</span>
- </components.Parent>
- <div>{{data.bar}}</div>
- </div>
- </template>`,
- {
- Parent: `<template><div><components.Child><slot/></components.Child></div></template>`,
- Child: `<template><div><slot/></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div><div>
- <!--[-->
- <!--[--><span>foo</span><!--]-->
- <!--]-->
- </div></div><div>bar</div></div>"
- `,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div><div>
- <!--[-->
- <!--[--><span>foo1</span><!--]-->
- <!--]-->
- </div></div><div>bar1</div></div>"
- `,
- )
- })
- test('forwarded slot with fallback', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Parent/>
- </template>`,
- {
- Parent: `<template><components.Child><slot/></components.Child></template>`,
- Child: `<template><div><slot>{{data.foo}}</slot></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->foo<!--]-->
- </div>"
- `,
- )
- data.foo = 'foo1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->foo1<!--]-->
- </div>"
- `,
- )
- })
- test('forwarded slot with empty content', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Foo/>
- </template>`,
- {
- Foo: `<template>
- <components.Bar>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Bar>
- </template>`,
- Bar: `<template>
- <components.Baz>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Baz>
- </template>`,
- Baz: `<template>
- <components.Qux>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Qux>
- </template>`,
- Qux: `<template>
- <div>
- <slot name="foo" />
- <div>{{data.foo}}</div>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <div>foo</div></div>"
- `,
- )
- data.foo = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- })
- describe('transition', async () => {
- test('transition appear', async () => {
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div>foo</div>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div style="" class="v-enter-from v-enter-active">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear work with pre-existing class', async () => {
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div class="foo">foo</div>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="foo v-enter-from v-enter-active" style="">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear work with empty content', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <slot v-if="data"></slot>
- <span v-else>foo</span>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--slot--><!--if-->"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span class="v-enter-from v-enter-active">foo</span><!--if-->"`,
- )
- })
- test('transition appear with v-if', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div v-if="data">foo</div>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear with v-show', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div v-show="data">foo</div>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div style="display:none;" class="v-enter-from v-enter-active v-leave-from v-leave-active">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear w/ event listener', async () => {
- const { container } = await testHydration(
- `<script setup>
- import { ref } from 'vue'
- const count = ref(0)
- </script>
- <template>
- <transition appear>
- <button @click="count++">{{ count }}</button>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button style="" class="v-enter-from v-enter-active">0</button>"`,
- )
- triggerEvent('click', container.querySelector('button')!)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button style="" class="v-enter-from v-enter-active">1</button>"`,
- )
- })
- })
- describe('teleport', () => {
- test('basic', async () => {
- const data = ref({
- msg: ref('foo'),
- disabled: ref(false),
- fn: vi.fn(),
- })
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport'
- teleportContainer.innerHTML =
- `<!--teleport start anchor-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<teleport to="#teleport" :disabled="data.disabled">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn"></span>
- </teleport>`,
- data,
- )
- const teleport = block as TeleportFragment
- expect(teleport.anchor).toBe(container.lastChild)
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect((teleport.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[1],
- )
- expect((teleport.nodes as Node[])[1]).toBe(
- teleportContainer.childNodes[2],
- )
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<!--teleport start--><!--teleport end-->"`,
- )
- // event handler
- triggerEvent('click', teleportContainer.querySelector('.foo')!)
- expect(data.value.fn).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(teleportContainer.innerHTML)).toBe(
- `<!--teleport start anchor-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport anchor-->`,
- )
- data.value.disabled = true
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport end-->`,
- )
- expect(formatHtml(teleportContainer.innerHTML)).toMatchInlineSnapshot(
- `"<!--teleport start anchor--><!--teleport anchor-->"`,
- )
- data.value.msg = 'baz'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start-->` +
- `<span>baz</span>` +
- `<span class="baz"></span>` +
- `<!--teleport end-->`,
- )
- data.value.disabled = false
- await nextTick()
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<!--teleport start--><!--teleport end-->"`,
- )
- expect(formatHtml(teleportContainer.innerHTML)).toBe(
- `<!--teleport start anchor-->` +
- `<span>baz</span>` +
- `<span class="baz"></span>` +
- `<!--teleport anchor-->`,
- )
- })
- test('multiple + integration', async () => {
- const data = ref({
- msg: ref('foo'),
- fn1: vi.fn(),
- fn2: vi.fn(),
- })
- const code = `
- <teleport to="#teleport2">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn1"></span>
- </teleport>
- <teleport to="#teleport2">
- <span>{{data.msg}}2</span>
- <span :class="data.msg + 2" @click="data.fn2"></span>
- </teleport>`
- const SSRComp = compileVaporComponent(code, data, undefined, true)
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport2'
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- ctx,
- )
- expect(mainHtml).toBe(
- `<!--[-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--]-->`,
- )
- const teleportHtml = ctx.teleports!['#teleport2']
- expect(teleportHtml).toBe(
- `<!--teleport start anchor-->` +
- `<span>foo</span><span class="foo"></span>` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<span>foo2</span><span class="foo2"></span>` +
- `<!--teleport anchor-->`,
- )
- teleportContainer.innerHTML = teleportHtml
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- mainHtml,
- code,
- data,
- )
- const teleports = block as any as TeleportFragment[]
- const teleport1 = teleports[0]
- const teleport2 = teleports[1]
- expect(teleport1.anchor).toBe(container.childNodes[2])
- expect(teleport2.anchor).toBe(container.childNodes[4])
- expect(teleport1.target).toBe(teleportContainer)
- expect(teleport1.targetStart).toBe(teleportContainer.childNodes[0])
- expect((teleport1.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[1],
- )
- expect(teleport1.targetAnchor).toBe(teleportContainer.childNodes[3])
- expect(teleport2.target).toBe(teleportContainer)
- expect(teleport2.targetStart).toBe(teleportContainer.childNodes[4])
- expect((teleport2.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[5],
- )
- expect(teleport2.targetAnchor).toBe(teleportContainer.childNodes[7])
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--]-->`,
- )
- // event handler
- triggerEvent('click', teleportContainer.querySelector('.foo')!)
- expect(data.value.fn1).toHaveBeenCalled()
- triggerEvent('click', teleportContainer.querySelector('.foo2')!)
- expect(data.value.fn2).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(teleportContainer.innerHTML).toBe(
- `<!--teleport start anchor-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<span>bar2</span>` +
- `<span class="bar2"></span>` +
- `<!--teleport anchor-->`,
- )
- })
- test('disabled', async () => {
- const data = ref({
- msg: ref('foo'),
- fn1: vi.fn(),
- fn2: vi.fn(),
- })
- const code = `
- <div>foo</div>
- <teleport to="#teleport3" disabled="true">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn1"></span>
- </teleport>
- <div :class="data.msg + 2" @click="data.fn2">bar</div>
- `
- const SSRComp = compileVaporComponent(code, data, undefined, true)
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport3'
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- ctx,
- )
- expect(mainHtml).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport end-->` +
- `<div class="foo2">bar</div>` +
- `<!--]-->`,
- )
- const teleportHtml = ctx.teleports!['#teleport3']
- expect(teleportHtml).toMatchInlineSnapshot(
- `"<!--teleport start anchor--><!--teleport anchor-->"`,
- )
- teleportContainer.innerHTML = teleportHtml
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- mainHtml,
- code,
- data,
- )
- const blocks = block as any[]
- expect(blocks[0]).toBe(container.childNodes[1])
- const teleport = blocks[1] as TeleportFragment
- expect((teleport.nodes as Node[])[0]).toBe(container.childNodes[3])
- expect((teleport.nodes as Node[])[1]).toBe(container.childNodes[4])
- expect(teleport.anchor).toBe(container.childNodes[5])
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[1])
- expect(blocks[2]).toBe(container.childNodes[6])
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport end-->` +
- `<div class="foo2">bar</div>` +
- `<!--]-->`,
- )
- // event handler
- triggerEvent('click', container.querySelector('.foo')!)
- expect(data.value.fn1).toHaveBeenCalled()
- triggerEvent('click', container.querySelector('.foo2')!)
- expect(data.value.fn2).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport end-->` +
- `<div class="bar2">bar</div>` +
- `<!--]-->`,
- )
- })
- test('disabled + as component root', async () => {
- const { container } = await mountWithHydration(
- `<!--[-->` +
- `<div>Parent fragment</div>` +
- `<!--teleport start--><div>Teleport content</div><!--teleport end-->` +
- `<!--]-->`,
- `
- <div>Parent fragment</div>
- <teleport to="body" disabled>
- <div>Teleport content</div>
- </teleport>
- `,
- )
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>Parent fragment</div>` +
- `<!--teleport start-->` +
- `<div>Teleport content</div>` +
- `<!--teleport end-->` +
- `<!--]-->`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('as component root', async () => {
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport4'
- teleportContainer.innerHTML = `<!--teleport start anchor-->hello<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<components.Wrapper></components.Wrapper>`,
- undefined,
- {
- Wrapper: compileVaporComponent(
- `<teleport to="#teleport4">hello</teleport>`,
- ),
- },
- )
- const teleport = (block as VaporComponentInstance)
- .block as TeleportFragment
- expect(teleport.anchor).toBe(container.childNodes[1])
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.nodes).toBe(teleportContainer.childNodes[1])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[2])
- })
- test('nested', async () => {
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport5'
- teleportContainer.innerHTML =
- `<!--teleport start anchor-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<div>child</div>` +
- `<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<teleport to="#teleport5">
- <teleport to="#teleport5"><div>child</div></teleport>
- </teleport>`,
- )
- const teleport = block as TeleportFragment
- expect(teleport.anchor).toBe(container.childNodes[1])
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
- const childTeleport = teleport.nodes as TeleportFragment
- expect(childTeleport.anchor).toBe(teleportContainer.childNodes[2])
- expect(childTeleport.targetStart).toBe(teleportContainer.childNodes[4])
- expect(childTeleport.targetAnchor).toBe(teleportContainer.childNodes[6])
- expect(childTeleport.nodes).toBe(teleportContainer.childNodes[5])
- })
- test('unmount (full integration)', async () => {
- const targetId = 'teleport6'
- const data = ref({
- toggle: ref(true),
- })
- const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
- const Comp1 = compileVaporComponent(template1)
- const SSRComp1 = compileVaporComponent(
- template1,
- undefined,
- undefined,
- true,
- )
- const template2 = `<div>Comp2</div>`
- const Comp2 = compileVaporComponent(template2)
- const SSRComp2 = compileVaporComponent(
- template2,
- undefined,
- undefined,
- true,
- )
- const appCode = `
- <div>
- <components.Comp1 v-if="data.toggle"/>
- <components.Comp2 v-else/>
- </div>
- `
- const SSRApp = compileVaporComponent(
- appCode,
- data,
- {
- Comp1: SSRComp1,
- Comp2: SSRComp2,
- },
- true,
- )
- const teleportContainer = document.createElement('div')
- teleportContainer.id = targetId
- document.body.appendChild(teleportContainer)
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- ctx,
- )
- expect(mainHtml).toBe(
- '<div><!--teleport start--><!--teleport end--></div>',
- )
- teleportContainer.innerHTML = ctx.teleports![`#${targetId}`]
- const { container } = await mountWithHydration(mainHtml, appCode, data, {
- Comp1,
- Comp2,
- })
- expect(container.innerHTML).toBe(
- '<div><!--teleport start--><!--teleport end--><!--if--></div>',
- )
- expect(teleportContainer.innerHTML).toBe(
- `<!--teleport start anchor-->` +
- `<span>Teleported Comp1</span>` +
- `<!--teleport anchor-->`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
- expect(teleportContainer.innerHTML).toBe('')
- })
- test('unmount (mismatch + full integration)', async () => {
- const targetId = 'teleport7'
- const data = ref({
- toggle: ref(true),
- })
- const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
- const Comp1 = compileVaporComponent(template1)
- const SSRComp1 = compileVaporComponent(
- template1,
- undefined,
- undefined,
- true,
- )
- const template2 = `<div>Comp2</div>`
- const Comp2 = compileVaporComponent(template2)
- const SSRComp2 = compileVaporComponent(
- template2,
- undefined,
- undefined,
- true,
- )
- const appCode = `
- <div>
- <components.Comp1 v-if="data.toggle"/>
- <components.Comp2 v-else/>
- </div>
- `
- const SSRApp = compileVaporComponent(
- appCode,
- data,
- {
- Comp1: SSRComp1,
- Comp2: SSRComp2,
- },
- true,
- )
- const teleportContainer = document.createElement('div')
- teleportContainer.id = targetId
- document.body.appendChild(teleportContainer)
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- expect(mainHtml).toBe(
- '<div><!--teleport start--><!--teleport end--></div>',
- )
- expect(teleportContainer.innerHTML).toBe('')
- const { container } = await mountWithHydration(mainHtml, appCode, data, {
- Comp1,
- Comp2,
- })
- expect(container.innerHTML).toBe(
- '<div><!--teleport start--><!--teleport end--><!--if--></div>',
- )
- expect(teleportContainer.innerHTML).toBe(`<span>Teleported Comp1</span>`)
- expect(`Hydration children mismatch`).toHaveBeenWarned()
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
- expect(teleportContainer.innerHTML).toBe('')
- })
- test('target change (mismatch + full integration)', async () => {
- const targetId1 = 'teleport8-1'
- const targetId2 = 'teleport8-2'
- const data = ref({
- target: ref(targetId1),
- msg: ref('foo'),
- })
- const template = `<Teleport :to="'#' + data.target"><span>{{data.msg}}</span></Teleport>`
- const Comp = compileVaporComponent(template, data)
- const SSRComp = compileVaporComponent(template, data, undefined, true)
- const teleportContainer1 = document.createElement('div')
- teleportContainer1.id = targetId1
- const teleportContainer2 = document.createElement('div')
- teleportContainer2.id = targetId2
- document.body.appendChild(teleportContainer1)
- document.body.appendChild(teleportContainer2)
- // server render
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- )
- expect(mainHtml).toBe(`<!--teleport start--><!--teleport end-->`)
- expect(teleportContainer1.innerHTML).toBe('')
- expect(teleportContainer2.innerHTML).toBe('')
- // hydrate
- const { container } = await mountWithHydration(mainHtml, template, data, {
- Comp,
- })
- expect(container.innerHTML).toBe(
- `<!--teleport start--><!--teleport end-->`,
- )
- expect(teleportContainer1.innerHTML).toBe(`<span>foo</span>`)
- expect(teleportContainer2.innerHTML).toBe('')
- expect(`Hydration children mismatch`).toHaveBeenWarned()
- data.value.target = targetId2
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start--><!--teleport end-->`,
- )
- expect(teleportContainer1.innerHTML).toBe('')
- expect(teleportContainer2.innerHTML).toBe(`<span>bar</span>`)
- })
- test('with disabled teleport + undefined target', async () => {
- const data = ref({
- msg: ref('foo'),
- })
- const { container } = await mountWithHydration(
- '<!--teleport start--><span>foo</span><!--teleport end-->',
- `<teleport :to="undefined" :disabled="true">
- <span>{{data.msg}}</span>
- </teleport>`,
- data,
- )
- expect(container.innerHTML).toBe(
- `<!--teleport start--><span>foo</span><!--teleport end-->`,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start--><span>bar</span><!--teleport end-->`,
- )
- })
- test('should apply css vars after hydration', async () => {
- const state = reactive({ color: 'red' })
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport-css-vars'
- teleportContainer.innerHTML =
- `<!--teleport start anchor-->` +
- `<span>content</span>` +
- `<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const App = defineVaporComponent({
- setup() {
- useVaporCssVars(() => state)
- return createComponent(
- VaporTeleport,
- { to: () => '#teleport-css-vars' },
- { default: () => template('<span>content</span>', true)() },
- )
- },
- })
- const container = document.createElement('div')
- container.innerHTML = '<!--teleport start--><!--teleport end-->'
- document.body.appendChild(container)
- const app = createVaporSSRApp(App)
- app.mount(container)
- await nextTick()
- // css vars should be applied after hydration
- const span = teleportContainer.querySelector('span') as HTMLElement
- expect(span).toBeTruthy()
- expect(span.style.getPropertyValue('--color')).toBe('red')
- expect(span.hasAttribute('data-v-owner')).toBe(true)
- // css vars should update reactively
- state.color = 'green'
- await nextTick()
- expect(span.style.getPropertyValue('--color')).toBe('green')
- })
- })
- describe('async component', async () => {
- test('async component', async () => {
- const data = ref({
- spy: vi.fn(),
- })
- const compCode = `<button @click="data.spy">hello!</button>`
- const SSRComp = compileVaporComponent(compCode, data, undefined, true)
- let serverResolve: any
- // use defineAsyncComponent in SSR
- let AsyncComp = defineAsyncComponent(
- () =>
- new Promise(r => {
- serverResolve = r
- }),
- )
- const appCode = `hello<components.AsyncComp/>world`
- const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
- // server render
- const htmlPromise = VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- serverResolve(SSRComp)
- const html = await htmlPromise
- expect(html).toMatchInlineSnapshot(
- `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
- )
- // hydration
- let clientResolve: any
- AsyncComp = defineVaporAsyncComponent(
- () =>
- new Promise(r => {
- clientResolve = r
- }),
- ) as any
- const Comp = compileVaporComponent(compCode, data)
- const App = compileVaporComponent(appCode, data, { AsyncComp })
- const container = document.createElement('div')
- container.innerHTML = html
- document.body.appendChild(container)
- createVaporSSRApp(App).mount(container)
- // hydration not complete yet
- triggerEvent('click', container.querySelector('button')!)
- expect(data.value.spy).not.toHaveBeenCalled()
- // resolve
- clientResolve(Comp)
- await new Promise(r => setTimeout(r))
- // should be hydrated now
- triggerEvent('click', container.querySelector('button')!)
- expect(data.value.spy).toHaveBeenCalled()
- })
- // No longer needed, parent component updates in vapor mode no longer
- // cause child components to re-render
- // test.todo('update async wrapper before resolve', async () => {})
- test('update async component after parent mount before async component resolve', async () => {
- const data = ref({
- toggle: true,
- })
- const compCode = `
- <script vapor>
- defineProps(['toggle'])
- </script>
- <template>
- <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
- </template>
- `
- const SSRComp = compileVaporComponent(
- compCode,
- undefined,
- undefined,
- true,
- )
- let serverResolve: any
- // use defineAsyncComponent in SSR
- let AsyncComp = defineAsyncComponent(
- () =>
- new Promise(r => {
- serverResolve = r
- }),
- )
- const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
- const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
- // server render
- const htmlPromise = VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- serverResolve(SSRComp)
- const html = await htmlPromise
- expect(html).toMatchInlineSnapshot(`"<h1>Async component</h1>"`)
- // hydration
- let clientResolve: any
- AsyncComp = defineVaporAsyncComponent(
- () =>
- new Promise(r => {
- clientResolve = r
- }),
- ) as any
- const Comp = compileVaporComponent(compCode)
- const App = compileVaporComponent(appCode, data, { AsyncComp })
- const container = document.createElement('div')
- container.innerHTML = html
- document.body.appendChild(container)
- createVaporSSRApp(App).mount(container)
- // update before resolve
- data.value.toggle = false
- await nextTick()
- // resolve
- clientResolve(Comp)
- await new Promise(r => setTimeout(r))
- // prevent lazy hydration since the component has been patched
- expect('Skipping lazy hydration for component').toHaveBeenWarned()
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<h1>Updated async component</h1><!--async component-->"`,
- )
- })
- test('update async component (fragment root) after parent mount before async component resolve', async () => {
- const data = ref({
- toggle: true,
- })
- const compCode = `
- <script vapor>
- defineProps(['toggle'])
- </script>
- <template>
- <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
- <h2>fragment root</h2>
- </template>
- `
- const SSRComp = compileVaporComponent(
- compCode,
- undefined,
- undefined,
- true,
- )
- let serverResolve: any
- // use defineAsyncComponent in SSR
- let AsyncComp = defineAsyncComponent(
- () =>
- new Promise(r => {
- serverResolve = r
- }),
- )
- const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
- const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
- // server render
- const htmlPromise = VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- serverResolve(SSRComp)
- const html = await htmlPromise
- expect(html).toMatchInlineSnapshot(
- `"<!--[--><h1>Async component</h1><h2>fragment root</h2><!--]-->"`,
- )
- // hydration
- let clientResolve: any
- AsyncComp = defineVaporAsyncComponent(
- () =>
- new Promise(r => {
- clientResolve = r
- }),
- ) as any
- const Comp = compileVaporComponent(compCode)
- const App = compileVaporComponent(appCode, data, { AsyncComp })
- const container = document.createElement('div')
- container.innerHTML = html
- document.body.appendChild(container)
- createVaporSSRApp(App).mount(container)
- // update before resolve
- data.value.toggle = false
- await nextTick()
- // resolve
- clientResolve(Comp)
- await new Promise(r => setTimeout(r))
- // prevent lazy hydration since the component has been patched
- expect('Skipping lazy hydration for component').toHaveBeenWarned()
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<!--[--><h1>Updated async component</h1><h2>fragment root</h2><!--async component--><!--]-->"`,
- )
- })
- // required vapor Suspense
- test.todo('hydrate safely when property used by async setup changed before render', async () => {})
- // required vapor Suspense
- test.todo('hydrate safely when property used by deep nested async setup changed before render', async () => {})
- test('unmount async wrapper before load', async () => {
- const data = ref({
- toggle: true,
- })
- const compCode = `<div>async</div>`
- const appCode = `
- <div>
- <components.AsyncComp v-if="data.toggle"/>
- <div v-else>hi</div>
- </div>
- `
- // hydration
- let clientResolve: any
- const AsyncComp = defineVaporAsyncComponent(
- () =>
- new Promise(r => {
- clientResolve = r
- }),
- )
- const Comp = compileVaporComponent(compCode)
- const App = compileVaporComponent(appCode, data, {
- AsyncComp,
- })
- const container = document.createElement('div')
- container.innerHTML = '<div><div>async</div></div>'
- createVaporSSRApp(App).mount(container)
- // unmount before resolve
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
- // resolve
- clientResolve(Comp)
- await new Promise(r => setTimeout(r))
- // should remain unmounted
- expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
- })
- test('unmount async wrapper before load (fragment)', async () => {
- const data = ref({
- toggle: true,
- })
- const compCode = `<div>async</div><div>fragment</div>`
- const appCode = `
- <div>
- <components.AsyncComp v-if="data.toggle"/>
- <div v-else>hi</div>
- </div>
- `
- // hydration
- let clientResolve: any
- const AsyncComp = defineVaporAsyncComponent(
- () =>
- new Promise(r => {
- clientResolve = r
- }),
- )
- const Comp = compileVaporComponent(compCode)
- const App = compileVaporComponent(appCode, data, {
- AsyncComp,
- })
- const container = document.createElement('div')
- container.innerHTML =
- '<div><!--[--><div>async</div><div>fragment</div><!--]--></div>'
- createVaporSSRApp(App).mount(container)
- // unmount before resolve
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
- // resolve
- clientResolve(Comp)
- await new Promise(r => setTimeout(r))
- // should remain unmounted
- expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
- })
- test('nested async wrapper', async () => {
- const toggleCode = `
- <script vapor>
- import { onMounted, ref, nextTick } from 'vue'
- const show = ref(false)
- onMounted(() => {
- nextTick(() => {
- show.value = true
- })
- })
- </script>
- <template>
- <div v-show="show">
- <slot />
- </div>
- </template>
- `
- const SSRToggle = compileVaporComponent(
- toggleCode,
- undefined,
- undefined,
- true,
- )
- const wrapperCode = `<slot/>`
- const SSRWrapper = compileVaporComponent(
- wrapperCode,
- undefined,
- undefined,
- true,
- )
- const data = ref({
- count: 0,
- fn: vi.fn(),
- })
- const childCode = `
- <script vapor>
- import { onMounted } from 'vue'
- const data = _data; const components = _components;
- onMounted(() => {
- data.value.fn()
- data.value.count++
- })
- </script>
- <template>
- <div>{{data.count}}</div>
- </template>
- `
- const SSRChild = compileVaporComponent(childCode, data, undefined, true)
- const appCode = `
- <components.Toggle>
- <components.Wrapper>
- <components.Wrapper>
- <components.Child/>
- </components.Wrapper>
- </components.Wrapper>
- </components.Toggle>
- `
- const SSRApp = compileVaporComponent(
- appCode,
- undefined,
- {
- Toggle: SSRToggle,
- Wrapper: SSRWrapper,
- Child: SSRChild,
- },
- true,
- )
- const root = document.createElement('div')
- // server render
- root.innerHTML = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div style="display:none;"><!--[--><!--[--><!--[--><div>0</div><!--]--><!--]--><!--]--></div>"`,
- )
- const Toggle = compileVaporComponent(toggleCode)
- const Wrapper = compileVaporComponent(wrapperCode)
- const Child = compileVaporComponent(childCode, data)
- const App = compileVaporComponent(appCode, undefined, {
- Toggle,
- Wrapper,
- Child,
- })
- // hydration
- createVaporSSRApp(App).mount(root)
- await nextTick()
- await nextTick()
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div style=""><!--[--><!--[--><!--[--><div>1</div><!--]--><!--]--><!--]--></div>"`,
- )
- expect(data.value.fn).toBeCalledTimes(1)
- })
- })
- describe.todo('Suspense')
- describe('force hydrate prop', async () => {
- test('force hydrate prop with `.prop` modifier', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox">',
- `<input type="checkbox" .indeterminate="true"/>`,
- )
- expect((container.firstChild! as any).indeterminate).toBe(true)
- })
- test('force hydrate input v-model with non-string value bindings', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox" value="true">',
- `<input type="checkbox" :true-value="true"/>`,
- )
- expect((container.firstChild as any)._trueValue).toBe(true)
- })
- test('force hydrate checkbox with indeterminate', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox" indeterminate/>',
- `<input type="checkbox" :indeterminate="true"/>`,
- )
- expect((container.firstChild! as any).indeterminate).toBe(true)
- })
- test('force hydrate select option with non-string value bindings', async () => {
- const { container } = await mountWithHydration(
- '<select><option value="true">ok</option></select>',
- `<select><option :value="true">ok</option></select>`,
- )
- expect((container.firstChild!.firstChild as any)._value).toBe(true)
- })
- test('force hydrate v-bind with .prop modifiers', async () => {
- const { container } = await mountWithHydration(
- '<div .foo="true"/>',
- `<div v-bind="data"/>`,
- ref({ '.foo': true }),
- )
- expect((container.firstChild! as any).foo).toBe(true)
- })
- test('force hydrate custom element with dynamic props', () => {
- class MyElement extends HTMLElement {
- foo = ''
- constructor() {
- super()
- }
- }
- customElements.define('my-element-7203', MyElement)
- const msg = ref('bar')
- const container = document.createElement('div')
- container.innerHTML = '<my-element-7203></my-element-7203>'
- const app = createVaporSSRApp({
- setup() {
- return createPlainElement('my-element-7203', { foo: () => msg.value })
- },
- })
- app.mount(container)
- expect((container.firstChild as any).foo).toBe(msg.value)
- })
- })
- })
- describe('mismatch handling', () => {
- test('text node', async () => {
- const foo = ref('bar')
- const { container } = await mountWithHydration(`foo`, `{{data}}`, foo)
- expect(container.textContent).toBe('bar')
- expect(`Hydration text mismatch`).toHaveBeenWarned()
- })
- test('element text content', async () => {
- const data = ref({ textContent: 'bar' })
- const { container } = await mountWithHydration(
- `<div>foo</div>`,
- `<div v-bind="data"></div>`,
- data,
- )
- expect(container.innerHTML).toBe('<div>bar</div>')
- expect(`Hydration text content mismatch`).toHaveBeenWarned()
- })
- // test('not enough children', () => {
- // const { container } = mountWithHydration(`<div></div>`, () =>
- // h('div', [h('span', 'foo'), h('span', 'bar')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><span>foo</span><span>bar</span></div>',
- // )
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('too many children', () => {
- // const { container } = mountWithHydration(
- // `<div><span>foo</span><span>bar</span></div>`,
- // () => h('div', [h('span', 'foo')]),
- // )
- // expect(container.innerHTML).toBe('<div><span>foo</span></div>')
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- test('complete mismatch', async () => {
- const data = ref('span')
- const { container } = await mountWithHydration(
- `<div>foo</div>`,
- `<component :is="data">foo</component>`,
- data,
- )
- expect(container.innerHTML).toBe('<span>foo</span><!--dynamic-component-->')
- expect(`Hydration node mismatch`).toHaveBeenWarned()
- })
- // test('fragment mismatch removal', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
- // () => h('div', [h('span', 'replaced')]),
- // )
- // expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('fragment not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
- // )
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('fragment too many children', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
- // )
- // // fragment ends early and attempts to hydrate the extra <div>bar</div>
- // // as 2nd fragment child.
- // expect(`Hydration text content mismatch`).toHaveBeenWarned()
- // // excessive children removal
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('Teleport target has empty children', () => {
- // const teleportContainer = document.createElement('div')
- // teleportContainer.id = 'teleport'
- // document.body.appendChild(teleportContainer)
- // mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
- // h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
- // )
- // expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('comment mismatch (element)', () => {
- // const { container } = mountWithHydration(`<div><span></span></div>`, () =>
- // h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe('<div><!--hi--></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('comment mismatch (text)', () => {
- // const { container } = mountWithHydration(`<div>foobar</div>`, () =>
- // h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe('<div><!--hi--></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- test('class mismatch', async () => {
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref(['foo', 'bar']),
- )
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref({ foo: true, bar: true }),
- )
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('foo bar'),
- )
- // svg classes
- await mountWithHydration(
- `<svg class="foo bar"></svg>`,
- `<svg :class="data"></svg>`,
- ref('foo bar'),
- )
- // class with different order
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('bar foo'),
- )
- expect(`Hydration class mismatch`).not.toHaveBeenWarned()
- // single root mismatch
- const { container: root } = await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('baz'),
- )
- expect(root.innerHTML).toBe('<div class="foo bar baz"></div>')
- expect(`Hydration class mismatch`).toHaveBeenWarned()
- // multiple root mismatch
- const { container } = await mountWithHydration(
- `<div class="foo bar"></div><span/>`,
- `<div :class="data"></div><span/>`,
- ref('foo'),
- )
- expect(container.innerHTML).toBe('<div class="foo"></div><span></span>')
- expect(`Hydration class mismatch`).toHaveBeenWarned()
- })
- test('style mismatch', async () => {
- await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'red' }),
- )
- await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref('color:red;'),
- )
- // style with different order
- await mountWithHydration(
- `<div style="color:red; font-size: 12px;"></div>`,
- `<div :style="data"></div>`,
- ref(`font-size: 12px; color:red;`),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- // single root mismatch
- const { container: root } = await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'green' }),
- )
- expect(root.innerHTML).toBe('<div style="color: green;"></div>')
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- // multiple root mismatch
- const { container } = await mountWithHydration(
- `<div style="color:red;"></div><span/>`,
- `<div :style="data"></div><span/>`,
- ref({ color: 'green' }),
- )
- expect(container.innerHTML).toBe(
- '<div style="color: green;"></div><span></span>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- })
- test('style mismatch when no style attribute is present', async () => {
- await mountWithHydration(
- `<div></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'red' }),
- )
- expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
- })
- test('style mismatch w/ v-show', async () => {
- await mountWithHydration(
- `<div style="color:red;display:none"></div>`,
- `<div v-show="data" style="color: red;"></div>`,
- ref(false),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- // mismatch with single root
- const { container: root } = await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div v-show="data" style="color: red;"></div>`,
- ref(false),
- )
- expect(root.innerHTML).toBe(
- '<div style="color: red; display: none;"></div>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- // mismatch with multiple root
- const { container } = await mountWithHydration(
- `<div style="color:red;"></div><span/>`,
- `<div v-show="data.show" :style="data.style"></div><span/>`,
- ref({ show: false, style: 'color: red' }),
- )
- expect(container.innerHTML).toBe(
- '<div style="color: red; display: none;"></div><span></span>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- })
- test('attr mismatch', async () => {
- await mountWithHydration(
- `<div id="foo"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<div spellcheck></div>`,
- `<div :spellcheck="data"></div>`,
- ref(''),
- )
- await mountWithHydration(
- `<div></div>`,
- `<div :id="data"></div>`,
- ref(undefined),
- )
- // boolean
- await mountWithHydration(
- `<select multiple></div>`,
- `<select :multiple="data"></select>`,
- ref(true),
- )
- await mountWithHydration(
- `<select multiple></div>`,
- `<select :multiple="data"></select>`,
- ref('multiple'),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(
- `<div></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
- await mountWithHydration(
- `<div id="bar"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
- })
- test('attr special case: textarea value', async () => {
- await mountWithHydration(
- `<textarea>foo</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<textarea></textarea>`,
- `<textarea :value="data"></textarea>`,
- ref(''),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(
- `<textarea>foo</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('bar'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarned()
- })
- test('<textarea> with newlines at the beginning', async () => {
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('\nhello'),
- )
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea v-text="data"></textarea>`,
- ref('\nhello'),
- )
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea v-bind="data"></textarea>`,
- ref({ textContent: '\nhello' }),
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- test('<pre> with newlines at the beginning', async () => {
- await mountWithHydration(`<pre>\n</pre>`, `<pre>{{data}}</pre>`, ref('\n'))
- await mountWithHydration(
- `<pre>\n</pre>`,
- `<pre v-text="data"></pre>`,
- ref('\n'),
- )
- await mountWithHydration(
- `<pre>\n</pre>`,
- `<pre v-bind="data"></pre>`,
- ref({ textContent: '\n' }),
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- test('boolean attr handling', async () => {
- await mountWithHydration(
- `<input />`,
- `<input :readonly="data" />`,
- ref(false),
- )
- await mountWithHydration(
- `<input readonly />`,
- `<input :readonly="data" />`,
- ref(true),
- )
- await mountWithHydration(
- `<input readonly="readonly" />`,
- `<input :readonly="data" />`,
- ref(true),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('client value is null or undefined', async () => {
- await mountWithHydration(
- `<div></div>`,
- `<div :draggable="data"></div>`,
- ref(undefined),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(`<input />`, `<input :type="data" />`, ref(null))
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn against object values', async () => {
- await mountWithHydration(`<input />`, `<input :from="data" />`, ref({}))
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn on falsy bindings of non-property keys', async () => {
- await mountWithHydration(
- `<button></button>`,
- `<button :href="data"></button>`,
- ref(undefined),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn on non-renderable option values', async () => {
- await mountWithHydration(
- `<select><option>hello</option></select>`,
- `<select><option :value="data">hello</option></select>`,
- ref(['foo']),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn css v-bind', async () => {
- const container = document.createElement('div')
- container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
- const app = createVaporSSRApp({
- setup() {
- useVaporCssVars(() => ({ foo: 'red' }))
- const n0 = template('<div></div>', true)() as any
- renderEffect(() => setStyle(n0, { color: 'var(--foo)' }))
- return n0
- },
- })
- app.mount(container)
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test('css vars should only be added to expected on component root dom', () => {
- const container = document.createElement('div')
- container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
- const app = createVaporSSRApp({
- setup() {
- useVaporCssVars(() => ({ foo: 'red' }))
- const n0 = template('<div><div></div></div>', true)() as any
- const n1 = child(n0) as any
- renderEffect(() => setStyle(n1, { color: 'var(--foo)' }))
- return n0
- },
- })
- app.mount(container)
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test('css vars support fallthrough', () => {
- const container = document.createElement('div')
- container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
- const app = createVaporSSRApp({
- setup() {
- useVaporCssVars(() => ({ foo: 'red' }))
- return createComponent(Child)
- },
- })
- const Child = defineVaporComponent({
- setup() {
- const n0 = template('<div></div>', true)() as any
- renderEffect(() => setStyle(n0, { padding: '4px' }))
- return n0
- },
- })
- app.mount(container)
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- // vapor directive does not have a created hook
- test('should not warn for directives that mutate DOM in created', () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div class="test red"></div>`
- // const vColor: ObjectDirective = {
- // created(el, binding) {
- // el.classList.add(binding.value)
- // },
- // }
- // const app = createSSRApp({
- // setup() {
- // return () =>
- // withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
- // },
- // })
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test('escape css var name', () => {
- const container = document.createElement('div')
- container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
- const app = createVaporSSRApp({
- setup() {
- useVaporCssVars(() => ({ 'foo.bar': 'red' }))
- return createComponent(Child)
- },
- })
- const Child = defineVaporComponent({
- setup() {
- const n0 = template('<div></div>', true)() as any
- renderEffect(() => setStyle(n0, { padding: '4px' }))
- return n0
- },
- })
- app.mount(container)
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- })
- describe('data-allow-mismatch', () => {
- test('element text content', async () => {
- const data = ref({ textContent: 'bar' })
- const { container } = await mountWithHydration(
- `<div data-allow-mismatch="text">foo</div>`,
- `<div v-bind="data"></div>`,
- data,
- )
- expect(container.innerHTML).toBe(
- '<div data-allow-mismatch="text">bar</div>',
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- // test('not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"></div>`,
- // () => h('div', [h('span', 'foo'), h('span', 'bar')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
- // )
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- // test('too many children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
- // () => h('div', [h('span', 'foo')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>foo</span></div>',
- // )
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- test('complete mismatch', async () => {
- const { container } = await mountWithHydration(
- `<div data-allow-mismatch="children"><div>foo</div></div>`,
- `<div><component :is="data">foo</component></div>`,
- ref('span'),
- )
- expect(container.innerHTML).toBe(
- '<div data-allow-mismatch="children"><span>foo</span><!--dynamic-component--></div>',
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- })
- // test('fragment mismatch removal', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
- // () => h('div', [h('span', 'replaced')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>replaced</span></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('fragment not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('fragment too many children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
- // )
- // // fragment ends early and attempts to hydrate the extra <div>bar</div>
- // // as 2nd fragment child.
- // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- // // excessive children removal
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- // test('comment mismatch (element)', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><span></span></div>`,
- // () => h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--hi--></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('comment mismatch (text)', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children">foobar</div>`,
- // () => h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--hi--></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- test('class mismatch', async () => {
- await mountWithHydration(
- `<div class="foo bar" data-allow-mismatch="class"></div>`,
- `<div :class="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration class mismatch`).not.toHaveBeenWarned()
- })
- test('style mismatch', async () => {
- await mountWithHydration(
- `<div style="color:red;" data-allow-mismatch="style"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'green' }),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test('attr mismatch', async () => {
- await mountWithHydration(
- `<div data-allow-mismatch="attribute"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<div id="bar" data-allow-mismatch="attribute"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- })
- describe('VDOM interop', () => {
- test('basic render vapor component', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>{{ data }}</template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
- })
- test('nested components (VDOM -> Vapor -> VDOM)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template><components.VdomChild/></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template>{{ data }}</template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
- })
- test('nested components (VDOM -> Vapor(multi-root) -> VDOM)', async () => {
- const data = ref('foo')
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- // Vapor component with multiple root nodes, VDOM child as first element
- // This ensures hydration starts at <!--[--> and tests skipFragmentAnchor
- VaporChild: {
- code: `<template><components.VdomChild/><div>second</div></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><span>{{ data }}</span></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><div>second</div><!--]-->
- "
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>bar</span><div>second</div><!--]-->
- "
- `,
- )
- })
- test('nested components (VDOM -> Vapor -> VDOM (with slot fallback))', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template><components.VdomChild/></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><slot><span>{{data}}</span></slot></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false</span><!--]-->
- "
- `,
- )
- })
- test('nested components (VDOM -> Vapor(with slot content) -> VDOM)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>
- <components.VdomChild>
- <template #default>
- <span>{{data}} vapor fallback</span>
- </template>
- </components.VdomChild>
- </template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><slot><span>vdom fallback</span></slot></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true vapor fallback</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false vapor fallback</span><!--]-->
- "
- `,
- )
- })
- test('nested components (VDOM -> Vapor(with slot content) -> Vapor)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>
- <components.VaporChild2>
- <template #default>
- <span>{{data}} vapor fallback</span>
- </template>
- </components.VaporChild2>
- </template>`,
- vapor: true,
- },
- VaporChild2: {
- code: `<template><slot><span>vapor fallback2</span></slot></template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true vapor fallback</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false vapor fallback</span><!--]-->
- "
- `,
- )
- })
- test('vapor slot render vdom component', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild>
- <components.VdomChild/>
- </components.VaporChild>
- </template>`,
- {
- VaporChild: {
- code: `<template><div><slot/></div></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template>{{ data }}</template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->true<!--]-->
- </div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->false<!--]-->
- </div>"
- `,
- )
- })
- test('vapor slot render vdom component (multi-root slot content)', async () => {
- const data = ref('foo')
- const { container } = await testWithVaporApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild>
- <components.VdomChild/>
- <div>vapor content</div>
- </components.VaporChild>
- </template>`,
- {
- VaporChild: {
- code: `<template><div><slot/></div></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><span>{{ data }}</span></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><div>vapor content</div><!--]-->
- </div>"
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>bar</span><div>vapor content</div><!--]-->
- </div>"
- `,
- )
- })
- test('vapor slot render vdom component (render function)', async () => {
- const data = ref(true)
- const { container } = await testWithVaporApp(
- `<script setup>
- import { h } from 'vue'
- const data = _data; const components = _components;
- const VdomChild = {
- setup() {
- return () => h('div', null, [h('div', [String(data.value)])])
- }
- }
- </script>
- <template>
- <components.VaporChild>
- <VdomChild/>
- </components.VaporChild>
- </template>`,
- {
- VaporChild: {
- code: `<template><div><slot/></div></template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div>true</div></div><!--]-->
- </div>"
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div>false</div></div><!--]-->
- </div>"
- `,
- )
- })
- test('hydrate VNode rendered via createDynamicComponent', async () => {
- const data = ref('foo')
- const { container } = await testWithVaporApp(
- `<script setup>
- import { h } from 'vue'
- const data = _data; const components = _components;
- // Simulating RouterView pattern: VDOM component passes VNode through slot
- const RouterView = {
- setup(_, { slots }) {
- return () => {
- const component = h(components.VaporChild)
- return slots.default({ Component: component })
- }
- }
- }
- </script>
- <template>
- <RouterView v-slot="{ Component }">
- <component :is="Component" />
- </RouterView>
- </template>`,
- {
- VaporChild: {
- code: `<template><div>{{ data }}</div></template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>foo</div><!--dynamic-component--><!--]-->
- "
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>bar</div><!--dynamic-component--><!--]-->
- "
- `,
- )
- })
- test('hydrate VDOM slot content', async () => {
- const data = ref('foo')
- const { container } = await testWithVaporApp(
- `<script setup>
- const data = _data; const components = _components;
- </script>
- <template>
- <components.VdomWrapper>
- <div>{{ data }}</div>
- </components.VdomWrapper>
- </template>`,
- {
- VdomWrapper: {
- code: `<script setup>const data = _data;</script>
- <template><slot /></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>foo</div><!--]-->
- "
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>bar</div><!--]-->
- "
- `,
- )
- })
- test('hydrate VDOM slot fallback', async () => {
- const data = ref('foo')
- const { container } = await testWithVaporApp(
- `<script setup>
- const data = _data; const components = _components;
- </script>
- <template>
- <components.VdomWrapper />
- </template>`,
- {
- VdomWrapper: {
- code: `<script setup>const data = _data;</script>
- <template><slot><div>{{ data }}</div></slot></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>foo</div><!--]-->
- "
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>bar</div><!--]-->
- "
- `,
- )
- })
- test('hydrate VDOM component returning Fragment', async () => {
- const data = ref('foo')
- const { container } = await testWithVaporApp(
- `<script setup>
- const data = _data; const components = _components;
- </script>
- <template>
- <components.VdomFragmentComp />
- </template>`,
- {
- // VDOM component that returns a Fragment (multiple root nodes)
- VdomFragmentComp: {
- code: `<script setup>const data = _data;</script>
- <template><div>first {{ data }}</div><div>second {{ data }}</div></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>first foo</div><div>second foo</div><!--]-->
- "
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>first bar</div><div>second bar</div><!--]-->
- "
- `,
- )
- })
- })
|