hydration.spec.ts 204 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363
  1. import {
  2. VaporTeleport,
  3. child,
  4. createComponent,
  5. createPlainElement,
  6. createVaporSSRApp,
  7. defineVaporAsyncComponent,
  8. defineVaporComponent,
  9. delegateEvents,
  10. renderEffect,
  11. setStyle,
  12. setText,
  13. template,
  14. useVaporCssVars,
  15. } from '../src'
  16. import { defineAsyncComponent, nextTick, reactive, ref } from '@vue/runtime-dom'
  17. import { isString } from '@vue/shared'
  18. import type { VaporComponentInstance } from '../src/component'
  19. import type { TeleportFragment } from '../src/components/Teleport'
  20. import { VueServerRenderer, compile, runtimeDom, runtimeVapor } from './_utils'
  21. import { setIsHydratingEnabled } from '../src/dom/hydration'
  22. const formatHtml = (raw: string) => {
  23. return raw
  24. .replace(/<!--\[/g, '\n<!--[')
  25. .replace(/]-->/g, ']-->\n')
  26. .replace(/<!--teleport (start|end)-->/g, '\n<!--teleport $1-->\n')
  27. .replace(/\n{2,}/g, '\n')
  28. }
  29. async function testWithVaporApp(
  30. code: string,
  31. components?: Record<string, string | { code: string; vapor: boolean }>,
  32. data?: any,
  33. ) {
  34. return testHydration(code, components, data, {
  35. isVaporApp: true,
  36. interop: true,
  37. })
  38. }
  39. async function testWithVDOMApp(
  40. code: string,
  41. components?: Record<string, string | { code: string; vapor: boolean }>,
  42. data?: any,
  43. ) {
  44. return testHydration(code, components, data, {
  45. isVaporApp: false,
  46. interop: true,
  47. })
  48. }
  49. function compileVaporComponent(
  50. code: string,
  51. data: runtimeDom.Ref<any> = ref({}),
  52. components?: Record<string, any>,
  53. ssr = false,
  54. ) {
  55. if (!code.includes(`<script`)) {
  56. code = `<template>${code}</template>`
  57. }
  58. return compile(code, data, components, {
  59. vapor: true,
  60. ssr,
  61. })
  62. }
  63. async function mountWithHydration(
  64. html: string,
  65. code: string,
  66. data: runtimeDom.Ref<any> = ref({}),
  67. components?: Record<string, any>,
  68. ) {
  69. const container = document.createElement('div')
  70. container.innerHTML = html
  71. document.body.appendChild(container)
  72. const clientComp = compileVaporComponent(code, data, components)
  73. const app = createVaporSSRApp(clientComp)
  74. app.mount(container)
  75. return {
  76. block: (app._instance! as VaporComponentInstance).block,
  77. container,
  78. }
  79. }
  80. async function testHydration(
  81. code: string,
  82. components: Record<string, string | { code: string; vapor: boolean }> = {},
  83. data: any = ref('foo'),
  84. { isVaporApp = true, interop = false } = {},
  85. ) {
  86. const ssrComponents: any = {}
  87. const clientComponents: any = {}
  88. for (const key in components) {
  89. const comp = components[key]
  90. const code = isString(comp) ? comp : comp.code
  91. const isVaporComp = isString(comp) || !!comp.vapor
  92. clientComponents[key] = compile(code, data, clientComponents, {
  93. vapor: isVaporComp,
  94. ssr: false,
  95. })
  96. ssrComponents[key] = compile(code, data, ssrComponents, {
  97. vapor: isVaporComp,
  98. ssr: true,
  99. })
  100. }
  101. const serverComp = compile(code, data, ssrComponents, {
  102. vapor: isVaporApp,
  103. ssr: true,
  104. })
  105. const html = await VueServerRenderer.renderToString(
  106. runtimeDom.createSSRApp(serverComp),
  107. )
  108. const container = document.createElement('div')
  109. document.body.appendChild(container)
  110. container.innerHTML = html
  111. const clientComp = compile(code, data, clientComponents, {
  112. vapor: isVaporApp,
  113. ssr: false,
  114. })
  115. let app
  116. if (isVaporApp) {
  117. app = createVaporSSRApp(clientComp)
  118. } else {
  119. app = runtimeDom.createSSRApp(clientComp)
  120. }
  121. if (interop) {
  122. app.use(runtimeVapor.vaporInteropPlugin)
  123. }
  124. app.mount(container)
  125. return { data, container }
  126. }
  127. const triggerEvent = (type: string, el: Element) => {
  128. const event = new Event(type, { bubbles: true })
  129. el.dispatchEvent(event)
  130. }
  131. delegateEvents('click')
  132. beforeEach(() => {
  133. document.body.innerHTML = ''
  134. })
  135. describe('Vapor Mode hydration', () => {
  136. describe('text', () => {
  137. test('root text', async () => {
  138. const { data, container } = await testHydration(`
  139. <template>{{ data }}</template>
  140. `)
  141. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foo"`)
  142. data.value = 'bar'
  143. await nextTick()
  144. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"bar"`)
  145. })
  146. test('consecutive text nodes', async () => {
  147. const { data, container } = await testHydration(`
  148. <template>{{ data }}{{ data }}</template>
  149. `)
  150. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foofoo"`)
  151. data.value = 'bar'
  152. await nextTick()
  153. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"barbar"`)
  154. })
  155. test('consecutive text nodes with insertion anchor', async () => {
  156. const { data, container } = await testHydration(`
  157. <template><span/>{{ data }}{{ data }}<span/></template>
  158. `)
  159. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  160. `
  161. "
  162. <!--[--><span></span>foofoo<span></span><!--]-->
  163. "
  164. `,
  165. )
  166. data.value = 'bar'
  167. await nextTick()
  168. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  169. `
  170. "
  171. <!--[--><span></span>barbar<span></span><!--]-->
  172. "
  173. `,
  174. )
  175. })
  176. test('mixed text nodes', async () => {
  177. const { data, container } = await testHydration(`
  178. <template>{{ data }}A{{ data }}B{{ data }}</template>
  179. `)
  180. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  181. `"fooAfooBfoo"`,
  182. )
  183. data.value = 'bar'
  184. await nextTick()
  185. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  186. `"barAbarBbar"`,
  187. )
  188. })
  189. test('mixed text nodes with insertion anchor', async () => {
  190. const { data, container } = await testHydration(`
  191. <template><span/>{{ data }}A{{ data }}B{{ data }}<span/></template>
  192. `)
  193. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  194. `
  195. "
  196. <!--[--><span></span>fooAfooBfoo<span></span><!--]-->
  197. "
  198. `,
  199. )
  200. data.value = 'bar'
  201. await nextTick()
  202. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  203. `
  204. "
  205. <!--[--><span></span>barAbarBbar<span></span><!--]-->
  206. "
  207. `,
  208. )
  209. })
  210. test('empty text node', async () => {
  211. const data = reactive({ txt: '' })
  212. const { container } = await testHydration(
  213. `<template><div>{{ data.txt }}</div></template>`,
  214. undefined,
  215. data,
  216. )
  217. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  218. `"<div></div>"`,
  219. )
  220. data.txt = 'foo'
  221. await nextTick()
  222. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  223. `"<div>foo</div>"`,
  224. )
  225. })
  226. test('empty text node in slot', async () => {
  227. const data = reactive({ txt: '' })
  228. const { container } = await testHydration(
  229. `<template><components.Child>{{data.txt}}</components.Child></template>`,
  230. {
  231. Child: `<template><slot/></template>`,
  232. },
  233. data,
  234. )
  235. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  236. `
  237. "
  238. <!--[--><!--]-->
  239. "
  240. `,
  241. )
  242. data.txt = 'foo'
  243. await nextTick()
  244. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  245. `
  246. "
  247. <!--[-->foo<!--]-->
  248. "
  249. `,
  250. )
  251. })
  252. })
  253. describe('element', () => {
  254. test('root comment', async () => {
  255. const { container } = await testHydration(`
  256. <template><!----></template>
  257. `)
  258. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"<!---->"`)
  259. expect(`mismatch in <div>`).not.toHaveBeenWarned()
  260. })
  261. test('root with mixed element and text', async () => {
  262. const { container, data } = await testHydration(`
  263. <template> A<span>{{ data }}</span>{{ data }}</template>
  264. `)
  265. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  266. `
  267. "
  268. <!--[--> A<span>foo</span>foo<!--]-->
  269. "
  270. `,
  271. )
  272. data.value = 'bar'
  273. await nextTick()
  274. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  275. `
  276. "
  277. <!--[--> A<span>bar</span>bar<!--]-->
  278. "
  279. `,
  280. )
  281. })
  282. test('empty element', async () => {
  283. const { container } = await testHydration(`
  284. <template><div/></template>
  285. `)
  286. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  287. `"<div></div>"`,
  288. )
  289. expect(`mismatch in <div>`).not.toHaveBeenWarned()
  290. })
  291. test('element with binding and text children', async () => {
  292. const { container, data } = await testHydration(`
  293. <template><div :class="data">{{ data }}</div></template>
  294. `)
  295. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  296. `"<div class="foo">foo</div>"`,
  297. )
  298. data.value = 'bar'
  299. await nextTick()
  300. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  301. `"<div class="bar">bar</div>"`,
  302. )
  303. })
  304. test('element with elements children', async () => {
  305. const { container } = await testHydration(`
  306. <template>
  307. <div>
  308. <span>{{ data }}</span>
  309. <span :class="data" @click="data = 'bar'"/>
  310. </div>
  311. </template>
  312. `)
  313. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  314. `"<div><span>foo</span><span class="foo"></span></div>"`,
  315. )
  316. // event handler
  317. triggerEvent('click', container.querySelector('.foo')!)
  318. await nextTick()
  319. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  320. `"<div><span>bar</span><span class="bar"></span></div>"`,
  321. )
  322. })
  323. test('element with ref', async () => {
  324. const { data, container } = await testHydration(
  325. `<template>
  326. <div ref="data">hi</div>
  327. </template>
  328. `,
  329. {},
  330. ref(null),
  331. )
  332. expect(data.value).toBe(container.firstChild)
  333. })
  334. })
  335. describe('component', () => {
  336. test('basic component', async () => {
  337. const { container, data } = await testHydration(
  338. `
  339. <template><div><span></span><components.Child/></div></template>
  340. `,
  341. { Child: `<template>{{ data }}</template>` },
  342. )
  343. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  344. `"<div><span></span>foo</div>"`,
  345. )
  346. data.value = 'bar'
  347. await nextTick()
  348. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  349. `"<div><span></span>bar</div>"`,
  350. )
  351. })
  352. test('fragment component', async () => {
  353. const { container, data } = await testHydration(
  354. `
  355. <template><div><span></span><components.Child/></div></template>
  356. `,
  357. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  358. )
  359. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  360. `
  361. "<div><span></span>
  362. <!--[--><div>foo</div>-foo-<!--]-->
  363. </div>"
  364. `,
  365. )
  366. data.value = 'bar'
  367. await nextTick()
  368. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  369. `
  370. "<div><span></span>
  371. <!--[--><div>bar</div>-bar-<!--]-->
  372. </div>"
  373. `,
  374. )
  375. })
  376. test('fragment component with prepend', async () => {
  377. const { container, data } = await testHydration(
  378. `
  379. <template><div><components.Child/><span></span></div></template>
  380. `,
  381. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  382. )
  383. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  384. `
  385. "<div>
  386. <!--[--><div>foo</div>-foo-<!--]-->
  387. <span></span></div>"
  388. `,
  389. )
  390. data.value = 'bar'
  391. await nextTick()
  392. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  393. `
  394. "<div>
  395. <!--[--><div>bar</div>-bar-<!--]-->
  396. <span></span></div>"
  397. `,
  398. )
  399. })
  400. test('nested fragment components', async () => {
  401. const { container, data } = await testHydration(
  402. `
  403. <template><div><components.Parent/><span></span></div></template>
  404. `,
  405. {
  406. Parent: `<template><div/><components.Child/><div/></template>`,
  407. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  408. },
  409. )
  410. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  411. `
  412. "<div>
  413. <!--[--><div></div>
  414. <!--[--><div>foo</div>-foo-<!--]-->
  415. <div></div><!--]-->
  416. <span></span></div>"
  417. `,
  418. )
  419. data.value = 'bar'
  420. await nextTick()
  421. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  422. `
  423. "<div>
  424. <!--[--><div></div>
  425. <!--[--><div>bar</div>-bar-<!--]-->
  426. <div></div><!--]-->
  427. <span></span></div>"
  428. `,
  429. )
  430. })
  431. test('component with insertion anchor', async () => {
  432. const { container, data } = await testHydration(
  433. `<template>
  434. <div>
  435. <span/>
  436. <components.Child/>
  437. <span/>
  438. </div>
  439. </template>
  440. `,
  441. {
  442. Child: `<template>{{ data }}</template>`,
  443. },
  444. )
  445. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  446. `"<div><span></span>foo<span></span></div>"`,
  447. )
  448. data.value = 'bar'
  449. await nextTick()
  450. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  451. `"<div><span></span>bar<span></span></div>"`,
  452. )
  453. })
  454. test('nested components with insertion anchor', async () => {
  455. const { container, data } = await testHydration(
  456. `
  457. <template><components.Parent/></template>
  458. `,
  459. {
  460. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  461. Child: `<template><div>{{ data }}</div></template>`,
  462. },
  463. )
  464. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  465. `"<div><span></span><div>foo</div><span></span></div>"`,
  466. )
  467. data.value = 'bar'
  468. await nextTick()
  469. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  470. `"<div><span></span><div>bar</div><span></span></div>"`,
  471. )
  472. })
  473. test('nested components with multi level anchor insertion', async () => {
  474. const { container, data } = await testHydration(
  475. `
  476. <template><div><span></span><components.Parent/><span></span></div></template>
  477. `,
  478. {
  479. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  480. Child: `<template><div>{{ data }}</div></template>`,
  481. },
  482. )
  483. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  484. `"<div><span></span><div><span></span><div>foo</div><span></span></div><span></span></div>"`,
  485. )
  486. data.value = 'bar'
  487. await nextTick()
  488. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  489. `"<div><span></span><div><span></span><div>bar</div><span></span></div><span></span></div>"`,
  490. )
  491. })
  492. test('consecutive components with insertion parent', async () => {
  493. const data = reactive({ foo: 'foo', bar: 'bar' })
  494. const { container } = await testHydration(
  495. `<template>
  496. <div>
  497. <components.Child1/>
  498. <components.Child2/>
  499. </div>
  500. </template>
  501. `,
  502. {
  503. Child1: `<template><span>{{ data.foo }}</span></template>`,
  504. Child2: `<template><span>{{ data.bar }}</span></template>`,
  505. },
  506. data,
  507. )
  508. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  509. `"<div><span>foo</span><span>bar</span></div>"`,
  510. )
  511. data.foo = 'foo1'
  512. data.bar = 'bar1'
  513. await nextTick()
  514. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  515. `"<div><span>foo1</span><span>bar1</span></div>"`,
  516. )
  517. })
  518. test('nested consecutive components with insertion anchor', async () => {
  519. const { container, data } = await testHydration(
  520. `
  521. <template><components.Parent/></template>
  522. `,
  523. {
  524. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  525. Child: `<template><div>{{ data }}</div></template>`,
  526. },
  527. )
  528. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  529. `"<div><span></span><div>foo</div><div>foo</div><span></span></div>"`,
  530. )
  531. data.value = 'bar'
  532. await nextTick()
  533. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  534. `"<div><span></span><div>bar</div><div>bar</div><span></span></div>"`,
  535. )
  536. })
  537. test('nested consecutive components with multi level anchor insertion', async () => {
  538. const { container, data } = await testHydration(
  539. `
  540. <template><div><span></span><components.Parent/><span></span></div></template>
  541. `,
  542. {
  543. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  544. Child: `<template><div>{{ data }}</div></template>`,
  545. },
  546. )
  547. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  548. `"<div><span></span><div><span></span><div>foo</div><div>foo</div><span></span></div><span></span></div>"`,
  549. )
  550. data.value = 'bar'
  551. await nextTick()
  552. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  553. `"<div><span></span><div><span></span><div>bar</div><div>bar</div><span></span></div><span></span></div>"`,
  554. )
  555. })
  556. test('mixed component and element with insertion anchor', async () => {
  557. const { container, data } = await testHydration(
  558. `<template>
  559. <div>
  560. <span/>
  561. <components.Child/>
  562. <span/>
  563. <components.Child/>
  564. <span/>
  565. </div>
  566. </template>
  567. `,
  568. {
  569. Child: `<template>{{ data }}</template>`,
  570. },
  571. )
  572. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  573. `"<div><span></span>foo<span></span>foo<span></span></div>"`,
  574. )
  575. data.value = 'bar'
  576. await nextTick()
  577. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  578. `"<div><span></span>bar<span></span>bar<span></span></div>"`,
  579. )
  580. })
  581. test('fragment component with insertion anchor', async () => {
  582. const { container, data } = await testHydration(
  583. `<template>
  584. <div>
  585. <span/>
  586. <components.Child/>
  587. <span/>
  588. </div>
  589. </template>
  590. `,
  591. {
  592. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  593. },
  594. )
  595. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  596. `
  597. "<div><span></span>
  598. <!--[--><div>foo</div>-foo<!--]-->
  599. <span></span></div>"
  600. `,
  601. )
  602. data.value = 'bar'
  603. await nextTick()
  604. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  605. `
  606. "<div><span></span>
  607. <!--[--><div>bar</div>-bar<!--]-->
  608. <span></span></div>"
  609. `,
  610. )
  611. })
  612. test('nested fragment component with insertion anchor', async () => {
  613. const { container, data } = await testHydration(
  614. `
  615. <template><components.Parent/></template>
  616. `,
  617. {
  618. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  619. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  620. },
  621. )
  622. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  623. `
  624. "<div><span></span>
  625. <!--[--><div>foo</div>-foo-<!--]-->
  626. <span></span></div>"
  627. `,
  628. )
  629. data.value = 'bar'
  630. await nextTick()
  631. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  632. `
  633. "<div><span></span>
  634. <!--[--><div>bar</div>-bar-<!--]-->
  635. <span></span></div>"
  636. `,
  637. )
  638. })
  639. test('nested fragment component with multi level anchor insertion', async () => {
  640. const { container, data } = await testHydration(
  641. `
  642. <template><div><span/><components.Parent/><span/></div></template>
  643. `,
  644. {
  645. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  646. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  647. },
  648. )
  649. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  650. `
  651. "<div><span></span><div><span></span>
  652. <!--[--><div>foo</div>-foo-<!--]-->
  653. <span></span></div><span></span></div>"
  654. `,
  655. )
  656. data.value = 'bar'
  657. await nextTick()
  658. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  659. `
  660. "<div><span></span><div><span></span>
  661. <!--[--><div>bar</div>-bar-<!--]-->
  662. <span></span></div><span></span></div>"
  663. `,
  664. )
  665. })
  666. test('consecutive fragment components with insertion anchor', async () => {
  667. const { container, data } = await testHydration(
  668. `<template>
  669. <div>
  670. <span/>
  671. <components.Child/>
  672. <components.Child/>
  673. <span/>
  674. </div>
  675. </template>
  676. `,
  677. {
  678. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  679. },
  680. )
  681. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  682. `
  683. "<div><span></span>
  684. <!--[--><div>foo</div>-foo<!--]-->
  685. <!--[--><div>foo</div>-foo<!--]-->
  686. <span></span></div>"
  687. `,
  688. )
  689. data.value = 'bar'
  690. await nextTick()
  691. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  692. `
  693. "<div><span></span>
  694. <!--[--><div>bar</div>-bar<!--]-->
  695. <!--[--><div>bar</div>-bar<!--]-->
  696. <span></span></div>"
  697. `,
  698. )
  699. })
  700. test('nested consecutive fragment components with insertion anchor', async () => {
  701. const { container, data } = await testHydration(
  702. `
  703. <template><components.Parent/></template>
  704. `,
  705. {
  706. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  707. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  708. },
  709. )
  710. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  711. `
  712. "<div><span></span>
  713. <!--[--><div>foo</div>-foo-<!--]-->
  714. <!--[--><div>foo</div>-foo-<!--]-->
  715. <span></span></div>"
  716. `,
  717. )
  718. data.value = 'bar'
  719. await nextTick()
  720. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  721. `
  722. "<div><span></span>
  723. <!--[--><div>bar</div>-bar-<!--]-->
  724. <!--[--><div>bar</div>-bar-<!--]-->
  725. <span></span></div>"
  726. `,
  727. )
  728. })
  729. test('nested consecutive fragment components with multi level anchor insertion', async () => {
  730. const { container, data } = await testHydration(
  731. `
  732. <template><div><span></span><components.Parent/><span></span></div></template>
  733. `,
  734. {
  735. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  736. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  737. },
  738. )
  739. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  740. `
  741. "<div><span></span><div><span></span>
  742. <!--[--><div>foo</div>-foo-<!--]-->
  743. <!--[--><div>foo</div>-foo-<!--]-->
  744. <span></span></div><span></span></div>"
  745. `,
  746. )
  747. data.value = 'bar'
  748. await nextTick()
  749. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  750. `
  751. "<div><span></span><div><span></span>
  752. <!--[--><div>bar</div>-bar-<!--]-->
  753. <!--[--><div>bar</div>-bar-<!--]-->
  754. <span></span></div><span></span></div>"
  755. `,
  756. )
  757. })
  758. test('nested consecutive fragment components with root level anchor insertion', async () => {
  759. const { container, data } = await testHydration(
  760. `
  761. <template><div><span></span><components.Parent/><span></span></div></template>
  762. `,
  763. {
  764. Parent: `<template><components.Child/><components.Child/></template>`,
  765. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  766. },
  767. )
  768. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  769. `
  770. "<div><span></span>
  771. <!--[-->
  772. <!--[--><div>foo</div>-foo-<!--]-->
  773. <!--[--><div>foo</div>-foo-<!--]-->
  774. <!--]-->
  775. <span></span></div>"
  776. `,
  777. )
  778. data.value = 'bar'
  779. await nextTick()
  780. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  781. `
  782. "<div><span></span>
  783. <!--[-->
  784. <!--[--><div>bar</div>-bar-<!--]-->
  785. <!--[--><div>bar</div>-bar-<!--]-->
  786. <!--]-->
  787. <span></span></div>"
  788. `,
  789. )
  790. })
  791. test('mixed fragment component and element with insertion anchor', async () => {
  792. const { container, data } = await testHydration(
  793. `<template>
  794. <div>
  795. <span/>
  796. <components.Child/>
  797. <span/>
  798. <components.Child/>
  799. <span/>
  800. </div>
  801. </template>
  802. `,
  803. {
  804. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  805. },
  806. )
  807. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  808. `
  809. "<div><span></span>
  810. <!--[--><div>foo</div>-foo<!--]-->
  811. <span></span>
  812. <!--[--><div>foo</div>-foo<!--]-->
  813. <span></span></div>"
  814. `,
  815. )
  816. data.value = 'bar'
  817. await nextTick()
  818. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  819. `
  820. "<div><span></span>
  821. <!--[--><div>bar</div>-bar<!--]-->
  822. <span></span>
  823. <!--[--><div>bar</div>-bar<!--]-->
  824. <span></span></div>"
  825. `,
  826. )
  827. })
  828. test('mixed fragment component and text with insertion anchor', async () => {
  829. const { container, data } = await testHydration(
  830. `<template>
  831. <div>
  832. <span/>
  833. <components.Child/>
  834. {{ data }}
  835. <components.Child/>
  836. <span/>
  837. </div>
  838. </template>
  839. `,
  840. {
  841. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  842. },
  843. )
  844. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  845. `
  846. "<div><span></span>
  847. <!--[--><div>foo</div>-foo<!--]-->
  848. foo
  849. <!--[--><div>foo</div>-foo<!--]-->
  850. <span></span></div>"
  851. `,
  852. )
  853. data.value = 'bar'
  854. await nextTick()
  855. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  856. `
  857. "<div><span></span>
  858. <!--[--><div>bar</div>-bar<!--]-->
  859. bar
  860. <!--[--><div>bar</div>-bar<!--]-->
  861. <span></span></div>"
  862. `,
  863. )
  864. })
  865. })
  866. describe('dynamic component', () => {
  867. test('basic dynamic component', async () => {
  868. const { container, data } = await testHydration(
  869. `<template>
  870. <component :is="components[data]"/>
  871. </template>`,
  872. {
  873. foo: `<template><div>foo</div></template>`,
  874. bar: `<template><div>bar</div></template>`,
  875. },
  876. ref('foo'),
  877. )
  878. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  879. `"<div>foo</div><!--dynamic-component-->"`,
  880. )
  881. data.value = 'bar'
  882. await nextTick()
  883. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  884. `"<div>bar</div><!--dynamic-component-->"`,
  885. )
  886. })
  887. test('dynamic component with insertion anchor', async () => {
  888. const { container, data } = await testHydration(
  889. `<template>
  890. <div>
  891. <span/>
  892. <component :is="components[data]"/>
  893. <span/>
  894. </div>
  895. </template>`,
  896. {
  897. foo: `<template><div>foo</div></template>`,
  898. bar: `<template><div>bar</div></template>`,
  899. },
  900. ref('foo'),
  901. )
  902. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  903. `"<div><span></span><div>foo</div><!--dynamic-component--><span></span></div>"`,
  904. )
  905. data.value = 'bar'
  906. await nextTick()
  907. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  908. `"<div><span></span><div>bar</div><!--dynamic-component--><span></span></div>"`,
  909. )
  910. })
  911. test('consecutive dynamic components with insertion anchor', async () => {
  912. const { container, data } = await testHydration(
  913. `<template>
  914. <div>
  915. <span/>
  916. <component :is="components[data]"/>
  917. <component :is="components[data]"/>
  918. <span/>
  919. </div>
  920. </template>`,
  921. {
  922. foo: `<template><div>foo</div></template>`,
  923. bar: `<template><div>bar</div></template>`,
  924. },
  925. ref('foo'),
  926. )
  927. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  928. `"<div><span></span><div>foo</div><!--dynamic-component--><div>foo</div><!--dynamic-component--><span></span></div>"`,
  929. )
  930. data.value = 'bar'
  931. await nextTick()
  932. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  933. `"<div><span></span><div>bar</div><!--dynamic-component--><div>bar</div><!--dynamic-component--><span></span></div>"`,
  934. )
  935. })
  936. test('dynamic component fallback', async () => {
  937. const { container, data } = await testHydration(
  938. `<template>
  939. <component :is="'button'">
  940. <span>{{ data }}</span>
  941. </component>
  942. </template>`,
  943. {},
  944. ref('foo'),
  945. )
  946. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  947. `"<button><span>foo</span></button><!--dynamic-component-->"`,
  948. )
  949. data.value = 'bar'
  950. await nextTick()
  951. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  952. `"<button><span>bar</span></button><!--dynamic-component-->"`,
  953. )
  954. })
  955. test('in ssr slot vnode fallback', async () => {
  956. const { container, data } = await testHydration(
  957. `<template>
  958. <components.Child>
  959. <span>{{ data }}</span>
  960. </components.Child>
  961. </template>`,
  962. {
  963. Child: `
  964. <template>
  965. <component :is="'div'">
  966. <slot />
  967. </component>
  968. </template>`,
  969. },
  970. ref('foo'),
  971. )
  972. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  973. `
  974. "<div>
  975. <!--[--><span>foo</span><!--]-->
  976. </div><!--dynamic-component-->"
  977. `,
  978. )
  979. data.value = 'bar'
  980. await nextTick()
  981. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  982. `
  983. "<div>
  984. <!--[--><span>bar</span><!--]-->
  985. </div><!--dynamic-component-->"
  986. `,
  987. )
  988. })
  989. test('dynamic component fallback with dynamic slots', async () => {
  990. const data = ref({
  991. name: 'default',
  992. msg: 'foo',
  993. })
  994. const { container } = await testHydration(
  995. `<template>
  996. <component :is="'div'">
  997. <template v-slot:[data.name]>
  998. <span>{{ data.msg }}</span>
  999. </template>
  1000. </component>
  1001. </template>`,
  1002. {},
  1003. data,
  1004. )
  1005. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1006. `"<div><span>foo</span><!----></div><!--dynamic-component-->"`,
  1007. )
  1008. data.value.msg = 'bar'
  1009. await nextTick()
  1010. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1011. `"<div><span>bar</span><!----></div><!--dynamic-component-->"`,
  1012. )
  1013. })
  1014. })
  1015. describe('if', () => {
  1016. test('basic toggle - true -> false', async () => {
  1017. const data = ref(true)
  1018. const { container } = await testHydration(
  1019. `<template>
  1020. <div v-if="data">foo</div>
  1021. </template>`,
  1022. undefined,
  1023. data,
  1024. )
  1025. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1026. `"<div>foo</div><!--if-->"`,
  1027. )
  1028. data.value = false
  1029. await nextTick()
  1030. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1031. `"<!--if-->"`,
  1032. )
  1033. })
  1034. test('basic toggle - false -> true', async () => {
  1035. const data = ref(false)
  1036. const { container } = await testHydration(
  1037. `<template>
  1038. <div v-if="data">foo</div>
  1039. </template>`,
  1040. undefined,
  1041. data,
  1042. )
  1043. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1044. `"<!--if-->"`,
  1045. )
  1046. data.value = true
  1047. await nextTick()
  1048. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1049. `"<div>foo</div><!--if-->"`,
  1050. )
  1051. })
  1052. test('v-if on insertion parent', async () => {
  1053. const data = ref(true)
  1054. const { container } = await testHydration(
  1055. `<template>
  1056. <div v-if="data">
  1057. <components.Child/>
  1058. </div>
  1059. </template>`,
  1060. { Child: `<template>foo</template>` },
  1061. data,
  1062. )
  1063. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1064. `"<div>foo</div><!--if-->"`,
  1065. )
  1066. data.value = false
  1067. await nextTick()
  1068. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1069. `"<!--if-->"`,
  1070. )
  1071. data.value = true
  1072. await nextTick()
  1073. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1074. `"<div>foo</div><!--if-->"`,
  1075. )
  1076. })
  1077. test('v-if/else-if/else chain - switch branches', async () => {
  1078. const data = ref('a')
  1079. const { container } = await testHydration(
  1080. `<template>
  1081. <div v-if="data === 'a'">foo</div>
  1082. <div v-else-if="data === 'b'">bar</div>
  1083. <div v-else>baz</div>
  1084. </template>`,
  1085. undefined,
  1086. data,
  1087. )
  1088. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1089. `"<div>foo</div><!--if-->"`,
  1090. )
  1091. data.value = 'b'
  1092. await nextTick()
  1093. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1094. `"<div>bar</div><!--if--><!--if-->"`,
  1095. )
  1096. data.value = 'c'
  1097. await nextTick()
  1098. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1099. `"<div>baz</div><!--if--><!--if-->"`,
  1100. )
  1101. data.value = 'a'
  1102. await nextTick()
  1103. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1104. `"<div>foo</div><!--if-->"`,
  1105. )
  1106. })
  1107. test('v-if/else with sibling components and elements', async () => {
  1108. const data = ref('a')
  1109. const { container } = await testHydration(
  1110. `<script setup>
  1111. const msg = _data
  1112. const { Comp } = _components
  1113. </script>
  1114. <template>
  1115. <div>
  1116. <Comp/>
  1117. <div>11</div>
  1118. <div v-if="msg === 'a'">foo</div>
  1119. <div v-else>baz</div>
  1120. <div>11</div>
  1121. <Comp/>
  1122. </div>
  1123. </template>`,
  1124. {
  1125. Comp: `<template><span>comp</span></template>`,
  1126. },
  1127. data,
  1128. )
  1129. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1130. `"<div><span>comp</span><div>11</div><div>foo</div><!--if--><div>11</div><span>comp</span></div>"`,
  1131. )
  1132. data.value = 'b'
  1133. await nextTick()
  1134. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1135. `"<div><span>comp</span><div>11</div><div>baz</div><!--if--><div>11</div><span>comp</span></div>"`,
  1136. )
  1137. })
  1138. test('nested if', async () => {
  1139. const data = reactive({ outer: true, inner: true })
  1140. const { container } = await testHydration(
  1141. `<template>
  1142. <div v-if="data.outer">
  1143. <span>outer</span>
  1144. <div v-if="data.inner">inner</div>
  1145. </div>
  1146. </template>`,
  1147. undefined,
  1148. data,
  1149. )
  1150. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1151. `"<div><span>outer</span><div>inner</div><!--if--></div><!--if-->"`,
  1152. )
  1153. data.inner = false
  1154. await nextTick()
  1155. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1156. `"<div><span>outer</span><!--if--></div><!--if-->"`,
  1157. )
  1158. data.outer = false
  1159. await nextTick()
  1160. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1161. `"<!--if-->"`,
  1162. )
  1163. })
  1164. test('on component', async () => {
  1165. const data = ref(true)
  1166. const { container } = await testHydration(
  1167. `<template>
  1168. <components.Child v-if="data"/>
  1169. </template>`,
  1170. { Child: `<template>foo</template>` },
  1171. data,
  1172. )
  1173. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1174. `"foo<!--if-->"`,
  1175. )
  1176. data.value = false
  1177. await nextTick()
  1178. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1179. `"<!--if-->"`,
  1180. )
  1181. })
  1182. test('consecutive if node', async () => {
  1183. const data = ref(true)
  1184. const { container } = await testHydration(
  1185. `<template>
  1186. <components.Child v-if="data"/>
  1187. </template>`,
  1188. { Child: `<template><div v-if="data">foo</div></template>` },
  1189. data,
  1190. )
  1191. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1192. `"<div>foo</div><!--if--><!--if-->"`,
  1193. )
  1194. data.value = false
  1195. await nextTick()
  1196. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1197. `"<!--if-->"`,
  1198. )
  1199. data.value = true
  1200. await nextTick()
  1201. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1202. `"<div>foo</div><!--if--><!--if-->"`,
  1203. )
  1204. })
  1205. test('mixed prepend and insertion anchor', async () => {
  1206. const data = reactive({
  1207. show: true,
  1208. foo: 'foo',
  1209. bar: 'bar',
  1210. qux: 'qux',
  1211. })
  1212. const { container } = await testHydration(
  1213. `<template>
  1214. <components.Child/>
  1215. </template>`,
  1216. {
  1217. Child: `<template>
  1218. <span v-if="data.show">
  1219. <span v-if="data.show">{{data.foo}}</span>
  1220. <span v-if="data.show">{{data.bar}}</span>
  1221. <span>baz</span>
  1222. <span v-if="data.show">{{data.qux}}</span>
  1223. <span>quux</span>
  1224. </span>
  1225. </template>`,
  1226. },
  1227. data,
  1228. )
  1229. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1230. `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux</span><!--if--><span>quux</span></span><!--if-->"`,
  1231. )
  1232. data.qux = 'qux1'
  1233. await nextTick()
  1234. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1235. `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
  1236. )
  1237. data.foo = 'foo1'
  1238. await nextTick()
  1239. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1240. `"<span><span>foo1</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
  1241. )
  1242. })
  1243. test('v-if/else-if/else chain on component - switch branches', async () => {
  1244. const data = ref('a')
  1245. const { container } = await testHydration(
  1246. `<template>
  1247. <components.Child1 v-if="data === 'a'"/>
  1248. <components.Child2 v-else-if="data === 'b'"/>
  1249. <components.Child3 v-else/>
  1250. </template>`,
  1251. {
  1252. Child1: `<template><span>{{data}} child1</span></template>`,
  1253. Child2: `<template><span>{{data}} child2</span></template>`,
  1254. Child3: `<template><span>{{data}} child3</span></template>`,
  1255. },
  1256. data,
  1257. )
  1258. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1259. `"<span>a child1</span><!--if-->"`,
  1260. )
  1261. data.value = 'b'
  1262. await nextTick()
  1263. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1264. `"<span>b child2</span><!--if--><!--if-->"`,
  1265. )
  1266. data.value = 'c'
  1267. await nextTick()
  1268. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1269. `"<span>c child3</span><!--if--><!--if-->"`,
  1270. )
  1271. data.value = 'a'
  1272. await nextTick()
  1273. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1274. `"<span>a child1</span><!--if-->"`,
  1275. )
  1276. })
  1277. test('on component with insertion anchor', async () => {
  1278. const data = ref(true)
  1279. const { container } = await testHydration(
  1280. `<template>
  1281. <div>
  1282. <span/>
  1283. <components.Child v-if="data"/>
  1284. <span/>
  1285. </div>
  1286. </template>`,
  1287. { Child: `<template>foo</template>` },
  1288. data,
  1289. )
  1290. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1291. `"<div><span></span>foo<!--if--><span></span></div>"`,
  1292. )
  1293. data.value = false
  1294. await nextTick()
  1295. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1296. `"<div><span></span><!--if--><span></span></div>"`,
  1297. )
  1298. })
  1299. test('consecutive component with insertion parent', async () => {
  1300. const data = reactive({
  1301. show: true,
  1302. foo: 'foo',
  1303. bar: 'bar',
  1304. })
  1305. const { container } = await testHydration(
  1306. `<template>
  1307. <div v-if="data.show">
  1308. <components.Child/>
  1309. <components.Child2/>
  1310. </div>
  1311. </template>`,
  1312. {
  1313. Child: `<template><span>{{data.foo}}</span></template>`,
  1314. Child2: `<template><span>{{data.bar}}</span></template>`,
  1315. },
  1316. data,
  1317. )
  1318. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1319. `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
  1320. )
  1321. data.show = false
  1322. await nextTick()
  1323. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1324. `"<!--if-->"`,
  1325. )
  1326. data.show = true
  1327. await nextTick()
  1328. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1329. `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
  1330. )
  1331. data.foo = 'foo1'
  1332. data.bar = 'bar1'
  1333. await nextTick()
  1334. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1335. `"<div><span>foo1</span><span>bar1</span></div><!--if-->"`,
  1336. )
  1337. })
  1338. test('on fragment component', async () => {
  1339. const data = ref(true)
  1340. const { container } = await testHydration(
  1341. `<template>
  1342. <div>
  1343. <components.Child v-if="data"/>
  1344. </div>
  1345. </template>`,
  1346. {
  1347. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1348. },
  1349. data,
  1350. )
  1351. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1352. `
  1353. "<div>
  1354. <!--[--><div>true</div>-true-<!--]-->
  1355. <!--if--></div>"
  1356. `,
  1357. )
  1358. data.value = false
  1359. await nextTick()
  1360. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1361. `
  1362. "<div>
  1363. <!--[--><!--]-->
  1364. <!--if--></div>"
  1365. `,
  1366. )
  1367. data.value = true
  1368. await nextTick()
  1369. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1370. `
  1371. "<div>
  1372. <!--[--><!--]-->
  1373. <div>true</div>-true-<!--if--></div>"
  1374. `,
  1375. )
  1376. })
  1377. test('on fragment component with insertion anchor', async () => {
  1378. const data = ref(true)
  1379. const { container } = await testHydration(
  1380. `<template>
  1381. <div>
  1382. <span/>
  1383. <components.Child v-if="data"/>
  1384. <span/>
  1385. </div>
  1386. </template>`,
  1387. {
  1388. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1389. },
  1390. data,
  1391. )
  1392. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1393. `
  1394. "<div><span></span>
  1395. <!--[--><div>true</div>-true-<!--]-->
  1396. <!--if--><span></span></div>"
  1397. `,
  1398. )
  1399. data.value = false
  1400. await nextTick()
  1401. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1402. `
  1403. "<div><span></span>
  1404. <!--[--><!--]-->
  1405. <!--if--><span></span></div>"
  1406. `,
  1407. )
  1408. data.value = true
  1409. await nextTick()
  1410. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1411. "<div><span></span>
  1412. <!--[--><!--]-->
  1413. <div>true</div>-true-<!--if--><span></span></div>"
  1414. `)
  1415. })
  1416. test('consecutive v-if on fragment component with insertion anchor', async () => {
  1417. const data = ref(true)
  1418. const { container } = await testHydration(
  1419. `<template>
  1420. <div>
  1421. <span/>
  1422. <components.Child v-if="data"/>
  1423. <components.Child v-if="data"/>
  1424. <span/>
  1425. </div>
  1426. </template>`,
  1427. {
  1428. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1429. },
  1430. data,
  1431. )
  1432. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1433. `
  1434. "<div><span></span>
  1435. <!--[--><div>true</div>-true-<!--]-->
  1436. <!--if-->
  1437. <!--[--><div>true</div>-true-<!--]-->
  1438. <!--if--><span></span></div>"
  1439. `,
  1440. )
  1441. data.value = false
  1442. await nextTick()
  1443. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1444. `
  1445. "<div><span></span>
  1446. <!--[--><!--]-->
  1447. <!--if-->
  1448. <!--[--><!--]-->
  1449. <!--if--><span></span></div>"
  1450. `,
  1451. )
  1452. data.value = true
  1453. await nextTick()
  1454. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1455. "<div><span></span>
  1456. <!--[--><!--]-->
  1457. <div>true</div>-true-<!--if-->
  1458. <!--[--><!--]-->
  1459. <div>true</div>-true-<!--if--><span></span></div>"
  1460. `)
  1461. })
  1462. test('on dynamic component with insertion anchor', async () => {
  1463. const data = ref(true)
  1464. const { container } = await testHydration(
  1465. `<template>
  1466. <div>
  1467. <span/>
  1468. <component :is="components.Child" v-if="data"/>
  1469. <span/>
  1470. </div>
  1471. </template>`,
  1472. { Child: `<template>foo</template>` },
  1473. data,
  1474. )
  1475. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1476. `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
  1477. )
  1478. data.value = false
  1479. await nextTick()
  1480. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1481. `"<div><span></span><!--if--><span></span></div>"`,
  1482. )
  1483. data.value = true
  1484. await nextTick()
  1485. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1486. `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
  1487. )
  1488. })
  1489. test('v-if with insertion parent + sibling component', async () => {
  1490. const data = ref(true)
  1491. const { container } = await testHydration(
  1492. `<template>
  1493. <div>
  1494. <span v-if="data">hello</span>
  1495. </div>
  1496. <components.Child/>
  1497. </template>`,
  1498. {
  1499. Child: `<template><div>child</div></template>`,
  1500. },
  1501. data,
  1502. )
  1503. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1504. `
  1505. "
  1506. <!--[--><div><span>hello</span><!--if--></div><div>child</div><!--]-->
  1507. "
  1508. `,
  1509. )
  1510. data.value = false
  1511. await nextTick()
  1512. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1513. `
  1514. "
  1515. <!--[--><div><!--if--></div><div>child</div><!--]-->
  1516. "
  1517. `,
  1518. )
  1519. data.value = true
  1520. await nextTick()
  1521. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1522. `
  1523. "
  1524. <!--[--><div><span>hello</span><!--if--></div><div>child</div><!--]-->
  1525. "
  1526. `,
  1527. )
  1528. })
  1529. test('v-if with static sibling + root sibling component', async () => {
  1530. const data = ref(true)
  1531. const { container } = await testHydration(
  1532. `<template>
  1533. <div>
  1534. <span v-if="data">hello</span>
  1535. <div>1</div>
  1536. </div>
  1537. <components.Child/>
  1538. </template>`,
  1539. {
  1540. Child: `<template><div>child</div></template>`,
  1541. },
  1542. data,
  1543. )
  1544. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1545. `
  1546. "
  1547. <!--[--><div><span>hello</span><!--if--><div>1</div></div><div>child</div><!--]-->
  1548. "
  1549. `,
  1550. )
  1551. data.value = false
  1552. await nextTick()
  1553. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1554. `
  1555. "
  1556. <!--[--><div><!--if--><div>1</div></div><div>child</div><!--]-->
  1557. "
  1558. `,
  1559. )
  1560. data.value = true
  1561. await nextTick()
  1562. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1563. `
  1564. "
  1565. <!--[--><div><span>hello</span><!--if--><div>1</div></div><div>child</div><!--]-->
  1566. "
  1567. `,
  1568. )
  1569. })
  1570. test('v-if + static sibling + root sibling component (flat)', async () => {
  1571. const data = ref(true)
  1572. const { container } = await testHydration(
  1573. `<template>
  1574. <span v-if="data">hello</span>
  1575. <span></span>
  1576. <components.Child/>
  1577. </template>`,
  1578. {
  1579. Child: `<template><div>child</div></template>`,
  1580. },
  1581. data,
  1582. )
  1583. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1584. `
  1585. "
  1586. <!--[--><span>hello</span><!--if--><span></span><div>child</div><!--]-->
  1587. "
  1588. `,
  1589. )
  1590. data.value = false
  1591. await nextTick()
  1592. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1593. `
  1594. "
  1595. <!--[--><!--if--><span></span><div>child</div><!--]-->
  1596. "
  1597. `,
  1598. )
  1599. data.value = true
  1600. await nextTick()
  1601. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1602. `
  1603. "
  1604. <!--[--><span>hello</span><!--if--><span></span><div>child</div><!--]-->
  1605. "
  1606. `,
  1607. )
  1608. })
  1609. })
  1610. describe('for', () => {
  1611. test('basic v-for', async () => {
  1612. const { container, data } = await testHydration(
  1613. `<template>
  1614. <span v-for="item in data" :key="item">{{ item }}</span>
  1615. </template>`,
  1616. undefined,
  1617. ref(['a', 'b', 'c']),
  1618. )
  1619. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1620. `
  1621. "
  1622. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1623. "
  1624. `,
  1625. )
  1626. data.value.push('d')
  1627. await nextTick()
  1628. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1629. `
  1630. "
  1631. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1632. "
  1633. `,
  1634. )
  1635. })
  1636. test('empty v-for', async () => {
  1637. const { container, data } = await testHydration(
  1638. `<template>
  1639. <span v-for="item in data" :key="item">{{ item }}</span>
  1640. </template>`,
  1641. undefined,
  1642. ref([]),
  1643. )
  1644. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1645. `
  1646. "
  1647. <!--[--><!--]-->
  1648. "
  1649. `,
  1650. )
  1651. data.value.push('a')
  1652. await nextTick()
  1653. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1654. `
  1655. "
  1656. <!--[--><span>a</span><!--]-->
  1657. "
  1658. `,
  1659. )
  1660. })
  1661. test('v-for with insertion parent + sibling component', async () => {
  1662. const { container, data } = await testHydration(
  1663. `<template>
  1664. <div>
  1665. <span v-for="item in data" :key="item">{{ item }}</span>
  1666. </div>
  1667. <components.Child/>
  1668. </template>`,
  1669. {
  1670. Child: `<template><div>{{data.length}}</div></template>`,
  1671. },
  1672. ref(['a', 'b', 'c']),
  1673. )
  1674. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1675. `
  1676. "
  1677. <!--[--><div>
  1678. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1679. </div><div>3</div><!--]-->
  1680. "
  1681. `,
  1682. )
  1683. data.value.push('d')
  1684. await nextTick()
  1685. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1686. `
  1687. "
  1688. <!--[--><div>
  1689. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1690. </div><div>4</div><!--]-->
  1691. "
  1692. `,
  1693. )
  1694. })
  1695. test('v-for with static sibling + root sibling component', async () => {
  1696. const { container, data } = await testHydration(
  1697. `<template>
  1698. <div>
  1699. <span v-for="item in data" :key="item">{{ item }}</span>
  1700. <div>1</div>
  1701. </div>
  1702. <components.Child/>
  1703. </template>`,
  1704. {
  1705. Child: `<template><div>{{data.length}}</div></template>`,
  1706. },
  1707. ref(['a', 'b', 'c']),
  1708. )
  1709. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1710. `
  1711. "
  1712. <!--[--><div>
  1713. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1714. <div>1</div></div><div>3</div><!--]-->
  1715. "
  1716. `,
  1717. )
  1718. data.value.push('d')
  1719. await nextTick()
  1720. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1721. `
  1722. "
  1723. <!--[--><div>
  1724. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1725. <div>1</div></div><div>4</div><!--]-->
  1726. "
  1727. `,
  1728. )
  1729. })
  1730. test('v-for with insertion anchor', async () => {
  1731. const { container, data } = await testHydration(
  1732. `<template>
  1733. <div>
  1734. <span/>
  1735. <span v-for="item in data" :key="item">{{ item }}</span>
  1736. <span/>
  1737. </div>
  1738. </template>`,
  1739. undefined,
  1740. ref(['a', 'b', 'c']),
  1741. )
  1742. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1743. `
  1744. "<div><span></span>
  1745. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1746. <span></span></div>"
  1747. `,
  1748. )
  1749. data.value.push('d')
  1750. await nextTick()
  1751. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1752. `
  1753. "<div><span></span>
  1754. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1755. <span></span></div>"
  1756. `,
  1757. )
  1758. data.value.splice(0, 1)
  1759. await nextTick()
  1760. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1761. `
  1762. "<div><span></span>
  1763. <!--[--><span>b</span><span>c</span><span>d</span><!--]-->
  1764. <span></span></div>"
  1765. `,
  1766. )
  1767. })
  1768. test('consecutive v-for with insertion anchor', async () => {
  1769. const { container, data } = await testHydration(
  1770. `<template>
  1771. <div>
  1772. <span/>
  1773. <span v-for="item in data" :key="item">{{ item }}</span>
  1774. <span v-for="item in data" :key="item">{{ item }}</span>
  1775. <span/>
  1776. </div>
  1777. </template>`,
  1778. undefined,
  1779. ref(['a', 'b', 'c']),
  1780. )
  1781. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1782. `
  1783. "<div><span></span>
  1784. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1785. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  1786. <span></span></div>"
  1787. `,
  1788. )
  1789. data.value.push('d')
  1790. await nextTick()
  1791. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1792. `
  1793. "<div><span></span>
  1794. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1795. <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
  1796. <span></span></div>"
  1797. `,
  1798. )
  1799. data.value.splice(0, 2)
  1800. await nextTick()
  1801. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1802. `
  1803. "<div><span></span>
  1804. <!--[--><span>c</span><span>d</span><!--]-->
  1805. <!--[--><span>c</span><span>d</span><!--]-->
  1806. <span></span></div>"
  1807. `,
  1808. )
  1809. })
  1810. test('v-for on component', async () => {
  1811. const { container, data } = await testHydration(
  1812. `<template>
  1813. <div>
  1814. <components.Child v-for="item in data" :key="item"/>
  1815. </div>
  1816. </template>`,
  1817. {
  1818. Child: `<template><div>comp</div></template>`,
  1819. },
  1820. ref(['a', 'b', 'c']),
  1821. )
  1822. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1823. `
  1824. "<div>
  1825. <!--[--><div>comp</div><div>comp</div><div>comp</div><!--]-->
  1826. </div>"
  1827. `,
  1828. )
  1829. data.value.push('d')
  1830. await nextTick()
  1831. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1832. `
  1833. "<div>
  1834. <!--[--><div>comp</div><div>comp</div><div>comp</div><div>comp</div><!--]-->
  1835. </div>"
  1836. `,
  1837. )
  1838. })
  1839. test('v-for on component with slots', async () => {
  1840. const { container, data } = await testHydration(
  1841. `<template>
  1842. <div>
  1843. <components.Child v-for="item in data" :key="item">
  1844. <span>{{ item }}</span>
  1845. </components.Child>
  1846. </div>
  1847. </template>`,
  1848. {
  1849. Child: `<template><slot/></template>`,
  1850. },
  1851. ref(['a', 'b', 'c']),
  1852. )
  1853. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1854. `
  1855. "<div>
  1856. <!--[-->
  1857. <!--[--><span>a</span><!--]-->
  1858. <!--[--><span>b</span><!--]-->
  1859. <!--[--><span>c</span><!--]-->
  1860. <!--]-->
  1861. </div>"
  1862. `,
  1863. )
  1864. data.value.push('d')
  1865. await nextTick()
  1866. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1867. `
  1868. "<div>
  1869. <!--[-->
  1870. <!--[--><span>a</span><!--]-->
  1871. <!--[--><span>b</span><!--]-->
  1872. <!--[--><span>c</span><!--]-->
  1873. <span>d</span><!--slot--><!--]-->
  1874. </div>"
  1875. `,
  1876. )
  1877. })
  1878. test('on fragment component', async () => {
  1879. const { container, data } = await testHydration(
  1880. `<template>
  1881. <div>
  1882. <components.Child v-for="item in data" :key="item"/>
  1883. </div>
  1884. </template>`,
  1885. {
  1886. Child: `<template><div>foo</div>-bar-</template>`,
  1887. },
  1888. ref(['a', 'b', 'c']),
  1889. )
  1890. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1891. `
  1892. "<div>
  1893. <!--[-->
  1894. <!--[--><div>foo</div>-bar-<!--]-->
  1895. <!--[--><div>foo</div>-bar-<!--]-->
  1896. <!--[--><div>foo</div>-bar-<!--]-->
  1897. <!--]-->
  1898. </div>"
  1899. `,
  1900. )
  1901. data.value.push('d')
  1902. await nextTick()
  1903. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1904. `
  1905. "<div>
  1906. <!--[-->
  1907. <!--[--><div>foo</div>-bar-<!--]-->
  1908. <!--[--><div>foo</div>-bar-<!--]-->
  1909. <!--[--><div>foo</div>-bar-<!--]-->
  1910. <div>foo</div>-bar-<!--]-->
  1911. </div>"
  1912. `,
  1913. )
  1914. })
  1915. test('on component with non-hydration node', async () => {
  1916. const data = ref({ show: true, msg: 'foo' })
  1917. const { container } = await testHydration(
  1918. `<template>
  1919. <div>
  1920. <components.Child v-for="item in 2" :key="item"/>
  1921. </div>
  1922. </template>`,
  1923. {
  1924. Child: `<template>
  1925. <div>
  1926. <div>
  1927. <div v-if="data.show">{{ data.msg }}</div>
  1928. </div>
  1929. <span>non-hydration node</span>
  1930. </div>
  1931. </template>`,
  1932. },
  1933. data,
  1934. )
  1935. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1936. `
  1937. "<div>
  1938. <!--[--><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><!--]-->
  1939. </div>"
  1940. `,
  1941. )
  1942. data.value.msg = 'bar'
  1943. await nextTick()
  1944. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1945. `
  1946. "<div>
  1947. <!--[--><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><!--]-->
  1948. </div>"
  1949. `,
  1950. )
  1951. data.value.show = false
  1952. await nextTick()
  1953. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1954. "<div>
  1955. <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
  1956. </div>"
  1957. `)
  1958. data.value.show = true
  1959. await nextTick()
  1960. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  1961. "<div>
  1962. <!--[--><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><!--]-->
  1963. </div>"
  1964. `)
  1965. })
  1966. test('with non-hydration node', async () => {
  1967. const data = ref({ show: true, msg: 'foo' })
  1968. const { container } = await testHydration(
  1969. `<template>
  1970. <div>
  1971. <div v-for="item in 2">
  1972. <div>
  1973. <div v-if="data.show">{{ data.msg }}</div>
  1974. </div>
  1975. <span>non-hydration node</span>
  1976. </div>
  1977. </div>
  1978. </template>`,
  1979. {},
  1980. data,
  1981. )
  1982. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1983. `
  1984. "<div>
  1985. <!--[--><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><!--]-->
  1986. </div>"
  1987. `,
  1988. )
  1989. data.value.msg = 'bar'
  1990. await nextTick()
  1991. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  1992. `
  1993. "<div>
  1994. <!--[--><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><!--]-->
  1995. </div>"
  1996. `,
  1997. )
  1998. data.value.show = false
  1999. await nextTick()
  2000. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2001. "<div>
  2002. <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
  2003. </div>"
  2004. `)
  2005. data.value.show = true
  2006. await nextTick()
  2007. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2008. "<div>
  2009. <!--[--><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><!--]-->
  2010. </div>"
  2011. `)
  2012. })
  2013. })
  2014. describe('slots', () => {
  2015. test('basic slot', async () => {
  2016. const { data, container } = await testHydration(
  2017. `<template>
  2018. <components.Child>
  2019. <span>{{data}}</span>
  2020. </components.Child>
  2021. </template>`,
  2022. {
  2023. Child: `<template><slot/></template>`,
  2024. },
  2025. )
  2026. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2027. `
  2028. "
  2029. <!--[--><span>foo</span><!--]-->
  2030. "
  2031. `,
  2032. )
  2033. data.value = 'bar'
  2034. await nextTick()
  2035. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2036. `
  2037. "
  2038. <!--[--><span>bar</span><!--]-->
  2039. "
  2040. `,
  2041. )
  2042. })
  2043. test('named slot', async () => {
  2044. const { data, container } = await testHydration(
  2045. `<template>
  2046. <components.Child>
  2047. <template #foo>
  2048. <span>{{data}}</span>
  2049. </template>
  2050. </components.Child>
  2051. </template>`,
  2052. {
  2053. Child: `<template><slot/><slot name="foo"/></template>`,
  2054. },
  2055. )
  2056. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2057. `
  2058. "
  2059. <!--[-->
  2060. <!--[--><!--]-->
  2061. <!--[--><span>foo</span><!--]-->
  2062. <!--]-->
  2063. "
  2064. `,
  2065. )
  2066. data.value = 'bar'
  2067. await nextTick()
  2068. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2069. `
  2070. "
  2071. <!--[-->
  2072. <!--[--><!--]-->
  2073. <!--[--><span>bar</span><!--]-->
  2074. <!--]-->
  2075. "
  2076. `,
  2077. )
  2078. })
  2079. test('named slot with v-if', async () => {
  2080. const { data, container } = await testHydration(
  2081. `<template>
  2082. <components.Child>
  2083. <template #foo v-if="data">
  2084. <span>{{data}}</span>
  2085. </template>
  2086. </components.Child>
  2087. </template>`,
  2088. {
  2089. Child: `<template><slot name="foo"/></template>`,
  2090. },
  2091. )
  2092. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2093. `
  2094. "
  2095. <!--[--><span>foo</span><!--]-->
  2096. "
  2097. `,
  2098. )
  2099. data.value = false
  2100. await nextTick()
  2101. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2102. `
  2103. "
  2104. <!--[--><!--]-->
  2105. "
  2106. `,
  2107. )
  2108. data.value = true
  2109. await nextTick()
  2110. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2111. "
  2112. <!--[--><span>true</span><!--]-->
  2113. "
  2114. `)
  2115. })
  2116. test('named slot with v-if and v-for', async () => {
  2117. const data = reactive({
  2118. show: true,
  2119. items: ['a', 'b', 'c'],
  2120. })
  2121. const { container } = await testHydration(
  2122. `<template>
  2123. <components.Child>
  2124. <template #foo v-if="data.show">
  2125. <span v-for="item in data.items" :key="item">{{item}}</span>
  2126. </template>
  2127. </components.Child>
  2128. </template>`,
  2129. {
  2130. Child: `<template><slot name="foo"/></template>`,
  2131. },
  2132. data,
  2133. )
  2134. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2135. `
  2136. "
  2137. <!--[-->
  2138. <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
  2139. <!--]-->
  2140. "
  2141. `,
  2142. )
  2143. data.show = false
  2144. await nextTick()
  2145. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2146. `
  2147. "
  2148. <!--[-->
  2149. <!--[--><!--]-->
  2150. "
  2151. `,
  2152. )
  2153. data.show = true
  2154. await nextTick()
  2155. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2156. `
  2157. "
  2158. <!--[-->
  2159. <!--[--><span>a</span><span>b</span><span>c</span><!--for--><!--]-->
  2160. "
  2161. `,
  2162. )
  2163. })
  2164. test('with insertion anchor', async () => {
  2165. const { data, container } = await testHydration(
  2166. `<template>
  2167. <components.Child>
  2168. <span/>
  2169. <span>{{data}}</span>
  2170. <span/>
  2171. </components.Child>
  2172. </template>`,
  2173. {
  2174. Child: `<template><slot/></template>`,
  2175. },
  2176. )
  2177. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2178. `
  2179. "
  2180. <!--[--><span></span><span>foo</span><span></span><!--]-->
  2181. "
  2182. `,
  2183. )
  2184. data.value = 'bar'
  2185. await nextTick()
  2186. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2187. `
  2188. "
  2189. <!--[--><span></span><span>bar</span><span></span><!--]-->
  2190. "
  2191. `,
  2192. )
  2193. })
  2194. test('with multi level anchor insertion', async () => {
  2195. const { data, container } = await testHydration(
  2196. `<template>
  2197. <components.Child>
  2198. <span/>
  2199. <span>{{data}}</span>
  2200. <span/>
  2201. </components.Child>
  2202. </template>`,
  2203. {
  2204. Child: `
  2205. <template>
  2206. <div/>
  2207. <div/>
  2208. <slot/>
  2209. <div/>
  2210. </div>
  2211. </template>`,
  2212. },
  2213. )
  2214. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2215. `
  2216. "
  2217. <!--[--><div></div><div></div>
  2218. <!--[--><span></span><span>foo</span><span></span><!--]-->
  2219. <div></div><!--]-->
  2220. "
  2221. `,
  2222. )
  2223. data.value = 'bar'
  2224. await nextTick()
  2225. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2226. `
  2227. "
  2228. <!--[--><div></div><div></div>
  2229. <!--[--><span></span><span>bar</span><span></span><!--]-->
  2230. <div></div><!--]-->
  2231. "
  2232. `,
  2233. )
  2234. })
  2235. test('mixed slot and text node', async () => {
  2236. const data = reactive({
  2237. text: 'foo',
  2238. msg: 'hi',
  2239. })
  2240. const { container } = await testHydration(
  2241. `<template>
  2242. <components.Child>
  2243. <span>{{data.text}}</span>
  2244. </components.Child>
  2245. </template>`,
  2246. {
  2247. Child: `<template><div><slot/>{{data.msg}}</div></template>`,
  2248. },
  2249. data,
  2250. )
  2251. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2252. `
  2253. "<div>
  2254. <!--[--><span>foo</span><!--]-->
  2255. hi</div>"
  2256. `,
  2257. )
  2258. data.msg = 'bar'
  2259. await nextTick()
  2260. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2261. `
  2262. "<div>
  2263. <!--[--><span>foo</span><!--]-->
  2264. bar</div>"
  2265. `,
  2266. )
  2267. })
  2268. test('mixed root slot and text node', async () => {
  2269. const data = reactive({
  2270. text: 'foo',
  2271. msg: 'hi',
  2272. })
  2273. const { container } = await testHydration(
  2274. `<template>
  2275. <components.Child>
  2276. <span>{{data.text}}</span>
  2277. </components.Child>
  2278. </template>`,
  2279. {
  2280. Child: `<template>{{data.text}}<slot/>{{data.msg}}</template>`,
  2281. },
  2282. data,
  2283. )
  2284. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2285. `
  2286. "
  2287. <!--[-->foo
  2288. <!--[--><span>foo</span><!--]-->
  2289. hi<!--]-->
  2290. "
  2291. `,
  2292. )
  2293. data.msg = 'bar'
  2294. await nextTick()
  2295. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2296. `
  2297. "
  2298. <!--[-->foo
  2299. <!--[--><span>foo</span><!--]-->
  2300. bar<!--]-->
  2301. "
  2302. `,
  2303. )
  2304. })
  2305. test('mixed consecutive slot and element', async () => {
  2306. const data = reactive({
  2307. text: 'foo',
  2308. msg: 'hi',
  2309. })
  2310. const { container } = await testHydration(
  2311. `<template>
  2312. <components.Child>
  2313. <template #foo><span>{{data.text}}</span></template>
  2314. <template #bar><span>bar</span></template>
  2315. </components.Child>
  2316. </template>`,
  2317. {
  2318. Child: `<template><div><slot name="foo"/><slot name="bar"/><div>{{data.msg}}</div></div></template>`,
  2319. },
  2320. data,
  2321. )
  2322. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2323. `
  2324. "<div>
  2325. <!--[--><span>foo</span><!--]-->
  2326. <!--[--><span>bar</span><!--]-->
  2327. <div>hi</div></div>"
  2328. `,
  2329. )
  2330. data.msg = 'bar'
  2331. await nextTick()
  2332. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2333. `
  2334. "<div>
  2335. <!--[--><span>foo</span><!--]-->
  2336. <!--[--><span>bar</span><!--]-->
  2337. <div>bar</div></div>"
  2338. `,
  2339. )
  2340. })
  2341. test('mixed slot and element', async () => {
  2342. const data = reactive({
  2343. text: 'foo',
  2344. msg: 'hi',
  2345. })
  2346. const { container } = await testHydration(
  2347. `<template>
  2348. <components.Child>
  2349. <span>{{data.text}}</span>
  2350. </components.Child>
  2351. </template>`,
  2352. {
  2353. Child: `<template><div><slot/><div>{{data.msg}}</div></div></template>`,
  2354. },
  2355. data,
  2356. )
  2357. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2358. `
  2359. "<div>
  2360. <!--[--><span>foo</span><!--]-->
  2361. <div>hi</div></div>"
  2362. `,
  2363. )
  2364. data.msg = 'bar'
  2365. await nextTick()
  2366. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2367. `
  2368. "<div>
  2369. <!--[--><span>foo</span><!--]-->
  2370. <div>bar</div></div>"
  2371. `,
  2372. )
  2373. })
  2374. test('mixed slot and component', 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. </components.Child>
  2384. </template>`,
  2385. {
  2386. Child: `
  2387. <template>
  2388. <div>
  2389. <components.Child2/>
  2390. <slot/>
  2391. <components.Child2/>
  2392. </div>
  2393. </template>`,
  2394. Child2: `
  2395. <template>
  2396. <div>{{data.msg2}}</div>
  2397. </template>`,
  2398. },
  2399. data,
  2400. )
  2401. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2402. `
  2403. "<div><div>bar</div>
  2404. <!--[--><span>foo</span><!--]-->
  2405. <div>bar</div></div>"
  2406. `,
  2407. )
  2408. data.msg2 = 'hello'
  2409. await nextTick()
  2410. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2411. `
  2412. "<div><div>hello</div>
  2413. <!--[--><span>foo</span><!--]-->
  2414. <div>hello</div></div>"
  2415. `,
  2416. )
  2417. })
  2418. test('mixed slot and fragment component', async () => {
  2419. const data = reactive({
  2420. msg1: 'foo',
  2421. msg2: 'bar',
  2422. })
  2423. const { container } = await testHydration(
  2424. `<template>
  2425. <components.Child>
  2426. <span>{{data.msg1}}</span>
  2427. </components.Child>
  2428. </template>`,
  2429. {
  2430. Child: `
  2431. <template>
  2432. <div>
  2433. <components.Child2/>
  2434. <slot/>
  2435. <components.Child2/>
  2436. </div>
  2437. </template>`,
  2438. Child2: `
  2439. <template>
  2440. <div>{{data.msg1}}</div> {{data.msg2}}
  2441. </template>`,
  2442. },
  2443. data,
  2444. )
  2445. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2446. `
  2447. "<div>
  2448. <!--[--><div>foo</div> bar<!--]-->
  2449. <!--[--><span>foo</span><!--]-->
  2450. <!--[--><div>foo</div> bar<!--]-->
  2451. </div>"
  2452. `,
  2453. )
  2454. data.msg1 = 'hello'
  2455. data.msg2 = 'vapor'
  2456. await nextTick()
  2457. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2458. `
  2459. "<div>
  2460. <!--[--><div>hello</div> vapor<!--]-->
  2461. <!--[--><span>hello</span><!--]-->
  2462. <!--[--><div>hello</div> vapor<!--]-->
  2463. </div>"
  2464. `,
  2465. )
  2466. })
  2467. test('mixed slot and v-if', async () => {
  2468. const data = reactive({
  2469. show: true,
  2470. msg: 'foo',
  2471. })
  2472. const { container } = await testHydration(
  2473. `<template>
  2474. <components.Child>
  2475. <span>{{data.msg}}</span>
  2476. </components.Child>
  2477. </template>`,
  2478. {
  2479. Child: `
  2480. <template>
  2481. <div v-if="data.show">{{data.msg}}</div>
  2482. <slot/>
  2483. <div v-if="data.show">{{data.msg}}</div>
  2484. </template>`,
  2485. },
  2486. data,
  2487. )
  2488. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2489. `
  2490. "
  2491. <!--[--><div>foo</div><!--if-->
  2492. <!--[--><span>foo</span><!--]-->
  2493. <div>foo</div><!--if--><!--]-->
  2494. "
  2495. `,
  2496. )
  2497. data.show = false
  2498. await nextTick()
  2499. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2500. `
  2501. "
  2502. <!--[--><!--if-->
  2503. <!--[--><span>foo</span><!--]-->
  2504. <!--if--><!--]-->
  2505. "
  2506. `,
  2507. )
  2508. data.show = true
  2509. await nextTick()
  2510. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2511. "
  2512. <!--[--><div>foo</div><!--if-->
  2513. <!--[--><span>foo</span><!--]-->
  2514. <div>foo</div><!--if--><!--]-->
  2515. "
  2516. `)
  2517. })
  2518. test('mixed slot and v-for', async () => {
  2519. const data = reactive({
  2520. items: ['a', 'b', 'c'],
  2521. msg: 'foo',
  2522. })
  2523. const { container } = await testHydration(
  2524. `<template>
  2525. <components.Child>
  2526. <span>{{data.msg}}</span>
  2527. </components.Child>
  2528. </template>`,
  2529. {
  2530. Child: `
  2531. <template>
  2532. <div v-for="item in data.items" :key="item">{{item}}</div>
  2533. <slot/>
  2534. <div v-for="item in data.items" :key="item">{{item}}</div>
  2535. </template>`,
  2536. },
  2537. data,
  2538. )
  2539. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2540. `
  2541. "
  2542. <!--[-->
  2543. <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
  2544. <!--[--><span>foo</span><!--]-->
  2545. <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
  2546. <!--]-->
  2547. "
  2548. `,
  2549. )
  2550. data.items.push('d')
  2551. await nextTick()
  2552. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2553. `
  2554. "
  2555. <!--[-->
  2556. <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
  2557. <!--[--><span>foo</span><!--]-->
  2558. <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
  2559. <!--]-->
  2560. "
  2561. `,
  2562. )
  2563. })
  2564. test('consecutive slots', async () => {
  2565. const data = reactive({
  2566. msg1: 'foo',
  2567. msg2: 'bar',
  2568. })
  2569. const { container } = await testHydration(
  2570. `<template>
  2571. <components.Child>
  2572. <span>{{data.msg1}}</span>
  2573. <template #bar>
  2574. <span>{{data.msg2}}</span>
  2575. </template>
  2576. </components.Child>
  2577. </template>`,
  2578. {
  2579. Child: `<template><slot/><slot name="bar"/></template>`,
  2580. },
  2581. data,
  2582. )
  2583. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2584. `
  2585. "
  2586. <!--[-->
  2587. <!--[--><span>foo</span><!--]-->
  2588. <!--[--><span>bar</span><!--]-->
  2589. <!--]-->
  2590. "
  2591. `,
  2592. )
  2593. data.msg1 = 'hello'
  2594. data.msg2 = 'vapor'
  2595. await nextTick()
  2596. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2597. `
  2598. "
  2599. <!--[-->
  2600. <!--[--><span>hello</span><!--]-->
  2601. <!--[--><span>vapor</span><!--]-->
  2602. <!--]-->
  2603. "
  2604. `,
  2605. )
  2606. })
  2607. test('consecutive slots with insertion anchor', async () => {
  2608. const data = reactive({
  2609. msg1: 'foo',
  2610. msg2: 'bar',
  2611. })
  2612. const { container } = await testHydration(
  2613. `<template>
  2614. <components.Child>
  2615. <span>{{data.msg1}}</span>
  2616. <template #bar>
  2617. <span>{{data.msg2}}</span>
  2618. </template>
  2619. </components.Child>
  2620. </template>`,
  2621. {
  2622. Child: `<template>
  2623. <div>
  2624. <span/>
  2625. <slot/>
  2626. <slot name="bar"/>
  2627. <span/>
  2628. </div>
  2629. </template>`,
  2630. },
  2631. data,
  2632. )
  2633. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2634. `
  2635. "<div><span></span>
  2636. <!--[--><span>foo</span><!--]-->
  2637. <!--[--><span>bar</span><!--]-->
  2638. <span></span></div>"
  2639. `,
  2640. )
  2641. data.msg1 = 'hello'
  2642. data.msg2 = 'vapor'
  2643. await nextTick()
  2644. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2645. `
  2646. "<div><span></span>
  2647. <!--[--><span>hello</span><!--]-->
  2648. <!--[--><span>vapor</span><!--]-->
  2649. <span></span></div>"
  2650. `,
  2651. )
  2652. })
  2653. test('consecutive slots prepend', async () => {
  2654. const data = reactive({
  2655. msg1: 'foo',
  2656. msg2: 'bar',
  2657. msg3: 'baz',
  2658. })
  2659. const { container } = await testHydration(
  2660. `<template>
  2661. <components.Child>
  2662. <template #foo>
  2663. <span>{{data.msg1}}</span>
  2664. </template>
  2665. <template #bar>
  2666. <span>{{data.msg2}}</span>
  2667. </template>
  2668. </components.Child>
  2669. </template>`,
  2670. {
  2671. Child: `<template>
  2672. <div>
  2673. <slot name="foo"/>
  2674. <slot name="bar"/>
  2675. <div>{{data.msg3}}</div>
  2676. </div>
  2677. </template>`,
  2678. },
  2679. data,
  2680. )
  2681. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2682. `
  2683. "<div>
  2684. <!--[--><span>foo</span><!--]-->
  2685. <!--[--><span>bar</span><!--]-->
  2686. <div>baz</div></div>"
  2687. `,
  2688. )
  2689. data.msg1 = 'hello'
  2690. data.msg2 = 'vapor'
  2691. await nextTick()
  2692. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2693. `
  2694. "<div>
  2695. <!--[--><span>hello</span><!--]-->
  2696. <!--[--><span>vapor</span><!--]-->
  2697. <div>baz</div></div>"
  2698. `,
  2699. )
  2700. })
  2701. test('slot fallback', async () => {
  2702. const data = reactive({
  2703. foo: 'foo',
  2704. })
  2705. const { container } = await testHydration(
  2706. `<template>
  2707. <components.Child>
  2708. </components.Child>
  2709. </template>`,
  2710. {
  2711. Child: `<template><slot><span>{{data.foo}}</span></slot></template>`,
  2712. },
  2713. data,
  2714. )
  2715. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2716. `
  2717. "
  2718. <!--[--><span>foo</span><!--]-->
  2719. "
  2720. `,
  2721. )
  2722. data.foo = 'bar'
  2723. await nextTick()
  2724. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2725. `
  2726. "
  2727. <!--[--><span>bar</span><!--]-->
  2728. "
  2729. `,
  2730. )
  2731. })
  2732. test('forwarded slot', async () => {
  2733. const data = reactive({
  2734. foo: 'foo',
  2735. bar: 'bar',
  2736. })
  2737. const { container } = await testHydration(
  2738. `<template>
  2739. <div>
  2740. <components.Parent>
  2741. <span>{{data.foo}}</span>
  2742. </components.Parent>
  2743. <div>{{data.bar}}</div>
  2744. </div>
  2745. </template>`,
  2746. {
  2747. Parent: `<template><div><components.Child><slot/></components.Child></div></template>`,
  2748. Child: `<template><div><slot/></div></template>`,
  2749. },
  2750. data,
  2751. )
  2752. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2753. `
  2754. "<div><div><div>
  2755. <!--[-->
  2756. <!--[--><span>foo</span><!--]-->
  2757. <!--]-->
  2758. </div></div><div>bar</div></div>"
  2759. `,
  2760. )
  2761. data.foo = 'foo1'
  2762. data.bar = 'bar1'
  2763. await nextTick()
  2764. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2765. `
  2766. "<div><div><div>
  2767. <!--[-->
  2768. <!--[--><span>foo1</span><!--]-->
  2769. <!--]-->
  2770. </div></div><div>bar1</div></div>"
  2771. `,
  2772. )
  2773. })
  2774. test('forwarded slot with fallback but rendered content', async () => {
  2775. const data = reactive({ foo: 'foo' })
  2776. const { container } = await testHydration(
  2777. `<template>
  2778. <components.Parent><span>{{data.foo}}</span></components.Parent>
  2779. </template>`,
  2780. {
  2781. Parent: `<template><components.Child><slot/></components.Child></template>`,
  2782. Child: `<template><div><slot>bar</slot></div></template>`,
  2783. },
  2784. data,
  2785. )
  2786. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2787. "<div>
  2788. <!--[-->
  2789. <!--[--><span>foo</span><!--]-->
  2790. <!--]-->
  2791. </div>"
  2792. `)
  2793. data.foo = 'foo1'
  2794. await nextTick()
  2795. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  2796. "<div>
  2797. <!--[-->
  2798. <!--[--><span>foo1</span><!--]-->
  2799. <!--]-->
  2800. </div>"
  2801. `)
  2802. })
  2803. test('forwarded slot with fallback', async () => {
  2804. const data = reactive({
  2805. foo: 'foo',
  2806. })
  2807. const { container } = await testHydration(
  2808. `<template>
  2809. <components.Parent/>
  2810. </template>`,
  2811. {
  2812. Parent: `<template><components.Child><slot/></components.Child></template>`,
  2813. Child: `<template><div><slot>{{data.foo}}</slot></div></template>`,
  2814. },
  2815. data,
  2816. )
  2817. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2818. `
  2819. "<div>
  2820. <!--[-->foo<!--]-->
  2821. <!--slot--></div>"
  2822. `,
  2823. )
  2824. data.foo = 'foo1'
  2825. await nextTick()
  2826. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2827. `
  2828. "<div>
  2829. <!--[-->foo1<!--]-->
  2830. <!--slot--></div>"
  2831. `,
  2832. )
  2833. })
  2834. test('forwarded slot with empty content', async () => {
  2835. const data = reactive({
  2836. foo: 'foo',
  2837. })
  2838. const { container } = await testHydration(
  2839. `<template>
  2840. <components.Foo/>
  2841. </template>`,
  2842. {
  2843. Foo: `<template>
  2844. <components.Bar>
  2845. <template #foo>
  2846. <slot name="foo" />
  2847. </template>
  2848. </components.Bar>
  2849. </template>`,
  2850. Bar: `<template>
  2851. <components.Baz>
  2852. <template #foo>
  2853. <slot name="foo" />
  2854. </template>
  2855. </components.Baz>
  2856. </template>`,
  2857. Baz: `<template>
  2858. <components.Qux>
  2859. <template #foo>
  2860. <slot name="foo" />
  2861. </template>
  2862. </components.Qux>
  2863. </template>`,
  2864. Qux: `<template>
  2865. <div>
  2866. <slot name="foo" />
  2867. <div>{{data.foo}}</div>
  2868. </div>
  2869. </template>`,
  2870. },
  2871. data,
  2872. )
  2873. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2874. `
  2875. "<div>
  2876. <!--[--><!--]-->
  2877. <!--slot--><!--slot--><!--slot--><div>foo</div></div>"
  2878. `,
  2879. )
  2880. data.foo = 'bar'
  2881. await nextTick()
  2882. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2883. `
  2884. "<div>
  2885. <!--[--><!--]-->
  2886. <!--slot--><!--slot--><!--slot--><div>bar</div></div>"
  2887. `,
  2888. )
  2889. })
  2890. test('forwarded named slot can appear after hydrating as empty', async () => {
  2891. const data = reactive({
  2892. show: false,
  2893. foo: 'foo',
  2894. bar: 'bar',
  2895. })
  2896. const { container } = await testHydration(
  2897. `<template>
  2898. <components.Foo>
  2899. <template v-if="data.show" #foo>
  2900. <span>{{data.foo}}</span>
  2901. </template>
  2902. </components.Foo>
  2903. </template>`,
  2904. {
  2905. Foo: `<template>
  2906. <components.Bar>
  2907. <template #foo>
  2908. <slot name="foo" />
  2909. </template>
  2910. </components.Bar>
  2911. </template>`,
  2912. Bar: `<template>
  2913. <div>
  2914. <slot name="foo" />
  2915. <div>{{data.bar}}</div>
  2916. </div>
  2917. </template>`,
  2918. },
  2919. data,
  2920. )
  2921. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2922. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2923. `
  2924. "<div>
  2925. <!--[--><!--]-->
  2926. <!--slot--><div>bar</div></div>"
  2927. `,
  2928. )
  2929. data.show = true
  2930. await nextTick()
  2931. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2932. `
  2933. "<div>
  2934. <!--[--><!--]-->
  2935. <span>foo</span><!--slot--><div>bar</div></div>"
  2936. `,
  2937. )
  2938. data.foo = 'foo1'
  2939. data.bar = 'bar1'
  2940. await nextTick()
  2941. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2942. `
  2943. "<div>
  2944. <!--[--><!--]-->
  2945. <span>foo1</span><!--slot--><div>bar1</div></div>"
  2946. `,
  2947. )
  2948. })
  2949. })
  2950. describe('transition', async () => {
  2951. test('transition appear', async () => {
  2952. const { container } = await testHydration(
  2953. `<template>
  2954. <transition appear>
  2955. <div>foo</div>
  2956. </transition>
  2957. </template>`,
  2958. )
  2959. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2960. `"<div style="" class="v-enter-from v-enter-active">foo</div>"`,
  2961. )
  2962. expect(`mismatch`).not.toHaveBeenWarned()
  2963. })
  2964. test('transition appear work with pre-existing class', async () => {
  2965. const { container } = await testHydration(
  2966. `<template>
  2967. <transition appear>
  2968. <div class="foo">foo</div>
  2969. </transition>
  2970. </template>`,
  2971. )
  2972. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2973. `"<div class="foo v-enter-from v-enter-active" style="">foo</div>"`,
  2974. )
  2975. expect(`mismatch`).not.toHaveBeenWarned()
  2976. })
  2977. test('transition appear work with empty content', async () => {
  2978. const data = ref(true)
  2979. const { container } = await testHydration(
  2980. `<template>
  2981. <transition appear>
  2982. <slot v-if="data"></slot>
  2983. <span v-else>foo</span>
  2984. </transition>
  2985. </template>`,
  2986. undefined,
  2987. data,
  2988. )
  2989. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2990. `"<!--slot--><!--if-->"`,
  2991. )
  2992. expect(`mismatch`).not.toHaveBeenWarned()
  2993. data.value = false
  2994. await nextTick()
  2995. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  2996. `"<span class="v-enter-from v-enter-active">foo</span><!--if-->"`,
  2997. )
  2998. })
  2999. test('transition appear with v-if', async () => {
  3000. const data = ref(false)
  3001. const { container } = await testHydration(
  3002. `<template>
  3003. <transition appear>
  3004. <div v-if="data">foo</div>
  3005. </transition>
  3006. </template>`,
  3007. undefined,
  3008. data,
  3009. )
  3010. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  3011. `"<!--if-->"`,
  3012. )
  3013. expect(`mismatch`).not.toHaveBeenWarned()
  3014. })
  3015. test('transition appear with v-show', async () => {
  3016. const data = ref(false)
  3017. const { container } = await testHydration(
  3018. `<template>
  3019. <transition appear>
  3020. <div v-show="data">foo</div>
  3021. </transition>
  3022. </template>`,
  3023. undefined,
  3024. data,
  3025. )
  3026. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  3027. `"<div style="display:none;" class="v-enter-from v-enter-active v-leave-from v-leave-active">foo</div>"`,
  3028. )
  3029. expect(`mismatch`).not.toHaveBeenWarned()
  3030. })
  3031. test('transition appear w/ event listener', async () => {
  3032. const { container } = await testHydration(
  3033. `<script setup>
  3034. import { ref } from 'vue'
  3035. const count = ref(0)
  3036. </script>
  3037. <template>
  3038. <transition appear>
  3039. <button @click="count++">{{ count }}</button>
  3040. </transition>
  3041. </template>`,
  3042. )
  3043. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  3044. `"<button style="" class="v-enter-from v-enter-active">0</button>"`,
  3045. )
  3046. triggerEvent('click', container.querySelector('button')!)
  3047. await nextTick()
  3048. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  3049. `"<button style="" class="v-enter-from v-enter-active">1</button>"`,
  3050. )
  3051. })
  3052. })
  3053. describe('teleport', () => {
  3054. test('basic', async () => {
  3055. const data = ref({
  3056. msg: ref('foo'),
  3057. disabled: ref(false),
  3058. fn: vi.fn(),
  3059. })
  3060. const teleportContainer = document.createElement('div')
  3061. teleportContainer.id = 'teleport'
  3062. teleportContainer.innerHTML =
  3063. `<!--teleport start anchor-->` +
  3064. `<span>foo</span>` +
  3065. `<span class="foo"></span>` +
  3066. `<!--teleport anchor-->`
  3067. document.body.appendChild(teleportContainer)
  3068. const { block, container } = await mountWithHydration(
  3069. '<!--teleport start--><!--teleport end-->',
  3070. `<teleport to="#teleport" :disabled="data.disabled">
  3071. <span>{{data.msg}}</span>
  3072. <span :class="data.msg" @click="data.fn"></span>
  3073. </teleport>`,
  3074. data,
  3075. )
  3076. const teleport = block as TeleportFragment
  3077. expect(teleport.anchor).toBe(container.lastChild)
  3078. expect(teleport.target).toBe(teleportContainer)
  3079. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3080. expect((teleport.nodes as Node[])[0]).toBe(
  3081. teleportContainer.childNodes[1],
  3082. )
  3083. expect((teleport.nodes as Node[])[1]).toBe(
  3084. teleportContainer.childNodes[2],
  3085. )
  3086. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
  3087. expect(container.innerHTML).toMatchInlineSnapshot(
  3088. `"<!--teleport start--><!--teleport end-->"`,
  3089. )
  3090. // event handler
  3091. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  3092. expect(data.value.fn).toHaveBeenCalled()
  3093. data.value.msg = 'bar'
  3094. await nextTick()
  3095. expect(formatHtml(teleportContainer.innerHTML)).toBe(
  3096. `<!--teleport start anchor-->` +
  3097. `<span>bar</span>` +
  3098. `<span class="bar"></span>` +
  3099. `<!--teleport anchor-->`,
  3100. )
  3101. data.value.disabled = true
  3102. await nextTick()
  3103. expect(container.innerHTML).toBe(
  3104. `<!--teleport start-->` +
  3105. `<span>bar</span>` +
  3106. `<span class="bar"></span>` +
  3107. `<!--teleport end-->`,
  3108. )
  3109. expect(formatHtml(teleportContainer.innerHTML)).toMatchInlineSnapshot(
  3110. `"<!--teleport start anchor--><!--teleport anchor-->"`,
  3111. )
  3112. data.value.msg = 'baz'
  3113. await nextTick()
  3114. expect(container.innerHTML).toBe(
  3115. `<!--teleport start-->` +
  3116. `<span>baz</span>` +
  3117. `<span class="baz"></span>` +
  3118. `<!--teleport end-->`,
  3119. )
  3120. data.value.disabled = false
  3121. await nextTick()
  3122. expect(container.innerHTML).toMatchInlineSnapshot(
  3123. `"<!--teleport start--><!--teleport end-->"`,
  3124. )
  3125. expect(formatHtml(teleportContainer.innerHTML)).toBe(
  3126. `<!--teleport start anchor-->` +
  3127. `<span>baz</span>` +
  3128. `<span class="baz"></span>` +
  3129. `<!--teleport anchor-->`,
  3130. )
  3131. })
  3132. test('multiple + integration', async () => {
  3133. const data = ref({
  3134. msg: ref('foo'),
  3135. fn1: vi.fn(),
  3136. fn2: vi.fn(),
  3137. })
  3138. const code = `
  3139. <teleport to="#teleport2">
  3140. <span>{{data.msg}}</span>
  3141. <span :class="data.msg" @click="data.fn1"></span>
  3142. </teleport>
  3143. <teleport to="#teleport2">
  3144. <span>{{data.msg}}2</span>
  3145. <span :class="data.msg + 2" @click="data.fn2"></span>
  3146. </teleport>`
  3147. const SSRComp = compileVaporComponent(code, data, undefined, true)
  3148. const teleportContainer = document.createElement('div')
  3149. teleportContainer.id = 'teleport2'
  3150. const ctx = {} as any
  3151. const mainHtml = await VueServerRenderer.renderToString(
  3152. runtimeDom.createSSRApp(SSRComp),
  3153. ctx,
  3154. )
  3155. expect(mainHtml).toBe(
  3156. `<!--[-->` +
  3157. `<!--teleport start--><!--teleport end-->` +
  3158. `<!--teleport start--><!--teleport end-->` +
  3159. `<!--]-->`,
  3160. )
  3161. const teleportHtml = ctx.teleports!['#teleport2']
  3162. expect(teleportHtml).toBe(
  3163. `<!--teleport start anchor-->` +
  3164. `<span>foo</span><span class="foo"></span>` +
  3165. `<!--teleport anchor-->` +
  3166. `<!--teleport start anchor-->` +
  3167. `<span>foo2</span><span class="foo2"></span>` +
  3168. `<!--teleport anchor-->`,
  3169. )
  3170. teleportContainer.innerHTML = teleportHtml
  3171. document.body.appendChild(teleportContainer)
  3172. const { block, container } = await mountWithHydration(
  3173. mainHtml,
  3174. code,
  3175. data,
  3176. )
  3177. const teleports = block as any as TeleportFragment[]
  3178. const teleport1 = teleports[0]
  3179. const teleport2 = teleports[1]
  3180. expect(teleport1.anchor).toBe(container.childNodes[2])
  3181. expect(teleport2.anchor).toBe(container.childNodes[4])
  3182. expect(teleport1.target).toBe(teleportContainer)
  3183. expect(teleport1.targetStart).toBe(teleportContainer.childNodes[0])
  3184. expect((teleport1.nodes as Node[])[0]).toBe(
  3185. teleportContainer.childNodes[1],
  3186. )
  3187. expect(teleport1.targetAnchor).toBe(teleportContainer.childNodes[3])
  3188. expect(teleport2.target).toBe(teleportContainer)
  3189. expect(teleport2.targetStart).toBe(teleportContainer.childNodes[4])
  3190. expect((teleport2.nodes as Node[])[0]).toBe(
  3191. teleportContainer.childNodes[5],
  3192. )
  3193. expect(teleport2.targetAnchor).toBe(teleportContainer.childNodes[7])
  3194. expect(container.innerHTML).toBe(
  3195. `<!--[-->` +
  3196. `<!--teleport start--><!--teleport end-->` +
  3197. `<!--teleport start--><!--teleport end-->` +
  3198. `<!--]-->`,
  3199. )
  3200. // event handler
  3201. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  3202. expect(data.value.fn1).toHaveBeenCalled()
  3203. triggerEvent('click', teleportContainer.querySelector('.foo2')!)
  3204. expect(data.value.fn2).toHaveBeenCalled()
  3205. data.value.msg = 'bar'
  3206. await nextTick()
  3207. expect(teleportContainer.innerHTML).toBe(
  3208. `<!--teleport start anchor-->` +
  3209. `<span>bar</span>` +
  3210. `<span class="bar"></span>` +
  3211. `<!--teleport anchor-->` +
  3212. `<!--teleport start anchor-->` +
  3213. `<span>bar2</span>` +
  3214. `<span class="bar2"></span>` +
  3215. `<!--teleport anchor-->`,
  3216. )
  3217. })
  3218. test('disabled', async () => {
  3219. const data = ref({
  3220. msg: ref('foo'),
  3221. fn1: vi.fn(),
  3222. fn2: vi.fn(),
  3223. })
  3224. const code = `
  3225. <div>foo</div>
  3226. <teleport to="#teleport3" disabled="true">
  3227. <span>{{data.msg}}</span>
  3228. <span :class="data.msg" @click="data.fn1"></span>
  3229. </teleport>
  3230. <div :class="data.msg + 2" @click="data.fn2">bar</div>
  3231. `
  3232. const SSRComp = compileVaporComponent(code, data, undefined, true)
  3233. const teleportContainer = document.createElement('div')
  3234. teleportContainer.id = 'teleport3'
  3235. const ctx = {} as any
  3236. const mainHtml = await VueServerRenderer.renderToString(
  3237. runtimeDom.createSSRApp(SSRComp),
  3238. ctx,
  3239. )
  3240. expect(mainHtml).toBe(
  3241. `<!--[-->` +
  3242. `<div>foo</div>` +
  3243. `<!--teleport start-->` +
  3244. `<span>foo</span>` +
  3245. `<span class="foo"></span>` +
  3246. `<!--teleport end-->` +
  3247. `<div class="foo2">bar</div>` +
  3248. `<!--]-->`,
  3249. )
  3250. const teleportHtml = ctx.teleports!['#teleport3']
  3251. expect(teleportHtml).toMatchInlineSnapshot(
  3252. `"<!--teleport start anchor--><!--teleport anchor-->"`,
  3253. )
  3254. teleportContainer.innerHTML = teleportHtml
  3255. document.body.appendChild(teleportContainer)
  3256. const { block, container } = await mountWithHydration(
  3257. mainHtml,
  3258. code,
  3259. data,
  3260. )
  3261. const blocks = block as any[]
  3262. expect(blocks[0]).toBe(container.childNodes[1])
  3263. const teleport = blocks[1] as TeleportFragment
  3264. expect((teleport.nodes as Node[])[0]).toBe(container.childNodes[3])
  3265. expect((teleport.nodes as Node[])[1]).toBe(container.childNodes[4])
  3266. expect(teleport.anchor).toBe(container.childNodes[5])
  3267. expect(teleport.target).toBe(teleportContainer)
  3268. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3269. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[1])
  3270. expect(blocks[2]).toBe(container.childNodes[6])
  3271. expect(container.innerHTML).toBe(
  3272. `<!--[-->` +
  3273. `<div>foo</div>` +
  3274. `<!--teleport start-->` +
  3275. `<span>foo</span>` +
  3276. `<span class="foo"></span>` +
  3277. `<!--teleport end-->` +
  3278. `<div class="foo2">bar</div>` +
  3279. `<!--]-->`,
  3280. )
  3281. // event handler
  3282. triggerEvent('click', container.querySelector('.foo')!)
  3283. expect(data.value.fn1).toHaveBeenCalled()
  3284. triggerEvent('click', container.querySelector('.foo2')!)
  3285. expect(data.value.fn2).toHaveBeenCalled()
  3286. data.value.msg = 'bar'
  3287. await nextTick()
  3288. expect(container.innerHTML).toBe(
  3289. `<!--[-->` +
  3290. `<div>foo</div>` +
  3291. `<!--teleport start-->` +
  3292. `<span>bar</span>` +
  3293. `<span class="bar"></span>` +
  3294. `<!--teleport end-->` +
  3295. `<div class="bar2">bar</div>` +
  3296. `<!--]-->`,
  3297. )
  3298. })
  3299. test('nested disabled teleport hydration should locate correct end anchor', async () => {
  3300. const data = ref({ msg: ref('after') })
  3301. const { block, container } = await mountWithHydration(
  3302. `<!--[-->` +
  3303. `<!--teleport start-->` +
  3304. `<div>outer</div>` +
  3305. `<!--teleport start-->` +
  3306. `<div>inner</div>` +
  3307. `<!--teleport end-->` +
  3308. `<!--teleport end-->` +
  3309. `<div>after</div>` +
  3310. `<!--]-->`,
  3311. `<teleport to="body" disabled>
  3312. <div>outer</div>
  3313. <teleport to="body" disabled>
  3314. <div>inner</div>
  3315. </teleport>
  3316. </teleport>
  3317. <div>{{data.msg}}</div>`,
  3318. data,
  3319. )
  3320. const blocks = block as any[]
  3321. const outerTeleport = blocks[0] as TeleportFragment
  3322. // The outer teleport's anchor must be the LAST <!--teleport end-->,
  3323. // NOT the inner one. If locateTeleportEndAnchor doesn't handle nesting,
  3324. // it would incorrectly pick the inner <!--teleport end-->.
  3325. const allEndComments = Array.from(container.childNodes).filter(
  3326. n => n.nodeType === 8 && (n as Comment).data === 'teleport end',
  3327. )
  3328. expect(allEndComments.length).toBe(2)
  3329. expect(outerTeleport.anchor).toBe(allEndComments[1]) // must be the LAST one
  3330. // The sibling <div>after</div> should hydrate correctly
  3331. // If the outer anchor is wrong, the hydration cursor is misaligned
  3332. // and the sibling element won't match.
  3333. expect(container.innerHTML).toBe(
  3334. `<!--[-->` +
  3335. `<!--teleport start-->` +
  3336. `<div>outer</div>` +
  3337. `<!--teleport start-->` +
  3338. `<div>inner</div>` +
  3339. `<!--teleport end-->` +
  3340. `<!--teleport end-->` +
  3341. `<div>after</div>` +
  3342. `<!--]-->`,
  3343. )
  3344. expect(`mismatch`).not.toHaveBeenWarned()
  3345. })
  3346. test('multiple disabled teleports hydrating to the same target should consume distinct target anchors', async () => {
  3347. const teleportContainer = document.createElement('div')
  3348. teleportContainer.id = 'teleport-disabled-shared'
  3349. teleportContainer.innerHTML =
  3350. `<!--teleport start anchor-->` +
  3351. `<!--teleport anchor-->` +
  3352. `<!--teleport start anchor-->` +
  3353. `<!--teleport anchor-->`
  3354. document.body.appendChild(teleportContainer)
  3355. const { block } = await mountWithHydration(
  3356. `<!--[-->` +
  3357. `<!--teleport start--><div>one</div><!--teleport end-->` +
  3358. `<!--teleport start--><div>two</div><!--teleport end-->` +
  3359. `<!--]-->`,
  3360. `<teleport to="#teleport-disabled-shared" disabled>
  3361. <div>one</div>
  3362. </teleport>
  3363. <teleport to="#teleport-disabled-shared" disabled>
  3364. <div>two</div>
  3365. </teleport>`,
  3366. )
  3367. const teleports = block as TeleportFragment[]
  3368. expect(teleports[0].targetStart).toBe(teleportContainer.childNodes[0])
  3369. expect(teleports[0].targetAnchor).toBe(teleportContainer.childNodes[1])
  3370. expect(teleports[1].targetStart).toBe(teleportContainer.childNodes[2])
  3371. expect(teleports[1].targetAnchor).toBe(teleportContainer.childNodes[3])
  3372. })
  3373. test('disabled + as component root', async () => {
  3374. const { container } = await mountWithHydration(
  3375. `<!--[-->` +
  3376. `<div>Parent fragment</div>` +
  3377. `<!--teleport start--><div>Teleport content</div><!--teleport end-->` +
  3378. `<!--]-->`,
  3379. `
  3380. <div>Parent fragment</div>
  3381. <teleport to="body" disabled>
  3382. <div>Teleport content</div>
  3383. </teleport>
  3384. `,
  3385. )
  3386. expect(container.innerHTML).toBe(
  3387. `<!--[-->` +
  3388. `<div>Parent fragment</div>` +
  3389. `<!--teleport start-->` +
  3390. `<div>Teleport content</div>` +
  3391. `<!--teleport end-->` +
  3392. `<!--]-->`,
  3393. )
  3394. expect(`mismatch`).not.toHaveBeenWarned()
  3395. })
  3396. test('as component root', async () => {
  3397. const teleportContainer = document.createElement('div')
  3398. teleportContainer.id = 'teleport4'
  3399. teleportContainer.innerHTML = `<!--teleport start anchor-->hello<!--teleport anchor-->`
  3400. document.body.appendChild(teleportContainer)
  3401. const { block, container } = await mountWithHydration(
  3402. '<!--teleport start--><!--teleport end-->',
  3403. `<components.Wrapper></components.Wrapper>`,
  3404. undefined,
  3405. {
  3406. Wrapper: compileVaporComponent(
  3407. `<teleport to="#teleport4">hello</teleport>`,
  3408. ),
  3409. },
  3410. )
  3411. const teleport = (block as VaporComponentInstance)
  3412. .block as TeleportFragment
  3413. expect(teleport.anchor).toBe(container.childNodes[1])
  3414. expect(teleport.target).toBe(teleportContainer)
  3415. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3416. expect(teleport.nodes).toBe(teleportContainer.childNodes[1])
  3417. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[2])
  3418. })
  3419. test('nested', async () => {
  3420. const teleportContainer = document.createElement('div')
  3421. teleportContainer.id = 'teleport5'
  3422. teleportContainer.innerHTML =
  3423. `<!--teleport start anchor-->` +
  3424. `<!--teleport start--><!--teleport end-->` +
  3425. `<!--teleport anchor-->` +
  3426. `<!--teleport start anchor-->` +
  3427. `<div>child</div>` +
  3428. `<!--teleport anchor-->`
  3429. document.body.appendChild(teleportContainer)
  3430. const { block, container } = await mountWithHydration(
  3431. '<!--teleport start--><!--teleport end-->',
  3432. `<teleport to="#teleport5">
  3433. <teleport to="#teleport5"><div>child</div></teleport>
  3434. </teleport>`,
  3435. )
  3436. const teleport = block as TeleportFragment
  3437. expect(teleport.anchor).toBe(container.childNodes[1])
  3438. expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
  3439. expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
  3440. const childTeleport = teleport.nodes as TeleportFragment
  3441. expect(childTeleport.anchor).toBe(teleportContainer.childNodes[2])
  3442. expect(childTeleport.targetStart).toBe(teleportContainer.childNodes[4])
  3443. expect(childTeleport.targetAnchor).toBe(teleportContainer.childNodes[6])
  3444. expect(childTeleport.nodes).toBe(teleportContainer.childNodes[5])
  3445. })
  3446. test('unmount (full integration)', async () => {
  3447. const targetId = 'teleport6'
  3448. const data = ref({
  3449. toggle: ref(true),
  3450. })
  3451. const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
  3452. const Comp1 = compileVaporComponent(template1)
  3453. const SSRComp1 = compileVaporComponent(
  3454. template1,
  3455. undefined,
  3456. undefined,
  3457. true,
  3458. )
  3459. const template2 = `<div>Comp2</div>`
  3460. const Comp2 = compileVaporComponent(template2)
  3461. const SSRComp2 = compileVaporComponent(
  3462. template2,
  3463. undefined,
  3464. undefined,
  3465. true,
  3466. )
  3467. const appCode = `
  3468. <div>
  3469. <components.Comp1 v-if="data.toggle"/>
  3470. <components.Comp2 v-else/>
  3471. </div>
  3472. `
  3473. const SSRApp = compileVaporComponent(
  3474. appCode,
  3475. data,
  3476. {
  3477. Comp1: SSRComp1,
  3478. Comp2: SSRComp2,
  3479. },
  3480. true,
  3481. )
  3482. const teleportContainer = document.createElement('div')
  3483. teleportContainer.id = targetId
  3484. document.body.appendChild(teleportContainer)
  3485. const ctx = {} as any
  3486. const mainHtml = await VueServerRenderer.renderToString(
  3487. runtimeDom.createSSRApp(SSRApp),
  3488. ctx,
  3489. )
  3490. expect(mainHtml).toBe(
  3491. '<div><!--teleport start--><!--teleport end--></div>',
  3492. )
  3493. teleportContainer.innerHTML = ctx.teleports![`#${targetId}`]
  3494. const { container } = await mountWithHydration(mainHtml, appCode, data, {
  3495. Comp1,
  3496. Comp2,
  3497. })
  3498. expect(container.innerHTML).toBe(
  3499. '<div><!--teleport start--><!--teleport end--><!--if--></div>',
  3500. )
  3501. expect(teleportContainer.innerHTML).toBe(
  3502. `<!--teleport start anchor-->` +
  3503. `<span>Teleported Comp1</span>` +
  3504. `<!--teleport anchor-->`,
  3505. )
  3506. expect(`mismatch`).not.toHaveBeenWarned()
  3507. data.value.toggle = false
  3508. await nextTick()
  3509. expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
  3510. expect(teleportContainer.innerHTML).toBe('')
  3511. })
  3512. test('unmount (mismatch + full integration)', async () => {
  3513. const targetId = 'teleport7'
  3514. const data = ref({
  3515. toggle: ref(true),
  3516. })
  3517. const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
  3518. const Comp1 = compileVaporComponent(template1)
  3519. const SSRComp1 = compileVaporComponent(
  3520. template1,
  3521. undefined,
  3522. undefined,
  3523. true,
  3524. )
  3525. const template2 = `<div>Comp2</div>`
  3526. const Comp2 = compileVaporComponent(template2)
  3527. const SSRComp2 = compileVaporComponent(
  3528. template2,
  3529. undefined,
  3530. undefined,
  3531. true,
  3532. )
  3533. const appCode = `
  3534. <div>
  3535. <components.Comp1 v-if="data.toggle"/>
  3536. <components.Comp2 v-else/>
  3537. </div>
  3538. `
  3539. const SSRApp = compileVaporComponent(
  3540. appCode,
  3541. data,
  3542. {
  3543. Comp1: SSRComp1,
  3544. Comp2: SSRComp2,
  3545. },
  3546. true,
  3547. )
  3548. const teleportContainer = document.createElement('div')
  3549. teleportContainer.id = targetId
  3550. document.body.appendChild(teleportContainer)
  3551. const mainHtml = await VueServerRenderer.renderToString(
  3552. runtimeDom.createSSRApp(SSRApp),
  3553. )
  3554. expect(mainHtml).toBe(
  3555. '<div><!--teleport start--><!--teleport end--></div>',
  3556. )
  3557. expect(teleportContainer.innerHTML).toBe('')
  3558. const { container } = await mountWithHydration(mainHtml, appCode, data, {
  3559. Comp1,
  3560. Comp2,
  3561. })
  3562. expect(container.innerHTML).toBe(
  3563. '<div><!--teleport start--><!--teleport end--><!--if--></div>',
  3564. )
  3565. expect(teleportContainer.innerHTML).toBe(`<span>Teleported Comp1</span>`)
  3566. expect(`Hydration children mismatch`).toHaveBeenWarned()
  3567. data.value.toggle = false
  3568. await nextTick()
  3569. expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
  3570. expect(teleportContainer.innerHTML).toBe('')
  3571. })
  3572. test('target change (mismatch + full integration)', async () => {
  3573. const targetId1 = 'teleport8-1'
  3574. const targetId2 = 'teleport8-2'
  3575. const data = ref({
  3576. target: ref(targetId1),
  3577. msg: ref('foo'),
  3578. })
  3579. const template = `<Teleport :to="'#' + data.target"><span>{{data.msg}}</span></Teleport>`
  3580. const Comp = compileVaporComponent(template, data)
  3581. const SSRComp = compileVaporComponent(template, data, undefined, true)
  3582. const teleportContainer1 = document.createElement('div')
  3583. teleportContainer1.id = targetId1
  3584. const teleportContainer2 = document.createElement('div')
  3585. teleportContainer2.id = targetId2
  3586. document.body.appendChild(teleportContainer1)
  3587. document.body.appendChild(teleportContainer2)
  3588. // server render
  3589. const mainHtml = await VueServerRenderer.renderToString(
  3590. runtimeDom.createSSRApp(SSRComp),
  3591. )
  3592. expect(mainHtml).toBe(`<!--teleport start--><!--teleport end-->`)
  3593. expect(teleportContainer1.innerHTML).toBe('')
  3594. expect(teleportContainer2.innerHTML).toBe('')
  3595. // hydrate
  3596. const { container } = await mountWithHydration(mainHtml, template, data, {
  3597. Comp,
  3598. })
  3599. expect(container.innerHTML).toBe(
  3600. `<!--teleport start--><!--teleport end-->`,
  3601. )
  3602. expect(teleportContainer1.innerHTML).toBe(`<span>foo</span>`)
  3603. expect(teleportContainer2.innerHTML).toBe('')
  3604. expect(`Hydration children mismatch`).toHaveBeenWarned()
  3605. data.value.target = targetId2
  3606. data.value.msg = 'bar'
  3607. await nextTick()
  3608. expect(container.innerHTML).toBe(
  3609. `<!--teleport start--><!--teleport end-->`,
  3610. )
  3611. expect(teleportContainer1.innerHTML).toBe('')
  3612. expect(teleportContainer2.innerHTML).toBe(`<span>bar</span>`)
  3613. })
  3614. test('with disabled teleport + undefined target', async () => {
  3615. const data = ref({
  3616. msg: ref('foo'),
  3617. })
  3618. const { container } = await mountWithHydration(
  3619. '<!--teleport start--><span>foo</span><!--teleport end-->',
  3620. `<teleport :to="undefined" :disabled="true">
  3621. <span>{{data.msg}}</span>
  3622. </teleport>`,
  3623. data,
  3624. )
  3625. expect(container.innerHTML).toBe(
  3626. `<!--teleport start--><span>foo</span><!--teleport end-->`,
  3627. )
  3628. data.value.msg = 'bar'
  3629. await nextTick()
  3630. expect(container.innerHTML).toBe(
  3631. `<!--teleport start--><span>bar</span><!--teleport end-->`,
  3632. )
  3633. })
  3634. test('disabled teleport with null target hydration', async () => {
  3635. const { block, container } = await mountWithHydration(
  3636. '<!--teleport start--><div>content</div><!--teleport end-->',
  3637. `<teleport :to="undefined" :disabled="true">
  3638. <div>content</div>
  3639. </teleport>`,
  3640. )
  3641. expect(container.innerHTML).toBe(
  3642. `<!--teleport start--><div>content</div><!--teleport end-->`,
  3643. )
  3644. expect(`mismatch`).not.toHaveBeenWarned()
  3645. // targetStart must NOT be set when there's no target
  3646. const teleport = block as TeleportFragment
  3647. expect(teleport.targetStart).toBeNull()
  3648. expect(teleport.targetAnchor).toBeNull()
  3649. })
  3650. test('enabled teleport with null target', async () => {
  3651. const { container } = await mountWithHydration(
  3652. '<!--teleport start--><!--teleport end-->',
  3653. `<teleport to="#non-existent-target-hydrate">
  3654. <div>content</div>
  3655. </teleport>`,
  3656. )
  3657. // Align with VDOM Teleport behavior
  3658. expect(container.innerHTML).toBe(
  3659. `<!--teleport start--><!--teleport end-->`,
  3660. )
  3661. expect('Failed to locate Teleport target').toHaveBeenWarned()
  3662. })
  3663. test('enabled teleport with null target should delay child setup until target becomes available', async () => {
  3664. const version = ref('one')
  3665. const target = ref<any>('#non-existent-target-hydrate-late')
  3666. const setups: string[] = []
  3667. const Child = defineVaporComponent({
  3668. props: { msg: String },
  3669. setup(props) {
  3670. setups.push(String(props.msg))
  3671. const n0 = template('<div> </div>')() as any
  3672. const x0 = child(n0) as any
  3673. renderEffect(() => setText(x0, String(props.msg)))
  3674. return n0
  3675. },
  3676. })
  3677. const App = defineVaporComponent({
  3678. setup() {
  3679. return createComponent(
  3680. VaporTeleport,
  3681. { to: () => target.value },
  3682. {
  3683. default: () => {
  3684. const current = version.value
  3685. return createComponent(Child, { msg: () => current })
  3686. },
  3687. },
  3688. )
  3689. },
  3690. })
  3691. const container = document.createElement('div')
  3692. container.innerHTML = '<!--teleport start--><!--teleport end-->'
  3693. document.body.appendChild(container)
  3694. const app = createVaporSSRApp(App)
  3695. app.mount(container)
  3696. expect(container.innerHTML).toBe(
  3697. `<!--teleport start--><!--teleport end-->`,
  3698. )
  3699. expect(setups).toEqual([])
  3700. expect('Failed to locate Teleport target').toHaveBeenWarned()
  3701. version.value = 'two'
  3702. await nextTick()
  3703. version.value = 'three'
  3704. await nextTick()
  3705. expect(setups).toEqual([])
  3706. const targetEl = document.createElement('div')
  3707. target.value = targetEl
  3708. await nextTick()
  3709. expect(setups).toEqual(['three'])
  3710. expect(targetEl.innerHTML).toBe('<div>three</div>')
  3711. })
  3712. test('should apply css vars after hydration', async () => {
  3713. const state = reactive({ color: 'red' })
  3714. const teleportContainer = document.createElement('div')
  3715. teleportContainer.id = 'teleport-css-vars'
  3716. teleportContainer.innerHTML =
  3717. `<!--teleport start anchor-->` +
  3718. `<span>content</span>` +
  3719. `<!--teleport anchor-->`
  3720. document.body.appendChild(teleportContainer)
  3721. const App = defineVaporComponent({
  3722. setup() {
  3723. useVaporCssVars(() => state)
  3724. return createComponent(
  3725. VaporTeleport,
  3726. { to: () => '#teleport-css-vars' },
  3727. { default: () => template('<span>content</span>', true)() },
  3728. )
  3729. },
  3730. })
  3731. const container = document.createElement('div')
  3732. container.innerHTML = '<!--teleport start--><!--teleport end-->'
  3733. document.body.appendChild(container)
  3734. const app = createVaporSSRApp(App)
  3735. app.mount(container)
  3736. await nextTick()
  3737. // css vars should be applied after hydration
  3738. const span = teleportContainer.querySelector('span') as HTMLElement
  3739. expect(span).toBeTruthy()
  3740. expect(span.style.getPropertyValue('--color')).toBe('red')
  3741. expect(span.hasAttribute('data-v-owner')).toBe(true)
  3742. // css vars should update reactively
  3743. state.color = 'green'
  3744. await nextTick()
  3745. expect(span.style.getPropertyValue('--color')).toBe('green')
  3746. })
  3747. })
  3748. describe('async component', async () => {
  3749. test('async component', async () => {
  3750. const data = ref({
  3751. spy: vi.fn(),
  3752. })
  3753. const compCode = `<button @click="data.spy">hello!</button>`
  3754. const SSRComp = compileVaporComponent(compCode, data, undefined, true)
  3755. let serverResolve: any
  3756. // use defineAsyncComponent in SSR
  3757. let AsyncComp = defineAsyncComponent(
  3758. () =>
  3759. new Promise(r => {
  3760. serverResolve = r
  3761. }),
  3762. )
  3763. const appCode = `hello<components.AsyncComp/>world`
  3764. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3765. // server render
  3766. const htmlPromise = VueServerRenderer.renderToString(
  3767. runtimeDom.createSSRApp(SSRApp),
  3768. )
  3769. serverResolve(SSRComp)
  3770. const html = await htmlPromise
  3771. expect(html).toMatchInlineSnapshot(
  3772. `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  3773. )
  3774. // hydration
  3775. let clientResolve: any
  3776. AsyncComp = defineVaporAsyncComponent(
  3777. () =>
  3778. new Promise(r => {
  3779. clientResolve = r
  3780. }),
  3781. ) as any
  3782. const Comp = compileVaporComponent(compCode, data)
  3783. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3784. const container = document.createElement('div')
  3785. container.innerHTML = html
  3786. document.body.appendChild(container)
  3787. createVaporSSRApp(App).mount(container)
  3788. // hydration not complete yet
  3789. triggerEvent('click', container.querySelector('button')!)
  3790. expect(data.value.spy).not.toHaveBeenCalled()
  3791. // resolve
  3792. clientResolve(Comp)
  3793. await new Promise(r => setTimeout(r))
  3794. // should be hydrated now
  3795. triggerEvent('click', container.querySelector('button')!)
  3796. expect(data.value.spy).toHaveBeenCalled()
  3797. })
  3798. // No longer needed, parent component updates in vapor mode no longer
  3799. // cause child components to re-render
  3800. // test.todo('update async wrapper before resolve', async () => {})
  3801. test('update async component after parent mount before async component resolve', async () => {
  3802. const data = ref({
  3803. toggle: true,
  3804. })
  3805. const compCode = `
  3806. <script vapor>
  3807. defineProps(['toggle'])
  3808. </script>
  3809. <template>
  3810. <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
  3811. </template>
  3812. `
  3813. const SSRComp = compileVaporComponent(
  3814. compCode,
  3815. undefined,
  3816. undefined,
  3817. true,
  3818. )
  3819. let serverResolve: any
  3820. // use defineAsyncComponent in SSR
  3821. let AsyncComp = defineAsyncComponent(
  3822. () =>
  3823. new Promise(r => {
  3824. serverResolve = r
  3825. }),
  3826. )
  3827. const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
  3828. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3829. // server render
  3830. const htmlPromise = VueServerRenderer.renderToString(
  3831. runtimeDom.createSSRApp(SSRApp),
  3832. )
  3833. serverResolve(SSRComp)
  3834. const html = await htmlPromise
  3835. expect(html).toMatchInlineSnapshot(`"<h1>Async component</h1>"`)
  3836. // hydration
  3837. let clientResolve: any
  3838. AsyncComp = defineVaporAsyncComponent(
  3839. () =>
  3840. new Promise(r => {
  3841. clientResolve = r
  3842. }),
  3843. ) as any
  3844. const Comp = compileVaporComponent(compCode)
  3845. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3846. const container = document.createElement('div')
  3847. container.innerHTML = html
  3848. document.body.appendChild(container)
  3849. createVaporSSRApp(App).mount(container)
  3850. // update before resolve
  3851. data.value.toggle = false
  3852. await nextTick()
  3853. // resolve
  3854. clientResolve(Comp)
  3855. await new Promise(r => setTimeout(r))
  3856. // prevent lazy hydration since the component has been patched
  3857. expect('Skipping lazy hydration for component').toHaveBeenWarned()
  3858. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3859. expect(container.innerHTML).toMatchInlineSnapshot(
  3860. `"<h1>Updated async component</h1><!--async component-->"`,
  3861. )
  3862. })
  3863. test('update async component (fragment root) after parent mount before async component resolve', async () => {
  3864. const data = ref({
  3865. toggle: true,
  3866. })
  3867. const compCode = `
  3868. <script vapor>
  3869. defineProps(['toggle'])
  3870. </script>
  3871. <template>
  3872. <h1>{{ toggle ? 'Async component' : 'Updated async component' }}</h1>
  3873. <h2>fragment root</h2>
  3874. </template>
  3875. `
  3876. const SSRComp = compileVaporComponent(
  3877. compCode,
  3878. undefined,
  3879. undefined,
  3880. true,
  3881. )
  3882. let serverResolve: any
  3883. // use defineAsyncComponent in SSR
  3884. let AsyncComp = defineAsyncComponent(
  3885. () =>
  3886. new Promise(r => {
  3887. serverResolve = r
  3888. }),
  3889. )
  3890. const appCode = `<components.AsyncComp :toggle="data.toggle"/>`
  3891. const SSRApp = compileVaporComponent(appCode, data, { AsyncComp }, true)
  3892. // server render
  3893. const htmlPromise = VueServerRenderer.renderToString(
  3894. runtimeDom.createSSRApp(SSRApp),
  3895. )
  3896. serverResolve(SSRComp)
  3897. const html = await htmlPromise
  3898. expect(html).toMatchInlineSnapshot(
  3899. `"<!--[--><h1>Async component</h1><h2>fragment root</h2><!--]-->"`,
  3900. )
  3901. // hydration
  3902. let clientResolve: any
  3903. AsyncComp = defineVaporAsyncComponent(
  3904. () =>
  3905. new Promise(r => {
  3906. clientResolve = r
  3907. }),
  3908. ) as any
  3909. const Comp = compileVaporComponent(compCode)
  3910. const App = compileVaporComponent(appCode, data, { AsyncComp })
  3911. const container = document.createElement('div')
  3912. container.innerHTML = html
  3913. document.body.appendChild(container)
  3914. createVaporSSRApp(App).mount(container)
  3915. // update before resolve
  3916. data.value.toggle = false
  3917. await nextTick()
  3918. // resolve
  3919. clientResolve(Comp)
  3920. await new Promise(r => setTimeout(r))
  3921. // prevent lazy hydration since the component has been patched
  3922. expect('Skipping lazy hydration for component').toHaveBeenWarned()
  3923. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3924. expect(container.innerHTML).toMatchInlineSnapshot(
  3925. `"<!--[--><h1>Updated async component</h1><h2>fragment root</h2><!--async component--><!--]-->"`,
  3926. )
  3927. })
  3928. // required vapor Suspense
  3929. test.todo('hydrate safely when property used by async setup changed before render', async () => {})
  3930. // required vapor Suspense
  3931. test.todo('hydrate safely when property used by deep nested async setup changed before render', async () => {})
  3932. test('unmount async wrapper before load', async () => {
  3933. const data = ref({
  3934. toggle: true,
  3935. })
  3936. const compCode = `<div>async</div>`
  3937. const appCode = `
  3938. <div>
  3939. <components.AsyncComp v-if="data.toggle"/>
  3940. <div v-else>hi</div>
  3941. </div>
  3942. `
  3943. // hydration
  3944. let clientResolve: any
  3945. const AsyncComp = defineVaporAsyncComponent(
  3946. () =>
  3947. new Promise(r => {
  3948. clientResolve = r
  3949. }),
  3950. )
  3951. const Comp = compileVaporComponent(compCode)
  3952. const App = compileVaporComponent(appCode, data, {
  3953. AsyncComp,
  3954. })
  3955. const container = document.createElement('div')
  3956. container.innerHTML = '<div><div>async</div></div>'
  3957. createVaporSSRApp(App).mount(container)
  3958. // unmount before resolve
  3959. data.value.toggle = false
  3960. await nextTick()
  3961. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3962. // resolve
  3963. clientResolve(Comp)
  3964. await new Promise(r => setTimeout(r))
  3965. // should remain unmounted
  3966. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3967. })
  3968. test('unmount async wrapper before load (fragment)', async () => {
  3969. const data = ref({
  3970. toggle: true,
  3971. })
  3972. const compCode = `<div>async</div><div>fragment</div>`
  3973. const appCode = `
  3974. <div>
  3975. <components.AsyncComp v-if="data.toggle"/>
  3976. <div v-else>hi</div>
  3977. </div>
  3978. `
  3979. // hydration
  3980. let clientResolve: any
  3981. const AsyncComp = defineVaporAsyncComponent(
  3982. () =>
  3983. new Promise(r => {
  3984. clientResolve = r
  3985. }),
  3986. )
  3987. const Comp = compileVaporComponent(compCode)
  3988. const App = compileVaporComponent(appCode, data, {
  3989. AsyncComp,
  3990. })
  3991. const container = document.createElement('div')
  3992. container.innerHTML =
  3993. '<div><!--[--><div>async</div><div>fragment</div><!--]--></div>'
  3994. createVaporSSRApp(App).mount(container)
  3995. // unmount before resolve
  3996. data.value.toggle = false
  3997. await nextTick()
  3998. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  3999. // resolve
  4000. clientResolve(Comp)
  4001. await new Promise(r => setTimeout(r))
  4002. // should remain unmounted
  4003. expect(container.innerHTML).toBe(`<div><div>hi</div><!--if--></div>`)
  4004. })
  4005. test('nested async wrapper', async () => {
  4006. const toggleCode = `
  4007. <script vapor>
  4008. import { onMounted, ref, nextTick } from 'vue'
  4009. const show = ref(false)
  4010. onMounted(() => {
  4011. nextTick(() => {
  4012. show.value = true
  4013. })
  4014. })
  4015. </script>
  4016. <template>
  4017. <div v-show="show">
  4018. <slot />
  4019. </div>
  4020. </template>
  4021. `
  4022. const SSRToggle = compileVaporComponent(
  4023. toggleCode,
  4024. undefined,
  4025. undefined,
  4026. true,
  4027. )
  4028. const wrapperCode = `<slot/>`
  4029. const SSRWrapper = compileVaporComponent(
  4030. wrapperCode,
  4031. undefined,
  4032. undefined,
  4033. true,
  4034. )
  4035. const data = ref({
  4036. count: 0,
  4037. fn: vi.fn(),
  4038. })
  4039. const childCode = `
  4040. <script vapor>
  4041. import { onMounted } from 'vue'
  4042. const data = _data; const components = _components;
  4043. onMounted(() => {
  4044. data.value.fn()
  4045. data.value.count++
  4046. })
  4047. </script>
  4048. <template>
  4049. <div>{{data.count}}</div>
  4050. </template>
  4051. `
  4052. const SSRChild = compileVaporComponent(childCode, data, undefined, true)
  4053. const appCode = `
  4054. <components.Toggle>
  4055. <components.Wrapper>
  4056. <components.Wrapper>
  4057. <components.Child/>
  4058. </components.Wrapper>
  4059. </components.Wrapper>
  4060. </components.Toggle>
  4061. `
  4062. const SSRApp = compileVaporComponent(
  4063. appCode,
  4064. undefined,
  4065. {
  4066. Toggle: SSRToggle,
  4067. Wrapper: SSRWrapper,
  4068. Child: SSRChild,
  4069. },
  4070. true,
  4071. )
  4072. const root = document.createElement('div')
  4073. // server render
  4074. root.innerHTML = await VueServerRenderer.renderToString(
  4075. runtimeDom.createSSRApp(SSRApp),
  4076. )
  4077. expect(root.innerHTML).toMatchInlineSnapshot(
  4078. `"<div style="display:none;"><!--[--><!--[--><!--[--><div>0</div><!--]--><!--]--><!--]--></div>"`,
  4079. )
  4080. const Toggle = compileVaporComponent(toggleCode)
  4081. const Wrapper = compileVaporComponent(wrapperCode)
  4082. const Child = compileVaporComponent(childCode, data)
  4083. const App = compileVaporComponent(appCode, undefined, {
  4084. Toggle,
  4085. Wrapper,
  4086. Child,
  4087. })
  4088. // hydration
  4089. createVaporSSRApp(App).mount(root)
  4090. await nextTick()
  4091. await nextTick()
  4092. expect(root.innerHTML).toMatchInlineSnapshot(
  4093. `"<div style=""><!--[--><!--[--><!--[--><div>1</div><!--]--><!--]--><!--]--></div>"`,
  4094. )
  4095. expect(data.value.fn).toBeCalledTimes(1)
  4096. })
  4097. })
  4098. describe.todo('Suspense')
  4099. describe('force hydrate prop', async () => {
  4100. test('force hydrate prop with `.prop` modifier', async () => {
  4101. const { container } = await mountWithHydration(
  4102. '<input type="checkbox">',
  4103. `<input type="checkbox" .indeterminate="true"/>`,
  4104. )
  4105. expect((container.firstChild! as any).indeterminate).toBe(true)
  4106. })
  4107. test('force hydrate input v-model with non-string value bindings', async () => {
  4108. const { container } = await mountWithHydration(
  4109. '<input type="checkbox" value="true">',
  4110. `<input type="checkbox" :true-value="true"/>`,
  4111. )
  4112. expect((container.firstChild as any)._trueValue).toBe(true)
  4113. })
  4114. test('force hydrate checkbox with indeterminate', async () => {
  4115. const { container } = await mountWithHydration(
  4116. '<input type="checkbox" indeterminate/>',
  4117. `<input type="checkbox" :indeterminate="true"/>`,
  4118. )
  4119. expect((container.firstChild! as any).indeterminate).toBe(true)
  4120. })
  4121. test('force hydrate select option with non-string value bindings', async () => {
  4122. const { container } = await mountWithHydration(
  4123. '<select><option value="true">ok</option></select>',
  4124. `<select><option :value="true">ok</option></select>`,
  4125. )
  4126. expect((container.firstChild!.firstChild as any)._value).toBe(true)
  4127. })
  4128. test('force hydrate v-bind with .prop modifiers', async () => {
  4129. const { container } = await mountWithHydration(
  4130. '<div .foo="true"/>',
  4131. `<div v-bind="data"/>`,
  4132. ref({ '.foo': true }),
  4133. )
  4134. expect((container.firstChild! as any).foo).toBe(true)
  4135. })
  4136. test('force hydrate custom element with dynamic props', () => {
  4137. class MyElement extends HTMLElement {
  4138. foo = ''
  4139. constructor() {
  4140. super()
  4141. }
  4142. }
  4143. customElements.define('my-element-7203', MyElement)
  4144. const msg = ref('bar')
  4145. const container = document.createElement('div')
  4146. container.innerHTML = '<my-element-7203></my-element-7203>'
  4147. const app = createVaporSSRApp({
  4148. setup() {
  4149. return createPlainElement('my-element-7203', { foo: () => msg.value })
  4150. },
  4151. })
  4152. app.mount(container)
  4153. expect((container.firstChild as any).foo).toBe(msg.value)
  4154. })
  4155. })
  4156. })
  4157. describe('mismatch handling', () => {
  4158. test('text node', async () => {
  4159. const foo = ref('bar')
  4160. const { container } = await mountWithHydration(`foo`, `{{data}}`, foo)
  4161. expect(container.textContent).toBe('bar')
  4162. expect(`Hydration text mismatch`).toHaveBeenWarned()
  4163. })
  4164. test('element text content', async () => {
  4165. const data = ref({ textContent: 'bar' })
  4166. const { container } = await mountWithHydration(
  4167. `<div>foo</div>`,
  4168. `<div v-bind="data"></div>`,
  4169. data,
  4170. )
  4171. expect(container.innerHTML).toBe('<div>bar</div>')
  4172. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  4173. })
  4174. // test('not enough children', () => {
  4175. // const { container } = mountWithHydration(`<div></div>`, () =>
  4176. // h('div', [h('span', 'foo'), h('span', 'bar')]),
  4177. // )
  4178. // expect(container.innerHTML).toBe(
  4179. // '<div><span>foo</span><span>bar</span></div>',
  4180. // )
  4181. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  4182. // })
  4183. // test('too many children', () => {
  4184. // const { container } = mountWithHydration(
  4185. // `<div><span>foo</span><span>bar</span></div>`,
  4186. // () => h('div', [h('span', 'foo')]),
  4187. // )
  4188. // expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  4189. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  4190. // })
  4191. test('complete mismatch', async () => {
  4192. const data = ref('span')
  4193. const { container } = await mountWithHydration(
  4194. `<div>foo</div>`,
  4195. `<component :is="data">foo</component>`,
  4196. data,
  4197. )
  4198. expect(container.innerHTML).toBe('<span>foo</span><!--dynamic-component-->')
  4199. expect(`Hydration node mismatch`).toHaveBeenWarned()
  4200. })
  4201. // test('fragment mismatch removal', () => {
  4202. // const { container } = mountWithHydration(
  4203. // `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  4204. // () => h('div', [h('span', 'replaced')]),
  4205. // )
  4206. // expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  4207. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  4208. // })
  4209. // test('fragment not enough children', () => {
  4210. // const { container } = mountWithHydration(
  4211. // `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  4212. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  4213. // )
  4214. // expect(container.innerHTML).toBe(
  4215. // '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  4216. // )
  4217. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  4218. // })
  4219. // test('fragment too many children', () => {
  4220. // const { container } = mountWithHydration(
  4221. // `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  4222. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  4223. // )
  4224. // expect(container.innerHTML).toBe(
  4225. // '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  4226. // )
  4227. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  4228. // // as 2nd fragment child.
  4229. // expect(`Hydration text content mismatch`).toHaveBeenWarned()
  4230. // // excessive children removal
  4231. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  4232. // })
  4233. // test('Teleport target has empty children', () => {
  4234. // const teleportContainer = document.createElement('div')
  4235. // teleportContainer.id = 'teleport'
  4236. // document.body.appendChild(teleportContainer)
  4237. // mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  4238. // h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  4239. // )
  4240. // expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  4241. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  4242. // })
  4243. // test('comment mismatch (element)', () => {
  4244. // const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  4245. // h('div', [createCommentVNode('hi')]),
  4246. // )
  4247. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  4248. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  4249. // })
  4250. // test('comment mismatch (text)', () => {
  4251. // const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  4252. // h('div', [createCommentVNode('hi')]),
  4253. // )
  4254. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  4255. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  4256. // })
  4257. test('class mismatch', async () => {
  4258. await mountWithHydration(
  4259. `<div class="foo bar"></div>`,
  4260. `<div :class="data"></div>`,
  4261. ref(['foo', 'bar']),
  4262. )
  4263. await mountWithHydration(
  4264. `<div class="foo bar"></div>`,
  4265. `<div :class="data"></div>`,
  4266. ref({ foo: true, bar: true }),
  4267. )
  4268. await mountWithHydration(
  4269. `<div class="foo bar"></div>`,
  4270. `<div :class="data"></div>`,
  4271. ref('foo bar'),
  4272. )
  4273. // svg classes
  4274. await mountWithHydration(
  4275. `<svg class="foo bar"></svg>`,
  4276. `<svg :class="data"></svg>`,
  4277. ref('foo bar'),
  4278. )
  4279. // class with different order
  4280. await mountWithHydration(
  4281. `<div class="foo bar"></div>`,
  4282. `<div :class="data"></div>`,
  4283. ref('bar foo'),
  4284. )
  4285. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  4286. // single root mismatch
  4287. const { container: root } = await mountWithHydration(
  4288. `<div class="foo bar"></div>`,
  4289. `<div :class="data"></div>`,
  4290. ref('baz'),
  4291. )
  4292. expect(root.innerHTML).toBe('<div class="foo bar baz"></div>')
  4293. expect(`Hydration class mismatch`).toHaveBeenWarned()
  4294. // multiple root mismatch
  4295. const { container } = await mountWithHydration(
  4296. `<div class="foo bar"></div><span/>`,
  4297. `<div :class="data"></div><span/>`,
  4298. ref('foo'),
  4299. )
  4300. expect(container.innerHTML).toBe('<div class="foo"></div><span></span>')
  4301. expect(`Hydration class mismatch`).toHaveBeenWarned()
  4302. })
  4303. test('style mismatch', async () => {
  4304. await mountWithHydration(
  4305. `<div style="color:red;"></div>`,
  4306. `<div :style="data"></div>`,
  4307. ref({ color: 'red' }),
  4308. )
  4309. await mountWithHydration(
  4310. `<div style="color:red;"></div>`,
  4311. `<div :style="data"></div>`,
  4312. ref('color:red;'),
  4313. )
  4314. // style with different order
  4315. await mountWithHydration(
  4316. `<div style="color:red; font-size: 12px;"></div>`,
  4317. `<div :style="data"></div>`,
  4318. ref(`font-size: 12px; color:red;`),
  4319. )
  4320. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4321. // single root mismatch
  4322. const { container: root } = await mountWithHydration(
  4323. `<div style="color:red;"></div>`,
  4324. `<div :style="data"></div>`,
  4325. ref({ color: 'green' }),
  4326. )
  4327. expect(root.innerHTML).toBe('<div style="color: green;"></div>')
  4328. expect(`Hydration style mismatch`).toHaveBeenWarned()
  4329. // multiple root mismatch
  4330. const { container } = await mountWithHydration(
  4331. `<div style="color:red;"></div><span/>`,
  4332. `<div :style="data"></div><span/>`,
  4333. ref({ color: 'green' }),
  4334. )
  4335. expect(container.innerHTML).toBe(
  4336. '<div style="color: green;"></div><span></span>',
  4337. )
  4338. expect(`Hydration style mismatch`).toHaveBeenWarned()
  4339. })
  4340. test('style mismatch when no style attribute is present', async () => {
  4341. await mountWithHydration(
  4342. `<div></div>`,
  4343. `<div :style="data"></div>`,
  4344. ref({ color: 'red' }),
  4345. )
  4346. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  4347. })
  4348. test('style mismatch w/ v-show', async () => {
  4349. await mountWithHydration(
  4350. `<div style="color:red;display:none"></div>`,
  4351. `<div v-show="data" style="color: red;"></div>`,
  4352. ref(false),
  4353. )
  4354. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4355. // mismatch with single root
  4356. const { container: root } = await mountWithHydration(
  4357. `<div style="color:red;"></div>`,
  4358. `<div v-show="data" style="color: red;"></div>`,
  4359. ref(false),
  4360. )
  4361. expect(root.innerHTML).toBe(
  4362. '<div style="color: red; display: none;"></div>',
  4363. )
  4364. expect(`Hydration style mismatch`).toHaveBeenWarned()
  4365. // mismatch with multiple root
  4366. const { container } = await mountWithHydration(
  4367. `<div style="color:red;"></div><span/>`,
  4368. `<div v-show="data.show" :style="data.style"></div><span/>`,
  4369. ref({ show: false, style: 'color: red' }),
  4370. )
  4371. expect(container.innerHTML).toBe(
  4372. '<div style="color: red; display: none;"></div><span></span>',
  4373. )
  4374. expect(`Hydration style mismatch`).toHaveBeenWarned()
  4375. })
  4376. test('attr mismatch', async () => {
  4377. await mountWithHydration(
  4378. `<div id="foo"></div>`,
  4379. `<div :id="data"></div>`,
  4380. ref('foo'),
  4381. )
  4382. await mountWithHydration(
  4383. `<div spellcheck></div>`,
  4384. `<div :spellcheck="data"></div>`,
  4385. ref(''),
  4386. )
  4387. await mountWithHydration(
  4388. `<div></div>`,
  4389. `<div :id="data"></div>`,
  4390. ref(undefined),
  4391. )
  4392. // boolean
  4393. await mountWithHydration(
  4394. `<select multiple></div>`,
  4395. `<select :multiple="data"></select>`,
  4396. ref(true),
  4397. )
  4398. await mountWithHydration(
  4399. `<select multiple></div>`,
  4400. `<select :multiple="data"></select>`,
  4401. ref('multiple'),
  4402. )
  4403. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4404. await mountWithHydration(
  4405. `<div></div>`,
  4406. `<div :id="data"></div>`,
  4407. ref('foo'),
  4408. )
  4409. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  4410. await mountWithHydration(
  4411. `<div id="bar"></div>`,
  4412. `<div :id="data"></div>`,
  4413. ref('foo'),
  4414. )
  4415. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  4416. })
  4417. test('attr special case: textarea value', async () => {
  4418. await mountWithHydration(
  4419. `<textarea>foo</textarea>`,
  4420. `<textarea :value="data"></textarea>`,
  4421. ref('foo'),
  4422. )
  4423. await mountWithHydration(
  4424. `<textarea></textarea>`,
  4425. `<textarea :value="data"></textarea>`,
  4426. ref(''),
  4427. )
  4428. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4429. await mountWithHydration(
  4430. `<textarea>foo</textarea>`,
  4431. `<textarea :value="data"></textarea>`,
  4432. ref('bar'),
  4433. )
  4434. expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  4435. })
  4436. test('<textarea> with newlines at the beginning', async () => {
  4437. await mountWithHydration(
  4438. `<textarea>\nhello</textarea>`,
  4439. `<textarea :value="data"></textarea>`,
  4440. ref('\nhello'),
  4441. )
  4442. await mountWithHydration(
  4443. `<textarea>\nhello</textarea>`,
  4444. `<textarea v-text="data"></textarea>`,
  4445. ref('\nhello'),
  4446. )
  4447. await mountWithHydration(
  4448. `<textarea>\nhello</textarea>`,
  4449. `<textarea v-bind="data"></textarea>`,
  4450. ref({ textContent: '\nhello' }),
  4451. )
  4452. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4453. })
  4454. test('<pre> with newlines at the beginning', async () => {
  4455. await mountWithHydration(`<pre>\n</pre>`, `<pre>{{data}}</pre>`, ref('\n'))
  4456. await mountWithHydration(
  4457. `<pre>\n</pre>`,
  4458. `<pre v-text="data"></pre>`,
  4459. ref('\n'),
  4460. )
  4461. await mountWithHydration(
  4462. `<pre>\n</pre>`,
  4463. `<pre v-bind="data"></pre>`,
  4464. ref({ textContent: '\n' }),
  4465. )
  4466. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4467. })
  4468. test('boolean attr handling', async () => {
  4469. await mountWithHydration(
  4470. `<input />`,
  4471. `<input :readonly="data" />`,
  4472. ref(false),
  4473. )
  4474. await mountWithHydration(
  4475. `<input readonly />`,
  4476. `<input :readonly="data" />`,
  4477. ref(true),
  4478. )
  4479. await mountWithHydration(
  4480. `<input readonly="readonly" />`,
  4481. `<input :readonly="data" />`,
  4482. ref(true),
  4483. )
  4484. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4485. })
  4486. test('client value is null or undefined', async () => {
  4487. await mountWithHydration(
  4488. `<div></div>`,
  4489. `<div :draggable="data"></div>`,
  4490. ref(undefined),
  4491. )
  4492. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4493. await mountWithHydration(`<input />`, `<input :type="data" />`, ref(null))
  4494. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4495. })
  4496. test('should not warn against object values', async () => {
  4497. await mountWithHydration(`<input />`, `<input :from="data" />`, ref({}))
  4498. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4499. })
  4500. test('should not warn on falsy bindings of non-property keys', async () => {
  4501. await mountWithHydration(
  4502. `<button></button>`,
  4503. `<button :href="data"></button>`,
  4504. ref(undefined),
  4505. )
  4506. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4507. })
  4508. test('should not warn on non-renderable option values', async () => {
  4509. await mountWithHydration(
  4510. `<select><option>hello</option></select>`,
  4511. `<select><option :value="data">hello</option></select>`,
  4512. ref(['foo']),
  4513. )
  4514. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4515. })
  4516. test('should not warn css v-bind', async () => {
  4517. const container = document.createElement('div')
  4518. container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  4519. const app = createVaporSSRApp({
  4520. setup() {
  4521. useVaporCssVars(() => ({ foo: 'red' }))
  4522. const n0 = template('<div></div>', true)() as any
  4523. renderEffect(() => setStyle(n0, { color: 'var(--foo)' }))
  4524. return n0
  4525. },
  4526. })
  4527. app.mount(container)
  4528. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4529. })
  4530. test('css vars should only be added to expected on component root dom', () => {
  4531. const container = document.createElement('div')
  4532. container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  4533. const app = createVaporSSRApp({
  4534. setup() {
  4535. useVaporCssVars(() => ({ foo: 'red' }))
  4536. const n0 = template('<div><div></div></div>', true)() as any
  4537. const n1 = child(n0) as any
  4538. renderEffect(() => setStyle(n1, { color: 'var(--foo)' }))
  4539. return n0
  4540. },
  4541. })
  4542. app.mount(container)
  4543. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4544. })
  4545. test('css vars support fallthrough', () => {
  4546. const container = document.createElement('div')
  4547. container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
  4548. const app = createVaporSSRApp({
  4549. setup() {
  4550. useVaporCssVars(() => ({ foo: 'red' }))
  4551. return createComponent(Child)
  4552. },
  4553. })
  4554. const Child = defineVaporComponent({
  4555. setup() {
  4556. const n0 = template('<div></div>', true)() as any
  4557. renderEffect(() => setStyle(n0, { padding: '4px' }))
  4558. return n0
  4559. },
  4560. })
  4561. app.mount(container)
  4562. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4563. })
  4564. // vapor directive does not have a created hook
  4565. test('should not warn for directives that mutate DOM in created', () => {
  4566. // const container = document.createElement('div')
  4567. // container.innerHTML = `<div class="test red"></div>`
  4568. // const vColor: ObjectDirective = {
  4569. // created(el, binding) {
  4570. // el.classList.add(binding.value)
  4571. // },
  4572. // }
  4573. // const app = createSSRApp({
  4574. // setup() {
  4575. // return () =>
  4576. // withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
  4577. // },
  4578. // })
  4579. // app.mount(container)
  4580. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4581. })
  4582. test('escape css var name', () => {
  4583. const container = document.createElement('div')
  4584. container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
  4585. const app = createVaporSSRApp({
  4586. setup() {
  4587. useVaporCssVars(() => ({ 'foo.bar': 'red' }))
  4588. return createComponent(Child)
  4589. },
  4590. })
  4591. const Child = defineVaporComponent({
  4592. setup() {
  4593. const n0 = template('<div></div>', true)() as any
  4594. renderEffect(() => setStyle(n0, { padding: '4px' }))
  4595. return n0
  4596. },
  4597. })
  4598. app.mount(container)
  4599. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4600. })
  4601. })
  4602. describe('data-allow-mismatch', () => {
  4603. test('element text content', async () => {
  4604. const data = ref({ textContent: 'bar' })
  4605. const { container } = await mountWithHydration(
  4606. `<div data-allow-mismatch="text">foo</div>`,
  4607. `<div v-bind="data"></div>`,
  4608. data,
  4609. )
  4610. expect(container.innerHTML).toBe(
  4611. '<div data-allow-mismatch="text">bar</div>',
  4612. )
  4613. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4614. })
  4615. // test('not enough children', () => {
  4616. // const { container } = mountWithHydration(
  4617. // `<div data-allow-mismatch="children"></div>`,
  4618. // () => h('div', [h('span', 'foo'), h('span', 'bar')]),
  4619. // )
  4620. // expect(container.innerHTML).toBe(
  4621. // '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
  4622. // )
  4623. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4624. // })
  4625. // test('too many children', () => {
  4626. // const { container } = mountWithHydration(
  4627. // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  4628. // () => h('div', [h('span', 'foo')]),
  4629. // )
  4630. // expect(container.innerHTML).toBe(
  4631. // '<div data-allow-mismatch="children"><span>foo</span></div>',
  4632. // )
  4633. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4634. // })
  4635. test('complete mismatch', async () => {
  4636. const { container } = await mountWithHydration(
  4637. `<div data-allow-mismatch="children"><div>foo</div></div>`,
  4638. `<div><component :is="data">foo</component></div>`,
  4639. ref('span'),
  4640. )
  4641. expect(container.innerHTML).toBe(
  4642. '<div data-allow-mismatch="children"><span>foo</span><!--dynamic-component--></div>',
  4643. )
  4644. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4645. })
  4646. // test('fragment mismatch removal', () => {
  4647. // const { container } = mountWithHydration(
  4648. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  4649. // () => h('div', [h('span', 'replaced')]),
  4650. // )
  4651. // expect(container.innerHTML).toBe(
  4652. // '<div data-allow-mismatch="children"><span>replaced</span></div>',
  4653. // )
  4654. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4655. // })
  4656. // test('fragment not enough children', () => {
  4657. // const { container } = mountWithHydration(
  4658. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  4659. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  4660. // )
  4661. // expect(container.innerHTML).toBe(
  4662. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  4663. // )
  4664. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4665. // })
  4666. // test('fragment too many children', () => {
  4667. // const { container } = mountWithHydration(
  4668. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  4669. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  4670. // )
  4671. // expect(container.innerHTML).toBe(
  4672. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  4673. // )
  4674. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  4675. // // as 2nd fragment child.
  4676. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  4677. // // excessive children removal
  4678. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  4679. // })
  4680. // test('comment mismatch (element)', () => {
  4681. // const { container } = mountWithHydration(
  4682. // `<div data-allow-mismatch="children"><span></span></div>`,
  4683. // () => h('div', [createCommentVNode('hi')]),
  4684. // )
  4685. // expect(container.innerHTML).toBe(
  4686. // '<div data-allow-mismatch="children"><!--hi--></div>',
  4687. // )
  4688. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4689. // })
  4690. // test('comment mismatch (text)', () => {
  4691. // const { container } = mountWithHydration(
  4692. // `<div data-allow-mismatch="children">foobar</div>`,
  4693. // () => h('div', [createCommentVNode('hi')]),
  4694. // )
  4695. // expect(container.innerHTML).toBe(
  4696. // '<div data-allow-mismatch="children"><!--hi--></div>',
  4697. // )
  4698. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4699. // })
  4700. test('class mismatch', async () => {
  4701. await mountWithHydration(
  4702. `<div class="foo bar" data-allow-mismatch="class"></div>`,
  4703. `<div :class="data"></div>`,
  4704. ref('foo'),
  4705. )
  4706. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  4707. })
  4708. test('style mismatch', async () => {
  4709. await mountWithHydration(
  4710. `<div style="color:red;" data-allow-mismatch="style"></div>`,
  4711. `<div :style="data"></div>`,
  4712. ref({ color: 'green' }),
  4713. )
  4714. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  4715. })
  4716. test('attr mismatch', async () => {
  4717. await mountWithHydration(
  4718. `<div data-allow-mismatch="attribute"></div>`,
  4719. `<div :id="data"></div>`,
  4720. ref('foo'),
  4721. )
  4722. await mountWithHydration(
  4723. `<div id="bar" data-allow-mismatch="attribute"></div>`,
  4724. `<div :id="data"></div>`,
  4725. ref('foo'),
  4726. )
  4727. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  4728. })
  4729. })
  4730. describe('VDOM interop', () => {
  4731. // Previous tests (e.g. createVaporSSRApp) leave isHydratingEnabled = true.
  4732. beforeEach(() => {
  4733. setIsHydratingEnabled(false)
  4734. })
  4735. test('basic render vapor component', async () => {
  4736. const data = ref(true)
  4737. const { container } = await testWithVDOMApp(
  4738. `<script setup>const data = _data; const components = _components;</script>
  4739. <template>
  4740. <components.VaporChild/>
  4741. </template>`,
  4742. {
  4743. VaporChild: {
  4744. code: `<template>{{ data }}</template>`,
  4745. vapor: true,
  4746. },
  4747. },
  4748. data,
  4749. )
  4750. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
  4751. data.value = false
  4752. await nextTick()
  4753. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
  4754. })
  4755. test('nested components (VDOM -> Vapor -> VDOM)', async () => {
  4756. const data = ref(true)
  4757. const { container } = await testWithVDOMApp(
  4758. `<script setup>const data = _data; const components = _components;</script>
  4759. <template>
  4760. <components.VaporChild/>
  4761. </template>`,
  4762. {
  4763. VaporChild: {
  4764. code: `<template><components.VdomChild/></template>`,
  4765. vapor: true,
  4766. },
  4767. VdomChild: {
  4768. code: `<script setup>const data = _data;</script>
  4769. <template>{{ data }}</template>`,
  4770. vapor: false,
  4771. },
  4772. },
  4773. data,
  4774. )
  4775. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
  4776. data.value = false
  4777. await nextTick()
  4778. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
  4779. })
  4780. test('nested components (VDOM -> Vapor(multi-root) -> VDOM)', async () => {
  4781. const data = ref('foo')
  4782. const { container } = await testWithVDOMApp(
  4783. `<script setup>const data = _data; const components = _components;</script>
  4784. <template>
  4785. <components.VaporChild/>
  4786. </template>`,
  4787. {
  4788. // Vapor component with multiple root nodes, VDOM child as first element
  4789. // This ensures hydration starts at <!--[--> and tests skipFragmentAnchor
  4790. VaporChild: {
  4791. code: `<template><components.VdomChild/><div>second</div></template>`,
  4792. vapor: true,
  4793. },
  4794. VdomChild: {
  4795. code: `<script setup>const data = _data;</script>
  4796. <template><span>{{ data }}</span></template>`,
  4797. vapor: false,
  4798. },
  4799. },
  4800. data,
  4801. )
  4802. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4803. `
  4804. "
  4805. <!--[--><span>foo</span><div>second</div><!--]-->
  4806. "
  4807. `,
  4808. )
  4809. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4810. data.value = 'bar'
  4811. await nextTick()
  4812. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4813. `
  4814. "
  4815. <!--[--><span>bar</span><div>second</div><!--]-->
  4816. "
  4817. `,
  4818. )
  4819. })
  4820. test('nested components (VDOM -> Vapor(multi-root) -> VDOM) with preceding sibling', async () => {
  4821. const data = ref('foo')
  4822. const { container } = await testWithVDOMApp(
  4823. `<script setup>const data = _data; const components = _components;</script>
  4824. <template>
  4825. <p>before</p>
  4826. <components.VaporChild/>
  4827. </template>`,
  4828. {
  4829. VaporChild: {
  4830. code: `<template><components.VdomChild/><div>second</div></template>`,
  4831. vapor: true,
  4832. },
  4833. VdomChild: {
  4834. code: `<script setup>const data = _data;</script>
  4835. <template><span>{{ data }}</span></template>`,
  4836. vapor: false,
  4837. },
  4838. },
  4839. data,
  4840. )
  4841. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4842. `
  4843. "
  4844. <!--[--><p>before</p>
  4845. <!--[--><span>foo</span><div>second</div><!--]-->
  4846. <!--]-->
  4847. "
  4848. `,
  4849. )
  4850. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4851. data.value = 'bar'
  4852. await nextTick()
  4853. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4854. `
  4855. "
  4856. <!--[--><p>before</p>
  4857. <!--[--><span>bar</span><div>second</div><!--]-->
  4858. <!--]-->
  4859. "
  4860. `,
  4861. )
  4862. })
  4863. test('nested components (VDOM -> Vapor) should not duplicate', async () => {
  4864. const { container } = await testWithVDOMApp(
  4865. `<script setup>const components = _components;</script>
  4866. <template>
  4867. <components.VaporChild/>
  4868. </template>`,
  4869. {
  4870. VaporChild: {
  4871. code: `<script vapor>
  4872. import { ref } from 'vue'
  4873. const show = ref(true)
  4874. </script>
  4875. <template>
  4876. <template v-if="show">
  4877. <div>1</div>
  4878. <div>2</div>
  4879. </template>
  4880. </template>`,
  4881. vapor: true,
  4882. },
  4883. },
  4884. )
  4885. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4886. `
  4887. "
  4888. <!--[--><div>1</div><div>2</div><!--]-->
  4889. "
  4890. `,
  4891. )
  4892. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  4893. })
  4894. test('nested components (VDOM -> Vapor -> VDOM (with slot fallback))', async () => {
  4895. const data = ref(true)
  4896. const { container } = await testWithVDOMApp(
  4897. `<script setup>const data = _data; const components = _components;</script>
  4898. <template>
  4899. <components.VaporChild/>
  4900. </template>`,
  4901. {
  4902. VaporChild: {
  4903. code: `<template><components.VdomChild/></template>`,
  4904. vapor: true,
  4905. },
  4906. VdomChild: {
  4907. code: `<script setup>const data = _data;</script>
  4908. <template><slot><span>{{data}}</span></slot></template>`,
  4909. vapor: false,
  4910. },
  4911. },
  4912. data,
  4913. )
  4914. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4915. `
  4916. "
  4917. <!--[--><span>true</span><!--]-->
  4918. "
  4919. `,
  4920. )
  4921. data.value = false
  4922. await nextTick()
  4923. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4924. `
  4925. "
  4926. <!--[--><span>false</span><!--]-->
  4927. "
  4928. `,
  4929. )
  4930. })
  4931. test('nested components (VDOM -> Vapor(with slot content) -> VDOM)', async () => {
  4932. const data = ref(true)
  4933. const { container } = await testWithVDOMApp(
  4934. `<script setup>const data = _data; const components = _components;</script>
  4935. <template>
  4936. <components.VaporChild/>
  4937. </template>`,
  4938. {
  4939. VaporChild: {
  4940. code: `<template>
  4941. <components.VdomChild>
  4942. <template #default>
  4943. <span>{{data}} vapor fallback</span>
  4944. </template>
  4945. </components.VdomChild>
  4946. </template>`,
  4947. vapor: true,
  4948. },
  4949. VdomChild: {
  4950. code: `<script setup>const data = _data;</script>
  4951. <template><slot><span>vdom fallback</span></slot></template>`,
  4952. vapor: false,
  4953. },
  4954. },
  4955. data,
  4956. )
  4957. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4958. `
  4959. "
  4960. <!--[--><span>true vapor fallback</span><!--]-->
  4961. "
  4962. `,
  4963. )
  4964. data.value = false
  4965. await nextTick()
  4966. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  4967. `
  4968. "
  4969. <!--[--><span>false vapor fallback</span><!--]-->
  4970. "
  4971. `,
  4972. )
  4973. })
  4974. test('nested components (VDOM -> Vapor(with slot content) -> Vapor)', async () => {
  4975. const data = ref(true)
  4976. const { container } = await testWithVDOMApp(
  4977. `<script setup>const data = _data; const components = _components;</script>
  4978. <template>
  4979. <components.VaporChild/>
  4980. </template>`,
  4981. {
  4982. VaporChild: {
  4983. code: `<template>
  4984. <components.VaporChild2>
  4985. <template #default>
  4986. <span>{{data}} vapor fallback</span>
  4987. </template>
  4988. </components.VaporChild2>
  4989. </template>`,
  4990. vapor: true,
  4991. },
  4992. VaporChild2: {
  4993. code: `<template><slot><span>vapor fallback2</span></slot></template>`,
  4994. vapor: true,
  4995. },
  4996. },
  4997. data,
  4998. )
  4999. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5000. `
  5001. "
  5002. <!--[--><span>true vapor fallback</span><!--]-->
  5003. "
  5004. `,
  5005. )
  5006. data.value = false
  5007. await nextTick()
  5008. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5009. `
  5010. "
  5011. <!--[--><span>false vapor fallback</span><!--]-->
  5012. "
  5013. `,
  5014. )
  5015. })
  5016. test('vapor slot render vdom component', async () => {
  5017. const data = ref(true)
  5018. const { container } = await testWithVDOMApp(
  5019. `<script setup>const data = _data; const components = _components;</script>
  5020. <template>
  5021. <components.VaporChild>
  5022. <components.VdomChild/>
  5023. </components.VaporChild>
  5024. </template>`,
  5025. {
  5026. VaporChild: {
  5027. code: `<template><div><slot/></div></template>`,
  5028. vapor: true,
  5029. },
  5030. VdomChild: {
  5031. code: `<script setup>const data = _data;</script>
  5032. <template>{{ data }}</template>`,
  5033. vapor: false,
  5034. },
  5035. },
  5036. data,
  5037. )
  5038. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5039. `
  5040. "<div>
  5041. <!--[-->true<!--]-->
  5042. </div>"
  5043. `,
  5044. )
  5045. data.value = false
  5046. await nextTick()
  5047. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5048. `
  5049. "<div>
  5050. <!--[-->false<!--]-->
  5051. </div>"
  5052. `,
  5053. )
  5054. })
  5055. test('vapor slot render vdom component (multi-root slot content)', async () => {
  5056. const data = ref('foo')
  5057. const { container } = await testWithVaporApp(
  5058. `<script setup>const data = _data; const components = _components;</script>
  5059. <template>
  5060. <components.VaporChild>
  5061. <components.VdomChild/>
  5062. <div>vapor content</div>
  5063. </components.VaporChild>
  5064. </template>`,
  5065. {
  5066. VaporChild: {
  5067. code: `<template><div><slot/></div></template>`,
  5068. vapor: true,
  5069. },
  5070. VdomChild: {
  5071. code: `<script setup>const data = _data;</script>
  5072. <template><span>{{ data }}</span></template>`,
  5073. vapor: false,
  5074. },
  5075. },
  5076. data,
  5077. )
  5078. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5079. `
  5080. "<div>
  5081. <!--[--><span>foo</span><div>vapor content</div><!--]-->
  5082. </div>"
  5083. `,
  5084. )
  5085. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5086. data.value = 'bar'
  5087. await nextTick()
  5088. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5089. `
  5090. "<div>
  5091. <!--[--><span>bar</span><div>vapor content</div><!--]-->
  5092. </div>"
  5093. `,
  5094. )
  5095. })
  5096. test('vapor slot render vdom component (render function)', async () => {
  5097. const data = ref(true)
  5098. const { container } = await testWithVaporApp(
  5099. `<script setup>
  5100. import { h } from 'vue'
  5101. const data = _data; const components = _components;
  5102. const VdomChild = {
  5103. setup() {
  5104. return () => h('div', null, [h('div', [String(data.value)])])
  5105. }
  5106. }
  5107. </script>
  5108. <template>
  5109. <components.VaporChild>
  5110. <VdomChild/>
  5111. </components.VaporChild>
  5112. </template>`,
  5113. {
  5114. VaporChild: {
  5115. code: `<template><div><slot/></div></template>`,
  5116. vapor: true,
  5117. },
  5118. },
  5119. data,
  5120. )
  5121. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5122. `
  5123. "<div>
  5124. <!--[--><div><div>true</div></div><!--]-->
  5125. </div>"
  5126. `,
  5127. )
  5128. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5129. data.value = false
  5130. await nextTick()
  5131. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5132. `
  5133. "<div>
  5134. <!--[--><div><div>false</div></div><!--]-->
  5135. </div>"
  5136. `,
  5137. )
  5138. })
  5139. test('hydrate VNode rendered via createDynamicComponent', async () => {
  5140. const data = ref('foo')
  5141. const { container } = await testWithVaporApp(
  5142. `<script setup>
  5143. import { h } from 'vue'
  5144. const data = _data; const components = _components;
  5145. // Simulating RouterView pattern: VDOM component passes VNode through slot
  5146. const RouterView = {
  5147. setup(_, { slots }) {
  5148. return () => {
  5149. const component = h(components.VaporChild)
  5150. return slots.default({ Component: component })
  5151. }
  5152. }
  5153. }
  5154. </script>
  5155. <template>
  5156. <RouterView v-slot="{ Component }">
  5157. <component :is="Component" />
  5158. </RouterView>
  5159. </template>`,
  5160. {
  5161. VaporChild: {
  5162. code: `<template><div>{{ data }}</div></template>`,
  5163. vapor: true,
  5164. },
  5165. },
  5166. data,
  5167. )
  5168. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5169. `
  5170. "
  5171. <!--[--><div>foo</div><!--dynamic-component--><!--]-->
  5172. "
  5173. `,
  5174. )
  5175. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5176. data.value = 'bar'
  5177. await nextTick()
  5178. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5179. `
  5180. "
  5181. <!--[--><div>bar</div><!--dynamic-component--><!--]-->
  5182. "
  5183. `,
  5184. )
  5185. })
  5186. test('hydrate static VNode chunk rendered via createDynamicComponent', async () => {
  5187. const data = ref('foo')
  5188. const { container } = await testWithVaporApp(
  5189. `<script setup>
  5190. import { createStaticVNode } from 'vue'
  5191. const data = _data
  5192. const StaticChunk = createStaticVNode(
  5193. '<div>first static</div><div>second static</div>',
  5194. 2,
  5195. )
  5196. </script>
  5197. <template>
  5198. <div>
  5199. <component :is="StaticChunk" />
  5200. </div>
  5201. <span>{{ data }}</span>
  5202. </template>`,
  5203. undefined,
  5204. data,
  5205. )
  5206. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5207. `
  5208. "
  5209. <!--[--><div><div>first static</div><div>second static</div><!--dynamic-component--></div><span>foo</span><!--]-->
  5210. "
  5211. `,
  5212. )
  5213. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5214. data.value = 'bar'
  5215. await nextTick()
  5216. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5217. `
  5218. "
  5219. <!--[--><div><div>first static</div><div>second static</div><!--dynamic-component--></div><span>bar</span><!--]-->
  5220. "
  5221. `,
  5222. )
  5223. })
  5224. test('hydrate Fragment VNode rendered via createDynamicComponent', async () => {
  5225. const data = ref('foo')
  5226. const { container } = await testWithVaporApp(
  5227. `<script setup>
  5228. import { Fragment, h } from 'vue'
  5229. const data = _data
  5230. const FragmentChunk = h(Fragment, null, [
  5231. h('div', null, 'first fragment'),
  5232. h('div', null, 'second fragment'),
  5233. ])
  5234. </script>
  5235. <template>
  5236. <div>
  5237. <component :is="FragmentChunk" />
  5238. </div>
  5239. <span>{{ data }}</span>
  5240. </template>`,
  5241. undefined,
  5242. data,
  5243. )
  5244. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5245. `
  5246. "
  5247. <!--[--><div>
  5248. <!--[--><div>first fragment</div><div>second fragment</div><!--]-->
  5249. <!--dynamic-component--></div><span>foo</span><!--]-->
  5250. "
  5251. `,
  5252. )
  5253. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5254. data.value = 'bar'
  5255. await nextTick()
  5256. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5257. `
  5258. "
  5259. <!--[--><div>
  5260. <!--[--><div>first fragment</div><div>second fragment</div><!--]-->
  5261. <!--dynamic-component--></div><span>bar</span><!--]-->
  5262. "
  5263. `,
  5264. )
  5265. })
  5266. test('hydrate vapor slot in vdom component with sibling nodes', async () => {
  5267. const msg = ref('Hello World!')
  5268. const { container } = await testWithVaporApp(
  5269. `<script setup vapor>
  5270. const msg = _data
  5271. const components = _components
  5272. </script>
  5273. <template>
  5274. <components.Comp>
  5275. <h1>{{ msg }}</h1>
  5276. </components.Comp>
  5277. <h1>{{ msg }}</h1>
  5278. </template>`,
  5279. {
  5280. Comp: {
  5281. code: `
  5282. <template>
  5283. <div>
  5284. <slot />
  5285. </div>
  5286. </template>`,
  5287. vapor: false,
  5288. },
  5289. },
  5290. msg,
  5291. )
  5292. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5293. `
  5294. "
  5295. <!--[--><div>
  5296. <!--[--><h1>Hello World!</h1><!--]-->
  5297. </div><h1>Hello World!</h1><!--]-->
  5298. "
  5299. `,
  5300. )
  5301. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5302. msg.value = 'Hi Vapor'
  5303. await nextTick()
  5304. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5305. `
  5306. "
  5307. <!--[--><div>
  5308. <!--[--><h1>Hi Vapor</h1><!--]-->
  5309. </div><h1>Hi Vapor</h1><!--]-->
  5310. "
  5311. `,
  5312. )
  5313. })
  5314. test('hydrate multi-root VNode component via createDynamicComponent and switch branch', async () => {
  5315. const data = ref({
  5316. showMulti: true,
  5317. tail: 'tail',
  5318. })
  5319. const { container } = await testWithVaporApp(
  5320. `<script setup>
  5321. import { computed, h } from 'vue'
  5322. const data = _data
  5323. const components = _components
  5324. const vnode = computed(() =>
  5325. data.value.showMulti
  5326. ? h(components.VdomMultiRoot)
  5327. : h('p', null, 'fallback')
  5328. )
  5329. </script>
  5330. <template>
  5331. <component :is="vnode" />
  5332. <span>{{ data.tail }}</span>
  5333. </template>`,
  5334. {
  5335. VdomMultiRoot: {
  5336. code: `<template><div>first</div><div>second</div></template>`,
  5337. vapor: false,
  5338. },
  5339. },
  5340. data,
  5341. )
  5342. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5343. `
  5344. "
  5345. <!--[-->
  5346. <!--[--><!--dynamic-component--><div>first</div><div>second</div><!--]-->
  5347. <span>tail</span><!--]-->
  5348. "
  5349. `,
  5350. )
  5351. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5352. data.value.showMulti = false
  5353. await nextTick()
  5354. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5355. `
  5356. "
  5357. <!--[-->
  5358. <!--[--><p>fallback</p><!--dynamic-component--><!--]-->
  5359. <span>tail</span><!--]-->
  5360. "
  5361. `,
  5362. )
  5363. data.value.showMulti = true
  5364. await nextTick()
  5365. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5366. `
  5367. "
  5368. <!--[-->
  5369. <!--[--><div>first</div><div>second</div><!--dynamic-component--><!--]-->
  5370. <span>tail</span><!--]-->
  5371. "
  5372. `,
  5373. )
  5374. data.value.tail = 'tail-updated'
  5375. await nextTick()
  5376. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5377. "
  5378. <!--[-->
  5379. <!--[--><div>first</div><div>second</div><!--dynamic-component--><!--]-->
  5380. <span>tail-updated</span><!--]-->
  5381. "
  5382. `)
  5383. })
  5384. test('hydrate vapor slot in vdom component with empty slot and sibling nodes', async () => {
  5385. const msg = ref('Hello World!')
  5386. const { container } = await testWithVaporApp(
  5387. `<script setup vapor>
  5388. const msg = _data
  5389. const components = _components
  5390. </script>
  5391. <template>
  5392. <components.Comp />
  5393. <h1>{{ msg }}</h1>
  5394. </template>`,
  5395. {
  5396. Comp: {
  5397. code: `
  5398. <template>
  5399. <div>
  5400. <slot />
  5401. </div>
  5402. </template>`,
  5403. vapor: false,
  5404. },
  5405. },
  5406. msg,
  5407. )
  5408. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5409. `
  5410. "
  5411. <!--[--><div>
  5412. <!--[--><!--]-->
  5413. </div><h1>Hello World!</h1><!--]-->
  5414. "
  5415. `,
  5416. )
  5417. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5418. msg.value = 'Hi Vapor'
  5419. await nextTick()
  5420. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5421. `
  5422. "
  5423. <!--[--><div>
  5424. <!--[--><!--]-->
  5425. </div><h1>Hi Vapor</h1><!--]-->
  5426. "
  5427. `,
  5428. )
  5429. })
  5430. test('hydrate static/fragment VNode via createDynamicComponent and switch type', async () => {
  5431. const data = ref({
  5432. useStatic: true,
  5433. tail: 'tail',
  5434. })
  5435. const { container } = await testWithVaporApp(
  5436. `<script setup>
  5437. import { Fragment, computed, createStaticVNode, h } from 'vue'
  5438. const data = _data
  5439. const vnode = computed(() =>
  5440. data.value.useStatic
  5441. ? createStaticVNode(
  5442. '<div>first static</div><div>second static</div>',
  5443. 2,
  5444. )
  5445. : h(Fragment, null, [
  5446. h('div', null, 'first fragment'),
  5447. h('div', null, 'second fragment'),
  5448. ])
  5449. )
  5450. </script>
  5451. <template>
  5452. <component :is="vnode" />
  5453. <span>{{ data.tail }}</span>
  5454. </template>`,
  5455. undefined,
  5456. data,
  5457. )
  5458. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5459. `
  5460. "
  5461. <!--[--><div>first static</div><div>second static</div><!--dynamic-component--><span>tail</span><!--]-->
  5462. "
  5463. `,
  5464. )
  5465. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5466. data.value.useStatic = false
  5467. await nextTick()
  5468. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5469. `
  5470. "
  5471. <!--[--><div>first fragment</div><div>second fragment</div><!--dynamic-component--><span>tail</span><!--]-->
  5472. "
  5473. `,
  5474. )
  5475. data.value.useStatic = true
  5476. await nextTick()
  5477. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5478. `
  5479. "
  5480. <!--[--><div>first static</div><div>second static</div><!--dynamic-component--><span>tail</span><!--]-->
  5481. "
  5482. `,
  5483. )
  5484. data.value.tail = 'tail-updated'
  5485. await nextTick()
  5486. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5487. "
  5488. <!--[--><div>first static</div><div>second static</div><!--dynamic-component--><span>tail-updated</span><!--]-->
  5489. "
  5490. `)
  5491. })
  5492. test('hydrate Teleport VNode via createDynamicComponent and switch branch', async () => {
  5493. const data = ref({
  5494. showTeleport: true,
  5495. tail: 'tail',
  5496. })
  5497. const { container } = await testWithVaporApp(
  5498. `<script setup>
  5499. import { Teleport, computed, h } from 'vue'
  5500. const data = _data
  5501. const vnode = computed(() =>
  5502. data.value.showTeleport
  5503. ? h(Teleport, { to: '#target', disabled: true }, [
  5504. h('div', null, 'teleported'),
  5505. ])
  5506. : h('p', null, 'fallback')
  5507. )
  5508. </script>
  5509. <template>
  5510. <component :is="vnode" />
  5511. <span>{{ data.tail }}</span>
  5512. </template>`,
  5513. undefined,
  5514. data,
  5515. )
  5516. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5517. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5518. "
  5519. <!--[-->
  5520. <!--teleport start-->
  5521. <div>teleported</div>
  5522. <!--teleport end-->
  5523. <!--dynamic-component--><span>tail</span><!--]-->
  5524. "
  5525. `)
  5526. data.value.showTeleport = false
  5527. await nextTick()
  5528. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5529. "
  5530. <!--[--><p>fallback</p><!--dynamic-component--><span>tail</span><!--]-->
  5531. "
  5532. `)
  5533. data.value.showTeleport = true
  5534. await nextTick()
  5535. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5536. "
  5537. <!--[-->
  5538. <!--teleport start-->
  5539. <div>teleported</div>
  5540. <!--teleport end-->
  5541. <!--dynamic-component--><span>tail</span><!--]-->
  5542. "
  5543. `)
  5544. data.value.tail = 'tail-updated'
  5545. await nextTick()
  5546. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5547. "
  5548. <!--[-->
  5549. <!--teleport start-->
  5550. <div>teleported</div>
  5551. <!--teleport end-->
  5552. <!--dynamic-component--><span>tail-updated</span><!--]-->
  5553. "
  5554. `)
  5555. })
  5556. test('hydrate enabled Teleport VNode via createDynamicComponent and switch branch', async () => {
  5557. const data = ref({
  5558. showTeleport: true,
  5559. tail: 'tail',
  5560. })
  5561. const code = `<script setup>
  5562. import { Teleport, computed, h } from 'vue'
  5563. const data = _data
  5564. const vnode = computed(() =>
  5565. data.value.showTeleport
  5566. ? h(Teleport, { to: '#target' }, [h('div', null, 'teleported')])
  5567. : h('p', null, 'fallback')
  5568. )
  5569. </script>
  5570. <template>
  5571. <component :is="vnode" />
  5572. <span>{{ data.tail }}</span>
  5573. </template>`
  5574. const serverComp = compile(code, data, {}, { vapor: true, ssr: true })
  5575. const ssrCtx: Record<string, any> = {}
  5576. const html = await VueServerRenderer.renderToString(
  5577. runtimeDom.createSSRApp(serverComp),
  5578. ssrCtx,
  5579. )
  5580. const target = document.createElement('div')
  5581. target.id = 'target'
  5582. target.innerHTML = ssrCtx.teleports['#target']
  5583. document.body.appendChild(target)
  5584. const container = document.createElement('div')
  5585. container.innerHTML = html
  5586. document.body.appendChild(container)
  5587. const clientComp = compile(code, data, {}, { vapor: true, ssr: false })
  5588. const app = createVaporSSRApp(clientComp)
  5589. app.use(runtimeVapor.vaporInteropPlugin)
  5590. app.mount(container)
  5591. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5592. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5593. `
  5594. "
  5595. <!--[-->
  5596. <!--teleport start-->
  5597. <!--teleport end-->
  5598. <!--dynamic-component--><span>tail</span><!--]-->
  5599. "
  5600. `,
  5601. )
  5602. expect(formatHtml(target.innerHTML)).toMatchInlineSnapshot(
  5603. `"<!--teleport start anchor--><div>teleported</div><!--teleport anchor-->"`,
  5604. )
  5605. data.value.showTeleport = false
  5606. await nextTick()
  5607. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5608. `
  5609. "
  5610. <!--[--><p>fallback</p><!--dynamic-component--><span>tail</span><!--]-->
  5611. "
  5612. `,
  5613. )
  5614. expect(formatHtml(target.innerHTML)).toMatchInlineSnapshot(`""`)
  5615. data.value.showTeleport = true
  5616. await nextTick()
  5617. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5618. `
  5619. "
  5620. <!--[-->
  5621. <!--teleport start-->
  5622. <!--teleport end-->
  5623. <!--dynamic-component--><span>tail</span><!--]-->
  5624. "
  5625. `,
  5626. )
  5627. expect(formatHtml(target.innerHTML)).toMatchInlineSnapshot(
  5628. `"<div>teleported</div>"`,
  5629. )
  5630. data.value.tail = 'tail-updated'
  5631. await nextTick()
  5632. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5633. "
  5634. <!--[-->
  5635. <!--teleport start-->
  5636. <!--teleport end-->
  5637. <!--dynamic-component--><span>tail-updated</span><!--]-->
  5638. "
  5639. `)
  5640. })
  5641. test('hydrate Suspense VNode via createDynamicComponent and switch branch', async () => {
  5642. const data = ref({
  5643. showSuspense: true,
  5644. msg: 'foo',
  5645. tail: 'tail',
  5646. })
  5647. const { container } = await testWithVaporApp(
  5648. `<script setup>
  5649. import { Suspense, computed, h } from 'vue'
  5650. const data = _data
  5651. const vnode = computed(() =>
  5652. data.value.showSuspense
  5653. ? h(Suspense, null, {
  5654. default: () => h('div', null, data.value.msg),
  5655. fallback: () => h('div', null, 'pending'),
  5656. })
  5657. : h('p', null, 'fallback')
  5658. )
  5659. </script>
  5660. <template>
  5661. <component :is="vnode" />
  5662. <span>{{ data.tail }}</span>
  5663. </template>`,
  5664. undefined,
  5665. data,
  5666. )
  5667. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5668. `
  5669. "
  5670. <!--[--><div>foo</div><!--dynamic-component--><span>tail</span><!--]-->
  5671. "
  5672. `,
  5673. )
  5674. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5675. data.value.msg = 'bar'
  5676. await nextTick()
  5677. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5678. `
  5679. "
  5680. <!--[--><div>bar</div><!--dynamic-component--><span>tail</span><!--]-->
  5681. "
  5682. `,
  5683. )
  5684. data.value.showSuspense = false
  5685. await nextTick()
  5686. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5687. `
  5688. "
  5689. <!--[--><p>fallback</p><!--dynamic-component--><span>tail</span><!--]-->
  5690. "
  5691. `,
  5692. )
  5693. data.value.showSuspense = true
  5694. await nextTick()
  5695. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5696. `
  5697. "
  5698. <!--[--><div>bar</div><!--dynamic-component--><span>tail</span><!--]-->
  5699. "
  5700. `,
  5701. )
  5702. data.value.tail = 'tail-updated'
  5703. await nextTick()
  5704. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5705. "
  5706. <!--[--><div>bar</div><!--dynamic-component--><span>tail-updated</span><!--]-->
  5707. "
  5708. `)
  5709. })
  5710. test('hydrate async Suspense VNode via createDynamicComponent and show fallback', async () => {
  5711. const data = ref({
  5712. showSuspense: true,
  5713. tail: 'tail',
  5714. })
  5715. const appCode = `<script setup>
  5716. import { Suspense, computed, h } from 'vue'
  5717. const data = _data
  5718. const components = _components
  5719. const vnode = computed(() =>
  5720. data.value.showSuspense
  5721. ? h(Suspense, { timeout: 0 }, {
  5722. default: () => h(components.AsyncComp),
  5723. fallback: () => h('div', null, 'pending'),
  5724. })
  5725. : h('p', null, 'fallback')
  5726. )
  5727. </script>
  5728. <template>
  5729. <component :is="vnode" />
  5730. <span>{{ data.tail }}</span>
  5731. </template>`
  5732. const AsyncResolvedComp = {
  5733. render: () => runtimeDom.h('div', null, 'async resolved'),
  5734. }
  5735. let serverResolve: (comp: any) => void
  5736. const ServerAsyncComp = defineAsyncComponent(
  5737. () =>
  5738. new Promise(r => {
  5739. serverResolve = r
  5740. }),
  5741. )
  5742. const SSRApp = compile(
  5743. appCode,
  5744. data,
  5745. { AsyncComp: ServerAsyncComp },
  5746. {
  5747. vapor: true,
  5748. ssr: true,
  5749. },
  5750. )
  5751. const htmlPromise = VueServerRenderer.renderToString(
  5752. runtimeDom.createSSRApp(SSRApp),
  5753. )
  5754. serverResolve!(AsyncResolvedComp)
  5755. const html = await htmlPromise
  5756. let clientResolve: (comp: any) => void
  5757. const ClientAsyncComp = defineAsyncComponent(
  5758. () =>
  5759. new Promise(r => {
  5760. clientResolve = r
  5761. }),
  5762. )
  5763. const App = compile(
  5764. appCode,
  5765. data,
  5766. { AsyncComp: ClientAsyncComp },
  5767. {
  5768. vapor: true,
  5769. ssr: false,
  5770. },
  5771. )
  5772. const container = document.createElement('div')
  5773. container.innerHTML = html
  5774. document.body.appendChild(container)
  5775. const app = createVaporSSRApp(App)
  5776. app.use(runtimeVapor.vaporInteropPlugin)
  5777. app.mount(container)
  5778. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5779. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5780. "
  5781. <!--[--><div>async resolved</div><!--dynamic-component--><span>tail</span><!--]-->
  5782. "
  5783. `)
  5784. data.value.showSuspense = false
  5785. await nextTick()
  5786. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5787. "
  5788. <!--[--><div>async resolved</div><p>fallback</p><!--dynamic-component--><span>tail</span><!--]-->
  5789. "
  5790. `)
  5791. data.value.showSuspense = true
  5792. await nextTick()
  5793. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5794. "
  5795. <!--[--><div>async resolved</div><div>pending</div><!--dynamic-component--><span>tail</span><!--]-->
  5796. "
  5797. `)
  5798. clientResolve!(AsyncResolvedComp)
  5799. await new Promise(r => setTimeout(r))
  5800. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5801. "
  5802. <!--[--><div>async resolved</div><div>async resolved</div><!--dynamic-component--><span>tail</span><!--]-->
  5803. "
  5804. `)
  5805. data.value.tail = 'tail-updated'
  5806. await nextTick()
  5807. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5808. "
  5809. <!--[--><div>async resolved</div><div>async resolved</div><!--dynamic-component--><span>tail-updated</span><!--]-->
  5810. "
  5811. `)
  5812. })
  5813. test('hydrate Suspense VNode via createDynamicComponent under KeepAlive', async () => {
  5814. const data = ref({
  5815. msg: 'foo',
  5816. tail: 'tail',
  5817. })
  5818. const { container } = await testWithVaporApp(
  5819. `<script setup>
  5820. import { KeepAlive, Suspense, computed, h } from 'vue'
  5821. const data = _data
  5822. const vnode = computed(() =>
  5823. h(Suspense, null, {
  5824. default: () => h('div', null, data.value.msg),
  5825. fallback: () => h('div', null, 'pending'),
  5826. })
  5827. )
  5828. </script>
  5829. <template>
  5830. <KeepAlive>
  5831. <component :is="vnode" />
  5832. </KeepAlive>
  5833. <span>{{ data.tail }}</span>
  5834. </template>`,
  5835. undefined,
  5836. data,
  5837. )
  5838. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5839. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5840. "
  5841. <!--[--><div>foo</div><!--dynamic-component--><span>tail</span><!--]-->
  5842. "
  5843. `)
  5844. data.value.tail = 'tail-updated'
  5845. await nextTick()
  5846. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5847. "
  5848. <!--[--><div>foo</div><!--dynamic-component--><span>tail-updated</span><!--]-->
  5849. "
  5850. `)
  5851. })
  5852. test('hydrate Teleport VNode via createDynamicComponent under Transition', async () => {
  5853. const data = ref({
  5854. showTeleport: true,
  5855. tail: 'tail',
  5856. })
  5857. const { container } = await testWithVaporApp(
  5858. `<script setup>
  5859. import { Teleport, Transition, computed, h } from 'vue'
  5860. const data = _data
  5861. const vnode = computed(() =>
  5862. data.value.showTeleport
  5863. ? h(Teleport, { to: '#target', disabled: true }, [
  5864. h('div', null, 'teleported'),
  5865. ])
  5866. : h('p', null, 'fallback')
  5867. )
  5868. </script>
  5869. <template>
  5870. <Transition>
  5871. <component :is="vnode" />
  5872. </Transition>
  5873. <span>{{ data.tail }}</span>
  5874. </template>`,
  5875. undefined,
  5876. data,
  5877. )
  5878. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5879. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5880. "
  5881. <!--[-->
  5882. <!--teleport start-->
  5883. <div>teleported</div>
  5884. <!--teleport end-->
  5885. <!--dynamic-component--><span>tail</span><!--]-->
  5886. "
  5887. `)
  5888. data.value.showTeleport = false
  5889. await nextTick()
  5890. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5891. "
  5892. <!--[--><p class="v-enter-from v-enter-active">fallback</p><!--dynamic-component--><span>tail</span><!--]-->
  5893. "
  5894. `)
  5895. data.value.showTeleport = true
  5896. await nextTick()
  5897. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5898. "
  5899. <!--[--><p class="v-enter-from v-leave-from v-leave-active">fallback</p>
  5900. <!--teleport start-->
  5901. <div>teleported</div>
  5902. <!--teleport end-->
  5903. <!--dynamic-component--><span>tail</span><!--]-->
  5904. "
  5905. `)
  5906. data.value.tail = 'tail-updated'
  5907. await nextTick()
  5908. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5909. "
  5910. <!--[--><p class="v-enter-from v-leave-from v-leave-active">fallback</p>
  5911. <!--teleport start-->
  5912. <div>teleported</div>
  5913. <!--teleport end-->
  5914. <!--dynamic-component--><span>tail-updated</span><!--]-->
  5915. "
  5916. `)
  5917. })
  5918. test('hydrate interop dynamic component under KeepAlive', async () => {
  5919. const data = ref({
  5920. show: true,
  5921. tail: 'tail',
  5922. })
  5923. const { container } = await testWithVaporApp(
  5924. `<script setup>
  5925. import { KeepAlive, computed, h } from 'vue'
  5926. const data = _data
  5927. const components = _components
  5928. const vnode = computed(() => h(components.Counter))
  5929. </script>
  5930. <template>
  5931. <KeepAlive>
  5932. <component v-if="data.show" :is="vnode" />
  5933. </KeepAlive>
  5934. <span>{{ data.tail }}</span>
  5935. </template>`,
  5936. {
  5937. Counter: {
  5938. code: `<script setup>
  5939. import { ref } from 'vue'
  5940. const count = ref(0)
  5941. </script>
  5942. <template><button @click="count++">{{ count }}</button></template>`,
  5943. vapor: false,
  5944. },
  5945. },
  5946. data,
  5947. )
  5948. const getButton = () =>
  5949. container.querySelector('button') as HTMLButtonElement
  5950. expect(getButton().textContent).toBe('0')
  5951. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5952. triggerEvent('click', getButton())
  5953. await nextTick()
  5954. expect(getButton().textContent).toBe('1')
  5955. data.value.show = false
  5956. await nextTick()
  5957. expect(container.querySelector('button')).toBeNull()
  5958. data.value.show = true
  5959. await nextTick()
  5960. expect(getButton().textContent).toBe('1')
  5961. data.value.tail = 'tail-updated'
  5962. await nextTick()
  5963. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
  5964. "
  5965. <!--[--><button>1</button><!--dynamic-component--><!--if--><span>tail-updated</span><!--]-->
  5966. "
  5967. `)
  5968. })
  5969. test('hydrate VDOM slot content', async () => {
  5970. const data = ref('foo')
  5971. const { container } = await testWithVaporApp(
  5972. `<script setup>
  5973. const data = _data; const components = _components;
  5974. </script>
  5975. <template>
  5976. <components.VdomWrapper>
  5977. <div>{{ data }}</div>
  5978. </components.VdomWrapper>
  5979. </template>`,
  5980. {
  5981. VdomWrapper: {
  5982. code: `<script setup>const data = _data;</script>
  5983. <template><slot /></template>`,
  5984. vapor: false,
  5985. },
  5986. },
  5987. data,
  5988. )
  5989. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  5990. `
  5991. "
  5992. <!--[--><div>foo</div><!--]-->
  5993. "
  5994. `,
  5995. )
  5996. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  5997. data.value = 'bar'
  5998. await nextTick()
  5999. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6000. `
  6001. "
  6002. <!--[--><div>bar</div><!--]-->
  6003. "
  6004. `,
  6005. )
  6006. })
  6007. test('hydrate VDOM slot fallback', async () => {
  6008. const data = ref('foo')
  6009. const { container } = await testWithVaporApp(
  6010. `<script setup>
  6011. const data = _data; const components = _components;
  6012. </script>
  6013. <template>
  6014. <components.VdomWrapper />
  6015. </template>`,
  6016. {
  6017. VdomWrapper: {
  6018. code: `<script setup>const data = _data;</script>
  6019. <template><slot><div>{{ data }}</div></slot></template>`,
  6020. vapor: false,
  6021. },
  6022. },
  6023. data,
  6024. )
  6025. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6026. `
  6027. "
  6028. <!--[--><div>foo</div><!--]-->
  6029. "
  6030. `,
  6031. )
  6032. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6033. data.value = 'bar'
  6034. await nextTick()
  6035. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6036. `
  6037. "
  6038. <!--[--><div>bar</div><!--]-->
  6039. "
  6040. `,
  6041. )
  6042. })
  6043. test('hydrate VDOM component returning Fragment', async () => {
  6044. const data = ref('foo')
  6045. const { container } = await testWithVaporApp(
  6046. `<script setup>
  6047. const data = _data; const components = _components;
  6048. </script>
  6049. <template>
  6050. <components.VdomFragmentComp />
  6051. </template>`,
  6052. {
  6053. // VDOM component that returns a Fragment (multiple root nodes)
  6054. VdomFragmentComp: {
  6055. code: `<script setup>const data = _data;</script>
  6056. <template><div>first {{ data }}</div><div>second {{ data }}</div></template>`,
  6057. vapor: false,
  6058. },
  6059. },
  6060. data,
  6061. )
  6062. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6063. `
  6064. "
  6065. <!--[--><div>first foo</div><div>second foo</div><!--]-->
  6066. "
  6067. `,
  6068. )
  6069. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6070. data.value = 'bar'
  6071. await nextTick()
  6072. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6073. `
  6074. "
  6075. <!--[--><div>first bar</div><div>second bar</div><!--]-->
  6076. "
  6077. `,
  6078. )
  6079. })
  6080. test('hydrate handwritten multi-root VDOM component inside multi-root Vapor component', async () => {
  6081. const first = ref('Hello')
  6082. const second = ref('World')
  6083. // Handwritten VDOM component that returns a Fragment (multi-root)
  6084. const MultiRootVDOM = {
  6085. setup() {
  6086. return () => [
  6087. runtimeDom.h('span', first.value),
  6088. runtimeDom.h('span', second.value),
  6089. ]
  6090. },
  6091. }
  6092. const { container } = await testWithVaporApp(
  6093. `<script setup>
  6094. import { h } from 'vue'
  6095. const MultiRootVDOM = _data.MultiRootVDOM
  6096. </script>
  6097. <template>
  6098. <div>Before</div>
  6099. <MultiRootVDOM />
  6100. <div>After</div>
  6101. </template>`,
  6102. {},
  6103. { MultiRootVDOM },
  6104. )
  6105. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6106. `
  6107. "
  6108. <!--[--><div>Before</div>
  6109. <!--[--><span>Hello</span><span>World</span><!--]-->
  6110. <div>After</div><!--]-->
  6111. "
  6112. `,
  6113. )
  6114. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6115. first.value = 'Updated'
  6116. second.value = 'Again'
  6117. await nextTick()
  6118. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6119. `
  6120. "
  6121. <!--[--><div>Before</div>
  6122. <!--[--><span>Updated</span><span>Again</span><!--]-->
  6123. <div>After</div><!--]-->
  6124. "
  6125. `,
  6126. )
  6127. })
  6128. test('hydrate handwritten multi-root VDOM component as first child in multi-root Vapor', async () => {
  6129. const MultiRootVDOM = {
  6130. setup() {
  6131. return () => [
  6132. runtimeDom.h('span', 'Hello'),
  6133. runtimeDom.h('span', 'World'),
  6134. ]
  6135. },
  6136. }
  6137. const { container } = await testWithVaporApp(
  6138. `<script setup>
  6139. const MultiRootVDOM = _data.MultiRootVDOM
  6140. </script>
  6141. <template>
  6142. <MultiRootVDOM />
  6143. <div>After</div>
  6144. </template>`,
  6145. {},
  6146. { MultiRootVDOM },
  6147. )
  6148. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6149. `
  6150. "
  6151. <!--[-->
  6152. <!--[--><span>Hello</span><span>World</span><!--]-->
  6153. <div>After</div><!--]-->
  6154. "
  6155. `,
  6156. )
  6157. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6158. })
  6159. test('hydrate SFC multi-root VDOM component inside multi-root Vapor', async () => {
  6160. const data = ref('foo')
  6161. const { container } = await testWithVaporApp(
  6162. `<script setup>
  6163. const data = _data; const components = _components;
  6164. </script>
  6165. <template>
  6166. <div>Before</div>
  6167. <components.VdomMultiRoot />
  6168. <div>After</div>
  6169. </template>`,
  6170. {
  6171. VdomMultiRoot: {
  6172. code: `<script setup>const data = _data;</script><template><div>first {{ data }}</div><div>second {{ data }}</div></template>`,
  6173. vapor: false,
  6174. },
  6175. },
  6176. data,
  6177. )
  6178. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6179. `
  6180. "
  6181. <!--[--><div>Before</div>
  6182. <!--[--><div>first foo</div><div>second foo</div><!--]-->
  6183. <div>After</div><!--]-->
  6184. "
  6185. `,
  6186. )
  6187. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6188. data.value = 'bar'
  6189. await nextTick()
  6190. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6191. `
  6192. "
  6193. <!--[--><div>Before</div>
  6194. <!--[--><div>first bar</div><div>second bar</div><!--]-->
  6195. <div>After</div><!--]-->
  6196. "
  6197. `,
  6198. )
  6199. })
  6200. test('hydrate handwritten multi-root VDOM via createDynamicComponent with siblings', async () => {
  6201. const MultiRootVDOM = {
  6202. setup() {
  6203. return () => [
  6204. runtimeDom.h('span', 'Hello'),
  6205. runtimeDom.h('span', 'World'),
  6206. ]
  6207. },
  6208. }
  6209. const { container } = await testWithVaporApp(
  6210. `<script setup>
  6211. import { h } from 'vue'
  6212. const MultiRootVDOM = _data.MultiRootVDOM
  6213. const vnode = h(MultiRootVDOM)
  6214. </script>
  6215. <template>
  6216. <component :is="vnode" />
  6217. <div>After</div>
  6218. </template>`,
  6219. {},
  6220. { MultiRootVDOM },
  6221. )
  6222. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6223. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6224. `
  6225. "
  6226. <!--[-->
  6227. <!--[--><span>Hello</span><span>World</span><!--]-->
  6228. <!--dynamic-component--><div>After</div><!--]-->
  6229. "
  6230. `,
  6231. )
  6232. })
  6233. test('hydrate handwritten multi-root VDOM via createDynamicComponent as first child of nested multi-root Vapor', async () => {
  6234. const first = ref('Hello')
  6235. const second = ref('World')
  6236. const after = ref('After')
  6237. const MultiRootVDOM = {
  6238. setup() {
  6239. return () => [
  6240. runtimeDom.h('span', first.value),
  6241. runtimeDom.h('span', second.value),
  6242. ]
  6243. },
  6244. }
  6245. const { container } = await testWithVDOMApp(
  6246. `<script setup>
  6247. const components = _components
  6248. </script>
  6249. <template>
  6250. <p>before</p>
  6251. <components.VaporChild />
  6252. </template>`,
  6253. {
  6254. VaporChild: {
  6255. code: `<script setup>
  6256. import { h } from 'vue'
  6257. const MultiRootVDOM = _data.MultiRootVDOM
  6258. const after = _data.after
  6259. const vnode = h(MultiRootVDOM)
  6260. </script>
  6261. <template>
  6262. <component :is="vnode" />
  6263. <div>{{ after }}</div>
  6264. </template>`,
  6265. vapor: true,
  6266. },
  6267. },
  6268. { MultiRootVDOM, after },
  6269. )
  6270. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6271. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6272. `
  6273. "
  6274. <!--[--><p>before</p>
  6275. <!--[-->
  6276. <!--[--><span>Hello</span><span>World</span><!--]-->
  6277. <!--dynamic-component--><div>After</div><!--]-->
  6278. <!--]-->
  6279. "
  6280. `,
  6281. )
  6282. first.value = 'Updated'
  6283. second.value = 'Again'
  6284. after.value = 'After updated'
  6285. await nextTick()
  6286. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6287. `
  6288. "
  6289. <!--[--><p>before</p>
  6290. <!--[-->
  6291. <!--[--><span>Updated</span><span>Again</span><!--]-->
  6292. <!--dynamic-component--><div>After updated</div><!--]-->
  6293. <!--]-->
  6294. "
  6295. `,
  6296. )
  6297. })
  6298. test('hydrate multi-root VDOM slot as first child of nested multi-root Vapor', async () => {
  6299. const msg = ref('Hello')
  6300. const after = ref('After')
  6301. const { container } = await testWithVDOMApp(
  6302. `<script setup>
  6303. const components = _components
  6304. const msg = _data.msg
  6305. </script>
  6306. <template>
  6307. <p>before</p>
  6308. <components.VaporChild>
  6309. <components.VdomChild />
  6310. </components.VaporChild>
  6311. </template>`,
  6312. {
  6313. VaporChild: {
  6314. code: `<script setup>
  6315. const after = _data.after
  6316. </script>
  6317. <template>
  6318. <slot />
  6319. <div>{{ after }}</div>
  6320. </template>`,
  6321. vapor: true,
  6322. },
  6323. VdomChild: {
  6324. code: `<script setup>const msg = _data.msg</script>
  6325. <template><span>{{ msg }}</span><span>{{ msg }}</span></template>`,
  6326. vapor: false,
  6327. },
  6328. },
  6329. { msg, after },
  6330. )
  6331. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6332. `
  6333. "
  6334. <!--[--><p>before</p>
  6335. <!--[-->
  6336. <!--[-->
  6337. <!--[--><span>Hello</span><span>Hello</span><!--]-->
  6338. <!--]-->
  6339. <div>After</div><!--]-->
  6340. <!--]-->
  6341. "
  6342. `,
  6343. )
  6344. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6345. msg.value = 'Updated'
  6346. after.value = 'After updated'
  6347. await nextTick()
  6348. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6349. `
  6350. "
  6351. <!--[--><p>before</p>
  6352. <!--[-->
  6353. <!--[-->
  6354. <!--[--><span>Updated</span><span>Updated</span><!--]-->
  6355. <!--]-->
  6356. <div>After updated</div><!--]-->
  6357. <!--]-->
  6358. "
  6359. `,
  6360. )
  6361. })
  6362. test('hydrate slot Fragment as first child of DynamicFragment with trailing sibling', async () => {
  6363. const msg = ref('2')
  6364. const tail = ref('1')
  6365. const { container } = await testWithVDOMApp(
  6366. `<script setup>
  6367. const components = _components
  6368. const msg = _data.msg
  6369. </script>
  6370. <template>
  6371. <components.Comp>{{ msg }}</components.Comp>
  6372. </template>`,
  6373. {
  6374. Comp: {
  6375. code: `<script setup>
  6376. const tail = _data.tail
  6377. </script>
  6378. <template>
  6379. <template v-if="true">
  6380. <slot />
  6381. <span>{{ tail }}</span>
  6382. </template>
  6383. </template>`,
  6384. vapor: true,
  6385. },
  6386. },
  6387. { msg, tail },
  6388. )
  6389. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6390. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6391. `
  6392. "
  6393. <!--[-->
  6394. <!--[-->2<!--]-->
  6395. <span>1</span><!--]-->
  6396. "
  6397. `,
  6398. )
  6399. msg.value = '3'
  6400. tail.value = '4'
  6401. await nextTick()
  6402. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6403. `
  6404. "
  6405. <!--[-->
  6406. <!--[-->3<!--]-->
  6407. <span>4</span><!--]-->
  6408. "
  6409. `,
  6410. )
  6411. })
  6412. test('hydrate VDOM component as first child of DynamicFragment with preceding sibling', async () => {
  6413. const data = ref({
  6414. show: true,
  6415. msg: 'Hello',
  6416. tail: 'Tail',
  6417. })
  6418. const { container } = await testWithVaporApp(
  6419. `<script setup>
  6420. const components = _components
  6421. </script>
  6422. <template>
  6423. <components.VaporChild />
  6424. </template>`,
  6425. {
  6426. VaporChild: {
  6427. code: `<script setup>
  6428. const components = _components
  6429. const data = _data
  6430. </script>
  6431. <template>
  6432. <div>Before</div>
  6433. <template v-if="data.show">
  6434. <components.VdomChild />
  6435. <span>{{ data.tail }}</span>
  6436. </template>
  6437. </template>`,
  6438. vapor: true,
  6439. },
  6440. VdomChild: {
  6441. code: `<script setup>const data = _data</script>
  6442. <template><span>{{ data.msg }}</span><span>{{ data.msg }}</span></template>`,
  6443. vapor: false,
  6444. },
  6445. },
  6446. data,
  6447. )
  6448. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6449. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6450. `
  6451. "
  6452. <!--[--><div>Before</div>
  6453. <!--[-->
  6454. <!--[--><span>Hello</span><span>Hello</span><!--]-->
  6455. <span>Tail</span><!--]-->
  6456. <!--]-->
  6457. "
  6458. `,
  6459. )
  6460. data.value.msg = 'Updated'
  6461. data.value.tail = 'Tail updated'
  6462. await nextTick()
  6463. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6464. `
  6465. "
  6466. <!--[--><div>Before</div>
  6467. <!--[-->
  6468. <!--[--><span>Updated</span><span>Updated</span><!--]-->
  6469. <span>Tail updated</span><!--]-->
  6470. <!--]-->
  6471. "
  6472. `,
  6473. )
  6474. })
  6475. test('hydrate empty slot as first child of DynamicFragment with preceding sibling', async () => {
  6476. const data = ref({
  6477. show: true,
  6478. tail: 'Tail',
  6479. })
  6480. const { container } = await testWithVaporApp(
  6481. `<script setup>
  6482. const components = _components
  6483. </script>
  6484. <template>
  6485. <components.VaporChild />
  6486. </template>`,
  6487. {
  6488. VaporChild: {
  6489. code: `<script setup>
  6490. const data = _data
  6491. </script>
  6492. <template>
  6493. <div>Before</div>
  6494. <template v-if="data.show">
  6495. <slot />
  6496. <span>{{ data.tail }}</span>
  6497. </template>
  6498. </template>`,
  6499. vapor: true,
  6500. },
  6501. },
  6502. data,
  6503. )
  6504. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6505. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6506. `
  6507. "
  6508. <!--[--><div>Before</div>
  6509. <!--[-->
  6510. <!--[--><!--]-->
  6511. <span>Tail</span><!--]-->
  6512. <!--]-->
  6513. "
  6514. `,
  6515. )
  6516. data.value.tail = 'Tail updated'
  6517. await nextTick()
  6518. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6519. `
  6520. "
  6521. <!--[--><div>Before</div>
  6522. <!--[-->
  6523. <!--[--><!--]-->
  6524. <span>Tail updated</span><!--]-->
  6525. <!--]-->
  6526. "
  6527. `,
  6528. )
  6529. })
  6530. test('hydrate empty multi-root if with preceding sibling', async () => {
  6531. const data = ref({
  6532. show: false,
  6533. msg: 'Hello',
  6534. tail: 'Tail',
  6535. })
  6536. const { container } = await testWithVaporApp(
  6537. `<script setup>
  6538. const data = _data
  6539. </script>
  6540. <template>
  6541. <div>Before</div>
  6542. <template v-if="data.show">
  6543. <span>{{ data.msg }}</span>
  6544. <span>{{ data.msg }}</span>
  6545. </template>
  6546. <span>{{ data.tail }}</span>
  6547. </template>`,
  6548. undefined,
  6549. data,
  6550. )
  6551. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6552. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6553. `
  6554. "
  6555. <!--[--><div>Before</div><!--if--><span>Tail</span><!--]-->
  6556. "
  6557. `,
  6558. )
  6559. data.value.show = true
  6560. data.value.msg = 'Updated'
  6561. data.value.tail = 'Tail updated'
  6562. await nextTick()
  6563. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6564. `
  6565. "
  6566. <!--[--><div>Before</div><span>Updated</span><span>Updated</span><!--if--><span>Tail updated</span><!--]-->
  6567. "
  6568. `,
  6569. )
  6570. data.value.show = false
  6571. await nextTick()
  6572. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6573. `
  6574. "
  6575. <!--[--><div>Before</div><!--if--><span>Tail updated</span><!--]-->
  6576. "
  6577. `,
  6578. )
  6579. })
  6580. test('hydrate multi-root VDOM component as first child of v-for boundary with sibling', async () => {
  6581. const first = ref('Hello')
  6582. const second = ref('World')
  6583. const items = ref([1])
  6584. const MultiRootVDOM = {
  6585. setup() {
  6586. return () => [
  6587. runtimeDom.h('span', first.value),
  6588. runtimeDom.h('span', second.value),
  6589. ]
  6590. },
  6591. }
  6592. const { container } = await testWithVaporApp(
  6593. `<script setup>
  6594. const items = _data.items
  6595. const MultiRootVDOM = _data.MultiRootVDOM
  6596. </script>
  6597. <template>
  6598. <div>Before</div>
  6599. <MultiRootVDOM v-for="item in items" :key="item" />
  6600. <div>After</div>
  6601. </template>`,
  6602. {},
  6603. { items, MultiRootVDOM },
  6604. )
  6605. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6606. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6607. `
  6608. "
  6609. <!--[--><div>Before</div>
  6610. <!--[-->
  6611. <!--[--><span>Hello</span><span>World</span><!--]-->
  6612. <!--]-->
  6613. <div>After</div><!--]-->
  6614. "
  6615. `,
  6616. )
  6617. first.value = 'Updated'
  6618. second.value = 'Again'
  6619. await nextTick()
  6620. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6621. `
  6622. "
  6623. <!--[--><div>Before</div>
  6624. <!--[-->
  6625. <!--[--><span>Updated</span><span>Again</span><!--]-->
  6626. <!--]-->
  6627. <div>After</div><!--]-->
  6628. "
  6629. `,
  6630. )
  6631. })
  6632. test('hydrate multi-root VDOM via mountVNode as non-first child', async () => {
  6633. const MultiRootVDOM = {
  6634. setup() {
  6635. return () => [
  6636. runtimeDom.h('span', 'Hello'),
  6637. runtimeDom.h('span', 'World'),
  6638. ]
  6639. },
  6640. }
  6641. const { container } = await testWithVaporApp(
  6642. `<script setup>
  6643. import { h } from 'vue'
  6644. const MultiRootVDOM = _data.MultiRootVDOM
  6645. const vnode = h(MultiRootVDOM)
  6646. </script>
  6647. <template>
  6648. <div>Before</div>
  6649. <component :is="vnode" />
  6650. <div>After</div>
  6651. </template>`,
  6652. {},
  6653. { MultiRootVDOM },
  6654. )
  6655. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6656. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6657. `
  6658. "
  6659. <!--[--><div>Before</div>
  6660. <!--[--><span>Hello</span><span>World</span><!--]-->
  6661. <!--dynamic-component--><div>After</div><!--]-->
  6662. "
  6663. `,
  6664. )
  6665. })
  6666. test('hydrate Fragment VNode as first child of multi-root Vapor via createDynamicComponent', async () => {
  6667. const { container } = await testWithVaporApp(
  6668. `<script setup>
  6669. import { Fragment, h } from 'vue'
  6670. const FragmentChunk = h(Fragment, null, [
  6671. h('div', null, 'first fragment'),
  6672. h('div', null, 'second fragment'),
  6673. ])
  6674. </script>
  6675. <template>
  6676. <component :is="FragmentChunk" />
  6677. <div>After</div>
  6678. </template>`,
  6679. )
  6680. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6681. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6682. `
  6683. "
  6684. <!--[-->
  6685. <!--[--><div>first fragment</div><div>second fragment</div><!--]-->
  6686. <!--dynamic-component--><div>After</div><!--]-->
  6687. "
  6688. `,
  6689. )
  6690. })
  6691. test('hydrate Element VNode as first child of multi-root Vapor via createDynamicComponent', async () => {
  6692. const { container } = await testWithVaporApp(
  6693. `<script setup>
  6694. import { h } from 'vue'
  6695. const elementVNode = h('span', null, 'hello')
  6696. </script>
  6697. <template>
  6698. <component :is="elementVNode" />
  6699. <div>After</div>
  6700. </template>`,
  6701. )
  6702. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  6703. expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
  6704. `
  6705. "
  6706. <!--[--><span>hello</span><!--dynamic-component--><div>After</div><!--]-->
  6707. "
  6708. `,
  6709. )
  6710. })
  6711. })