hydration.spec.ts 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219
  1. import {
  2. child,
  3. createComponent,
  4. createPlainElement,
  5. createVaporSSRApp,
  6. defineVaporAsyncComponent,
  7. defineVaporComponent,
  8. delegateEvents,
  9. renderEffect,
  10. setStyle,
  11. template,
  12. useVaporCssVars,
  13. } from '../src'
  14. import { defineAsyncComponent, nextTick, reactive, ref } from '@vue/runtime-dom'
  15. import { isString } from '@vue/shared'
  16. import type { VaporComponentInstance } from '../src/component'
  17. import type { TeleportFragment } from '../src/components/Teleport'
  18. import { VueServerRenderer, compile, runtimeDom, runtimeVapor } from './_utils'
  19. const formatHtml = (raw: string) => {
  20. return raw
  21. .replace(/<!--\[/g, '\n<!--[')
  22. .replace(/]-->/g, ']-->\n')
  23. .replace(/\n{2,}/g, '\n')
  24. }
  25. async function testWithVaporApp(
  26. code: string,
  27. components?: Record<string, string | { code: string; vapor: boolean }>,
  28. data?: any,
  29. ) {
  30. return testHydration(code, components, data, {
  31. isVaporApp: true,
  32. interop: true,
  33. })
  34. }
  35. async function testWithVDOMApp(
  36. code: string,
  37. components?: Record<string, string | { code: string; vapor: boolean }>,
  38. data?: any,
  39. ) {
  40. return testHydration(code, components, data, {
  41. isVaporApp: false,
  42. interop: true,
  43. })
  44. }
  45. function compileVaporComponent(
  46. code: string,
  47. data: runtimeDom.Ref<any> = ref({}),
  48. components?: Record<string, any>,
  49. ssr = false,
  50. ) {
  51. if (!code.includes(`<script`)) {
  52. code = `<template>${code}</template>`
  53. }
  54. return compile(code, data, components, {
  55. vapor: true,
  56. ssr,
  57. })
  58. }
  59. async function mountWithHydration(
  60. html: string,
  61. code: string,
  62. data: runtimeDom.Ref<any> = ref({}),
  63. components?: Record<string, any>,
  64. ) {
  65. const container = document.createElement('div')
  66. container.innerHTML = html
  67. document.body.appendChild(container)
  68. const clientComp = compileVaporComponent(code, data, components)
  69. const app = createVaporSSRApp(clientComp)
  70. app.mount(container)
  71. return {
  72. block: (app._instance! as VaporComponentInstance).block,
  73. container,
  74. }
  75. }
  76. async function testHydration(
  77. code: string,
  78. components: Record<string, string | { code: string; vapor: boolean }> = {},
  79. data: any = ref('foo'),
  80. { isVaporApp = true, interop = false } = {},
  81. ) {
  82. const ssrComponents: any = {}
  83. const clientComponents: any = {}
  84. for (const key in components) {
  85. const comp = components[key]
  86. const code = isString(comp) ? comp : comp.code
  87. const isVaporComp = isString(comp) || !!comp.vapor
  88. clientComponents[key] = compile(code, data, clientComponents, {
  89. vapor: isVaporComp,
  90. ssr: false,
  91. })
  92. ssrComponents[key] = compile(code, data, ssrComponents, {
  93. vapor: isVaporComp,
  94. ssr: true,
  95. })
  96. }
  97. const serverComp = compile(code, data, ssrComponents, {
  98. vapor: isVaporApp,
  99. ssr: true,
  100. })
  101. const html = await VueServerRenderer.renderToString(
  102. runtimeDom.createSSRApp(serverComp),
  103. )
  104. const container = document.createElement('div')
  105. document.body.appendChild(container)
  106. container.innerHTML = html
  107. const clientComp = compile(code, data, clientComponents, {
  108. vapor: isVaporApp,
  109. ssr: false,
  110. })
  111. let app
  112. if (isVaporApp) {
  113. app = createVaporSSRApp(clientComp)
  114. } else {
  115. app = runtimeDom.createSSRApp(clientComp)
  116. }
  117. if (interop) {
  118. app.use(runtimeVapor.vaporInteropPlugin)
  119. }
  120. app.mount(container)
  121. return { data, container }
  122. }
  123. const triggerEvent = (type: string, el: Element) => {
  124. const event = new Event(type, { bubbles: true })
  125. el.dispatchEvent(event)
  126. }
  127. delegateEvents('click')
  128. beforeEach(() => {
  129. document.body.innerHTML = ''
  130. })
  131. describe('Vapor Mode hydration', () => {
  132. describe('text', () => {
  133. test('root text', async () => {
  134. const { data, container } = await testHydration(`
  135. <template>{{ data }}</template>
  136. `)
  137. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foo"`)
  138. data.value = 'bar'
  139. await nextTick()
  140. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"bar"`)
  141. })
  142. test('consecutive text nodes', async () => {
  143. const { data, container } = await testHydration(`
  144. <template>{{ data }}{{ data }}</template>
  145. `)
  146. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foofoo"`)
  147. data.value = 'bar'
  148. await nextTick()
  149. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"barbar"`)
  150. })
  151. test('consecutive text nodes with insertion anchor', async () => {
  152. const { data, container } = await testHydration(`
  153. <template><span/>{{ data }}{{ data }}<span/></template>
  154. `)
  155. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  156. `
  157. "
  158. <!--[--><span></span>foofoo<span></span><!--]-->
  159. "
  160. `,
  161. )
  162. data.value = 'bar'
  163. await nextTick()
  164. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  165. `
  166. "
  167. <!--[--><span></span>barbar<span></span><!--]-->
  168. "
  169. `,
  170. )
  171. })
  172. test('mixed text nodes', async () => {
  173. const { data, container } = await testHydration(`
  174. <template>{{ data }}A{{ data }}B{{ data }}</template>
  175. `)
  176. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  177. `"fooAfooBfoo"`,
  178. )
  179. data.value = 'bar'
  180. await nextTick()
  181. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  182. `"barAbarBbar"`,
  183. )
  184. })
  185. test('mixed text nodes with insertion anchor', async () => {
  186. const { data, container } = await testHydration(`
  187. <template><span/>{{ data }}A{{ data }}B{{ data }}<span/></template>
  188. `)
  189. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  190. `
  191. "
  192. <!--[--><span></span>fooAfooBfoo<span></span><!--]-->
  193. "
  194. `,
  195. )
  196. data.value = 'bar'
  197. await nextTick()
  198. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  199. `
  200. "
  201. <!--[--><span></span>barAbarBbar<span></span><!--]-->
  202. "
  203. `,
  204. )
  205. })
  206. test('empty text node', async () => {
  207. const data = reactive({ txt: '' })
  208. const { container } = await testHydration(
  209. `<template><div>{{ data.txt }}</div></template>`,
  210. undefined,
  211. data,
  212. )
  213. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  214. `"<div></div>"`,
  215. )
  216. data.txt = 'foo'
  217. await nextTick()
  218. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  219. `"<div>foo</div>"`,
  220. )
  221. })
  222. test('empty text node in slot', async () => {
  223. const data = reactive({ txt: '' })
  224. const { container } = await testHydration(
  225. `<template><components.Child>{{data.txt}}</components.Child></template>`,
  226. {
  227. Child: `<template><slot/></template>`,
  228. },
  229. data,
  230. )
  231. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  232. `
  233. "
  234. <!--[--><!--]-->
  235. "
  236. `,
  237. )
  238. data.txt = 'foo'
  239. await nextTick()
  240. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  241. `
  242. "
  243. <!--[-->foo<!--]-->
  244. "
  245. `,
  246. )
  247. })
  248. })
  249. describe('element', () => {
  250. test('root comment', async () => {
  251. const { container } = await testHydration(`
  252. <template><!----></template>
  253. `)
  254. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"<!---->"`)
  255. expect(`mismatch in <div>`).not.toHaveBeenWarned()
  256. })
  257. test('root with mixed element and text', async () => {
  258. const { container, data } = await testHydration(`
  259. <template> A<span>{{ data }}</span>{{ data }}</template>
  260. `)
  261. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  262. `
  263. "
  264. <!--[--> A<span>foo</span>foo<!--]-->
  265. "
  266. `,
  267. )
  268. data.value = 'bar'
  269. await nextTick()
  270. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  271. `
  272. "
  273. <!--[--> A<span>bar</span>bar<!--]-->
  274. "
  275. `,
  276. )
  277. })
  278. test('empty element', async () => {
  279. const { container } = await testHydration(`
  280. <template><div/></template>
  281. `)
  282. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  283. `"<div></div>"`,
  284. )
  285. expect(`mismatch in <div>`).not.toHaveBeenWarned()
  286. })
  287. test('element with binding and text children', async () => {
  288. const { container, data } = await testHydration(`
  289. <template><div :class="data">{{ data }}</div></template>
  290. `)
  291. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  292. `"<div class="foo">foo</div>"`,
  293. )
  294. data.value = 'bar'
  295. await nextTick()
  296. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  297. `"<div class="bar">bar</div>"`,
  298. )
  299. })
  300. test('element with elements children', async () => {
  301. const { container } = await testHydration(`
  302. <template>
  303. <div>
  304. <span>{{ data }}</span>
  305. <span :class="data" @click="data = 'bar'"/>
  306. </div>
  307. </template>
  308. `)
  309. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  310. `"<div><span>foo</span><span class="foo"></span></div>"`,
  311. )
  312. // event handler
  313. triggerEvent('click', container.querySelector('.foo')!)
  314. await nextTick()
  315. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  316. `"<div><span>bar</span><span class="bar"></span></div>"`,
  317. )
  318. })
  319. test('element with ref', async () => {
  320. const { data, container } = await testHydration(
  321. `<template>
  322. <div ref="data">hi</div>
  323. </template>
  324. `,
  325. {},
  326. ref(null),
  327. )
  328. expect(data.value).toBe(container.firstChild)
  329. })
  330. })
  331. describe('component', () => {
  332. test('basic component', async () => {
  333. const { container, data } = await testHydration(
  334. `
  335. <template><div><span></span><components.Child/></div></template>
  336. `,
  337. { Child: `<template>{{ data }}</template>` },
  338. )
  339. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  340. `"<div><span></span>foo</div>"`,
  341. )
  342. data.value = 'bar'
  343. await nextTick()
  344. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  345. `"<div><span></span>bar</div>"`,
  346. )
  347. })
  348. test('fragment component', async () => {
  349. const { container, data } = await testHydration(
  350. `
  351. <template><div><span></span><components.Child/></div></template>
  352. `,
  353. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  354. )
  355. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  356. `
  357. "<div><span></span>
  358. <!--[--><div>foo</div>-foo-<!--]-->
  359. </div>"
  360. `,
  361. )
  362. data.value = 'bar'
  363. await nextTick()
  364. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  365. `
  366. "<div><span></span>
  367. <!--[--><div>bar</div>-bar-<!--]-->
  368. </div>"
  369. `,
  370. )
  371. })
  372. test('fragment component with prepend', async () => {
  373. const { container, data } = await testHydration(
  374. `
  375. <template><div><components.Child/><span></span></div></template>
  376. `,
  377. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  378. )
  379. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  380. `
  381. "<div>
  382. <!--[--><div>foo</div>-foo-<!--]-->
  383. <span></span></div>"
  384. `,
  385. )
  386. data.value = 'bar'
  387. await nextTick()
  388. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  389. `
  390. "<div>
  391. <!--[--><div>bar</div>-bar-<!--]-->
  392. <span></span></div>"
  393. `,
  394. )
  395. })
  396. test('nested fragment components', async () => {
  397. const { container, data } = await testHydration(
  398. `
  399. <template><div><components.Parent/><span></span></div></template>
  400. `,
  401. {
  402. Parent: `<template><div/><components.Child/><div/></template>`,
  403. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  404. },
  405. )
  406. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  407. `
  408. "<div>
  409. <!--[--><div></div>
  410. <!--[--><div>foo</div>-foo-<!--]-->
  411. <div></div><!--]-->
  412. <span></span></div>"
  413. `,
  414. )
  415. data.value = 'bar'
  416. await nextTick()
  417. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  418. `
  419. "<div>
  420. <!--[--><div></div>
  421. <!--[--><div>bar</div>-bar-<!--]-->
  422. <div></div><!--]-->
  423. <span></span></div>"
  424. `,
  425. )
  426. })
  427. test('component with insertion anchor', async () => {
  428. const { container, data } = await testHydration(
  429. `<template>
  430. <div>
  431. <span/>
  432. <components.Child/>
  433. <span/>
  434. </div>
  435. </template>
  436. `,
  437. {
  438. Child: `<template>{{ data }}</template>`,
  439. },
  440. )
  441. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  442. `"<div><span></span>foo<span></span></div>"`,
  443. )
  444. data.value = 'bar'
  445. await nextTick()
  446. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  447. `"<div><span></span>bar<span></span></div>"`,
  448. )
  449. })
  450. test('nested components with insertion anchor', async () => {
  451. const { container, data } = await testHydration(
  452. `
  453. <template><components.Parent/></template>
  454. `,
  455. {
  456. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  457. Child: `<template><div>{{ data }}</div></template>`,
  458. },
  459. )
  460. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  461. `"<div><span></span><div>foo</div><span></span></div>"`,
  462. )
  463. data.value = 'bar'
  464. await nextTick()
  465. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  466. `"<div><span></span><div>bar</div><span></span></div>"`,
  467. )
  468. })
  469. test('nested components with multi level anchor insertion', async () => {
  470. const { container, data } = await testHydration(
  471. `
  472. <template><div><span></span><components.Parent/><span></span></div></template>
  473. `,
  474. {
  475. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  476. Child: `<template><div>{{ data }}</div></template>`,
  477. },
  478. )
  479. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  480. `"<div><span></span><div><span></span><div>foo</div><span></span></div><span></span></div>"`,
  481. )
  482. data.value = 'bar'
  483. await nextTick()
  484. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  485. `"<div><span></span><div><span></span><div>bar</div><span></span></div><span></span></div>"`,
  486. )
  487. })
  488. test('consecutive components with insertion parent', async () => {
  489. const data = reactive({ foo: 'foo', bar: 'bar' })
  490. const { container } = await testHydration(
  491. `<template>
  492. <div>
  493. <components.Child1/>
  494. <components.Child2/>
  495. </div>
  496. </template>
  497. `,
  498. {
  499. Child1: `<template><span>{{ data.foo }}</span></template>`,
  500. Child2: `<template><span>{{ data.bar }}</span></template>`,
  501. },
  502. data,
  503. )
  504. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  505. `"<div><span>foo</span><span>bar</span></div>"`,
  506. )
  507. data.foo = 'foo1'
  508. data.bar = 'bar1'
  509. await nextTick()
  510. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  511. `"<div><span>foo1</span><span>bar1</span></div>"`,
  512. )
  513. })
  514. test('nested consecutive components with insertion anchor', async () => {
  515. const { container, data } = await testHydration(
  516. `
  517. <template><components.Parent/></template>
  518. `,
  519. {
  520. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  521. Child: `<template><div>{{ data }}</div></template>`,
  522. },
  523. )
  524. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  525. `"<div><span></span><div>foo</div><div>foo</div><span></span></div>"`,
  526. )
  527. data.value = 'bar'
  528. await nextTick()
  529. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  530. `"<div><span></span><div>bar</div><div>bar</div><span></span></div>"`,
  531. )
  532. })
  533. test('nested consecutive components with multi level anchor insertion', async () => {
  534. const { container, data } = await testHydration(
  535. `
  536. <template><div><span></span><components.Parent/><span></span></div></template>
  537. `,
  538. {
  539. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  540. Child: `<template><div>{{ data }}</div></template>`,
  541. },
  542. )
  543. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  544. `"<div><span></span><div><span></span><div>foo</div><div>foo</div><span></span></div><span></span></div>"`,
  545. )
  546. data.value = 'bar'
  547. await nextTick()
  548. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  549. `"<div><span></span><div><span></span><div>bar</div><div>bar</div><span></span></div><span></span></div>"`,
  550. )
  551. })
  552. test('mixed component and element with insertion anchor', async () => {
  553. const { container, data } = await testHydration(
  554. `<template>
  555. <div>
  556. <span/>
  557. <components.Child/>
  558. <span/>
  559. <components.Child/>
  560. <span/>
  561. </div>
  562. </template>
  563. `,
  564. {
  565. Child: `<template>{{ data }}</template>`,
  566. },
  567. )
  568. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  569. `"<div><span></span>foo<span></span>foo<span></span></div>"`,
  570. )
  571. data.value = 'bar'
  572. await nextTick()
  573. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  574. `"<div><span></span>bar<span></span>bar<span></span></div>"`,
  575. )
  576. })
  577. test('fragment component with insertion anchor', async () => {
  578. const { container, data } = await testHydration(
  579. `<template>
  580. <div>
  581. <span/>
  582. <components.Child/>
  583. <span/>
  584. </div>
  585. </template>
  586. `,
  587. {
  588. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  589. },
  590. )
  591. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  592. `
  593. "<div><span></span>
  594. <!--[--><div>foo</div>-foo<!--]-->
  595. <span></span></div>"
  596. `,
  597. )
  598. data.value = 'bar'
  599. await nextTick()
  600. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  601. `
  602. "<div><span></span>
  603. <!--[--><div>bar</div>-bar<!--]-->
  604. <span></span></div>"
  605. `,
  606. )
  607. })
  608. test('nested fragment component with insertion anchor', async () => {
  609. const { container, data } = await testHydration(
  610. `
  611. <template><components.Parent/></template>
  612. `,
  613. {
  614. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  615. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  616. },
  617. )
  618. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  619. `
  620. "<div><span></span>
  621. <!--[--><div>foo</div>-foo-<!--]-->
  622. <span></span></div>"
  623. `,
  624. )
  625. data.value = 'bar'
  626. await nextTick()
  627. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  628. `
  629. "<div><span></span>
  630. <!--[--><div>bar</div>-bar-<!--]-->
  631. <span></span></div>"
  632. `,
  633. )
  634. })
  635. test('nested fragment component with multi level anchor insertion', async () => {
  636. const { container, data } = await testHydration(
  637. `
  638. <template><div><span/><components.Parent/><span/></div></template>
  639. `,
  640. {
  641. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  642. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  643. },
  644. )
  645. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  646. `
  647. "<div><span></span><div><span></span>
  648. <!--[--><div>foo</div>-foo-<!--]-->
  649. <span></span></div><span></span></div>"
  650. `,
  651. )
  652. data.value = 'bar'
  653. await nextTick()
  654. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  655. `
  656. "<div><span></span><div><span></span>
  657. <!--[--><div>bar</div>-bar-<!--]-->
  658. <span></span></div><span></span></div>"
  659. `,
  660. )
  661. })
  662. test('consecutive fragment components with insertion anchor', async () => {
  663. const { container, data } = await testHydration(
  664. `<template>
  665. <div>
  666. <span/>
  667. <components.Child/>
  668. <components.Child/>
  669. <span/>
  670. </div>
  671. </template>
  672. `,
  673. {
  674. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  675. },
  676. )
  677. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  678. `
  679. "<div><span></span>
  680. <!--[--><div>foo</div>-foo<!--]-->
  681. <!--[--><div>foo</div>-foo<!--]-->
  682. <span></span></div>"
  683. `,
  684. )
  685. data.value = 'bar'
  686. await nextTick()
  687. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  688. `
  689. "<div><span></span>
  690. <!--[--><div>bar</div>-bar<!--]-->
  691. <!--[--><div>bar</div>-bar<!--]-->
  692. <span></span></div>"
  693. `,
  694. )
  695. })
  696. test('nested consecutive fragment components with insertion anchor', async () => {
  697. const { container, data } = await testHydration(
  698. `
  699. <template><components.Parent/></template>
  700. `,
  701. {
  702. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  703. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  704. },
  705. )
  706. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  707. `
  708. "<div><span></span>
  709. <!--[--><div>foo</div>-foo-<!--]-->
  710. <!--[--><div>foo</div>-foo-<!--]-->
  711. <span></span></div>"
  712. `,
  713. )
  714. data.value = 'bar'
  715. await nextTick()
  716. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  717. `
  718. "<div><span></span>
  719. <!--[--><div>bar</div>-bar-<!--]-->
  720. <!--[--><div>bar</div>-bar-<!--]-->
  721. <span></span></div>"
  722. `,
  723. )
  724. })
  725. test('nested consecutive fragment components with multi level anchor insertion', async () => {
  726. const { container, data } = await testHydration(
  727. `
  728. <template><div><span></span><components.Parent/><span></span></div></template>
  729. `,
  730. {
  731. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  732. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  733. },
  734. )
  735. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  736. `
  737. "<div><span></span><div><span></span>
  738. <!--[--><div>foo</div>-foo-<!--]-->
  739. <!--[--><div>foo</div>-foo-<!--]-->
  740. <span></span></div><span></span></div>"
  741. `,
  742. )
  743. data.value = 'bar'
  744. await nextTick()
  745. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  746. `
  747. "<div><span></span><div><span></span>
  748. <!--[--><div>bar</div>-bar-<!--]-->
  749. <!--[--><div>bar</div>-bar-<!--]-->
  750. <span></span></div><span></span></div>"
  751. `,
  752. )
  753. })
  754. test('nested consecutive fragment components with root level anchor insertion', async () => {
  755. const { container, data } = await testHydration(
  756. `
  757. <template><div><span></span><components.Parent/><span></span></div></template>
  758. `,
  759. {
  760. Parent: `<template><components.Child/><components.Child/></template>`,
  761. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  762. },
  763. )
  764. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  765. `
  766. "<div><span></span>
  767. <!--[-->
  768. <!--[--><div>foo</div>-foo-<!--]-->
  769. <!--[--><div>foo</div>-foo-<!--]-->
  770. <!--]-->
  771. <span></span></div>"
  772. `,
  773. )
  774. data.value = 'bar'
  775. await nextTick()
  776. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  777. `
  778. "<div><span></span>
  779. <!--[-->
  780. <!--[--><div>bar</div>-bar-<!--]-->
  781. <!--[--><div>bar</div>-bar-<!--]-->
  782. <!--]-->
  783. <span></span></div>"
  784. `,
  785. )
  786. })
  787. test('mixed fragment component and element with insertion anchor', async () => {
  788. const { container, data } = await testHydration(
  789. `<template>
  790. <div>
  791. <span/>
  792. <components.Child/>
  793. <span/>
  794. <components.Child/>
  795. <span/>
  796. </div>
  797. </template>
  798. `,
  799. {
  800. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  801. },
  802. )
  803. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  804. `
  805. "<div><span></span>
  806. <!--[--><div>foo</div>-foo<!--]-->
  807. <span></span>
  808. <!--[--><div>foo</div>-foo<!--]-->
  809. <span></span></div>"
  810. `,
  811. )
  812. data.value = 'bar'
  813. await nextTick()
  814. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  815. `
  816. "<div><span></span>
  817. <!--[--><div>bar</div>-bar<!--]-->
  818. <span></span>
  819. <!--[--><div>bar</div>-bar<!--]-->
  820. <span></span></div>"
  821. `,
  822. )
  823. })
  824. test('mixed fragment component and text with insertion anchor', async () => {
  825. const { container, data } = await testHydration(
  826. `<template>
  827. <div>
  828. <span/>
  829. <components.Child/>
  830. {{ data }}
  831. <components.Child/>
  832. <span/>
  833. </div>
  834. </template>
  835. `,
  836. {
  837. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  838. },
  839. )
  840. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  841. `
  842. "<div><span></span>
  843. <!--[--><div>foo</div>-foo<!--]-->
  844. foo
  845. <!--[--><div>foo</div>-foo<!--]-->
  846. <span></span></div>"
  847. `,
  848. )
  849. data.value = 'bar'
  850. await nextTick()
  851. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  852. `
  853. "<div><span></span>
  854. <!--[--><div>bar</div>-bar<!--]-->
  855. bar
  856. <!--[--><div>bar</div>-bar<!--]-->
  857. <span></span></div>"
  858. `,
  859. )
  860. })
  861. })
  862. describe('dynamic component', () => {
  863. test('basic dynamic component', async () => {
  864. const { container, data } = await testHydration(
  865. `<template>
  866. <component :is="components[data]"/>
  867. </template>`,
  868. {
  869. foo: `<template><div>foo</div></template>`,
  870. bar: `<template><div>bar</div></template>`,
  871. },
  872. ref('foo'),
  873. )
  874. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  875. `"<div>foo</div><!--dynamic-component-->"`,
  876. )
  877. data.value = 'bar'
  878. await nextTick()
  879. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  880. `"<div>bar</div><!--dynamic-component-->"`,
  881. )
  882. })
  883. test('dynamic component with insertion anchor', async () => {
  884. const { container, data } = await testHydration(
  885. `<template>
  886. <div>
  887. <span/>
  888. <component :is="components[data]"/>
  889. <span/>
  890. </div>
  891. </template>`,
  892. {
  893. foo: `<template><div>foo</div></template>`,
  894. bar: `<template><div>bar</div></template>`,
  895. },
  896. ref('foo'),
  897. )
  898. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  899. `"<div><span></span><div>foo</div><!--dynamic-component--><span></span></div>"`,
  900. )
  901. data.value = 'bar'
  902. await nextTick()
  903. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  904. `"<div><span></span><div>bar</div><!--dynamic-component--><span></span></div>"`,
  905. )
  906. })
  907. test('consecutive dynamic components with insertion anchor', async () => {
  908. const { container, data } = await testHydration(
  909. `<template>
  910. <div>
  911. <span/>
  912. <component :is="components[data]"/>
  913. <component :is="components[data]"/>
  914. <span/>
  915. </div>
  916. </template>`,
  917. {
  918. foo: `<template><div>foo</div></template>`,
  919. bar: `<template><div>bar</div></template>`,
  920. },
  921. ref('foo'),
  922. )
  923. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  924. `"<div><span></span><div>foo</div><!--dynamic-component--><div>foo</div><!--dynamic-component--><span></span></div>"`,
  925. )
  926. data.value = 'bar'
  927. await nextTick()
  928. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  929. `"<div><span></span><div>bar</div><!--dynamic-component--><div>bar</div><!--dynamic-component--><span></span></div>"`,
  930. )
  931. })
  932. test('dynamic component fallback', async () => {
  933. const { container, data } = await testHydration(
  934. `<template>
  935. <component :is="'button'">
  936. <span>{{ data }}</span>
  937. </component>
  938. </template>`,
  939. {},
  940. ref('foo'),
  941. )
  942. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  943. `"<button><span>foo</span></button><!--dynamic-component-->"`,
  944. )
  945. data.value = 'bar'
  946. await nextTick()
  947. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  948. `"<button><span>bar</span></button><!--dynamic-component-->"`,
  949. )
  950. })
  951. test('in ssr slot vnode fallback', async () => {
  952. const { container, data } = await testHydration(
  953. `<template>
  954. <components.Child>
  955. <span>{{ data }}</span>
  956. </components.Child>
  957. </template>`,
  958. {
  959. Child: `
  960. <template>
  961. <component :is="'div'">
  962. <slot />
  963. </component>
  964. </template>`,
  965. },
  966. ref('foo'),
  967. )
  968. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  969. `
  970. "<div>
  971. <!--[--><span>foo</span><!--]-->
  972. </div><!--dynamic-component-->"
  973. `,
  974. )
  975. data.value = 'bar'
  976. await nextTick()
  977. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  978. `
  979. "<div>
  980. <!--[--><span>bar</span><!--]-->
  981. </div><!--dynamic-component-->"
  982. `,
  983. )
  984. })
  985. test('dynamic component fallback with dynamic slots', async () => {
  986. const data = ref({
  987. name: 'default',
  988. msg: 'foo',
  989. })
  990. const { container } = await testHydration(
  991. `<template>
  992. <component :is="'div'">
  993. <template v-slot:[data.name]>
  994. <span>{{ data.msg }}</span>
  995. </template>
  996. </component>
  997. </template>`,
  998. {},
  999. data,
  1000. )
  1001. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1002. `"<div><span>foo</span><!----></div><!--dynamic-component-->"`,
  1003. )
  1004. data.value.msg = 'bar'
  1005. await nextTick()
  1006. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1007. `"<div><span>bar</span><!----></div><!--dynamic-component-->"`,
  1008. )
  1009. })
  1010. })
  1011. describe('if', () => {
  1012. test('basic toggle - true -> false', async () => {
  1013. const data = ref(true)
  1014. const { container } = await testHydration(
  1015. `<template>
  1016. <div v-if="data">foo</div>
  1017. </template>`,
  1018. undefined,
  1019. data,
  1020. )
  1021. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1022. `"<div>foo</div><!--if-->"`,
  1023. )
  1024. data.value = false
  1025. await nextTick()
  1026. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1027. `"<!--if-->"`,
  1028. )
  1029. })
  1030. test('basic toggle - false -> true', async () => {
  1031. const data = ref(false)
  1032. const { container } = await testHydration(
  1033. `<template>
  1034. <div v-if="data">foo</div>
  1035. </template>`,
  1036. undefined,
  1037. data,
  1038. )
  1039. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1040. `"<!--if-->"`,
  1041. )
  1042. data.value = true
  1043. await nextTick()
  1044. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1045. `"<div>foo</div><!--if-->"`,
  1046. )
  1047. })
  1048. test('v-if on insertion parent', async () => {
  1049. const data = ref(true)
  1050. const { container } = await testHydration(
  1051. `<template>
  1052. <div v-if="data">
  1053. <components.Child/>
  1054. </div>
  1055. </template>`,
  1056. { Child: `<template>foo</template>` },
  1057. data,
  1058. )
  1059. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1060. `"<div>foo</div><!--if-->"`,
  1061. )
  1062. data.value = false
  1063. await nextTick()
  1064. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1065. `"<!--if-->"`,
  1066. )
  1067. data.value = true
  1068. await nextTick()
  1069. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1070. `"<div>foo</div><!--if-->"`,
  1071. )
  1072. })
  1073. test('v-if/else-if/else chain - switch branches', async () => {
  1074. const data = ref('a')
  1075. const { container } = await testHydration(
  1076. `<template>
  1077. <div v-if="data === 'a'">foo</div>
  1078. <div v-else-if="data === 'b'">bar</div>
  1079. <div v-else>baz</div>
  1080. </template>`,
  1081. undefined,
  1082. data,
  1083. )
  1084. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1085. `"<div>foo</div><!--if-->"`,
  1086. )
  1087. data.value = 'b'
  1088. await nextTick()
  1089. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1090. `"<div>bar</div><!--if--><!--if-->"`,
  1091. )
  1092. data.value = 'c'
  1093. await nextTick()
  1094. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1095. `"<div>baz</div><!--if--><!--if-->"`,
  1096. )
  1097. data.value = 'a'
  1098. await nextTick()
  1099. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1100. `"<div>foo</div><!--if-->"`,
  1101. )
  1102. })
  1103. test('nested if', async () => {
  1104. const data = reactive({ outer: true, inner: true })
  1105. const { container } = await testHydration(
  1106. `<template>
  1107. <div v-if="data.outer">
  1108. <span>outer</span>
  1109. <div v-if="data.inner">inner</div>
  1110. </div>
  1111. </template>`,
  1112. undefined,
  1113. data,
  1114. )
  1115. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1116. `"<div><span>outer</span><div>inner</div><!--if--></div><!--if-->"`,
  1117. )
  1118. data.inner = false
  1119. await nextTick()
  1120. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1121. `"<div><span>outer</span><!--if--></div><!--if-->"`,
  1122. )
  1123. data.outer = false
  1124. await nextTick()
  1125. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1126. `"<!--if-->"`,
  1127. )
  1128. })
  1129. test('on component', async () => {
  1130. const data = ref(true)
  1131. const { container } = await testHydration(
  1132. `<template>
  1133. <components.Child v-if="data"/>
  1134. </template>`,
  1135. { Child: `<template>foo</template>` },
  1136. data,
  1137. )
  1138. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1139. `"foo<!--if-->"`,
  1140. )
  1141. data.value = false
  1142. await nextTick()
  1143. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1144. `"<!--if-->"`,
  1145. )
  1146. })
  1147. test('consecutive if node', async () => {
  1148. const data = ref(true)
  1149. const { container } = await testHydration(
  1150. `<template>
  1151. <components.Child v-if="data"/>
  1152. </template>`,
  1153. { Child: `<template><div v-if="data">foo</div></template>` },
  1154. data,
  1155. )
  1156. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1157. `"<div>foo</div><!--if--><!--if-->"`,
  1158. )
  1159. data.value = false
  1160. await nextTick()
  1161. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1162. `"<!--if-->"`,
  1163. )
  1164. data.value = true
  1165. await nextTick()
  1166. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1167. `"<div>foo</div><!--if--><!--if-->"`,
  1168. )
  1169. })
  1170. test('mixed prepend and insertion anchor', async () => {
  1171. const data = reactive({
  1172. show: true,
  1173. foo: 'foo',
  1174. bar: 'bar',
  1175. qux: 'qux',
  1176. })
  1177. const { container } = await testHydration(
  1178. `<template>
  1179. <components.Child/>
  1180. </template>`,
  1181. {
  1182. Child: `<template>
  1183. <span v-if="data.show">
  1184. <span v-if="data.show">{{data.foo}}</span>
  1185. <span v-if="data.show">{{data.bar}}</span>
  1186. <span>baz</span>
  1187. <span v-if="data.show">{{data.qux}}</span>
  1188. <span>quux</span>
  1189. </span>
  1190. </template>`,
  1191. },
  1192. data,
  1193. )
  1194. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1195. `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux</span><!--if--><span>quux</span></span><!--if-->"`,
  1196. )
  1197. data.qux = 'qux1'
  1198. await nextTick()
  1199. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1200. `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
  1201. )
  1202. data.foo = 'foo1'
  1203. await nextTick()
  1204. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1205. `"<span><span>foo1</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
  1206. )
  1207. })
  1208. test('v-if/else-if/else chain on component - switch branches', async () => {
  1209. const data = ref('a')
  1210. const { container } = await testHydration(
  1211. `<template>
  1212. <components.Child1 v-if="data === 'a'"/>
  1213. <components.Child2 v-else-if="data === 'b'"/>
  1214. <components.Child3 v-else/>
  1215. </template>`,
  1216. {
  1217. Child1: `<template><span>{{data}} child1</span></template>`,
  1218. Child2: `<template><span>{{data}} child2</span></template>`,
  1219. Child3: `<template><span>{{data}} child3</span></template>`,
  1220. },
  1221. data,
  1222. )
  1223. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1224. `"<span>a child1</span><!--if-->"`,
  1225. )
  1226. data.value = 'b'
  1227. await nextTick()
  1228. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1229. `"<span>b child2</span><!--if--><!--if-->"`,
  1230. )
  1231. data.value = 'c'
  1232. await nextTick()
  1233. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1234. `"<span>c child3</span><!--if--><!--if-->"`,
  1235. )
  1236. data.value = 'a'
  1237. await nextTick()
  1238. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1239. `"<span>a child1</span><!--if-->"`,
  1240. )
  1241. })
  1242. test('on component with insertion anchor', async () => {
  1243. const data = ref(true)
  1244. const { container } = await testHydration(
  1245. `<template>
  1246. <div>
  1247. <span/>
  1248. <components.Child v-if="data"/>
  1249. <span/>
  1250. </div>
  1251. </template>`,
  1252. { Child: `<template>foo</template>` },
  1253. data,
  1254. )
  1255. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1256. `"<div><span></span>foo<!--if--><span></span></div>"`,
  1257. )
  1258. data.value = false
  1259. await nextTick()
  1260. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1261. `"<div><span></span><!--if--><span></span></div>"`,
  1262. )
  1263. })
  1264. test('consecutive component with insertion parent', async () => {
  1265. const data = reactive({
  1266. show: true,
  1267. foo: 'foo',
  1268. bar: 'bar',
  1269. })
  1270. const { container } = await testHydration(
  1271. `<template>
  1272. <div v-if="data.show">
  1273. <components.Child/>
  1274. <components.Child2/>
  1275. </div>
  1276. </template>`,
  1277. {
  1278. Child: `<template><span>{{data.foo}}</span></template>`,
  1279. Child2: `<template><span>{{data.bar}}</span></template>`,
  1280. },
  1281. data,
  1282. )
  1283. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1284. `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
  1285. )
  1286. data.show = false
  1287. await nextTick()
  1288. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1289. `"<!--if-->"`,
  1290. )
  1291. data.show = true
  1292. await nextTick()
  1293. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1294. `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
  1295. )
  1296. data.foo = 'foo1'
  1297. data.bar = 'bar1'
  1298. await nextTick()
  1299. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1300. `"<div><span>foo1</span><span>bar1</span></div><!--if-->"`,
  1301. )
  1302. })
  1303. test('on fragment component', async () => {
  1304. const data = ref(true)
  1305. const { container } = await testHydration(
  1306. `<template>
  1307. <div>
  1308. <components.Child v-if="data"/>
  1309. </div>
  1310. </template>`,
  1311. {
  1312. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1313. },
  1314. data,
  1315. )
  1316. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1317. `
  1318. "<div>
  1319. <!--[--><div>true</div>-true-<!--]-->
  1320. <!--if--></div>"
  1321. `,
  1322. )
  1323. data.value = false
  1324. await nextTick()
  1325. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1326. `
  1327. "<div>
  1328. <!--[--><!--]-->
  1329. <!--if--></div>"
  1330. `,
  1331. )
  1332. data.value = true
  1333. await nextTick()
  1334. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1335. `
  1336. "<div>
  1337. <!--[--><!--]-->
  1338. <div>true</div>-true-<!--if--></div>"
  1339. `,
  1340. )
  1341. })
  1342. test('on fragment component with insertion anchor', async () => {
  1343. const data = ref(true)
  1344. const { container } = await testHydration(
  1345. `<template>
  1346. <div>
  1347. <span/>
  1348. <components.Child v-if="data"/>
  1349. <span/>
  1350. </div>
  1351. </template>`,
  1352. {
  1353. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1354. },
  1355. data,
  1356. )
  1357. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1358. `
  1359. "<div><span></span>
  1360. <!--[--><div>true</div>-true-<!--]-->
  1361. <!--if--><span></span></div>"
  1362. `,
  1363. )
  1364. data.value = false
  1365. await nextTick()
  1366. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1367. `
  1368. "<div><span></span>
  1369. <!--[--><!--]-->
  1370. <!--if--><span></span></div>"
  1371. `,
  1372. )
  1373. data.value = true
  1374. await nextTick()
  1375. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1376. "<div><span></span>
  1377. <!--[--><!--]-->
  1378. <div>true</div>-true-<!--if--><span></span></div>"
  1379. `)
  1380. })
  1381. test('consecutive v-if on fragment component with insertion anchor', async () => {
  1382. const data = ref(true)
  1383. const { container } = await testHydration(
  1384. `<template>
  1385. <div>
  1386. <span/>
  1387. <components.Child v-if="data"/>
  1388. <components.Child v-if="data"/>
  1389. <span/>
  1390. </div>
  1391. </template>`,
  1392. {
  1393. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1394. },
  1395. data,
  1396. )
  1397. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1398. `
  1399. "<div><span></span>
  1400. <!--[--><div>true</div>-true-<!--]-->
  1401. <!--if-->
  1402. <!--[--><div>true</div>-true-<!--]-->
  1403. <!--if--><span></span></div>"
  1404. `,
  1405. )
  1406. data.value = false
  1407. await nextTick()
  1408. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1409. `
  1410. "<div><span></span>
  1411. <!--[--><!--]-->
  1412. <!--if-->
  1413. <!--[--><!--]-->
  1414. <!--if--><span></span></div>"
  1415. `,
  1416. )
  1417. data.value = true
  1418. await nextTick()
  1419. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1420. "<div><span></span>
  1421. <!--[--><!--]-->
  1422. <div>true</div>-true-<!--if-->
  1423. <!--[--><!--]-->
  1424. <div>true</div>-true-<!--if--><span></span></div>"
  1425. `)
  1426. })
  1427. test('on dynamic component with insertion anchor', async () => {
  1428. const data = ref(true)
  1429. const { container } = await testHydration(
  1430. `<template>
  1431. <div>
  1432. <span/>
  1433. <component :is="components.Child" v-if="data"/>
  1434. <span/>
  1435. </div>
  1436. </template>`,
  1437. { Child: `<template>foo</template>` },
  1438. data,
  1439. )
  1440. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1441. `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
  1442. )
  1443. data.value = false
  1444. await nextTick()
  1445. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1446. `"<div><span></span><!--if--><span></span></div>"`,
  1447. )
  1448. data.value = true
  1449. await nextTick()
  1450. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1451. `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
  1452. )
  1453. })
  1454. })
  1455. describe('for', () => {
  1456. test('basic v-for', async () => {
  1457. const { container, data } = await testHydration(
  1458. `<template>
  1459. <span v-for="item in data" :key="item">{{ item }}</span>
  1460. </template>`,
  1461. undefined,
  1462. ref(['a', 'b', 'c']),
  1463. )
  1464. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1465. `
  1466. "
  1467. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1468. "
  1469. `,
  1470. )
  1471. data.value.push('d')
  1472. await nextTick()
  1473. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1474. `
  1475. "
  1476. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1477. "
  1478. `,
  1479. )
  1480. })
  1481. test('empty v-for', async () => {
  1482. const { container, data } = await testHydration(
  1483. `<template>
  1484. <span v-for="item in data" :key="item">{{ item }}</span>
  1485. </template>`,
  1486. undefined,
  1487. ref([]),
  1488. )
  1489. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1490. `
  1491. "
  1492. <!--[--><!--]-->
  1493. "
  1494. `,
  1495. )
  1496. data.value.push('a')
  1497. await nextTick()
  1498. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1499. `
  1500. "
  1501. <!--[--><span>a</span><!--]-->
  1502. "
  1503. `,
  1504. )
  1505. })
  1506. test('v-for with insertion parent + sibling component', async () => {
  1507. const { container, data } = await testHydration(
  1508. `<template>
  1509. <div>
  1510. <span v-for="item in data" :key="item">{{ item }}</span>
  1511. </div>
  1512. <components.Child/>
  1513. </template>`,
  1514. {
  1515. Child: `<template><div>{{data.length}}</div></template>`,
  1516. },
  1517. ref(['a', 'b', 'c']),
  1518. )
  1519. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1520. `
  1521. "
  1522. <!--[--><div>
  1523. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1524. </div><div>3</div><!--]-->
  1525. "
  1526. `,
  1527. )
  1528. data.value.push('d')
  1529. await nextTick()
  1530. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1531. `
  1532. "
  1533. <!--[--><div>
  1534. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1535. </div><div>4</div><!--]-->
  1536. "
  1537. `,
  1538. )
  1539. })
  1540. test('v-for with insertion anchor', async () => {
  1541. const { container, data } = await testHydration(
  1542. `<template>
  1543. <div>
  1544. <span/>
  1545. <span v-for="item in data" :key="item">{{ item }}</span>
  1546. <span/>
  1547. </div>
  1548. </template>`,
  1549. undefined,
  1550. ref(['a', 'b', 'c']),
  1551. )
  1552. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1553. `
  1554. "<div><span></span>
  1555. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1556. <span></span></div>"
  1557. `,
  1558. )
  1559. data.value.push('d')
  1560. await nextTick()
  1561. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1562. `
  1563. "<div><span></span>
  1564. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1565. <span></span></div>"
  1566. `,
  1567. )
  1568. data.value.splice(0, 1)
  1569. await nextTick()
  1570. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1571. `
  1572. "<div><span></span>
  1573. <!--[--><span>b</span><span>c</span><span>d</span><!--]-->
  1574. <span></span></div>"
  1575. `,
  1576. )
  1577. })
  1578. test('consecutive v-for with insertion anchor', async () => {
  1579. const { container, data } = await testHydration(
  1580. `<template>
  1581. <div>
  1582. <span/>
  1583. <span v-for="item in data" :key="item">{{ item }}</span>
  1584. <span v-for="item in data" :key="item">{{ item }}</span>
  1585. <span/>
  1586. </div>
  1587. </template>`,
  1588. undefined,
  1589. ref(['a', 'b', 'c']),
  1590. )
  1591. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1592. `
  1593. "<div><span></span>
  1594. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1595. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1596. <span></span></div>"
  1597. `,
  1598. )
  1599. data.value.push('d')
  1600. await nextTick()
  1601. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1602. `
  1603. "<div><span></span>
  1604. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1605. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1606. <span></span></div>"
  1607. `,
  1608. )
  1609. data.value.splice(0, 2)
  1610. await nextTick()
  1611. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1612. `
  1613. "<div><span></span>
  1614. <!--[--><span>c</span><span>d</span><!--]-->
  1615. <!--[--><span>c</span><span>d</span><!--]-->
  1616. <span></span></div>"
  1617. `,
  1618. )
  1619. })
  1620. test('v-for on component', async () => {
  1621. const { container, data } = await testHydration(
  1622. `<template>
  1623. <div>
  1624. <components.Child v-for="item in data" :key="item"/>
  1625. </div>
  1626. </template>`,
  1627. {
  1628. Child: `<template><div>comp</div></template>`,
  1629. },
  1630. ref(['a', 'b', 'c']),
  1631. )
  1632. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1633. `
  1634. "<div>
  1635. <!--[--><div>comp</div><div>comp</div><div>comp</div><!--]-->
  1636. </div>"
  1637. `,
  1638. )
  1639. data.value.push('d')
  1640. await nextTick()
  1641. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1642. `
  1643. "<div>
  1644. <!--[--><div>comp</div><div>comp</div><div>comp</div><div>comp</div><!--]-->
  1645. </div>"
  1646. `,
  1647. )
  1648. })
  1649. test('v-for on component with slots', async () => {
  1650. const { container, data } = await testHydration(
  1651. `<template>
  1652. <div>
  1653. <components.Child v-for="item in data" :key="item">
  1654. <span>{{ item }}</span>
  1655. </components.Child>
  1656. </div>
  1657. </template>`,
  1658. {
  1659. Child: `<template><slot/></template>`,
  1660. },
  1661. ref(['a', 'b', 'c']),
  1662. )
  1663. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1664. `
  1665. "<div>
  1666. <!--[-->
  1667. <!--[--><span>a</span><!--]-->
  1668. <!--[--><span>b</span><!--]-->
  1669. <!--[--><span>c</span><!--]-->
  1670. <!--]-->
  1671. </div>"
  1672. `,
  1673. )
  1674. data.value.push('d')
  1675. await nextTick()
  1676. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1677. `
  1678. "<div>
  1679. <!--[-->
  1680. <!--[--><span>a</span><!--]-->
  1681. <!--[--><span>b</span><!--]-->
  1682. <!--[--><span>c</span><!--]-->
  1683. <span>d</span><!--slot--><!--]-->
  1684. </div>"
  1685. `,
  1686. )
  1687. })
  1688. test('on fragment component', async () => {
  1689. const { container, data } = await testHydration(
  1690. `<template>
  1691. <div>
  1692. <components.Child v-for="item in data" :key="item"/>
  1693. </div>
  1694. </template>`,
  1695. {
  1696. Child: `<template><div>foo</div>-bar-</template>`,
  1697. },
  1698. ref(['a', 'b', 'c']),
  1699. )
  1700. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1701. `
  1702. "<div>
  1703. <!--[-->
  1704. <!--[--><div>foo</div>-bar-<!--]-->
  1705. <!--[--><div>foo</div>-bar-<!--]-->
  1706. <!--[--><div>foo</div>-bar-<!--]-->
  1707. <!--]-->
  1708. </div>"
  1709. `,
  1710. )
  1711. data.value.push('d')
  1712. await nextTick()
  1713. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1714. `
  1715. "<div>
  1716. <!--[-->
  1717. <!--[--><div>foo</div>-bar-<!--]-->
  1718. <!--[--><div>foo</div>-bar-<!--]-->
  1719. <!--[--><div>foo</div>-bar-<!--]-->
  1720. <div>foo</div>-bar-<!--]-->
  1721. </div>"
  1722. `,
  1723. )
  1724. })
  1725. test('on component with non-hydration node', async () => {
  1726. const data = ref({ show: true, msg: 'foo' })
  1727. const { container } = await testHydration(
  1728. `<template>
  1729. <div>
  1730. <components.Child v-for="item in 2" :key="item"/>
  1731. </div>
  1732. </template>`,
  1733. {
  1734. Child: `<template>
  1735. <div>
  1736. <div>
  1737. <div v-if="data.show">{{ data.msg }}</div>
  1738. </div>
  1739. <span>non-hydration node</span>
  1740. </div>
  1741. </template>`,
  1742. },
  1743. data,
  1744. )
  1745. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1746. `
  1747. "<div>
  1748. <!--[--><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><!--]-->
  1749. </div>"
  1750. `,
  1751. )
  1752. data.value.msg = 'bar'
  1753. await nextTick()
  1754. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1755. `
  1756. "<div>
  1757. <!--[--><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><!--]-->
  1758. </div>"
  1759. `,
  1760. )
  1761. data.value.show = false
  1762. await nextTick()
  1763. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1764. "<div>
  1765. <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
  1766. </div>"
  1767. `)
  1768. data.value.show = true
  1769. await nextTick()
  1770. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1771. "<div>
  1772. <!--[--><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><!--]-->
  1773. </div>"
  1774. `)
  1775. })
  1776. test('with non-hydration node', async () => {
  1777. const data = ref({ show: true, msg: 'foo' })
  1778. const { container } = await testHydration(
  1779. `<template>
  1780. <div>
  1781. <div v-for="item in 2">
  1782. <div>
  1783. <div v-if="data.show">{{ data.msg }}</div>
  1784. </div>
  1785. <span>non-hydration node</span>
  1786. </div>
  1787. </div>
  1788. </template>`,
  1789. {},
  1790. data,
  1791. )
  1792. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1793. `
  1794. "<div>
  1795. <!--[--><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><!--]-->
  1796. </div>"
  1797. `,
  1798. )
  1799. data.value.msg = 'bar'
  1800. await nextTick()
  1801. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1802. `
  1803. "<div>
  1804. <!--[--><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><!--]-->
  1805. </div>"
  1806. `,
  1807. )
  1808. data.value.show = false
  1809. await nextTick()
  1810. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1811. "<div>
  1812. <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
  1813. </div>"
  1814. `)
  1815. data.value.show = true
  1816. await nextTick()
  1817. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1818. "<div>
  1819. <!--[--><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><!--]-->
  1820. </div>"
  1821. `)
  1822. })
  1823. })
  1824. describe('slots', () => {
  1825. test('basic slot', async () => {
  1826. const { data, container } = await testHydration(
  1827. `<template>
  1828. <components.Child>
  1829. <span>{{data}}</span>
  1830. </components.Child>
  1831. </template>`,
  1832. {
  1833. Child: `<template><slot/></template>`,
  1834. },
  1835. )
  1836. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1837. `
  1838. "
  1839. <!--[--><span>foo</span><!--]-->
  1840. "
  1841. `,
  1842. )
  1843. data.value = 'bar'
  1844. await nextTick()
  1845. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1846. `
  1847. "
  1848. <!--[--><span>bar</span><!--]-->
  1849. "
  1850. `,
  1851. )
  1852. })
  1853. test('named slot', async () => {
  1854. const { data, container } = await testHydration(
  1855. `<template>
  1856. <components.Child>
  1857. <template #foo>
  1858. <span>{{data}}</span>
  1859. </template>
  1860. </components.Child>
  1861. </template>`,
  1862. {
  1863. Child: `<template><slot/><slot name="foo"/></template>`,
  1864. },
  1865. )
  1866. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1867. `
  1868. "
  1869. <!--[-->
  1870. <!--[--><!--]-->
  1871. <!--[--><span>foo</span><!--]-->
  1872. <!--]-->
  1873. "
  1874. `,
  1875. )
  1876. data.value = 'bar'
  1877. await nextTick()
  1878. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1879. `
  1880. "
  1881. <!--[-->
  1882. <!--[--><!--]-->
  1883. <!--[--><span>bar</span><!--]-->
  1884. <!--]-->
  1885. "
  1886. `,
  1887. )
  1888. })
  1889. test('named slot with v-if', async () => {
  1890. const { data, container } = await testHydration(
  1891. `<template>
  1892. <components.Child>
  1893. <template #foo v-if="data">
  1894. <span>{{data}}</span>
  1895. </template>
  1896. </components.Child>
  1897. </template>`,
  1898. {
  1899. Child: `<template><slot name="foo"/></template>`,
  1900. },
  1901. )
  1902. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1903. `
  1904. "
  1905. <!--[--><span>foo</span><!--]-->
  1906. "
  1907. `,
  1908. )
  1909. data.value = false
  1910. await nextTick()
  1911. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1912. `
  1913. "
  1914. <!--[--><!--]-->
  1915. "
  1916. `,
  1917. )
  1918. data.value = true
  1919. await nextTick()
  1920. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1921. "
  1922. <!--[--><span>true</span><!--]-->
  1923. "
  1924. `)
  1925. })
  1926. test('named slot with v-if and v-for', async () => {
  1927. const data = reactive({
  1928. show: true,
  1929. items: ['a', 'b', 'c'],
  1930. })
  1931. const { container } = await testHydration(
  1932. `<template>
  1933. <components.Child>
  1934. <template #foo v-if="data.show">
  1935. <span v-for="item in data.items" :key="item">{{item}}</span>
  1936. </template>
  1937. </components.Child>
  1938. </template>`,
  1939. {
  1940. Child: `<template><slot name="foo"/></template>`,
  1941. },
  1942. data,
  1943. )
  1944. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1945. `
  1946. "
  1947. <!--[-->
  1948. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1949. <!--]-->
  1950. "
  1951. `,
  1952. )
  1953. data.show = false
  1954. await nextTick()
  1955. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1956. `
  1957. "
  1958. <!--[-->
  1959. <!--[--><!--]-->
  1960. "
  1961. `,
  1962. )
  1963. data.show = true
  1964. await nextTick()
  1965. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1966. `
  1967. "
  1968. <!--[-->
  1969. <!--[--><span>a</span><span>b</span><span>c</span><!--for--><!--]-->
  1970. "
  1971. `,
  1972. )
  1973. })
  1974. test('with insertion anchor', async () => {
  1975. const { data, container } = await testHydration(
  1976. `<template>
  1977. <components.Child>
  1978. <span/>
  1979. <span>{{data}}</span>
  1980. <span/>
  1981. </components.Child>
  1982. </template>`,
  1983. {
  1984. Child: `<template><slot/></template>`,
  1985. },
  1986. )
  1987. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1988. `
  1989. "
  1990. <!--[--><span></span><span>foo</span><span></span><!--]-->
  1991. "
  1992. `,
  1993. )
  1994. data.value = 'bar'
  1995. await nextTick()
  1996. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1997. `
  1998. "
  1999. <!--[--><span></span><span>bar</span><span></span><!--]-->
  2000. "
  2001. `,
  2002. )
  2003. })
  2004. test('with multi level anchor insertion', async () => {
  2005. const { data, container } = await testHydration(
  2006. `<template>
  2007. <components.Child>
  2008. <span/>
  2009. <span>{{data}}</span>
  2010. <span/>
  2011. </components.Child>
  2012. </template>`,
  2013. {
  2014. Child: `
  2015. <template>
  2016. <div/>
  2017. <div/>
  2018. <slot/>
  2019. <div/>
  2020. </div>
  2021. </template>`,
  2022. },
  2023. )
  2024. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2025. `
  2026. "
  2027. <!--[--><div></div><div></div>
  2028. <!--[--><span></span><span>foo</span><span></span><!--]-->
  2029. <div></div><!--]-->
  2030. "
  2031. `,
  2032. )
  2033. data.value = 'bar'
  2034. await nextTick()
  2035. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2036. `
  2037. "
  2038. <!--[--><div></div><div></div>
  2039. <!--[--><span></span><span>bar</span><span></span><!--]-->
  2040. <div></div><!--]-->
  2041. "
  2042. `,
  2043. )
  2044. })
  2045. test('mixed slot and text node', async () => {
  2046. const data = reactive({
  2047. text: 'foo',
  2048. msg: 'hi',
  2049. })
  2050. const { container } = await testHydration(
  2051. `<template>
  2052. <components.Child>
  2053. <span>{{data.text}}</span>
  2054. </components.Child>
  2055. </template>`,
  2056. {
  2057. Child: `<template><div><slot/>{{data.msg}}</div></template>`,
  2058. },
  2059. data,
  2060. )
  2061. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2062. `
  2063. "<div>
  2064. <!--[--><span>foo</span><!--]-->
  2065. hi</div>"
  2066. `,
  2067. )
  2068. data.msg = 'bar'
  2069. await nextTick()
  2070. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2071. `
  2072. "<div>
  2073. <!--[--><span>foo</span><!--]-->
  2074. bar</div>"
  2075. `,
  2076. )
  2077. })
  2078. test('mixed root slot and text node', async () => {
  2079. const data = reactive({
  2080. text: 'foo',
  2081. msg: 'hi',
  2082. })
  2083. const { container } = await testHydration(
  2084. `<template>
  2085. <components.Child>
  2086. <span>{{data.text}}</span>
  2087. </components.Child>
  2088. </template>`,
  2089. {
  2090. Child: `<template>{{data.text}}<slot/>{{data.msg}}</template>`,
  2091. },
  2092. data,
  2093. )
  2094. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2095. `
  2096. "
  2097. <!--[-->foo
  2098. <!--[--><span>foo</span><!--]-->
  2099. hi<!--]-->
  2100. "
  2101. `,
  2102. )
  2103. data.msg = 'bar'
  2104. await nextTick()
  2105. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2106. `
  2107. "
  2108. <!--[-->foo
  2109. <!--[--><span>foo</span><!--]-->
  2110. bar<!--]-->
  2111. "
  2112. `,
  2113. )
  2114. })
  2115. test('mixed consecutive slot and element', async () => {
  2116. const data = reactive({
  2117. text: 'foo',
  2118. msg: 'hi',
  2119. })
  2120. const { container } = await testHydration(
  2121. `<template>
  2122. <components.Child>
  2123. <template #foo><span>{{data.text}}</span></template>
  2124. <template #bar><span>bar</span></template>
  2125. </components.Child>
  2126. </template>`,
  2127. {
  2128. Child: `<template><div><slot name="foo"/><slot name="bar"/><div>{{data.msg}}</div></div></template>`,
  2129. },
  2130. data,
  2131. )
  2132. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2133. `
  2134. "<div>
  2135. <!--[--><span>foo</span><!--]-->
  2136. <!--[--><span>bar</span><!--]-->
  2137. <div>hi</div></div>"
  2138. `,
  2139. )
  2140. data.msg = 'bar'
  2141. await nextTick()
  2142. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2143. `
  2144. "<div>
  2145. <!--[--><span>foo</span><!--]-->
  2146. <!--[--><span>bar</span><!--]-->
  2147. <div>bar</div></div>"
  2148. `,
  2149. )
  2150. })
  2151. test('mixed slot and element', async () => {
  2152. const data = reactive({
  2153. text: 'foo',
  2154. msg: 'hi',
  2155. })
  2156. const { container } = await testHydration(
  2157. `<template>
  2158. <components.Child>
  2159. <span>{{data.text}}</span>
  2160. </components.Child>
  2161. </template>`,
  2162. {
  2163. Child: `<template><div><slot/><div>{{data.msg}}</div></div></template>`,
  2164. },
  2165. data,
  2166. )
  2167. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2168. `
  2169. "<div>
  2170. <!--[--><span>foo</span><!--]-->
  2171. <div>hi</div></div>"
  2172. `,
  2173. )
  2174. data.msg = 'bar'
  2175. await nextTick()
  2176. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2177. `
  2178. "<div>
  2179. <!--[--><span>foo</span><!--]-->
  2180. <div>bar</div></div>"
  2181. `,
  2182. )
  2183. })
  2184. test('mixed slot and component', async () => {
  2185. const data = reactive({
  2186. msg1: 'foo',
  2187. msg2: 'bar',
  2188. })
  2189. const { container } = await testHydration(
  2190. `<template>
  2191. <components.Child>
  2192. <span>{{data.msg1}}</span>
  2193. </components.Child>
  2194. </template>`,
  2195. {
  2196. Child: `
  2197. <template>
  2198. <div>
  2199. <components.Child2/>
  2200. <slot/>
  2201. <components.Child2/>
  2202. </div>
  2203. </template>`,
  2204. Child2: `
  2205. <template>
  2206. <div>{{data.msg2}}</div>
  2207. </template>`,
  2208. },
  2209. data,
  2210. )
  2211. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2212. `
  2213. "<div><div>bar</div>
  2214. <!--[--><span>foo</span><!--]-->
  2215. <div>bar</div></div>"
  2216. `,
  2217. )
  2218. data.msg2 = 'hello'
  2219. await nextTick()
  2220. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2221. `
  2222. "<div><div>hello</div>
  2223. <!--[--><span>foo</span><!--]-->
  2224. <div>hello</div></div>"
  2225. `,
  2226. )
  2227. })
  2228. test('mixed slot and fragment component', async () => {
  2229. const data = reactive({
  2230. msg1: 'foo',
  2231. msg2: 'bar',
  2232. })
  2233. const { container } = await testHydration(
  2234. `<template>
  2235. <components.Child>
  2236. <span>{{data.msg1}}</span>
  2237. </components.Child>
  2238. </template>`,
  2239. {
  2240. Child: `
  2241. <template>
  2242. <div>
  2243. <components.Child2/>
  2244. <slot/>
  2245. <components.Child2/>
  2246. </div>
  2247. </template>`,
  2248. Child2: `
  2249. <template>
  2250. <div>{{data.msg1}}</div> {{data.msg2}}
  2251. </template>`,
  2252. },
  2253. data,
  2254. )
  2255. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2256. `
  2257. "<div>
  2258. <!--[--><div>foo</div> bar<!--]-->
  2259. <!--[--><span>foo</span><!--]-->
  2260. <!--[--><div>foo</div> bar<!--]-->
  2261. </div>"
  2262. `,
  2263. )
  2264. data.msg1 = 'hello'
  2265. data.msg2 = 'vapor'
  2266. await nextTick()
  2267. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2268. `
  2269. "<div>
  2270. <!--[--><div>hello</div> vapor<!--]-->
  2271. <!--[--><span>hello</span><!--]-->
  2272. <!--[--><div>hello</div> vapor<!--]-->
  2273. </div>"
  2274. `,
  2275. )
  2276. })
  2277. test('mixed slot and v-if', async () => {
  2278. const data = reactive({
  2279. show: true,
  2280. msg: 'foo',
  2281. })
  2282. const { container } = await testHydration(
  2283. `<template>
  2284. <components.Child>
  2285. <span>{{data.msg}}</span>
  2286. </components.Child>
  2287. </template>`,
  2288. {
  2289. Child: `
  2290. <template>
  2291. <div v-if="data.show">{{data.msg}}</div>
  2292. <slot/>
  2293. <div v-if="data.show">{{data.msg}}</div>
  2294. </template>`,
  2295. },
  2296. data,
  2297. )
  2298. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2299. `
  2300. "
  2301. <!--[--><div>foo</div><!--if-->
  2302. <!--[--><span>foo</span><!--]-->
  2303. <div>foo</div><!--if--><!--]-->
  2304. "
  2305. `,
  2306. )
  2307. data.show = false
  2308. await nextTick()
  2309. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2310. `
  2311. "
  2312. <!--[--><!--if-->
  2313. <!--[--><span>foo</span><!--]-->
  2314. <!--if--><!--]-->
  2315. "
  2316. `,
  2317. )
  2318. data.show = true
  2319. await nextTick()
  2320. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2321. "
  2322. <!--[--><div>foo</div><!--if-->
  2323. <!--[--><span>foo</span><!--]-->
  2324. <div>foo</div><!--if--><!--]-->
  2325. "
  2326. `)
  2327. })
  2328. test('mixed slot and v-for', async () => {
  2329. const data = reactive({
  2330. items: ['a', 'b', 'c'],
  2331. msg: 'foo',
  2332. })
  2333. const { container } = await testHydration(
  2334. `<template>
  2335. <components.Child>
  2336. <span>{{data.msg}}</span>
  2337. </components.Child>
  2338. </template>`,
  2339. {
  2340. Child: `
  2341. <template>
  2342. <div v-for="item in data.items" :key="item">{{item}}</div>
  2343. <slot/>
  2344. <div v-for="item in data.items" :key="item">{{item}}</div>
  2345. </template>`,
  2346. },
  2347. data,
  2348. )
  2349. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2350. `
  2351. "
  2352. <!--[-->
  2353. <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
  2354. <!--[--><span>foo</span><!--]-->
  2355. <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
  2356. <!--]-->
  2357. "
  2358. `,
  2359. )
  2360. data.items.push('d')
  2361. await nextTick()
  2362. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2363. `
  2364. "
  2365. <!--[-->
  2366. <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
  2367. <!--[--><span>foo</span><!--]-->
  2368. <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
  2369. <!--]-->
  2370. "
  2371. `,
  2372. )
  2373. })
  2374. test('consecutive slots', async () => {
  2375. const data = reactive({
  2376. msg1: 'foo',
  2377. msg2: 'bar',
  2378. })
  2379. const { container } = await testHydration(
  2380. `<template>
  2381. <components.Child>
  2382. <span>{{data.msg1}}</span>
  2383. <template #bar>
  2384. <span>{{data.msg2}}</span>
  2385. </template>
  2386. </components.Child>
  2387. </template>`,
  2388. {
  2389. Child: `<template><slot/><slot name="bar"/></template>`,
  2390. },
  2391. data,
  2392. )
  2393. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2394. `
  2395. "
  2396. <!--[-->
  2397. <!--[--><span>foo</span><!--]-->
  2398. <!--[--><span>bar</span><!--]-->
  2399. <!--]-->
  2400. "
  2401. `,
  2402. )
  2403. data.msg1 = 'hello'
  2404. data.msg2 = 'vapor'
  2405. await nextTick()
  2406. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2407. `
  2408. "
  2409. <!--[-->
  2410. <!--[--><span>hello</span><!--]-->
  2411. <!--[--><span>vapor</span><!--]-->
  2412. <!--]-->
  2413. "
  2414. `,
  2415. )
  2416. })
  2417. test('consecutive slots with insertion anchor', async () => {
  2418. const data = reactive({
  2419. msg1: 'foo',
  2420. msg2: 'bar',
  2421. })
  2422. const { container } = await testHydration(
  2423. `<template>
  2424. <components.Child>
  2425. <span>{{data.msg1}}</span>
  2426. <template #bar>
  2427. <span>{{data.msg2}}</span>
  2428. </template>
  2429. </components.Child>
  2430. </template>`,
  2431. {
  2432. Child: `<template>
  2433. <div>
  2434. <span/>
  2435. <slot/>
  2436. <slot name="bar"/>
  2437. <span/>
  2438. </div>
  2439. </template>`,
  2440. },
  2441. data,
  2442. )
  2443. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2444. `
  2445. "<div><span></span>
  2446. <!--[--><span>foo</span><!--]-->
  2447. <!--[--><span>bar</span><!--]-->
  2448. <span></span></div>"
  2449. `,
  2450. )
  2451. data.msg1 = 'hello'
  2452. data.msg2 = 'vapor'
  2453. await nextTick()
  2454. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2455. `
  2456. "<div><span></span>
  2457. <!--[--><span>hello</span><!--]-->
  2458. <!--[--><span>vapor</span><!--]-->
  2459. <span></span></div>"
  2460. `,
  2461. )
  2462. })
  2463. test('consecutive slots prepend', async () => {
  2464. const data = reactive({
  2465. msg1: 'foo',
  2466. msg2: 'bar',
  2467. msg3: 'baz',
  2468. })
  2469. const { container } = await testHydration(
  2470. `<template>
  2471. <components.Child>
  2472. <template #foo>
  2473. <span>{{data.msg1}}</span>
  2474. </template>
  2475. <template #bar>
  2476. <span>{{data.msg2}}</span>
  2477. </template>
  2478. </components.Child>
  2479. </template>`,
  2480. {
  2481. Child: `<template>
  2482. <div>
  2483. <slot name="foo"/>
  2484. <slot name="bar"/>
  2485. <div>{{data.msg3}}</div>
  2486. </div>
  2487. </template>`,
  2488. },
  2489. data,
  2490. )
  2491. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2492. `
  2493. "<div>
  2494. <!--[--><span>foo</span><!--]-->
  2495. <!--[--><span>bar</span><!--]-->
  2496. <div>baz</div></div>"
  2497. `,
  2498. )
  2499. data.msg1 = 'hello'
  2500. data.msg2 = 'vapor'
  2501. await nextTick()
  2502. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2503. `
  2504. "<div>
  2505. <!--[--><span>hello</span><!--]-->
  2506. <!--[--><span>vapor</span><!--]-->
  2507. <div>baz</div></div>"
  2508. `,
  2509. )
  2510. })
  2511. test('slot fallback', async () => {
  2512. const data = reactive({
  2513. foo: 'foo',
  2514. })
  2515. const { container } = await testHydration(
  2516. `<template>
  2517. <components.Child>
  2518. </components.Child>
  2519. </template>`,
  2520. {
  2521. Child: `<template><slot><span>{{data.foo}}</span></slot></template>`,
  2522. },
  2523. data,
  2524. )
  2525. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2526. `
  2527. "
  2528. <!--[--><span>foo</span><!--]-->
  2529. "
  2530. `,
  2531. )
  2532. data.foo = 'bar'
  2533. await nextTick()
  2534. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2535. `
  2536. "
  2537. <!--[--><span>bar</span><!--]-->
  2538. "
  2539. `,
  2540. )
  2541. })
  2542. test('forwarded slot', async () => {
  2543. const data = reactive({
  2544. foo: 'foo',
  2545. bar: 'bar',
  2546. })
  2547. const { container } = await testHydration(
  2548. `<template>
  2549. <div>
  2550. <components.Parent>
  2551. <span>{{data.foo}}</span>
  2552. </components.Parent>
  2553. <div>{{data.bar}}</div>
  2554. </div>
  2555. </template>`,
  2556. {
  2557. Parent: `<template><div><components.Child><slot/></components.Child></div></template>`,
  2558. Child: `<template><div><slot/></div></template>`,
  2559. },
  2560. data,
  2561. )
  2562. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2563. `
  2564. "<div><div><div>
  2565. <!--[-->
  2566. <!--[--><span>foo</span><!--]-->
  2567. <!--]-->
  2568. </div></div><div>bar</div></div>"
  2569. `,
  2570. )
  2571. data.foo = 'foo1'
  2572. data.bar = 'bar1'
  2573. await nextTick()
  2574. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2575. `
  2576. "<div><div><div>
  2577. <!--[-->
  2578. <!--[--><span>foo1</span><!--]-->
  2579. <!--]-->
  2580. </div></div><div>bar1</div></div>"
  2581. `,
  2582. )
  2583. })
  2584. test('forwarded slot with fallback', async () => {
  2585. const data = reactive({
  2586. foo: 'foo',
  2587. })
  2588. const { container } = await testHydration(
  2589. `<template>
  2590. <components.Parent/>
  2591. </template>`,
  2592. {
  2593. Parent: `<template><components.Child><slot/></components.Child></template>`,
  2594. Child: `<template><div><slot>{{data.foo}}</slot></div></template>`,
  2595. },
  2596. data,
  2597. )
  2598. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2599. `
  2600. "<div>
  2601. <!--[-->foo<!--]-->
  2602. </div>"
  2603. `,
  2604. )
  2605. data.foo = 'foo1'
  2606. await nextTick()
  2607. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2608. `
  2609. "<div>
  2610. <!--[-->foo1<!--]-->
  2611. </div>"
  2612. `,
  2613. )
  2614. })
  2615. test('forwarded slot with empty content', async () => {
  2616. const data = reactive({
  2617. foo: 'foo',
  2618. })
  2619. const { container } = await testHydration(
  2620. `<template>
  2621. <components.Foo/>
  2622. </template>`,
  2623. {
  2624. Foo: `<template>
  2625. <components.Bar>
  2626. <template #foo>
  2627. <slot name="foo" />
  2628. </template>
  2629. </components.Bar>
  2630. </template>`,
  2631. Bar: `<template>
  2632. <components.Baz>
  2633. <template #foo>
  2634. <slot name="foo" />
  2635. </template>
  2636. </components.Baz>
  2637. </template>`,
  2638. Baz: `<template>
  2639. <components.Qux>
  2640. <template #foo>
  2641. <slot name="foo" />
  2642. </template>
  2643. </components.Qux>
  2644. </template>`,
  2645. Qux: `<template>
  2646. <div>
  2647. <slot name="foo" />
  2648. <div>{{data.foo}}</div>
  2649. </div>
  2650. </template>`,
  2651. },
  2652. data,
  2653. )
  2654. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2655. `
  2656. "<div>
  2657. <!--[--><!--]-->
  2658. <div>foo</div></div>"
  2659. `,
  2660. )
  2661. data.foo = 'bar'
  2662. await nextTick()
  2663. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2664. `
  2665. "<div>
  2666. <!--[--><!--]-->
  2667. <div>bar</div></div>"
  2668. `,
  2669. )
  2670. })
  2671. })
  2672. describe('transition', async () => {
  2673. test('transition appear', async () => {
  2674. const { container } = await testHydration(
  2675. `<template>
  2676. <transition appear>
  2677. <div>foo</div>
  2678. </transition>
  2679. </template>`,
  2680. )
  2681. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2682. `"<div style="" class="v-enter-from v-enter-active">foo</div>"`,
  2683. )
  2684. expect(`mismatch`).not.toHaveBeenWarned()
  2685. })
  2686. test('transition appear work with pre-existing class', async () => {
  2687. const { container } = await testHydration(
  2688. `<template>
  2689. <transition appear>
  2690. <div class="foo">foo</div>
  2691. </transition>
  2692. </template>`,
  2693. )
  2694. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2695. `"<div class="foo v-enter-from v-enter-active" style="">foo</div>"`,
  2696. )
  2697. expect(`mismatch`).not.toHaveBeenWarned()
  2698. })
  2699. test('transition appear work with empty content', async () => {
  2700. const data = ref(true)
  2701. const { container } = await testHydration(
  2702. `<template>
  2703. <transition appear>
  2704. <slot v-if="data"></slot>
  2705. <span v-else>foo</span>
  2706. </transition>
  2707. </template>`,
  2708. undefined,
  2709. data,
  2710. )
  2711. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2712. `"<!--slot--><!--if-->"`,
  2713. )
  2714. expect(`mismatch`).not.toHaveBeenWarned()
  2715. data.value = false
  2716. await nextTick()
  2717. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2718. `"<span class="v-enter-from v-enter-active">foo</span><!--if-->"`,
  2719. )
  2720. })
  2721. test('transition appear with v-if', async () => {
  2722. const data = ref(false)
  2723. const { container } = await testHydration(
  2724. `<template>
  2725. <transition appear>
  2726. <div v-if="data">foo</div>
  2727. </transition>
  2728. </template>`,
  2729. undefined,
  2730. data,
  2731. )
  2732. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2733. `"<!--if-->"`,
  2734. )
  2735. expect(`mismatch`).not.toHaveBeenWarned()
  2736. })
  2737. test('transition appear with v-show', async () => {
  2738. const data = ref(false)
  2739. const { container } = await testHydration(
  2740. `<template>
  2741. <transition appear>
  2742. <div v-show="data">foo</div>
  2743. </transition>
  2744. </template>`,
  2745. undefined,
  2746. data,
  2747. )
  2748. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2749. `"<div style="display:none;" class="v-enter-from v-enter-active v-leave-from v-leave-active">foo</div>"`,
  2750. )
  2751. expect(`mismatch`).not.toHaveBeenWarned()
  2752. })
  2753. test('transition appear w/ event listener', async () => {
  2754. const { container } = await testHydration(
  2755. `<script setup>
  2756. import { ref } from 'vue'
  2757. const count = ref(0)
  2758. </script>
  2759. <template>
  2760. <transition appear>
  2761. <button @click="count++">{{ count }}</button>
  2762. </transition>
  2763. </template>`,
  2764. )
  2765. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2766. `"<button style="" class="v-enter-from v-enter-active">0</button>"`,
  2767. )
  2768. triggerEvent('click', container.querySelector('button')!)
  2769. await nextTick()
  2770. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2771. `"<button style="" class="v-enter-from v-enter-active">1</button>"`,
  2772. )
  2773. })
  2774. })
  2775. describe('teleport', () => {
  2776. test('basic', async () => {
  2777. const data = ref({
  2778. msg: ref('foo'),
  2779. disabled: ref(false),
  2780. fn: vi.fn(),
  2781. })
  2782. const teleportContainer = document.createElement('div')
  2783. teleportContainer.id = 'teleport'
  2784. teleportContainer.innerHTML =
  2785. `<!--teleport start anchor-->` +
  2786. `<span>foo</span>` +
  2787. `<span class="foo"></span>` +
  2788. `<!--teleport anchor-->`
  2789. document.body.appendChild(teleportContainer)
  2790. const { block, container } = await mountWithHydration(
  2791. '<!--teleport start--><!--teleport end-->',
  2792. `<teleport to="#teleport" :disabled="data.disabled">
  2793. <span>{{data.msg}}</span>
  2794. <span :class="data.msg" @click="data.fn"></span>
  2795. </teleport>`,
  2796. data,
  2797. )
  2798. const teleport = block as TeleportFragment
  2799. expect(teleport.anchor).toBe(container.lastChild)
  2800. expect(teleport.target).toBe(teleportContainer)
  2801. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  2802. expect((teleport.nodes as Node[])[0]).toBe(
  2803. teleportContainer.childNodes[1],
  2804. )
  2805. expect((teleport.nodes as Node[])[1]).toBe(
  2806. teleportContainer.childNodes[2],
  2807. )
  2808. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
  2809. expect(container.innerHTML).toMatchInlineSnapshot(
  2810. `"<!--teleport start--><!--teleport end-->"`,
  2811. )
  2812. // event handler
  2813. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  2814. expect(data.value.fn).toHaveBeenCalled()
  2815. data.value.msg = 'bar'
  2816. await nextTick()
  2817. expect(formatHtml(teleportContainer.innerHTML)).toBe(
  2818. `<!--teleport start anchor-->` +
  2819. `<span>bar</span>` +
  2820. `<span class="bar"></span>` +
  2821. `<!--teleport anchor-->`,
  2822. )
  2823. data.value.disabled = true
  2824. await nextTick()
  2825. expect(container.innerHTML).toBe(
  2826. `<!--teleport start-->` +
  2827. `<span>bar</span>` +
  2828. `<span class="bar"></span>` +
  2829. `<!--teleport end-->`,
  2830. )
  2831. expect(formatHtml(teleportContainer.innerHTML)).toMatchInlineSnapshot(
  2832. `"<!--teleport start anchor--><!--teleport anchor-->"`,
  2833. )
  2834. data.value.msg = 'baz'
  2835. await nextTick()
  2836. expect(container.innerHTML).toBe(
  2837. `<!--teleport start-->` +
  2838. `<span>baz</span>` +
  2839. `<span class="baz"></span>` +
  2840. `<!--teleport end-->`,
  2841. )
  2842. data.value.disabled = false
  2843. await nextTick()
  2844. expect(container.innerHTML).toMatchInlineSnapshot(
  2845. `"<!--teleport start--><!--teleport end-->"`,
  2846. )
  2847. expect(formatHtml(teleportContainer.innerHTML)).toBe(
  2848. `<!--teleport start anchor-->` +
  2849. `<span>baz</span>` +
  2850. `<span class="baz"></span>` +
  2851. `<!--teleport anchor-->`,
  2852. )
  2853. })
  2854. test('multiple + integration', async () => {
  2855. const data = ref({
  2856. msg: ref('foo'),
  2857. fn1: vi.fn(),
  2858. fn2: vi.fn(),
  2859. })
  2860. const code = `
  2861. <teleport to="#teleport2">
  2862. <span>{{data.msg}}</span>
  2863. <span :class="data.msg" @click="data.fn1"></span>
  2864. </teleport>
  2865. <teleport to="#teleport2">
  2866. <span>{{data.msg}}2</span>
  2867. <span :class="data.msg + 2" @click="data.fn2"></span>
  2868. </teleport>`
  2869. const SSRComp = compileVaporComponent(code, data, undefined, true)
  2870. const teleportContainer = document.createElement('div')
  2871. teleportContainer.id = 'teleport2'
  2872. const ctx = {} as any
  2873. const mainHtml = await VueServerRenderer.renderToString(
  2874. runtimeDom.createSSRApp(SSRComp),
  2875. ctx,
  2876. )
  2877. expect(mainHtml).toBe(
  2878. `<!--[-->` +
  2879. `<!--teleport start--><!--teleport end-->` +
  2880. `<!--teleport start--><!--teleport end-->` +
  2881. `<!--]-->`,
  2882. )
  2883. const teleportHtml = ctx.teleports!['#teleport2']
  2884. expect(teleportHtml).toBe(
  2885. `<!--teleport start anchor-->` +
  2886. `<span>foo</span><span class="foo"></span>` +
  2887. `<!--teleport anchor-->` +
  2888. `<!--teleport start anchor-->` +
  2889. `<span>foo2</span><span class="foo2"></span>` +
  2890. `<!--teleport anchor-->`,
  2891. )
  2892. teleportContainer.innerHTML = teleportHtml
  2893. document.body.appendChild(teleportContainer)
  2894. const { block, container } = await mountWithHydration(
  2895. mainHtml,
  2896. code,
  2897. data,
  2898. )
  2899. const teleports = block as any as TeleportFragment[]
  2900. const teleport1 = teleports[0]
  2901. const teleport2 = teleports[1]
  2902. expect(teleport1.anchor).toBe(container.childNodes[2])
  2903. expect(teleport2.anchor).toBe(container.childNodes[4])
  2904. expect(teleport1.target).toBe(teleportContainer)
  2905. expect(teleport1.targetStart).toBe(teleportContainer.childNodes[0])
  2906. expect((teleport1.nodes as Node[])[0]).toBe(
  2907. teleportContainer.childNodes[1],
  2908. )
  2909. expect(teleport1.targetAnchor).toBe(teleportContainer.childNodes[3])
  2910. expect(teleport2.target).toBe(teleportContainer)
  2911. expect(teleport2.targetStart).toBe(teleportContainer.childNodes[4])
  2912. expect((teleport2.nodes as Node[])[0]).toBe(
  2913. teleportContainer.childNodes[5],
  2914. )
  2915. expect(teleport2.targetAnchor).toBe(teleportContainer.childNodes[7])
  2916. expect(container.innerHTML).toBe(
  2917. `<!--[-->` +
  2918. `<!--teleport start--><!--teleport end-->` +
  2919. `<!--teleport start--><!--teleport end-->` +
  2920. `<!--]-->`,
  2921. )
  2922. // event handler
  2923. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  2924. expect(data.value.fn1).toHaveBeenCalled()
  2925. triggerEvent('click', teleportContainer.querySelector('.foo2')!)
  2926. expect(data.value.fn2).toHaveBeenCalled()
  2927. data.value.msg = 'bar'
  2928. await nextTick()
  2929. expect(teleportContainer.innerHTML).toBe(
  2930. `<!--teleport start anchor-->` +
  2931. `<span>bar</span>` +
  2932. `<span class="bar"></span>` +
  2933. `<!--teleport anchor-->` +
  2934. `<!--teleport start anchor-->` +
  2935. `<span>bar2</span>` +
  2936. `<span class="bar2"></span>` +
  2937. `<!--teleport anchor-->`,
  2938. )
  2939. })
  2940. test('disabled', async () => {
  2941. const data = ref({
  2942. msg: ref('foo'),
  2943. fn1: vi.fn(),
  2944. fn2: vi.fn(),
  2945. })
  2946. const code = `
  2947. <div>foo</div>
  2948. <teleport to="#teleport3" disabled="true">
  2949. <span>{{data.msg}}</span>
  2950. <span :class="data.msg" @click="data.fn1"></span>
  2951. </teleport>
  2952. <div :class="data.msg + 2" @click="data.fn2">bar</div>
  2953. `
  2954. const SSRComp = compileVaporComponent(code, data, undefined, true)
  2955. const teleportContainer = document.createElement('div')
  2956. teleportContainer.id = 'teleport3'
  2957. const ctx = {} as any
  2958. const mainHtml = await VueServerRenderer.renderToString(
  2959. runtimeDom.createSSRApp(SSRComp),
  2960. ctx,
  2961. )
  2962. expect(mainHtml).toBe(
  2963. `<!--[-->` +
  2964. `<div>foo</div>` +
  2965. `<!--teleport start-->` +
  2966. `<span>foo</span>` +
  2967. `<span class="foo"></span>` +
  2968. `<!--teleport end-->` +
  2969. `<div class="foo2">bar</div>` +
  2970. `<!--]-->`,
  2971. )
  2972. const teleportHtml = ctx.teleports!['#teleport3']
  2973. expect(teleportHtml).toMatchInlineSnapshot(
  2974. `"<!--teleport start anchor--><!--teleport anchor-->"`,
  2975. )
  2976. teleportContainer.innerHTML = teleportHtml
  2977. document.body.appendChild(teleportContainer)
  2978. const { block, container } = await mountWithHydration(
  2979. mainHtml,
  2980. code,
  2981. data,
  2982. )
  2983. const blocks = block as any[]
  2984. expect(blocks[0]).toBe(container.childNodes[1])
  2985. const teleport = blocks[1] as TeleportFragment
  2986. expect((teleport.nodes as Node[])[0]).toBe(container.childNodes[3])
  2987. expect((teleport.nodes as Node[])[1]).toBe(container.childNodes[4])
  2988. expect(teleport.anchor).toBe(container.childNodes[5])
  2989. expect(teleport.target).toBe(teleportContainer)
  2990. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  2991. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[1])
  2992. expect(blocks[2]).toBe(container.childNodes[6])
  2993. expect(container.innerHTML).toBe(
  2994. `<!--[-->` +
  2995. `<div>foo</div>` +
  2996. `<!--teleport start-->` +
  2997. `<span>foo</span>` +
  2998. `<span class="foo"></span>` +
  2999. `<!--teleport end-->` +
  3000. `<div class="foo2">bar</div>` +
  3001. `<!--]-->`,
  3002. )
  3003. // event handler
  3004. triggerEvent('click', container.querySelector('.foo')!)
  3005. expect(data.value.fn1).toHaveBeenCalled()
  3006. triggerEvent('click', container.querySelector('.foo2')!)
  3007. expect(data.value.fn2).toHaveBeenCalled()
  3008. data.value.msg = 'bar'
  3009. await nextTick()
  3010. expect(container.innerHTML).toBe(
  3011. `<!--[-->` +
  3012. `<div>foo</div>` +
  3013. `<!--teleport start-->` +
  3014. `<span>bar</span>` +
  3015. `<span class="bar"></span>` +
  3016. `<!--teleport end-->` +
  3017. `<div class="bar2">bar</div>` +
  3018. `<!--]-->`,
  3019. )
  3020. })
  3021. test('disabled + as component root', async () => {
  3022. const { container } = await mountWithHydration(
  3023. `<!--[-->` +
  3024. `<div>Parent fragment</div>` +
  3025. `<!--teleport start--><div>Teleport content</div><!--teleport end-->` +
  3026. `<!--]-->`,
  3027. `
  3028. <div>Parent fragment</div>
  3029. <teleport to="body" disabled>
  3030. <div>Teleport content</div>
  3031. </teleport>
  3032. `,
  3033. )
  3034. expect(container.innerHTML).toBe(
  3035. `<!--[-->` +
  3036. `<div>Parent fragment</div>` +
  3037. `<!--teleport start-->` +
  3038. `<div>Teleport content</div>` +
  3039. `<!--teleport end-->` +
  3040. `<!--]-->`,
  3041. )
  3042. expect(`mismatch`).not.toHaveBeenWarned()
  3043. })
  3044. test('as component root', async () => {
  3045. const teleportContainer = document.createElement('div')
  3046. teleportContainer.id = 'teleport4'
  3047. teleportContainer.innerHTML = `<!--teleport start anchor-->hello<!--teleport anchor-->`
  3048. document.body.appendChild(teleportContainer)
  3049. const { block, container } = await mountWithHydration(
  3050. '<!--teleport start--><!--teleport end-->',
  3051. `<components.Wrapper></components.Wrapper>`,
  3052. undefined,
  3053. {
  3054. Wrapper: compileVaporComponent(
  3055. `<teleport to="#teleport4">hello</teleport>`,
  3056. ),
  3057. },
  3058. )
  3059. const teleport = (block as VaporComponentInstance)
  3060. .block as TeleportFragment
  3061. expect(teleport.anchor).toBe(container.childNodes[1])
  3062. expect(teleport.target).toBe(teleportContainer)
  3063. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3064. expect(teleport.nodes).toBe(teleportContainer.childNodes[1])
  3065. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[2])
  3066. })
  3067. test('nested', async () => {
  3068. const teleportContainer = document.createElement('div')
  3069. teleportContainer.id = 'teleport5'
  3070. teleportContainer.innerHTML =
  3071. `<!--teleport start anchor-->` +
  3072. `<!--teleport start--><!--teleport end-->` +
  3073. `<!--teleport anchor-->` +
  3074. `<!--teleport start anchor-->` +
  3075. `<div>child</div>` +
  3076. `<!--teleport anchor-->`
  3077. document.body.appendChild(teleportContainer)
  3078. const { block, container } = await mountWithHydration(
  3079. '<!--teleport start--><!--teleport end-->',
  3080. `<teleport to="#teleport5">
  3081. <teleport to="#teleport5"><div>child</div></teleport>
  3082. </teleport>`,
  3083. )
  3084. const teleport = block as TeleportFragment
  3085. expect(teleport.anchor).toBe(container.childNodes[1])
  3086. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3087. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
  3088. const childTeleport = teleport.nodes as TeleportFragment
  3089. expect(childTeleport.anchor).toBe(teleportContainer.childNodes[2])
  3090. expect(childTeleport.targetStart).toBe(teleportContainer.childNodes[4])
  3091. expect(childTeleport.targetAnchor).toBe(teleportContainer.childNodes[6])
  3092. expect(childTeleport.nodes).toBe(teleportContainer.childNodes[5])
  3093. })
  3094. test('unmount (full integration)', async () => {
  3095. const targetId = 'teleport6'
  3096. const data = ref({
  3097. toggle: ref(true),
  3098. })
  3099. const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
  3100. const Comp1 = compileVaporComponent(template1)
  3101. const SSRComp1 = compileVaporComponent(
  3102. template1,
  3103. undefined,
  3104. undefined,
  3105. true,
  3106. )
  3107. const template2 = `<div>Comp2</div>`
  3108. const Comp2 = compileVaporComponent(template2)
  3109. const SSRComp2 = compileVaporComponent(
  3110. template2,
  3111. undefined,
  3112. undefined,
  3113. true,
  3114. )
  3115. const appCode = `
  3116. <div>
  3117. <components.Comp1 v-if="data.toggle"/>
  3118. <components.Comp2 v-else/>
  3119. </div>
  3120. `
  3121. const SSRApp = compileVaporComponent(
  3122. appCode,
  3123. data,
  3124. {
  3125. Comp1: SSRComp1,
  3126. Comp2: SSRComp2,
  3127. },
  3128. true,
  3129. )
  3130. const teleportContainer = document.createElement('div')
  3131. teleportContainer.id = targetId
  3132. document.body.appendChild(teleportContainer)
  3133. const ctx = {} as any
  3134. const mainHtml = await VueServerRenderer.renderToString(
  3135. runtimeDom.createSSRApp(SSRApp),
  3136. ctx,
  3137. )
  3138. expect(mainHtml).toBe(
  3139. '<div><!--teleport start--><!--teleport end--></div>',
  3140. )
  3141. teleportContainer.innerHTML = ctx.teleports![`#${targetId}`]
  3142. const { container } = await mountWithHydration(mainHtml, appCode, data, {
  3143. Comp1,
  3144. Comp2,
  3145. })
  3146. expect(container.innerHTML).toBe(
  3147. '<div><!--teleport start--><!--teleport end--><!--if--></div>',
  3148. )
  3149. expect(teleportContainer.innerHTML).toBe(
  3150. `<!--teleport start anchor-->` +
  3151. `<span>Teleported Comp1</span>` +
  3152. `<!--teleport anchor-->`,
  3153. )
  3154. expect(`mismatch`).not.toHaveBeenWarned()
  3155. data.value.toggle = false
  3156. await nextTick()
  3157. expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
  3158. expect(teleportContainer.innerHTML).toBe('')
  3159. })
  3160. test('unmount (mismatch + full integration)', async () => {
  3161. const targetId = 'teleport7'
  3162. const data = ref({
  3163. toggle: ref(true),
  3164. })
  3165. const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
  3166. const Comp1 = compileVaporComponent(template1)
  3167. const SSRComp1 = compileVaporComponent(
  3168. template1,
  3169. undefined,
  3170. undefined,
  3171. true,
  3172. )
  3173. const template2 = `<div>Comp2</div>`
  3174. const Comp2 = compileVaporComponent(template2)
  3175. const SSRComp2 = compileVaporComponent(
  3176. template2,
  3177. undefined,
  3178. undefined,
  3179. true,
  3180. )
  3181. const appCode = `
  3182. <div>
  3183. <components.Comp1 v-if="data.toggle"/>
  3184. <components.Comp2 v-else/>
  3185. </div>
  3186. `
  3187. const SSRApp = compileVaporComponent(
  3188. appCode,
  3189. data,
  3190. {
  3191. Comp1: SSRComp1,
  3192. Comp2: SSRComp2,
  3193. },
  3194. true,
  3195. )
  3196. const teleportContainer = document.createElement('div')
  3197. teleportContainer.id = targetId
  3198. document.body.appendChild(teleportContainer)
  3199. const mainHtml = await VueServerRenderer.renderToString(
  3200. runtimeDom.createSSRApp(SSRApp),
  3201. )
  3202. expect(mainHtml).toBe(
  3203. '<div><!--teleport start--><!--teleport end--></div>',
  3204. )
  3205. expect(teleportContainer.innerHTML).toBe('')
  3206. const { container } = await mountWithHydration(mainHtml, appCode, data, {
  3207. Comp1,
  3208. Comp2,
  3209. })
  3210. expect(container.innerHTML).toBe(
  3211. '<div><!--teleport start--><!--teleport end--><!--if--></div>',
  3212. )
  3213. expect(teleportContainer.innerHTML).toBe(`<span>Teleported Comp1</span>`)
  3214. expect(`Hydration children mismatch`).toHaveBeenWarned()
  3215. data.value.toggle = false
  3216. await nextTick()
  3217. expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
  3218. expect(teleportContainer.innerHTML).toBe('')
  3219. })
  3220. test('target change (mismatch + full integration)', async () => {
  3221. const targetId1 = 'teleport8-1'
  3222. const targetId2 = 'teleport8-2'
  3223. const data = ref({
  3224. target: ref(targetId1),
  3225. msg: ref('foo'),
  3226. })
  3227. const template = `<Teleport :to="'#' + data.target"><span>{{data.msg}}</span></Teleport>`
  3228. const Comp = compileVaporComponent(template, data)
  3229. const SSRComp = compileVaporComponent(template, data, undefined, true)
  3230. const teleportContainer1 = document.createElement('div')
  3231. teleportContainer1.id = targetId1
  3232. const teleportContainer2 = document.createElement('div')
  3233. teleportContainer2.id = targetId2
  3234. document.body.appendChild(teleportContainer1)
  3235. document.body.appendChild(teleportContainer2)
  3236. // server render
  3237. const mainHtml = await VueServerRenderer.renderToString(
  3238. runtimeDom.createSSRApp(SSRComp),
  3239. )
  3240. expect(mainHtml).toBe(`<!--teleport start--><!--teleport end-->`)
  3241. expect(teleportContainer1.innerHTML).toBe('')
  3242. expect(teleportContainer2.innerHTML).toBe('')
  3243. // hydrate
  3244. const { container } = await mountWithHydration(mainHtml, template, data, {
  3245. Comp,
  3246. })
  3247. expect(container.innerHTML).toBe(
  3248. `<!--teleport start--><!--teleport end-->`,
  3249. )
  3250. expect(teleportContainer1.innerHTML).toBe(`<span>foo</span>`)
  3251. expect(teleportContainer2.innerHTML).toBe('')
  3252. expect(`Hydration children mismatch`).toHaveBeenWarned()
  3253. data.value.target = targetId2
  3254. data.value.msg = 'bar'
  3255. await nextTick()
  3256. expect(container.innerHTML).toBe(
  3257. `<!--teleport start--><!--teleport end-->`,
  3258. )
  3259. expect(teleportContainer1.innerHTML).toBe('')
  3260. expect(teleportContainer2.innerHTML).toBe(`<span>bar</span>`)
  3261. })
  3262. test('with disabled teleport + undefined target', async () => {
  3263. const data = ref({
  3264. msg: ref('foo'),
  3265. })
  3266. const { container } = await mountWithHydration(
  3267. '<!--teleport start--><span>foo</span><!--teleport end-->',
  3268. `<teleport :to="undefined" :disabled="true">
  3269. <span>{{data.msg}}</span>
  3270. </teleport>`,
  3271. data,
  3272. )
  3273. expect(container.innerHTML).toBe(
  3274. `<!--teleport start--><span>foo</span><!--teleport end-->`,
  3275. )
  3276. data.value.msg = 'bar'
  3277. await nextTick()
  3278. expect(container.innerHTML).toBe(
  3279. `<!--teleport start--><span>bar</span><!--teleport end-->`,
  3280. )
  3281. })
  3282. })
  3283. describe('async component', async () => {
  3284. test('async component', async () => {
  3285. const data = ref({
  3286. spy: vi.fn(),
  3287. })
  3288. const compCode = `<button @click="data.spy">hello!</button>`
  3289. const SSRComp = compileVaporComponent(compCode, data, undefined, true)
  3290. let serverResolve: any
  3291. // use defineAsyncComponent in SSR
  3292. let AsyncComp = defineAsyncComponent(
  3293. () =>
  3294. new Promise(r => {
  3295. serverResolve = r
  3296. }),
  3297. )
  3298. const appCode = `hello<components.AsyncComp/>world`
  3299. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3300. // server render
  3301. const htmlPromise = VueServerRenderer.renderToString(
  3302. runtimeDom.createSSRApp(SSRApp),
  3303. )
  3304. serverResolve(SSRComp)
  3305. const html = await htmlPromise
  3306. expect(html).toMatchInlineSnapshot(
  3307. `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  3308. )
  3309. // hydration
  3310. let clientResolve: any
  3311. AsyncComp = defineVaporAsyncComponent(
  3312. () =>
  3313. new Promise(r => {
  3314. clientResolve = r
  3315. }),
  3316. ) as any
  3317. const Comp = compileVaporComponent(compCode, data)
  3318. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3319. const container = document.createElement('div')
  3320. container.innerHTML = html
  3321. document.body.appendChild(container)
  3322. createVaporSSRApp(App).mount(container)
  3323. // hydration not complete yet
  3324. triggerEvent('click', container.querySelector('button')!)
  3325. expect(data.value.spy).not.toHaveBeenCalled()
  3326. // resolve
  3327. clientResolve(Comp)
  3328. await new Promise(r => setTimeout(r))
  3329. // should be hydrated now
  3330. triggerEvent('click', container.querySelector('button')!)
  3331. expect(data.value.spy).toHaveBeenCalled()
  3332. })
  3333. // No longer needed, parent component updates in vapor mode no longer
  3334. // cause child components to re-render
  3335. // test.todo('update async wrapper before resolve', async () => {})
  3336. test('update async component after parent mount before async component resolve', async () => {
  3337. const data = ref({
  3338. toggle: true,
  3339. })
  3340. const compCode = `
  3341. <script vapor>
  3342. defineProps(['toggle'])
  3343. </script>
  3344. <template>
  3345. <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
  3346. </template>
  3347. `
  3348. const SSRComp = compileVaporComponent(
  3349. compCode,
  3350. undefined,
  3351. undefined,
  3352. true,
  3353. )
  3354. let serverResolve: any
  3355. // use defineAsyncComponent in SSR
  3356. let AsyncComp = defineAsyncComponent(
  3357. () =>
  3358. new Promise(r => {
  3359. serverResolve = r
  3360. }),
  3361. )
  3362. const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
  3363. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3364. // server render
  3365. const htmlPromise = VueServerRenderer.renderToString(
  3366. runtimeDom.createSSRApp(SSRApp),
  3367. )
  3368. serverResolve(SSRComp)
  3369. const html = await htmlPromise
  3370. expect(html).toMatchInlineSnapshot(`"<h1>Async component</h1>"`)
  3371. // hydration
  3372. let clientResolve: any
  3373. AsyncComp = defineVaporAsyncComponent(
  3374. () =>
  3375. new Promise(r => {
  3376. clientResolve = r
  3377. }),
  3378. ) as any
  3379. const Comp = compileVaporComponent(compCode)
  3380. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3381. const container = document.createElement('div')
  3382. container.innerHTML = html
  3383. document.body.appendChild(container)
  3384. createVaporSSRApp(App).mount(container)
  3385. // update before resolve
  3386. data.value.toggle = false
  3387. await nextTick()
  3388. // resolve
  3389. clientResolve(Comp)
  3390. await new Promise(r => setTimeout(r))
  3391. // prevent lazy hydration since the component has been patched
  3392. expect('Skipping lazy hydration for component').toHaveBeenWarned()
  3393. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3394. expect(container.innerHTML).toMatchInlineSnapshot(
  3395. `"<h1>Updated async component</h1><!--async component-->"`,
  3396. )
  3397. })
  3398. test('update async component (fragment root) after parent mount before async component resolve', async () => {
  3399. const data = ref({
  3400. toggle: true,
  3401. })
  3402. const compCode = `
  3403. <script vapor>
  3404. defineProps(['toggle'])
  3405. </script>
  3406. <template>
  3407. <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
  3408. <h2>fragment root</h2>
  3409. </template>
  3410. `
  3411. const SSRComp = compileVaporComponent(
  3412. compCode,
  3413. undefined,
  3414. undefined,
  3415. true,
  3416. )
  3417. let serverResolve: any
  3418. // use defineAsyncComponent in SSR
  3419. let AsyncComp = defineAsyncComponent(
  3420. () =>
  3421. new Promise(r => {
  3422. serverResolve = r
  3423. }),
  3424. )
  3425. const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
  3426. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3427. // server render
  3428. const htmlPromise = VueServerRenderer.renderToString(
  3429. runtimeDom.createSSRApp(SSRApp),
  3430. )
  3431. serverResolve(SSRComp)
  3432. const html = await htmlPromise
  3433. expect(html).toMatchInlineSnapshot(
  3434. `"<!--[--><h1>Async component</h1><h2>fragment root</h2><!--]-->"`,
  3435. )
  3436. // hydration
  3437. let clientResolve: any
  3438. AsyncComp = defineVaporAsyncComponent(
  3439. () =>
  3440. new Promise(r => {
  3441. clientResolve = r
  3442. }),
  3443. ) as any
  3444. const Comp = compileVaporComponent(compCode)
  3445. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3446. const container = document.createElement('div')
  3447. container.innerHTML = html
  3448. document.body.appendChild(container)
  3449. createVaporSSRApp(App).mount(container)
  3450. // update before resolve
  3451. data.value.toggle = false
  3452. await nextTick()
  3453. // resolve
  3454. clientResolve(Comp)
  3455. await new Promise(r => setTimeout(r))
  3456. // prevent lazy hydration since the component has been patched
  3457. expect('Skipping lazy hydration for component').toHaveBeenWarned()
  3458. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3459. expect(container.innerHTML).toMatchInlineSnapshot(
  3460. `"<!--[--><h1>Updated async component</h1><h2>fragment root</h2><!--async component--><!--]-->"`,
  3461. )
  3462. })
  3463. // required vapor Suspense
  3464. test.todo('hydrate safely when property used by async setup changed before render', async () => {})
  3465. // required vapor Suspense
  3466. test.todo('hydrate safely when property used by deep nested async setup changed before render', async () => {})
  3467. test('unmount async wrapper before load', async () => {
  3468. const data = ref({
  3469. toggle: true,
  3470. })
  3471. const compCode = `<div>async</div>`
  3472. const appCode = `
  3473. <div>
  3474. <components.AsyncComp v-if="data.toggle"/>
  3475. <div v-else>hi</div>
  3476. </div>
  3477. `
  3478. // hydration
  3479. let clientResolve: any
  3480. const AsyncComp = defineVaporAsyncComponent(
  3481. () =>
  3482. new Promise(r => {
  3483. clientResolve = r
  3484. }),
  3485. )
  3486. const Comp = compileVaporComponent(compCode)
  3487. const App = compileVaporComponent(appCode, data, {
  3488. AsyncComp,
  3489. })
  3490. const container = document.createElement('div')
  3491. container.innerHTML = '<div><div>async</div></div>'
  3492. createVaporSSRApp(App).mount(container)
  3493. // unmount before resolve
  3494. data.value.toggle = false
  3495. await nextTick()
  3496. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3497. // resolve
  3498. clientResolve(Comp)
  3499. await new Promise(r => setTimeout(r))
  3500. // should remain unmounted
  3501. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3502. })
  3503. test('unmount async wrapper before load (fragment)', async () => {
  3504. const data = ref({
  3505. toggle: true,
  3506. })
  3507. const compCode = `<div>async</div><div>fragment</div>`
  3508. const appCode = `
  3509. <div>
  3510. <components.AsyncComp v-if="data.toggle"/>
  3511. <div v-else>hi</div>
  3512. </div>
  3513. `
  3514. // hydration
  3515. let clientResolve: any
  3516. const AsyncComp = defineVaporAsyncComponent(
  3517. () =>
  3518. new Promise(r => {
  3519. clientResolve = r
  3520. }),
  3521. )
  3522. const Comp = compileVaporComponent(compCode)
  3523. const App = compileVaporComponent(appCode, data, {
  3524. AsyncComp,
  3525. })
  3526. const container = document.createElement('div')
  3527. container.innerHTML =
  3528. '<div><!--[--><div>async</div><div>fragment</div><!--]--></div>'
  3529. createVaporSSRApp(App).mount(container)
  3530. // unmount before resolve
  3531. data.value.toggle = false
  3532. await nextTick()
  3533. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3534. // resolve
  3535. clientResolve(Comp)
  3536. await new Promise(r => setTimeout(r))
  3537. // should remain unmounted
  3538. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3539. })
  3540. test('nested async wrapper', async () => {
  3541. const toggleCode = `
  3542. <script vapor>
  3543. import { onMounted, ref, nextTick } from 'vue'
  3544. const show = ref(false)
  3545. onMounted(() => {
  3546. nextTick(() => {
  3547. show.value = true
  3548. })
  3549. })
  3550. </script>
  3551. <template>
  3552. <div v-show="show">
  3553. <slot />
  3554. </div>
  3555. </template>
  3556. `
  3557. const SSRToggle = compileVaporComponent(
  3558. toggleCode,
  3559. undefined,
  3560. undefined,
  3561. true,
  3562. )
  3563. const wrapperCode = `<slot/>`
  3564. const SSRWrapper = compileVaporComponent(
  3565. wrapperCode,
  3566. undefined,
  3567. undefined,
  3568. true,
  3569. )
  3570. const data = ref({
  3571. count: 0,
  3572. fn: vi.fn(),
  3573. })
  3574. const childCode = `
  3575. <script vapor>
  3576. import { onMounted } from 'vue'
  3577. const data = _data; const components = _components;
  3578. onMounted(() => {
  3579. data.value.fn()
  3580. data.value.count++
  3581. })
  3582. </script>
  3583. <template>
  3584. <div>{{data.count}}</div>
  3585. </template>
  3586. `
  3587. const SSRChild = compileVaporComponent(childCode, data, undefined, true)
  3588. const appCode = `
  3589. <components.Toggle>
  3590. <components.Wrapper>
  3591. <components.Wrapper>
  3592. <components.Child/>
  3593. </components.Wrapper>
  3594. </components.Wrapper>
  3595. </components.Toggle>
  3596. `
  3597. const SSRApp = compileVaporComponent(
  3598. appCode,
  3599. undefined,
  3600. {
  3601. Toggle: SSRToggle,
  3602. Wrapper: SSRWrapper,
  3603. Child: SSRChild,
  3604. },
  3605. true,
  3606. )
  3607. const root = document.createElement('div')
  3608. // server render
  3609. root.innerHTML = await VueServerRenderer.renderToString(
  3610. runtimeDom.createSSRApp(SSRApp),
  3611. )
  3612. expect(root.innerHTML).toMatchInlineSnapshot(
  3613. `"<div style="display:none;"><!--[--><!--[--><!--[--><div>0</div><!--]--><!--]--><!--]--></div>"`,
  3614. )
  3615. const Toggle = compileVaporComponent(toggleCode)
  3616. const Wrapper = compileVaporComponent(wrapperCode)
  3617. const Child = compileVaporComponent(childCode, data)
  3618. const App = compileVaporComponent(appCode, undefined, {
  3619. Toggle,
  3620. Wrapper,
  3621. Child,
  3622. })
  3623. // hydration
  3624. createVaporSSRApp(App).mount(root)
  3625. await nextTick()
  3626. await nextTick()
  3627. expect(root.innerHTML).toMatchInlineSnapshot(
  3628. `"<div style=""><!--[--><!--[--><!--[--><div>1</div><!--]--><!--]--><!--]--></div>"`,
  3629. )
  3630. expect(data.value.fn).toBeCalledTimes(1)
  3631. })
  3632. })
  3633. describe.todo('Suspense')
  3634. describe('force hydrate prop', async () => {
  3635. test('force hydrate prop with `.prop` modifier', async () => {
  3636. const { container } = await mountWithHydration(
  3637. '<input type="checkbox">',
  3638. `<input type="checkbox" .indeterminate="true"/>`,
  3639. )
  3640. expect((container.firstChild! as any).indeterminate).toBe(true)
  3641. })
  3642. test('force hydrate input v-model with non-string value bindings', async () => {
  3643. const { container } = await mountWithHydration(
  3644. '<input type="checkbox" value="true">',
  3645. `<input type="checkbox" :true-value="true"/>`,
  3646. )
  3647. expect((container.firstChild as any)._trueValue).toBe(true)
  3648. })
  3649. test('force hydrate checkbox with indeterminate', async () => {
  3650. const { container } = await mountWithHydration(
  3651. '<input type="checkbox" indeterminate/>',
  3652. `<input type="checkbox" :indeterminate="true"/>`,
  3653. )
  3654. expect((container.firstChild! as any).indeterminate).toBe(true)
  3655. })
  3656. test('force hydrate select option with non-string value bindings', async () => {
  3657. const { container } = await mountWithHydration(
  3658. '<select><option value="true">ok</option></select>',
  3659. `<select><option :value="true">ok</option></select>`,
  3660. )
  3661. expect((container.firstChild!.firstChild as any)._value).toBe(true)
  3662. })
  3663. test('force hydrate v-bind with .prop modifiers', async () => {
  3664. const { container } = await mountWithHydration(
  3665. '<div .foo="true"/>',
  3666. `<div v-bind="data"/>`,
  3667. ref({ '.foo': true }),
  3668. )
  3669. expect((container.firstChild! as any).foo).toBe(true)
  3670. })
  3671. test('force hydrate custom element with dynamic props', () => {
  3672. class MyElement extends HTMLElement {
  3673. foo = ''
  3674. constructor() {
  3675. super()
  3676. }
  3677. }
  3678. customElements.define('my-element-7203', MyElement)
  3679. const msg = ref('bar')
  3680. const container = document.createElement('div')
  3681. container.innerHTML = '<my-element-7203></my-element-7203>'
  3682. const app = createVaporSSRApp({
  3683. setup() {
  3684. return createPlainElement('my-element-7203', { foo: () => msg.value })
  3685. },
  3686. })
  3687. app.mount(container)
  3688. expect((container.firstChild as any).foo).toBe(msg.value)
  3689. })
  3690. })
  3691. })
  3692. describe('mismatch handling', () => {
  3693. test('text node', async () => {
  3694. const foo = ref('bar')
  3695. const { container } = await mountWithHydration(`foo`, `{{data}}`, foo)
  3696. expect(container.textContent).toBe('bar')
  3697. expect(`Hydration text mismatch`).toHaveBeenWarned()
  3698. })
  3699. test('element text content', async () => {
  3700. const data = ref({ textContent: 'bar' })
  3701. const { container } = await mountWithHydration(
  3702. `<div>foo</div>`,
  3703. `<div v-bind="data"></div>`,
  3704. data,
  3705. )
  3706. expect(container.innerHTML).toBe('<div>bar</div>')
  3707. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  3708. })
  3709. // test('not enough children', () => {
  3710. // const { container } = mountWithHydration(`<div></div>`, () =>
  3711. // h('div', [h('span', 'foo'), h('span', 'bar')]),
  3712. // )
  3713. // expect(container.innerHTML).toBe(
  3714. // '<div><span>foo</span><span>bar</span></div>',
  3715. // )
  3716. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3717. // })
  3718. // test('too many children', () => {
  3719. // const { container } = mountWithHydration(
  3720. // `<div><span>foo</span><span>bar</span></div>`,
  3721. // () => h('div', [h('span', 'foo')]),
  3722. // )
  3723. // expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  3724. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3725. // })
  3726. test('complete mismatch', async () => {
  3727. const data = ref('span')
  3728. const { container } = await mountWithHydration(
  3729. `<div>foo</div>`,
  3730. `<component :is="data">foo</component>`,
  3731. data,
  3732. )
  3733. expect(container.innerHTML).toBe('<span>foo</span><!--dynamic-component-->')
  3734. expect(`Hydration node mismatch`).toHaveBeenWarned()
  3735. })
  3736. // test('fragment mismatch removal', () => {
  3737. // const { container } = mountWithHydration(
  3738. // `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  3739. // () => h('div', [h('span', 'replaced')]),
  3740. // )
  3741. // expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  3742. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3743. // })
  3744. // test('fragment not enough children', () => {
  3745. // const { container } = mountWithHydration(
  3746. // `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  3747. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  3748. // )
  3749. // expect(container.innerHTML).toBe(
  3750. // '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  3751. // )
  3752. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3753. // })
  3754. // test('fragment too many children', () => {
  3755. // const { container } = mountWithHydration(
  3756. // `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  3757. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  3758. // )
  3759. // expect(container.innerHTML).toBe(
  3760. // '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  3761. // )
  3762. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  3763. // // as 2nd fragment child.
  3764. // expect(`Hydration text content mismatch`).toHaveBeenWarned()
  3765. // // excessive children removal
  3766. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3767. // })
  3768. // test('Teleport target has empty children', () => {
  3769. // const teleportContainer = document.createElement('div')
  3770. // teleportContainer.id = 'teleport'
  3771. // document.body.appendChild(teleportContainer)
  3772. // mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  3773. // h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  3774. // )
  3775. // expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  3776. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3777. // })
  3778. // test('comment mismatch (element)', () => {
  3779. // const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  3780. // h('div', [createCommentVNode('hi')]),
  3781. // )
  3782. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  3783. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3784. // })
  3785. // test('comment mismatch (text)', () => {
  3786. // const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  3787. // h('div', [createCommentVNode('hi')]),
  3788. // )
  3789. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  3790. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3791. // })
  3792. test('class mismatch', async () => {
  3793. await mountWithHydration(
  3794. `<div class="foo bar"></div>`,
  3795. `<div :class="data"></div>`,
  3796. ref(['foo', 'bar']),
  3797. )
  3798. await mountWithHydration(
  3799. `<div class="foo bar"></div>`,
  3800. `<div :class="data"></div>`,
  3801. ref({ foo: true, bar: true }),
  3802. )
  3803. await mountWithHydration(
  3804. `<div class="foo bar"></div>`,
  3805. `<div :class="data"></div>`,
  3806. ref('foo bar'),
  3807. )
  3808. // svg classes
  3809. await mountWithHydration(
  3810. `<svg class="foo bar"></svg>`,
  3811. `<svg :class="data"></svg>`,
  3812. ref('foo bar'),
  3813. )
  3814. // class with different order
  3815. await mountWithHydration(
  3816. `<div class="foo bar"></div>`,
  3817. `<div :class="data"></div>`,
  3818. ref('bar foo'),
  3819. )
  3820. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  3821. // single root mismatch
  3822. const { container: root } = await mountWithHydration(
  3823. `<div class="foo bar"></div>`,
  3824. `<div :class="data"></div>`,
  3825. ref('baz'),
  3826. )
  3827. expect(root.innerHTML).toBe('<div class="foo bar baz"></div>')
  3828. expect(`Hydration class mismatch`).toHaveBeenWarned()
  3829. // multiple root mismatch
  3830. const { container } = await mountWithHydration(
  3831. `<div class="foo bar"></div><span/>`,
  3832. `<div :class="data"></div><span/>`,
  3833. ref('foo'),
  3834. )
  3835. expect(container.innerHTML).toBe('<div class="foo"></div><span></span>')
  3836. expect(`Hydration class mismatch`).toHaveBeenWarned()
  3837. })
  3838. test('style mismatch', async () => {
  3839. await mountWithHydration(
  3840. `<div style="color:red;"></div>`,
  3841. `<div :style="data"></div>`,
  3842. ref({ color: 'red' }),
  3843. )
  3844. await mountWithHydration(
  3845. `<div style="color:red;"></div>`,
  3846. `<div :style="data"></div>`,
  3847. ref('color:red;'),
  3848. )
  3849. // style with different order
  3850. await mountWithHydration(
  3851. `<div style="color:red; font-size: 12px;"></div>`,
  3852. `<div :style="data"></div>`,
  3853. ref(`font-size: 12px; color:red;`),
  3854. )
  3855. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3856. // single root mismatch
  3857. const { container: root } = await mountWithHydration(
  3858. `<div style="color:red;"></div>`,
  3859. `<div :style="data"></div>`,
  3860. ref({ color: 'green' }),
  3861. )
  3862. expect(root.innerHTML).toBe('<div style="color: green;"></div>')
  3863. expect(`Hydration style mismatch`).toHaveBeenWarned()
  3864. // multiple root mismatch
  3865. const { container } = await mountWithHydration(
  3866. `<div style="color:red;"></div><span/>`,
  3867. `<div :style="data"></div><span/>`,
  3868. ref({ color: 'green' }),
  3869. )
  3870. expect(container.innerHTML).toBe(
  3871. '<div style="color: green;"></div><span></span>',
  3872. )
  3873. expect(`Hydration style mismatch`).toHaveBeenWarned()
  3874. })
  3875. test('style mismatch when no style attribute is present', async () => {
  3876. await mountWithHydration(
  3877. `<div></div>`,
  3878. `<div :style="data"></div>`,
  3879. ref({ color: 'red' }),
  3880. )
  3881. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  3882. })
  3883. test('style mismatch w/ v-show', async () => {
  3884. await mountWithHydration(
  3885. `<div style="color:red;display:none"></div>`,
  3886. `<div v-show="data" style="color: red;"></div>`,
  3887. ref(false),
  3888. )
  3889. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3890. // mismatch with single root
  3891. const { container: root } = await mountWithHydration(
  3892. `<div style="color:red;"></div>`,
  3893. `<div v-show="data" style="color: red;"></div>`,
  3894. ref(false),
  3895. )
  3896. expect(root.innerHTML).toBe(
  3897. '<div style="color: red; display: none;"></div>',
  3898. )
  3899. expect(`Hydration style mismatch`).toHaveBeenWarned()
  3900. // mismatch with multiple root
  3901. const { container } = await mountWithHydration(
  3902. `<div style="color:red;"></div><span/>`,
  3903. `<div v-show="data.show" :style="data.style"></div><span/>`,
  3904. ref({ show: false, style: 'color: red' }),
  3905. )
  3906. expect(container.innerHTML).toBe(
  3907. '<div style="color: red; display: none;"></div><span></span>',
  3908. )
  3909. expect(`Hydration style mismatch`).toHaveBeenWarned()
  3910. })
  3911. test('attr mismatch', async () => {
  3912. await mountWithHydration(
  3913. `<div id="foo"></div>`,
  3914. `<div :id="data"></div>`,
  3915. ref('foo'),
  3916. )
  3917. await mountWithHydration(
  3918. `<div spellcheck></div>`,
  3919. `<div :spellcheck="data"></div>`,
  3920. ref(''),
  3921. )
  3922. await mountWithHydration(
  3923. `<div></div>`,
  3924. `<div :id="data"></div>`,
  3925. ref(undefined),
  3926. )
  3927. // boolean
  3928. await mountWithHydration(
  3929. `<select multiple></div>`,
  3930. `<select :multiple="data"></select>`,
  3931. ref(true),
  3932. )
  3933. await mountWithHydration(
  3934. `<select multiple></div>`,
  3935. `<select :multiple="data"></select>`,
  3936. ref('multiple'),
  3937. )
  3938. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3939. await mountWithHydration(
  3940. `<div></div>`,
  3941. `<div :id="data"></div>`,
  3942. ref('foo'),
  3943. )
  3944. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  3945. await mountWithHydration(
  3946. `<div id="bar"></div>`,
  3947. `<div :id="data"></div>`,
  3948. ref('foo'),
  3949. )
  3950. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  3951. })
  3952. test('attr special case: textarea value', async () => {
  3953. await mountWithHydration(
  3954. `<textarea>foo</textarea>`,
  3955. `<textarea :value="data"></textarea>`,
  3956. ref('foo'),
  3957. )
  3958. await mountWithHydration(
  3959. `<textarea></textarea>`,
  3960. `<textarea :value="data"></textarea>`,
  3961. ref(''),
  3962. )
  3963. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3964. await mountWithHydration(
  3965. `<textarea>foo</textarea>`,
  3966. `<textarea :value="data"></textarea>`,
  3967. ref('bar'),
  3968. )
  3969. expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  3970. })
  3971. test('<textarea> with newlines at the beginning', async () => {
  3972. await mountWithHydration(
  3973. `<textarea>\nhello</textarea>`,
  3974. `<textarea :value="data"></textarea>`,
  3975. ref('\nhello'),
  3976. )
  3977. await mountWithHydration(
  3978. `<textarea>\nhello</textarea>`,
  3979. `<textarea v-text="data"></textarea>`,
  3980. ref('\nhello'),
  3981. )
  3982. await mountWithHydration(
  3983. `<textarea>\nhello</textarea>`,
  3984. `<textarea v-bind="data"></textarea>`,
  3985. ref({ textContent: '\nhello' }),
  3986. )
  3987. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  3988. })
  3989. test('<pre> with newlines at the beginning', async () => {
  3990. await mountWithHydration(`<pre>\n</pre>`, `<pre>{{data}}</pre>`, ref('\n'))
  3991. await mountWithHydration(
  3992. `<pre>\n</pre>`,
  3993. `<pre v-text="data"></pre>`,
  3994. ref('\n'),
  3995. )
  3996. await mountWithHydration(
  3997. `<pre>\n</pre>`,
  3998. `<pre v-bind="data"></pre>`,
  3999. ref({ textContent: '\n' }),
  4000. )
  4001. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4002. })
  4003. test('boolean attr handling', async () => {
  4004. await mountWithHydration(
  4005. `<input />`,
  4006. `<input :readonly="data" />`,
  4007. ref(false),
  4008. )
  4009. await mountWithHydration(
  4010. `<input readonly />`,
  4011. `<input :readonly="data" />`,
  4012. ref(true),
  4013. )
  4014. await mountWithHydration(
  4015. `<input readonly="readonly" />`,
  4016. `<input :readonly="data" />`,
  4017. ref(true),
  4018. )
  4019. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4020. })
  4021. test('client value is null or undefined', async () => {
  4022. await mountWithHydration(
  4023. `<div></div>`,
  4024. `<div :draggable="data"></div>`,
  4025. ref(undefined),
  4026. )
  4027. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4028. await mountWithHydration(`<input />`, `<input :type="data" />`, ref(null))
  4029. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4030. })
  4031. test('should not warn against object values', async () => {
  4032. await mountWithHydration(`<input />`, `<input :from="data" />`, ref({}))
  4033. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4034. })
  4035. test('should not warn on falsy bindings of non-property keys', async () => {
  4036. await mountWithHydration(
  4037. `<button></button>`,
  4038. `<button :href="data"></button>`,
  4039. ref(undefined),
  4040. )
  4041. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4042. })
  4043. test('should not warn on non-renderable option values', async () => {
  4044. await mountWithHydration(
  4045. `<select><option>hello</option></select>`,
  4046. `<select><option :value="data">hello</option></select>`,
  4047. ref(['foo']),
  4048. )
  4049. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4050. })
  4051. test('should not warn css v-bind', async () => {
  4052. const container = document.createElement('div')
  4053. container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  4054. const app = createVaporSSRApp({
  4055. setup() {
  4056. useVaporCssVars(() => ({ foo: 'red' }))
  4057. const n0 = template('<div></div>', true)() as any
  4058. renderEffect(() => setStyle(n0, { color: 'var(--foo)' }))
  4059. return n0
  4060. },
  4061. })
  4062. app.mount(container)
  4063. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4064. })
  4065. test('css vars should only be added to expected on component root dom', () => {
  4066. const container = document.createElement('div')
  4067. container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  4068. const app = createVaporSSRApp({
  4069. setup() {
  4070. useVaporCssVars(() => ({ foo: 'red' }))
  4071. const n0 = template('<div><div></div></div>', true)() as any
  4072. const n1 = child(n0) as any
  4073. renderEffect(() => setStyle(n1, { color: 'var(--foo)' }))
  4074. return n0
  4075. },
  4076. })
  4077. app.mount(container)
  4078. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4079. })
  4080. test('css vars support fallthrough', () => {
  4081. const container = document.createElement('div')
  4082. container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
  4083. const app = createVaporSSRApp({
  4084. setup() {
  4085. useVaporCssVars(() => ({ foo: 'red' }))
  4086. return createComponent(Child)
  4087. },
  4088. })
  4089. const Child = defineVaporComponent({
  4090. setup() {
  4091. const n0 = template('<div></div>', true)() as any
  4092. renderEffect(() => setStyle(n0, { padding: '4px' }))
  4093. return n0
  4094. },
  4095. })
  4096. app.mount(container)
  4097. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4098. })
  4099. // vapor directive does not have a created hook
  4100. test('should not warn for directives that mutate DOM in created', () => {
  4101. // const container = document.createElement('div')
  4102. // container.innerHTML = `<div class="test red"></div>`
  4103. // const vColor: ObjectDirective = {
  4104. // created(el, binding) {
  4105. // el.classList.add(binding.value)
  4106. // },
  4107. // }
  4108. // const app = createSSRApp({
  4109. // setup() {
  4110. // return () =>
  4111. // withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
  4112. // },
  4113. // })
  4114. // app.mount(container)
  4115. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4116. })
  4117. test('escape css var name', () => {
  4118. const container = document.createElement('div')
  4119. container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
  4120. const app = createVaporSSRApp({
  4121. setup() {
  4122. useVaporCssVars(() => ({ 'foo.bar': 'red' }))
  4123. return createComponent(Child)
  4124. },
  4125. })
  4126. const Child = defineVaporComponent({
  4127. setup() {
  4128. const n0 = template('<div></div>', true)() as any
  4129. renderEffect(() => setStyle(n0, { padding: '4px' }))
  4130. return n0
  4131. },
  4132. })
  4133. app.mount(container)
  4134. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4135. })
  4136. })
  4137. describe('data-allow-mismatch', () => {
  4138. test('element text content', async () => {
  4139. const data = ref({ textContent: 'bar' })
  4140. const { container } = await mountWithHydration(
  4141. `<div data-allow-mismatch="text">foo</div>`,
  4142. `<div v-bind="data"></div>`,
  4143. data,
  4144. )
  4145. expect(container.innerHTML).toBe(
  4146. '<div data-allow-mismatch="text">bar</div>',
  4147. )
  4148. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4149. })
  4150. // test('not enough children', () => {
  4151. // const { container } = mountWithHydration(
  4152. // `<div data-allow-mismatch="children"></div>`,
  4153. // () => h('div', [h('span', 'foo'), h('span', 'bar')]),
  4154. // )
  4155. // expect(container.innerHTML).toBe(
  4156. // '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
  4157. // )
  4158. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4159. // })
  4160. // test('too many children', () => {
  4161. // const { container } = mountWithHydration(
  4162. // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  4163. // () => h('div', [h('span', 'foo')]),
  4164. // )
  4165. // expect(container.innerHTML).toBe(
  4166. // '<div data-allow-mismatch="children"><span>foo</span></div>',
  4167. // )
  4168. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4169. // })
  4170. test('complete mismatch', async () => {
  4171. const { container } = await mountWithHydration(
  4172. `<div data-allow-mismatch="children"><div>foo</div></div>`,
  4173. `<div><component :is="data">foo</component></div>`,
  4174. ref('span'),
  4175. )
  4176. expect(container.innerHTML).toBe(
  4177. '<div data-allow-mismatch="children"><span>foo</span><!--dynamic-component--></div>',
  4178. )
  4179. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4180. })
  4181. // test('fragment mismatch removal', () => {
  4182. // const { container } = mountWithHydration(
  4183. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  4184. // () => h('div', [h('span', 'replaced')]),
  4185. // )
  4186. // expect(container.innerHTML).toBe(
  4187. // '<div data-allow-mismatch="children"><span>replaced</span></div>',
  4188. // )
  4189. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4190. // })
  4191. // test('fragment not enough children', () => {
  4192. // const { container } = mountWithHydration(
  4193. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  4194. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  4195. // )
  4196. // expect(container.innerHTML).toBe(
  4197. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  4198. // )
  4199. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4200. // })
  4201. // test('fragment too many children', () => {
  4202. // const { container } = mountWithHydration(
  4203. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  4204. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  4205. // )
  4206. // expect(container.innerHTML).toBe(
  4207. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  4208. // )
  4209. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  4210. // // as 2nd fragment child.
  4211. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4212. // // excessive children removal
  4213. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4214. // })
  4215. // test('comment mismatch (element)', () => {
  4216. // const { container } = mountWithHydration(
  4217. // `<div data-allow-mismatch="children"><span></span></div>`,
  4218. // () => h('div', [createCommentVNode('hi')]),
  4219. // )
  4220. // expect(container.innerHTML).toBe(
  4221. // '<div data-allow-mismatch="children"><!--hi--></div>',
  4222. // )
  4223. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4224. // })
  4225. // test('comment mismatch (text)', () => {
  4226. // const { container } = mountWithHydration(
  4227. // `<div data-allow-mismatch="children">foobar</div>`,
  4228. // () => h('div', [createCommentVNode('hi')]),
  4229. // )
  4230. // expect(container.innerHTML).toBe(
  4231. // '<div data-allow-mismatch="children"><!--hi--></div>',
  4232. // )
  4233. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4234. // })
  4235. test('class mismatch', async () => {
  4236. await mountWithHydration(
  4237. `<div class="foo bar" data-allow-mismatch="class"></div>`,
  4238. `<div :class="data"></div>`,
  4239. ref('foo'),
  4240. )
  4241. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  4242. })
  4243. test('style mismatch', async () => {
  4244. await mountWithHydration(
  4245. `<div style="color:red;" data-allow-mismatch="style"></div>`,
  4246. `<div :style="data"></div>`,
  4247. ref({ color: 'green' }),
  4248. )
  4249. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4250. })
  4251. test('attr mismatch', async () => {
  4252. await mountWithHydration(
  4253. `<div data-allow-mismatch="attribute"></div>`,
  4254. `<div :id="data"></div>`,
  4255. ref('foo'),
  4256. )
  4257. await mountWithHydration(
  4258. `<div id="bar" data-allow-mismatch="attribute"></div>`,
  4259. `<div :id="data"></div>`,
  4260. ref('foo'),
  4261. )
  4262. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4263. })
  4264. })
  4265. describe('VDOM interop', () => {
  4266. test('basic render vapor component', async () => {
  4267. const data = ref(true)
  4268. const { container } = await testWithVDOMApp(
  4269. `<script setup>const data = _data; const components = _components;</script>
  4270. <template>
  4271. <components.VaporChild/>
  4272. </template>`,
  4273. {
  4274. VaporChild: {
  4275. code: `<template>{{ data }}</template>`,
  4276. vapor: true,
  4277. },
  4278. },
  4279. data,
  4280. )
  4281. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
  4282. data.value = false
  4283. await nextTick()
  4284. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
  4285. })
  4286. test('nested components (VDOM -> Vapor -> VDOM)', async () => {
  4287. const data = ref(true)
  4288. const { container } = await testWithVDOMApp(
  4289. `<script setup>const data = _data; const components = _components;</script>
  4290. <template>
  4291. <components.VaporChild/>
  4292. </template>`,
  4293. {
  4294. VaporChild: {
  4295. code: `<template><components.VdomChild/></template>`,
  4296. vapor: true,
  4297. },
  4298. VdomChild: {
  4299. code: `<script setup>const data = _data;</script>
  4300. <template>{{ data }}</template>`,
  4301. vapor: false,
  4302. },
  4303. },
  4304. data,
  4305. )
  4306. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
  4307. data.value = false
  4308. await nextTick()
  4309. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
  4310. })
  4311. test('nested components (VDOM -> Vapor(multi-root) -> VDOM)', async () => {
  4312. const data = ref('foo')
  4313. const { container } = await testWithVDOMApp(
  4314. `<script setup>const data = _data; const components = _components;</script>
  4315. <template>
  4316. <components.VaporChild/>
  4317. </template>`,
  4318. {
  4319. // Vapor component with multiple root nodes, VDOM child as first element
  4320. // This ensures hydration starts at <!--[--> and tests skipFragmentAnchor
  4321. VaporChild: {
  4322. code: `<template><components.VdomChild/><div>second</div></template>`,
  4323. vapor: true,
  4324. },
  4325. VdomChild: {
  4326. code: `<script setup>const data = _data;</script>
  4327. <template><span>{{ data }}</span></template>`,
  4328. vapor: false,
  4329. },
  4330. },
  4331. data,
  4332. )
  4333. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4334. `
  4335. "
  4336. <!--[--><span>foo</span><div>second</div><!--]-->
  4337. "
  4338. `,
  4339. )
  4340. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4341. data.value = 'bar'
  4342. await nextTick()
  4343. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4344. `
  4345. "
  4346. <!--[--><span>bar</span><div>second</div><!--]-->
  4347. "
  4348. `,
  4349. )
  4350. })
  4351. test('nested components (VDOM -> Vapor -> VDOM (with slot fallback))', async () => {
  4352. const data = ref(true)
  4353. const { container } = await testWithVDOMApp(
  4354. `<script setup>const data = _data; const components = _components;</script>
  4355. <template>
  4356. <components.VaporChild/>
  4357. </template>`,
  4358. {
  4359. VaporChild: {
  4360. code: `<template><components.VdomChild/></template>`,
  4361. vapor: true,
  4362. },
  4363. VdomChild: {
  4364. code: `<script setup>const data = _data;</script>
  4365. <template><slot><span>{{data}}</span></slot></template>`,
  4366. vapor: false,
  4367. },
  4368. },
  4369. data,
  4370. )
  4371. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4372. `
  4373. "
  4374. <!--[--><span>true</span><!--]-->
  4375. "
  4376. `,
  4377. )
  4378. data.value = false
  4379. await nextTick()
  4380. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4381. `
  4382. "
  4383. <!--[--><span>false</span><!--]-->
  4384. "
  4385. `,
  4386. )
  4387. })
  4388. test('nested components (VDOM -> Vapor(with slot content) -> VDOM)', async () => {
  4389. const data = ref(true)
  4390. const { container } = await testWithVDOMApp(
  4391. `<script setup>const data = _data; const components = _components;</script>
  4392. <template>
  4393. <components.VaporChild/>
  4394. </template>`,
  4395. {
  4396. VaporChild: {
  4397. code: `<template>
  4398. <components.VdomChild>
  4399. <template #default>
  4400. <span>{{data}} vapor fallback</span>
  4401. </template>
  4402. </components.VdomChild>
  4403. </template>`,
  4404. vapor: true,
  4405. },
  4406. VdomChild: {
  4407. code: `<script setup>const data = _data;</script>
  4408. <template><slot><span>vdom fallback</span></slot></template>`,
  4409. vapor: false,
  4410. },
  4411. },
  4412. data,
  4413. )
  4414. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4415. `
  4416. "
  4417. <!--[--><span>true vapor fallback</span><!--]-->
  4418. "
  4419. `,
  4420. )
  4421. data.value = false
  4422. await nextTick()
  4423. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4424. `
  4425. "
  4426. <!--[--><span>false vapor fallback</span><!--]-->
  4427. "
  4428. `,
  4429. )
  4430. })
  4431. test('nested components (VDOM -> Vapor(with slot content) -> Vapor)', async () => {
  4432. const data = ref(true)
  4433. const { container } = await testWithVDOMApp(
  4434. `<script setup>const data = _data; const components = _components;</script>
  4435. <template>
  4436. <components.VaporChild/>
  4437. </template>`,
  4438. {
  4439. VaporChild: {
  4440. code: `<template>
  4441. <components.VaporChild2>
  4442. <template #default>
  4443. <span>{{data}} vapor fallback</span>
  4444. </template>
  4445. </components.VaporChild2>
  4446. </template>`,
  4447. vapor: true,
  4448. },
  4449. VaporChild2: {
  4450. code: `<template><slot><span>vapor fallback2</span></slot></template>`,
  4451. vapor: true,
  4452. },
  4453. },
  4454. data,
  4455. )
  4456. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4457. `
  4458. "
  4459. <!--[--><span>true vapor fallback</span><!--]-->
  4460. "
  4461. `,
  4462. )
  4463. data.value = false
  4464. await nextTick()
  4465. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4466. `
  4467. "
  4468. <!--[--><span>false vapor fallback</span><!--]-->
  4469. "
  4470. `,
  4471. )
  4472. })
  4473. test('vapor slot render vdom component', async () => {
  4474. const data = ref(true)
  4475. const { container } = await testWithVDOMApp(
  4476. `<script setup>const data = _data; const components = _components;</script>
  4477. <template>
  4478. <components.VaporChild>
  4479. <components.VdomChild/>
  4480. </components.VaporChild>
  4481. </template>`,
  4482. {
  4483. VaporChild: {
  4484. code: `<template><div><slot/></div></template>`,
  4485. vapor: true,
  4486. },
  4487. VdomChild: {
  4488. code: `<script setup>const data = _data;</script>
  4489. <template>{{ data }}</template>`,
  4490. vapor: false,
  4491. },
  4492. },
  4493. data,
  4494. )
  4495. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4496. `
  4497. "<div>
  4498. <!--[-->true<!--]-->
  4499. </div>"
  4500. `,
  4501. )
  4502. data.value = false
  4503. await nextTick()
  4504. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4505. `
  4506. "<div>
  4507. <!--[-->false<!--]-->
  4508. </div>"
  4509. `,
  4510. )
  4511. })
  4512. test('vapor slot render vdom component (multi-root slot content)', async () => {
  4513. const data = ref('foo')
  4514. const { container } = await testWithVaporApp(
  4515. `<script setup>const data = _data; const components = _components;</script>
  4516. <template>
  4517. <components.VaporChild>
  4518. <components.VdomChild/>
  4519. <div>vapor content</div>
  4520. </components.VaporChild>
  4521. </template>`,
  4522. {
  4523. VaporChild: {
  4524. code: `<template><div><slot/></div></template>`,
  4525. vapor: true,
  4526. },
  4527. VdomChild: {
  4528. code: `<script setup>const data = _data;</script>
  4529. <template><span>{{ data }}</span></template>`,
  4530. vapor: false,
  4531. },
  4532. },
  4533. data,
  4534. )
  4535. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4536. `
  4537. "<div>
  4538. <!--[--><span>foo</span><div>vapor content</div><!--]-->
  4539. </div>"
  4540. `,
  4541. )
  4542. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4543. data.value = 'bar'
  4544. await nextTick()
  4545. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4546. `
  4547. "<div>
  4548. <!--[--><span>bar</span><div>vapor content</div><!--]-->
  4549. </div>"
  4550. `,
  4551. )
  4552. })
  4553. test('vapor slot render vdom component (render function)', async () => {
  4554. const data = ref(true)
  4555. const { container } = await testWithVaporApp(
  4556. `<script setup>
  4557. import { h } from 'vue'
  4558. const data = _data; const components = _components;
  4559. const VdomChild = {
  4560. setup() {
  4561. return () => h('div', null, [h('div', [String(data.value)])])
  4562. }
  4563. }
  4564. </script>
  4565. <template>
  4566. <components.VaporChild>
  4567. <VdomChild/>
  4568. </components.VaporChild>
  4569. </template>`,
  4570. {
  4571. VaporChild: {
  4572. code: `<template><div><slot/></div></template>`,
  4573. vapor: true,
  4574. },
  4575. },
  4576. data,
  4577. )
  4578. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4579. `
  4580. "<div>
  4581. <!--[--><div><div>true</div></div><!--]-->
  4582. </div>"
  4583. `,
  4584. )
  4585. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4586. data.value = false
  4587. await nextTick()
  4588. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4589. `
  4590. "<div>
  4591. <!--[--><div><div>false</div></div><!--]-->
  4592. </div>"
  4593. `,
  4594. )
  4595. })
  4596. test('hydrate VNode rendered via createDynamicComponent', async () => {
  4597. const data = ref('foo')
  4598. const { container } = await testWithVaporApp(
  4599. `<script setup>
  4600. import { h } from 'vue'
  4601. const data = _data; const components = _components;
  4602. // Simulating RouterView pattern: VDOM component passes VNode through slot
  4603. const RouterView = {
  4604. setup(_, { slots }) {
  4605. return () => {
  4606. const component = h(components.VaporChild)
  4607. return slots.default({ Component: component })
  4608. }
  4609. }
  4610. }
  4611. </script>
  4612. <template>
  4613. <RouterView v-slot="{ Component }">
  4614. <component :is="Component" />
  4615. </RouterView>
  4616. </template>`,
  4617. {
  4618. VaporChild: {
  4619. code: `<template><div>{{ data }}</div></template>`,
  4620. vapor: true,
  4621. },
  4622. },
  4623. data,
  4624. )
  4625. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4626. `
  4627. "
  4628. <!--[--><div>foo</div><!--dynamic-component--><!--]-->
  4629. "
  4630. `,
  4631. )
  4632. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4633. data.value = 'bar'
  4634. await nextTick()
  4635. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4636. `
  4637. "
  4638. <!--[--><div>bar</div><!--dynamic-component--><!--]-->
  4639. "
  4640. `,
  4641. )
  4642. })
  4643. test('hydrate VDOM slot content', async () => {
  4644. const data = ref('foo')
  4645. const { container } = await testWithVaporApp(
  4646. `<script setup>
  4647. const data = _data; const components = _components;
  4648. </script>
  4649. <template>
  4650. <components.VdomWrapper>
  4651. <div>{{ data }}</div>
  4652. </components.VdomWrapper>
  4653. </template>`,
  4654. {
  4655. VdomWrapper: {
  4656. code: `<script setup>const data = _data;</script>
  4657. <template><slot /></template>`,
  4658. vapor: false,
  4659. },
  4660. },
  4661. data,
  4662. )
  4663. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4664. `
  4665. "
  4666. <!--[--><div>foo</div><!--]-->
  4667. "
  4668. `,
  4669. )
  4670. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4671. data.value = 'bar'
  4672. await nextTick()
  4673. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4674. `
  4675. "
  4676. <!--[--><div>bar</div><!--]-->
  4677. "
  4678. `,
  4679. )
  4680. })
  4681. test('hydrate VDOM slot fallback', async () => {
  4682. const data = ref('foo')
  4683. const { container } = await testWithVaporApp(
  4684. `<script setup>
  4685. const data = _data; const components = _components;
  4686. </script>
  4687. <template>
  4688. <components.VdomWrapper />
  4689. </template>`,
  4690. {
  4691. VdomWrapper: {
  4692. code: `<script setup>const data = _data;</script>
  4693. <template><slot><div>{{ data }}</div></slot></template>`,
  4694. vapor: false,
  4695. },
  4696. },
  4697. data,
  4698. )
  4699. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4700. `
  4701. "
  4702. <!--[--><div>foo</div><!--]-->
  4703. "
  4704. `,
  4705. )
  4706. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4707. data.value = 'bar'
  4708. await nextTick()
  4709. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4710. `
  4711. "
  4712. <!--[--><div>bar</div><!--]-->
  4713. "
  4714. `,
  4715. )
  4716. })
  4717. test('hydrate VDOM component returning Fragment', async () => {
  4718. const data = ref('foo')
  4719. const { container } = await testWithVaporApp(
  4720. `<script setup>
  4721. const data = _data; const components = _components;
  4722. </script>
  4723. <template>
  4724. <components.VdomFragmentComp />
  4725. </template>`,
  4726. {
  4727. // VDOM component that returns a Fragment (multiple root nodes)
  4728. VdomFragmentComp: {
  4729. code: `<script setup>const data = _data;</script>
  4730. <template><div>first {{ data }}</div><div>second {{ data }}</div></template>`,
  4731. vapor: false,
  4732. },
  4733. },
  4734. data,
  4735. )
  4736. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4737. `
  4738. "
  4739. <!--[--><div>first foo</div><div>second foo</div><!--]-->
  4740. "
  4741. `,
  4742. )
  4743. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4744. data.value = 'bar'
  4745. await nextTick()
  4746. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4747. `
  4748. "
  4749. <!--[--><div>first bar</div><div>second bar</div><!--]-->
  4750. "
  4751. `,
  4752. )
  4753. })
  4754. })