| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486 |
- import { createVaporSSRApp, delegateEvents } from '../src'
- import { nextTick, reactive, ref } from '@vue/runtime-dom'
- import { compileScript, parse } from '@vue/compiler-sfc'
- import * as runtimeVapor from '../src'
- import * as runtimeDom from '@vue/runtime-dom'
- import * as VueServerRenderer from '@vue/server-renderer'
- import { isString } from '@vue/shared'
- import type { VaporComponentInstance } from '../src/component'
- import type { TeleportFragment } from '../src/components/Teleport'
- const formatHtml = (raw: string) => {
- return raw
- .replace(/<!--\[/g, '\n<!--[')
- .replace(/]-->/g, ']-->\n')
- .replace(/\n{2,}/g, '\n')
- }
- const Vue = { ...runtimeDom, ...runtimeVapor }
- function compile(
- sfc: string,
- data: runtimeDom.Ref<any>,
- components: Record<string, any> = {},
- { vapor = true, ssr = false } = {},
- ) {
- if (!sfc.includes(`<script`)) {
- sfc =
- `<script vapor>const data = _data; const components = _components;</script>` +
- sfc
- }
- const descriptor = parse(sfc).descriptor
- const script = compileScript(descriptor, {
- id: 'x',
- isProd: true,
- inlineTemplate: true,
- genDefaultAs: '__sfc__',
- vapor,
- templateOptions: {
- ssr,
- },
- })
- const code =
- script.content
- .replace(/\bimport {/g, 'const {')
- .replace(/ as _/g, ': _')
- .replace(/} from ['"]vue['"]/g, `} = Vue`)
- .replace(/} from "vue\/server-renderer"/g, '} = VueServerRenderer') +
- '\nreturn __sfc__'
- return new Function('Vue', 'VueServerRenderer', '_data', '_components', code)(
- Vue,
- VueServerRenderer,
- data,
- components,
- )
- }
- async function testWithVaporApp(
- code: string,
- components?: Record<string, string | { code: string; vapor: boolean }>,
- data?: any,
- ) {
- return testHydration(code, components, data, {
- isVaporApp: true,
- interop: true,
- })
- }
- async function testWithVDOMApp(
- code: string,
- components?: Record<string, string | { code: string; vapor: boolean }>,
- data?: any,
- ) {
- return testHydration(code, components, data, {
- isVaporApp: false,
- interop: true,
- })
- }
- function compileVaporComponent(
- code: string,
- data: runtimeDom.Ref<any> = ref({}),
- components?: Record<string, any>,
- ssr = false,
- ) {
- return compile(`<template>${code}</template>`, data, components, {
- vapor: true,
- ssr,
- })
- }
- async function mountWithHydration(
- html: string,
- code: string,
- data: runtimeDom.Ref<any> = ref({}),
- components?: Record<string, any>,
- ) {
- const container = document.createElement('div')
- container.innerHTML = html
- document.body.appendChild(container)
- const clientComp = compileVaporComponent(code, data, components)
- const app = createVaporSSRApp(clientComp)
- app.mount(container)
- return {
- block: (app._instance! as VaporComponentInstance).block,
- container,
- }
- }
- async function testHydration(
- code: string,
- components: Record<string, string | { code: string; vapor: boolean }> = {},
- data: any = ref('foo'),
- { isVaporApp = true, interop = false } = {},
- ) {
- const ssrComponents: any = {}
- const clientComponents: any = {}
- for (const key in components) {
- const comp = components[key]
- const code = isString(comp) ? comp : comp.code
- const isVaporComp = isString(comp) || !!comp.vapor
- clientComponents[key] = compile(code, data, clientComponents, {
- vapor: isVaporComp,
- ssr: false,
- })
- ssrComponents[key] = compile(code, data, ssrComponents, {
- vapor: isVaporComp,
- ssr: true,
- })
- }
- const serverComp = compile(code, data, ssrComponents, {
- vapor: isVaporApp,
- ssr: true,
- })
- const html = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(serverComp),
- )
- const container = document.createElement('div')
- document.body.appendChild(container)
- container.innerHTML = html
- const clientComp = compile(code, data, clientComponents, {
- vapor: isVaporApp,
- ssr: false,
- })
- let app
- if (isVaporApp) {
- app = createVaporSSRApp(clientComp)
- } else {
- app = runtimeDom.createSSRApp(clientComp)
- }
- if (interop) {
- app.use(runtimeVapor.vaporInteropPlugin)
- }
- app.mount(container)
- return { data, container }
- }
- const triggerEvent = (type: string, el: Element) => {
- const event = new Event(type, { bubbles: true })
- el.dispatchEvent(event)
- }
- delegateEvents('click')
- beforeEach(() => {
- document.body.innerHTML = ''
- })
- describe('Vapor Mode hydration', () => {
- describe('text', () => {
- test('root text', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foo"`)
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"bar"`)
- })
- test('consecutive text nodes', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"foofoo"`)
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"barbar"`)
- })
- test('consecutive text nodes with insertion anchor', async () => {
- const { data, container } = await testHydration(`
- <template><span/>{{ data }}{{ data }}<span/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>foofoo<span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>barbar<span></span><!--]-->
- "
- `,
- )
- })
- test('mixed text nodes', async () => {
- const { data, container } = await testHydration(`
- <template>{{ data }}A{{ data }}B{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"fooAfooBfoo"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"barAbarBbar"`,
- )
- })
- test('mixed text nodes with insertion anchor', async () => {
- const { data, container } = await testHydration(`
- <template><span/>{{ data }}A{{ data }}B{{ data }}<span/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>fooAfooBfoo<span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span>barAbarBbar<span></span><!--]-->
- "
- `,
- )
- })
- test('empty text node', async () => {
- const data = reactive({ txt: '' })
- const { container } = await testHydration(
- `<template><div>{{ data.txt }}</div></template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div></div>"`,
- )
- data.txt = 'foo'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div>"`,
- )
- })
- test('empty text node in slot', async () => {
- const data = reactive({ txt: '' })
- const { container } = await testHydration(
- `<template><components.Child>{{data.txt}}</components.Child></template>`,
- {
- Child: `<template><slot/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.txt = 'foo'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo<!--]-->
- "
- `,
- )
- })
- })
- describe('element', () => {
- test('root comment', async () => {
- const { container } = await testHydration(`
- <template><!----></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"<!---->"`)
- expect(`mismatch in <div>`).not.toHaveBeenWarned()
- })
- test('root with mixed element and text', async () => {
- const { container, data } = await testHydration(`
- <template> A<span>{{ data }}</span>{{ data }}</template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--> A<span>foo</span>foo<!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--> A<span>bar</span>bar<!--]-->
- "
- `,
- )
- })
- test('empty element', async () => {
- const { container } = await testHydration(`
- <template><div/></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div></div>"`,
- )
- expect(`mismatch in <div>`).not.toHaveBeenWarned()
- })
- test('element with binding and text children', async () => {
- const { container, data } = await testHydration(`
- <template><div :class="data">{{ data }}</div></template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="foo">foo</div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="bar">bar</div>"`,
- )
- })
- test('element with elements children', async () => {
- const { container } = await testHydration(`
- <template>
- <div>
- <span>{{ data }}</span>
- <span :class="data" @click="data = 'bar'"/>
- </div>
- </template>
- `)
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span class="foo"></span></div>"`,
- )
- // event handler
- triggerEvent('click', container.querySelector('.foo')!)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>bar</span><span class="bar"></span></div>"`,
- )
- })
- test('element with ref', async () => {
- const { data, container } = await testHydration(
- `<template>
- <div ref="data">hi</div>
- </template>
- `,
- {},
- ref(null),
- )
- expect(data.value).toBe(container.firstChild)
- })
- })
- describe('component', () => {
- test('basic component', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Child/></div></template>
- `,
- { Child: `<template>{{ data }}</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo</div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar</div>"`,
- )
- })
- test('fragment component', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Child/></div></template>
- `,
- { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- </div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- </div>"
- `,
- )
- })
- test('fragment component with prepend', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><components.Child/><span></span></div></template>
- `,
- { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment components', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div/><components.Child/><div/></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div></div>
- <!--[--><div>foo</div>-foo-<!--]-->
- <div></div><!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div></div>
- <!--[--><div>bar</div>-bar-<!--]-->
- <div></div><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template>{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar<span></span></div>"`,
- )
- })
- test('nested components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><span></span></div>"`,
- )
- })
- test('nested components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>foo</div><span></span></div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>bar</div><span></span></div><span></span></div>"`,
- )
- })
- test('consecutive components with insertion parent', async () => {
- const data = reactive({ foo: 'foo', bar: 'bar' })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child1/>
- <components.Child2/>
- </div>
- </template>
- `,
- {
- Child1: `<template><span>{{ data.foo }}</span></template>`,
- Child2: `<template><span>{{ data.bar }}</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div>"`,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo1</span><span>bar1</span></div>"`,
- )
- })
- test('nested consecutive components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><div>foo</div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><div>bar</div><span></span></div>"`,
- )
- })
- test('nested consecutive components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>foo</div><div>foo</div><span></span></div><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div><span></span><div>bar</div><div>bar</div><span></span></div><span></span></div>"`,
- )
- })
- test('mixed component and element with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template>{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<span></span>foo<span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>bar<span></span>bar<span></span></div>"`,
- )
- })
- test('fragment component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested fragment component with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span/><components.Parent/><span/></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- })
- test('consecutive fragment components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `
- <template><components.Parent/></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with multi level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span><div><span></span>
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <span></span></div><span></span></div>"
- `,
- )
- })
- test('nested consecutive fragment components with root level anchor insertion', async () => {
- const { container, data } = await testHydration(
- `
- <template><div><span></span><components.Parent/><span></span></div></template>
- `,
- {
- Parent: `<template><components.Child/><components.Child/></template>`,
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--[--><div>foo</div>-foo-<!--]-->
- <!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--[--><div>bar</div>-bar-<!--]-->
- <!--]-->
- <span></span></div>"
- `,
- )
- })
- test('mixed fragment component and element with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- <span/>
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- test('mixed fragment component and text with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child/>
- {{ data }}
- <components.Child/>
- <span/>
- </div>
- </template>
- `,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>foo</div>-foo<!--]-->
- foo
- <!--[--><div>foo</div>-foo<!--]-->
- <span></span></div>"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>bar</div>-bar<!--]-->
- bar
- <!--[--><div>bar</div>-bar<!--]-->
- <span></span></div>"
- `,
- )
- })
- })
- describe('dynamic component', () => {
- test('basic dynamic component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <component :is="components[data]"/>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--dynamic-component-->"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>bar</div><!--dynamic-component-->"`,
- )
- })
- test('dynamic component with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components[data]"/>
- <span/>
- </div>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><!--dynamic-component--><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><!--dynamic-component--><span></span></div>"`,
- )
- })
- test('consecutive dynamic components with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components[data]"/>
- <component :is="components[data]"/>
- <span/>
- </div>
- </template>`,
- {
- foo: `<template><div>foo</div></template>`,
- bar: `<template><div>bar</div></template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>foo</div><!--dynamic-component--><div>foo</div><!--dynamic-component--><span></span></div>"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><div>bar</div><!--dynamic-component--><div>bar</div><!--dynamic-component--><span></span></div>"`,
- )
- })
- test('dynamic component fallback', async () => {
- const { container, data } = await testHydration(
- `<template>
- <component :is="'button'">
- <span>{{ data }}</span>
- </component>
- </template>`,
- {},
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button><span>foo</span></button><!--dynamic-component-->"`,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button><span>bar</span></button><!--dynamic-component-->"`,
- )
- })
- test('in ssr slot vnode fallback', async () => {
- const { container, data } = await testHydration(
- `<template>
- <components.Child>
- <span>{{ data }}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <component :is="'div'">
- <slot />
- </component>
- </template>`,
- },
- ref('foo'),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- </div><!--dynamic-component-->"
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>bar</span><!--]-->
- </div><!--dynamic-component-->"
- `,
- )
- })
- })
- describe('if', () => {
- test('basic toggle - true -> false', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">foo</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('basic toggle - false -> true', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">foo</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('v-if on insertion parent', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div v-if="data">
- <components.Child/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('v-if/else-if/else chain - switch branches', async () => {
- const data = ref('a')
- const { container } = await testHydration(
- `<template>
- <div v-if="data === 'a'">foo</div>
- <div v-else-if="data === 'b'">bar</div>
- <div v-else>baz</div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- data.value = 'b'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>bar</div><!--if--><!--if-->"`,
- )
- data.value = 'c'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>baz</div><!--if--><!--if-->"`,
- )
- data.value = 'a'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if-->"`,
- )
- })
- test('nested if', async () => {
- const data = reactive({ outer: true, inner: true })
- const { container } = await testHydration(
- `<template>
- <div v-if="data.outer">
- <span>outer</span>
- <div v-if="data.inner">inner</div>
- </div>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>outer</span><div>inner</div><!--if--></div><!--if-->"`,
- )
- data.inner = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>outer</span><!--if--></div><!--if-->"`,
- )
- data.outer = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('on component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <components.Child v-if="data"/>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"foo<!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- })
- test('consecutive if node', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <components.Child v-if="data"/>
- </template>`,
- { Child: `<template><div v-if="data">foo</div></template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if--><!--if-->"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div>foo</div><!--if--><!--if-->"`,
- )
- })
- test('mixed prepend and insertion anchor', async () => {
- const data = reactive({
- show: true,
- foo: 'foo',
- bar: 'bar',
- qux: 'qux',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child/>
- </template>`,
- {
- Child: `<template>
- <span v-if="data.show">
- <span v-if="data.show">{{data.foo}}</span>
- <span v-if="data.show">{{data.bar}}</span>
- <span>baz</span>
- <span v-if="data.show">{{data.qux}}</span>
- <span>quux</span>
- </span>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- data.qux = 'qux1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- data.foo = 'foo1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span><span>foo1</span><!--if--><span>bar</span><!--if--><span>baz</span><span>qux1</span><!--if--><span>quux</span></span><!--if-->"`,
- )
- })
- test('v-if/else-if/else chain on component - switch branches', async () => {
- const data = ref('a')
- const { container } = await testHydration(
- `<template>
- <components.Child1 v-if="data === 'a'"/>
- <components.Child2 v-else-if="data === 'b'"/>
- <components.Child3 v-else/>
- </template>`,
- {
- Child1: `<template><span>{{data}} child1</span></template>`,
- Child2: `<template><span>{{data}} child2</span></template>`,
- Child3: `<template><span>{{data}} child3</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>a child1</span><!--if-->"`,
- )
- data.value = 'b'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>b child2</span><!--if--><!--if-->"`,
- )
- data.value = 'c'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>c child3</span><!--if--><!--if-->"`,
- )
- data.value = 'a'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span>a child1</span><!--if-->"`,
- )
- })
- test('on component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--if--><span></span></div>"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><!--if--><span></span></div>"`,
- )
- })
- test('consecutive component with insertion parent', async () => {
- const data = reactive({
- show: true,
- foo: 'foo',
- bar: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <div v-if="data.show">
- <components.Child/>
- <components.Child2/>
- </div>
- </template>`,
- {
- Child: `<template><span>{{data.foo}}</span></template>`,
- Child2: `<template><span>{{data.bar}}</span></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo</span><span>bar</span></div><!--if-->"`,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span>foo1</span><span>bar1</span></div><!--if-->"`,
- )
- })
- test('on fragment component', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child v-if="data"/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- </div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--if--><!--]-->
- </div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- </div>"
- `,
- )
- })
- test('on fragment component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <span></span></div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><!--if--><!--]-->
- <span></span></div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <span></span></div>"
- `)
- })
- test('consecutive v-if on fragment component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <components.Child v-if="data"/>
- <components.Child v-if="data"/>
- <span/>
- </div>
- </template>`,
- {
- Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <span></span></div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><!--if--><!--]-->
- <!--[--><!--if--><!--]-->
- <span></span></div>"
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div><span></span>
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <!--[--><div>true</div>-true-<!--if--><!--]-->
- <span></span></div>"
- `)
- })
- test('on dynamic component with insertion anchor', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <div>
- <span/>
- <component :is="components.Child" v-if="data"/>
- <span/>
- </div>
- </template>`,
- { Child: `<template>foo</template>` },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span><!--if--><span></span></div>"`,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div><span></span>foo<!--dynamic-component--><!--if--><span></span></div>"`,
- )
- })
- })
- describe('for', () => {
- test('basic v-for', async () => {
- const { container, data } = await testHydration(
- `<template>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- "
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- "
- `,
- )
- })
- test('empty v-for', async () => {
- const { container, data } = await testHydration(
- `<template>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </template>`,
- undefined,
- ref([]),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.value.push('a')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>a</span><!--]-->
- "
- `,
- )
- })
- test('v-for with insertion parent + sibling component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span v-for="item in data" :key="item">{{ item }}</span>
- </div>
- <components.Child/>
- </template>`,
- {
- Child: `<template><div>{{data.length}}</div></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- </div><div>3</div><!--]-->
- "
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- </div><div>4</div><!--]-->
- "
- `,
- )
- })
- test('v-for with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span/>
- </div>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.splice(0, 1)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('consecutive v-for with insertion anchor', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <span/>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span v-for="item in data" :key="item">{{ item }}</span>
- <span/>
- </div>
- </template>`,
- undefined,
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <!--[--><span>a</span><span>b</span><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- data.value.splice(0, 2)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>c</span><span>d</span><!--]-->
- <!--[--><span>c</span><span>d</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('v-for on component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template><div>comp</div></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>comp</div><div>comp</div><div>comp</div><!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>comp</div><div>comp</div><div>comp</div><div>comp</div><!--]-->
- </div>"
- `,
- )
- })
- test('v-for on component with slots', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item">
- <span>{{ item }}</span>
- </components.Child>
- </div>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><span>a</span><!--]-->
- <!--[--><span>b</span><!--]-->
- <!--[--><span>c</span><!--]-->
- <!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><span>a</span><!--]-->
- <!--[--><span>b</span><!--]-->
- <!--[--><span>c</span><!--]-->
- <span>d</span><!--slot--><!--]-->
- </div>"
- `,
- )
- })
- test('on fragment component', async () => {
- const { container, data } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in data" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template><div>foo</div>-bar-</template>`,
- },
- ref(['a', 'b', 'c']),
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--]-->
- </div>"
- `,
- )
- data.value.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<!--]-->
- <!--[--><div>foo</div>-bar-<div>foo</div>-bar-<!--]-->
- <!--]-->
- </div>"
- `,
- )
- })
- test('on component with non-hydration node', async () => {
- const data = ref({ show: true, msg: 'foo' })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Child v-for="item in 2" :key="item"/>
- </div>
- </template>`,
- {
- Child: `<template>
- <div>
- <div>
- <div v-if="data.show">{{ data.msg }}</div>
- </div>
- <span>non-hydration node</span>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><div><div><div>foo</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `,
- )
- data.value.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><!--if--></div><span>non-hydration node</span></div><div><div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- data.value.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "<div>
- <!--[--><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><div><div><div>bar</div><!--if--></div><span>non-hydration node</span></div><!--]-->
- </div>"
- `)
- })
- })
- describe('slots', () => {
- test('basic slot', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>bar</span><!--]-->
- "
- `,
- )
- })
- test('named slot', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo>
- <span>{{data}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/><slot name="foo"/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- <!--[--><span>bar</span><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('named slot with v-if', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo v-if="data">
- <span>{{data}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot name="foo"/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--]-->
- "
- `,
- )
- data.value = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "
- <!--[--><span>true</span><!--]-->
- "
- `)
- })
- test('named slot with v-if and v-for', async () => {
- const data = reactive({
- show: true,
- items: ['a', 'b', 'c'],
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo v-if="data.show">
- <span v-for="item in data.items" :key="item">{{item}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot name="foo"/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><!--]-->
- "
- `,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>a</span><span>b</span><span>c</span><!--for--><!--]-->
- "
- `,
- )
- })
- test('with insertion anchor', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span/>
- <span>{{data}}</span>
- <span/>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/></template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span><span>foo</span><span></span><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span></span><span>bar</span><span></span><!--]-->
- "
- `,
- )
- })
- test('with multi level anchor insertion', async () => {
- const { data, container } = await testHydration(
- `<template>
- <components.Child>
- <span/>
- <span>{{data}}</span>
- <span/>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div/>
- <div/>
- <slot/>
- <div/>
- </div>
- </template>`,
- },
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div></div><div></div>
- <!--[--><span></span><span>foo</span><span></span><!--]-->
- <div></div><!--]-->
- "
- `,
- )
- data.value = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div></div><div></div>
- <!--[--><span></span><span>bar</span><span></span><!--]-->
- <div></div><!--]-->
- "
- `,
- )
- })
- test('mixed slot and text node', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot/>{{data.msg}}</div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- hi</div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- bar</div>"
- `,
- )
- })
- test('mixed root slot and text node', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template>{{data.text}}<slot/>{{data.msg}}</template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo
- <!--[--><span>foo</span><!--]-->
- hi<!--]-->
- "
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->foo
- <!--[--><span>foo</span><!--]-->
- bar<!--]-->
- "
- `,
- )
- })
- test('mixed consecutive slot and element', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo><span>{{data.text}}</span></template>
- <template #bar><span>bar</span></template>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot name="foo"/><slot name="bar"/><div>{{data.msg}}</div></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>hi</div></div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- test('mixed slot and element', async () => {
- const data = reactive({
- text: 'foo',
- msg: 'hi',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.text}}</span>
- </components.Child>
- </template>`,
- {
- Child: `<template><div><slot/><div>{{data.msg}}</div></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <div>hi</div></div>"
- `,
- )
- data.msg = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- test('mixed slot and component', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div>
- <components.Child2/>
- <slot/>
- <components.Child2/>
- </div>
- </template>`,
- Child2: `
- <template>
- <div>{{data.msg2}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div>bar</div>
- <!--[--><span>foo</span><!--]-->
- <div>bar</div></div>"
- `,
- )
- data.msg2 = 'hello'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div>hello</div>
- <!--[--><span>foo</span><!--]-->
- <div>hello</div></div>"
- `,
- )
- })
- test('mixed slot and fragment component', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div>
- <components.Child2/>
- <slot/>
- <components.Child2/>
- </div>
- </template>`,
- Child2: `
- <template>
- <div>{{data.msg1}}</div> {{data.msg2}}
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>foo</div> bar<!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>foo</div> bar<!--]-->
- </div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div>hello</div> vapor<!--]-->
- <!--[--><span>hello</span><!--]-->
- <!--[--><div>hello</div> vapor<!--]-->
- </div>"
- `,
- )
- })
- test('mixed slot and v-if', async () => {
- const data = reactive({
- show: true,
- msg: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div v-if="data.show">{{data.msg}}</div>
- <slot/>
- <div v-if="data.show">{{data.msg}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><div>foo</div><!--if-->
- <!--[--><span>foo</span><!--]-->
- <div>foo</div><!--if--><!--]-->
- "
- `,
- )
- data.show = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><!--if-->
- <!--[--><span>foo</span><!--]-->
- <!--if--><!--]-->
- "
- `,
- )
- data.show = true
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`
- "
- <!--[--><div>foo</div><!--if-->
- <!--[--><span>foo</span><!--]-->
- <div>foo</div><!--if--><!--]-->
- "
- `)
- })
- test('mixed slot and v-for', async () => {
- const data = reactive({
- items: ['a', 'b', 'c'],
- msg: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg}}</span>
- </components.Child>
- </template>`,
- {
- Child: `
- <template>
- <div v-for="item in data.items" :key="item">{{item}}</div>
- <slot/>
- <div v-for="item in data.items" :key="item">{{item}}</div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>a</div><div>b</div><div>c</div><!--]-->
- <!--]-->
- "
- `,
- )
- data.items.push('d')
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><div>a</div><div>b</div><div>c</div><div>d</div><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('consecutive slots', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot/><slot name="bar"/></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <!--]-->
- "
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[-->
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <!--]-->
- "
- `,
- )
- })
- test('consecutive slots with insertion anchor', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <span>{{data.msg1}}</span>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template>
- <div>
- <span/>
- <slot/>
- <slot name="bar"/>
- <span/>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <span></span></div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><span></span>
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <span></span></div>"
- `,
- )
- })
- test('consecutive slots prepend', async () => {
- const data = reactive({
- msg1: 'foo',
- msg2: 'bar',
- msg3: 'baz',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- <template #foo>
- <span>{{data.msg1}}</span>
- </template>
- <template #bar>
- <span>{{data.msg2}}</span>
- </template>
- </components.Child>
- </template>`,
- {
- Child: `<template>
- <div>
- <slot name="foo"/>
- <slot name="bar"/>
- <div>{{data.msg3}}</div>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>foo</span><!--]-->
- <!--[--><span>bar</span><!--]-->
- <div>baz</div></div>"
- `,
- )
- data.msg1 = 'hello'
- data.msg2 = 'vapor'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><span>hello</span><!--]-->
- <!--[--><span>vapor</span><!--]-->
- <div>baz</div></div>"
- `,
- )
- })
- test('slot fallback', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Child>
- </components.Child>
- </template>`,
- {
- Child: `<template><slot><span>{{data.foo}}</span></slot></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>foo</span><!--]-->
- "
- `,
- )
- data.foo = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>bar</span><!--]-->
- "
- `,
- )
- })
- test('forwarded slot', async () => {
- const data = reactive({
- foo: 'foo',
- bar: 'bar',
- })
- const { container } = await testHydration(
- `<template>
- <div>
- <components.Parent>
- <span>{{data.foo}}</span>
- </components.Parent>
- <div>{{data.bar}}</div>
- </div>
- </template>`,
- {
- Parent: `<template><div><components.Child><slot/></components.Child></div></template>`,
- Child: `<template><div><slot/></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div><div>
- <!--[-->
- <!--[--><span>foo</span><!--]-->
- <!--]-->
- </div></div><div>bar</div></div>"
- `,
- )
- data.foo = 'foo1'
- data.bar = 'bar1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div><div><div>
- <!--[-->
- <!--[--><span>foo1</span><!--]-->
- <!--]-->
- </div></div><div>bar1</div></div>"
- `,
- )
- })
- test('forwarded slot with fallback', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Parent/>
- </template>`,
- {
- Parent: `<template><components.Child><slot/></components.Child></template>`,
- Child: `<template><div><slot>{{data.foo}}</slot></div></template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->foo<!--]-->
- </div>"
- `,
- )
- data.foo = 'foo1'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->foo1<!--]-->
- </div>"
- `,
- )
- })
- test('forwarded slot with empty content', async () => {
- const data = reactive({
- foo: 'foo',
- })
- const { container } = await testHydration(
- `<template>
- <components.Foo/>
- </template>`,
- {
- Foo: `<template>
- <components.Bar>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Bar>
- </template>`,
- Bar: `<template>
- <components.Baz>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Baz>
- </template>`,
- Baz: `<template>
- <components.Qux>
- <template #foo>
- <slot name="foo" />
- </template>
- </components.Qux>
- </template>`,
- Qux: `<template>
- <div>
- <slot name="foo" />
- <div>{{data.foo}}</div>
- </div>
- </template>`,
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <div>foo</div></div>"
- `,
- )
- data.foo = 'bar'
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><!--]-->
- <div>bar</div></div>"
- `,
- )
- })
- })
- describe('transition', async () => {
- test('transition appear', async () => {
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div>foo</div>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div style="" class="v-enter-from v-enter-active">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear work with pre-existing class', async () => {
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div class="foo">foo</div>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div class="foo v-enter-from v-enter-active" style="">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear work with empty content', async () => {
- const data = ref(true)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <slot v-if="data"></slot>
- <span v-else>foo</span>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--slot--><!--if-->"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<span class="v-enter-from v-enter-active">foo</span><!--if-->"`,
- )
- })
- test('transition appear with v-if', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div v-if="data">foo</div>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<!--if-->"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear with v-show', async () => {
- const data = ref(false)
- const { container } = await testHydration(
- `<template>
- <transition appear>
- <div v-show="data">foo</div>
- </transition>
- </template>`,
- undefined,
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<div style="display:none;" class="v-enter-from v-enter-active v-leave-from v-leave-active">foo</div>"`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('transition appear w/ event listener', async () => {
- const { container } = await testHydration(
- `<script setup>
- import { ref } from 'vue'
- const count = ref(0)
- </script>
- <template>
- <transition appear>
- <button @click="count++">{{ count }}</button>
- </transition>
- </template>`,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button style="" class="v-enter-from v-enter-active">0</button>"`,
- )
- triggerEvent('click', container.querySelector('button')!)
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `"<button style="" class="v-enter-from v-enter-active">1</button>"`,
- )
- })
- })
- describe.todo('async component', async () => {
- test('async component', async () => {})
- test('update async wrapper before resolve', async () => {})
- test('hydrate safely when property used by async setup changed before render', async () => {})
- test('unmount async wrapper before load', async () => {})
- test('nested async wrapper', async () => {})
- test('unmount async wrapper before load (fragment)', async () => {})
- })
- describe('force hydrate prop', async () => {
- test('force hydrate prop with `.prop` modifier', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox">',
- `<input type="checkbox" .indeterminate="true"/>`,
- )
- expect((container.firstChild! as any).indeterminate).toBe(true)
- })
- test('force hydrate input v-model with non-string value bindings', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox" value="true">',
- `<input type="checkbox" :true-value="true"/>`,
- )
- expect((container.firstChild as any)._trueValue).toBe(true)
- })
- test.todo('force hydrate checkbox with indeterminate', async () => {
- const { container } = await mountWithHydration(
- '<input type="checkbox" indeterminate/>',
- `<input type="checkbox" :indeterminate="true"/>`,
- )
- expect((container.firstChild! as any).indeterminate).toBe(true)
- })
- test.todo(
- 'force hydrate select option with non-string value bindings',
- () => {},
- )
- test.todo('force hydrate custom element with dynamic props', () => {})
- })
- describe('Teleport', () => {
- test('basic', async () => {
- const data = ref({
- msg: ref('foo'),
- disabled: ref(false),
- fn: vi.fn(),
- })
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport'
- teleportContainer.innerHTML =
- `<!--teleport start anchor-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<teleport to="#teleport" :disabled="data.disabled">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn"></span>
- </teleport>`,
- data,
- )
- const teleport = block as TeleportFragment
- expect(teleport.anchor).toBe(container.lastChild)
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect((teleport.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[1],
- )
- expect((teleport.nodes as Node[])[1]).toBe(
- teleportContainer.childNodes[2],
- )
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<!--teleport start--><!--teleport end-->"`,
- )
- // event handler
- triggerEvent('click', teleportContainer.querySelector('.foo')!)
- expect(data.value.fn).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(formatHtml(teleportContainer.innerHTML)).toBe(
- `<!--teleport start anchor-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport anchor-->`,
- )
- data.value.disabled = true
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport end-->`,
- )
- expect(formatHtml(teleportContainer.innerHTML)).toMatchInlineSnapshot(
- `"<!--teleport start anchor--><!--teleport anchor-->"`,
- )
- data.value.msg = 'baz'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start-->` +
- `<span>baz</span>` +
- `<span class="baz"></span>` +
- `<!--teleport end-->`,
- )
- data.value.disabled = false
- await nextTick()
- expect(container.innerHTML).toMatchInlineSnapshot(
- `"<!--teleport start--><!--teleport end-->"`,
- )
- expect(formatHtml(teleportContainer.innerHTML)).toBe(
- `<!--teleport start anchor-->` +
- `<span>baz</span>` +
- `<span class="baz"></span>` +
- `<!--teleport anchor-->`,
- )
- })
- test('multiple + integration', async () => {
- const data = ref({
- msg: ref('foo'),
- fn1: vi.fn(),
- fn2: vi.fn(),
- })
- const code = `
- <teleport to="#teleport2">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn1"></span>
- </teleport>
- <teleport to="#teleport2">
- <span>{{data.msg}}2</span>
- <span :class="data.msg + 2" @click="data.fn2"></span>
- </teleport>`
- const SSRComp = compileVaporComponent(code, data, undefined, true)
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport2'
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- ctx,
- )
- expect(mainHtml).toBe(
- `<!--[-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--]-->`,
- )
- const teleportHtml = ctx.teleports!['#teleport2']
- expect(teleportHtml).toBe(
- `<!--teleport start anchor-->` +
- `<span>foo</span><span class="foo"></span>` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<span>foo2</span><span class="foo2"></span>` +
- `<!--teleport anchor-->`,
- )
- teleportContainer.innerHTML = teleportHtml
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- mainHtml,
- code,
- data,
- )
- const teleports = block as any as TeleportFragment[]
- const teleport1 = teleports[0]
- const teleport2 = teleports[1]
- expect(teleport1.anchor).toBe(container.childNodes[2])
- expect(teleport2.anchor).toBe(container.childNodes[4])
- expect(teleport1.target).toBe(teleportContainer)
- expect(teleport1.targetStart).toBe(teleportContainer.childNodes[0])
- expect((teleport1.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[1],
- )
- expect(teleport1.targetAnchor).toBe(teleportContainer.childNodes[3])
- expect(teleport2.target).toBe(teleportContainer)
- expect(teleport2.targetStart).toBe(teleportContainer.childNodes[4])
- expect((teleport2.nodes as Node[])[0]).toBe(
- teleportContainer.childNodes[5],
- )
- expect(teleport2.targetAnchor).toBe(teleportContainer.childNodes[7])
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--]-->`,
- )
- // event handler
- triggerEvent('click', teleportContainer.querySelector('.foo')!)
- expect(data.value.fn1).toHaveBeenCalled()
- triggerEvent('click', teleportContainer.querySelector('.foo2')!)
- expect(data.value.fn2).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(teleportContainer.innerHTML).toBe(
- `<!--teleport start anchor-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<span>bar2</span>` +
- `<span class="bar2"></span>` +
- `<!--teleport anchor-->`,
- )
- })
- test('disabled', async () => {
- const data = ref({
- msg: ref('foo'),
- fn1: vi.fn(),
- fn2: vi.fn(),
- })
- const code = `
- <div>foo</div>
- <teleport to="#teleport3" disabled="true">
- <span>{{data.msg}}</span>
- <span :class="data.msg" @click="data.fn1"></span>
- </teleport>
- <div :class="data.msg + 2" @click="data.fn2">bar</div>
- `
- const SSRComp = compileVaporComponent(code, data, undefined, true)
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport3'
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- ctx,
- )
- expect(mainHtml).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport end-->` +
- `<div class="foo2">bar</div>` +
- `<!--]-->`,
- )
- const teleportHtml = ctx.teleports!['#teleport3']
- expect(teleportHtml).toMatchInlineSnapshot(
- `"<!--teleport start anchor--><!--teleport anchor-->"`,
- )
- teleportContainer.innerHTML = teleportHtml
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- mainHtml,
- code,
- data,
- )
- const blocks = block as any[]
- expect(blocks[0]).toBe(container.childNodes[1])
- const teleport = blocks[1] as TeleportFragment
- expect((teleport.nodes as Node[])[0]).toBe(container.childNodes[3])
- expect((teleport.nodes as Node[])[1]).toBe(container.childNodes[4])
- expect(teleport.anchor).toBe(container.childNodes[5])
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[1])
- expect(blocks[2]).toBe(container.childNodes[6])
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>foo</span>` +
- `<span class="foo"></span>` +
- `<!--teleport end-->` +
- `<div class="foo2">bar</div>` +
- `<!--]-->`,
- )
- // event handler
- triggerEvent('click', container.querySelector('.foo')!)
- expect(data.value.fn1).toHaveBeenCalled()
- triggerEvent('click', container.querySelector('.foo2')!)
- expect(data.value.fn2).toHaveBeenCalled()
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>foo</div>` +
- `<!--teleport start-->` +
- `<span>bar</span>` +
- `<span class="bar"></span>` +
- `<!--teleport end-->` +
- `<div class="bar2">bar</div>` +
- `<!--]-->`,
- )
- })
- test('disabled + as component root', async () => {
- const { container } = await mountWithHydration(
- `<!--[-->` +
- `<div>Parent fragment</div>` +
- `<!--teleport start--><div>Teleport content</div><!--teleport end-->` +
- `<!--]-->`,
- `
- <div>Parent fragment</div>
- <teleport to="body" disabled>
- <div>Teleport content</div>
- </teleport>
- `,
- )
- expect(container.innerHTML).toBe(
- `<!--[-->` +
- `<div>Parent fragment</div>` +
- `<!--teleport start-->` +
- `<div>Teleport content</div>` +
- `<!--teleport end-->` +
- `<!--]-->`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- })
- test('as component root', async () => {
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport4'
- teleportContainer.innerHTML = `<!--teleport start anchor-->hello<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<components.Wrapper></components.Wrapper>`,
- undefined,
- {
- Wrapper: compileVaporComponent(
- `<teleport to="#teleport4">hello</teleport>`,
- ),
- },
- )
- const teleport = (block as VaporComponentInstance)
- .block as TeleportFragment
- expect(teleport.anchor).toBe(container.childNodes[1])
- expect(teleport.target).toBe(teleportContainer)
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.nodes).toBe(teleportContainer.childNodes[1])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[2])
- })
- test('nested', async () => {
- const teleportContainer = document.createElement('div')
- teleportContainer.id = 'teleport5'
- teleportContainer.innerHTML =
- `<!--teleport start anchor-->` +
- `<!--teleport start--><!--teleport end-->` +
- `<!--teleport anchor-->` +
- `<!--teleport start anchor-->` +
- `<div>child</div>` +
- `<!--teleport anchor-->`
- document.body.appendChild(teleportContainer)
- const { block, container } = await mountWithHydration(
- '<!--teleport start--><!--teleport end-->',
- `<teleport to="#teleport5">
- <teleport to="#teleport5"><div>child</div></teleport>
- </teleport>`,
- )
- const teleport = block as TeleportFragment
- expect(teleport.anchor).toBe(container.childNodes[1])
- expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
- expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
- const childTeleport = teleport.nodes as TeleportFragment
- expect(childTeleport.anchor).toBe(teleportContainer.childNodes[2])
- expect(childTeleport.targetStart).toBe(teleportContainer.childNodes[4])
- expect(childTeleport.targetAnchor).toBe(teleportContainer.childNodes[6])
- expect(childTeleport.nodes).toBe(teleportContainer.childNodes[5])
- })
- test('unmount (full integration)', async () => {
- const targetId = 'teleport6'
- const data = ref({
- toggle: ref(true),
- })
- const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
- const Comp1 = compileVaporComponent(template1)
- const SSRComp1 = compileVaporComponent(
- template1,
- undefined,
- undefined,
- true,
- )
- const template2 = `<div>Comp2</div>`
- const Comp2 = compileVaporComponent(template2)
- const SSRComp2 = compileVaporComponent(
- template2,
- undefined,
- undefined,
- true,
- )
- const appCode = `
- <div>
- <components.Comp1 v-if="data.toggle"/>
- <components.Comp2 v-else/>
- </div>
- `
- const SSRApp = compileVaporComponent(
- appCode,
- data,
- {
- Comp1: SSRComp1,
- Comp2: SSRComp2,
- },
- true,
- )
- const teleportContainer = document.createElement('div')
- teleportContainer.id = targetId
- document.body.appendChild(teleportContainer)
- const ctx = {} as any
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- ctx,
- )
- expect(mainHtml).toBe(
- '<div><!--teleport start--><!--teleport end--></div>',
- )
- teleportContainer.innerHTML = ctx.teleports![`#${targetId}`]
- const { container } = await mountWithHydration(mainHtml, appCode, data, {
- Comp1,
- Comp2,
- })
- expect(container.innerHTML).toBe(
- '<div><!--teleport start--><!--teleport end--><!--if--></div>',
- )
- expect(teleportContainer.innerHTML).toBe(
- `<!--teleport start anchor-->` +
- `<span>Teleported Comp1</span>` +
- `<!--teleport anchor-->`,
- )
- expect(`mismatch`).not.toHaveBeenWarned()
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
- expect(teleportContainer.innerHTML).toBe('')
- })
- test('unmount (mismatch + full integration)', async () => {
- const targetId = 'teleport7'
- const data = ref({
- toggle: ref(true),
- })
- const template1 = `<Teleport to="#${targetId}"><span>Teleported Comp1</span></Teleport>`
- const Comp1 = compileVaporComponent(template1)
- const SSRComp1 = compileVaporComponent(
- template1,
- undefined,
- undefined,
- true,
- )
- const template2 = `<div>Comp2</div>`
- const Comp2 = compileVaporComponent(template2)
- const SSRComp2 = compileVaporComponent(
- template2,
- undefined,
- undefined,
- true,
- )
- const appCode = `
- <div>
- <components.Comp1 v-if="data.toggle"/>
- <components.Comp2 v-else/>
- </div>
- `
- const SSRApp = compileVaporComponent(
- appCode,
- data,
- {
- Comp1: SSRComp1,
- Comp2: SSRComp2,
- },
- true,
- )
- const teleportContainer = document.createElement('div')
- teleportContainer.id = targetId
- document.body.appendChild(teleportContainer)
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRApp),
- )
- expect(mainHtml).toBe(
- '<div><!--teleport start--><!--teleport end--></div>',
- )
- expect(teleportContainer.innerHTML).toBe('')
- const { container } = await mountWithHydration(mainHtml, appCode, data, {
- Comp1,
- Comp2,
- })
- expect(container.innerHTML).toBe(
- '<div><!--teleport start--><!--teleport end--><!--if--></div>',
- )
- expect(teleportContainer.innerHTML).toBe(`<span>Teleported Comp1</span>`)
- expect(`Hydration children mismatch`).toHaveBeenWarned()
- data.value.toggle = false
- await nextTick()
- expect(container.innerHTML).toBe('<div><div>Comp2</div><!--if--></div>')
- expect(teleportContainer.innerHTML).toBe('')
- })
- test('target change (mismatch + full integration)', async () => {
- const targetId1 = 'teleport8-1'
- const targetId2 = 'teleport8-2'
- const data = ref({
- target: ref(targetId1),
- msg: ref('foo'),
- })
- const template = `<Teleport :to="'#' + data.target"><span>{{data.msg}}</span></Teleport>`
- const Comp = compileVaporComponent(template, data)
- const SSRComp = compileVaporComponent(template, data, undefined, true)
- const teleportContainer1 = document.createElement('div')
- teleportContainer1.id = targetId1
- const teleportContainer2 = document.createElement('div')
- teleportContainer2.id = targetId2
- document.body.appendChild(teleportContainer1)
- document.body.appendChild(teleportContainer2)
- // server render
- const mainHtml = await VueServerRenderer.renderToString(
- runtimeDom.createSSRApp(SSRComp),
- )
- expect(mainHtml).toBe(`<!--teleport start--><!--teleport end-->`)
- expect(teleportContainer1.innerHTML).toBe('')
- expect(teleportContainer2.innerHTML).toBe('')
- // hydrate
- const { container } = await mountWithHydration(mainHtml, template, data, {
- Comp,
- })
- expect(container.innerHTML).toBe(
- `<!--teleport start--><!--teleport end-->`,
- )
- expect(teleportContainer1.innerHTML).toBe(`<span>foo</span>`)
- expect(teleportContainer2.innerHTML).toBe('')
- expect(`Hydration children mismatch`).toHaveBeenWarned()
- data.value.target = targetId2
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start--><!--teleport end-->`,
- )
- expect(teleportContainer1.innerHTML).toBe('')
- expect(teleportContainer2.innerHTML).toBe(`<span>bar</span>`)
- })
- test('with disabled teleport + undefined target', async () => {
- const data = ref({
- msg: ref('foo'),
- })
- const { container } = await mountWithHydration(
- '<!--teleport start--><span>foo</span><!--teleport end-->',
- `<teleport :to="undefined" :disabled="true">
- <span>{{data.msg}}</span>
- </teleport>`,
- data,
- )
- expect(container.innerHTML).toBe(
- `<!--teleport start--><span>foo</span><!--teleport end-->`,
- )
- data.value.msg = 'bar'
- await nextTick()
- expect(container.innerHTML).toBe(
- `<!--teleport start--><span>bar</span><!--teleport end-->`,
- )
- })
- })
- describe.todo('Suspense')
- })
- describe('mismatch handling', () => {
- test('text node', async () => {
- const foo = ref('bar')
- const { container } = await mountWithHydration(`foo`, `{{data}}`, foo)
- expect(container.textContent).toBe('bar')
- expect(`Hydration text mismatch`).toHaveBeenWarned()
- })
- test('element text content', async () => {
- const data = ref({ textContent: 'bar' })
- const { container } = await mountWithHydration(
- `<div>foo</div>`,
- `<div v-bind="data"></div>`,
- data,
- )
- expect(container.innerHTML).toBe('<div>bar</div>')
- expect(`Hydration text content mismatch`).toHaveBeenWarned()
- })
- // test('not enough children', () => {
- // const { container } = mountWithHydration(`<div></div>`, () =>
- // h('div', [h('span', 'foo'), h('span', 'bar')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><span>foo</span><span>bar</span></div>',
- // )
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('too many children', () => {
- // const { container } = mountWithHydration(
- // `<div><span>foo</span><span>bar</span></div>`,
- // () => h('div', [h('span', 'foo')]),
- // )
- // expect(container.innerHTML).toBe('<div><span>foo</span></div>')
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- test('complete mismatch', async () => {
- const data = ref('span')
- const { container } = await mountWithHydration(
- `<div>foo</div>`,
- `<component :is="data">foo</component>`,
- data,
- )
- expect(container.innerHTML).toBe('<span>foo</span><!--dynamic-component-->')
- expect(`Hydration node mismatch`).toHaveBeenWarned()
- })
- // test('fragment mismatch removal', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
- // () => h('div', [h('span', 'replaced')]),
- // )
- // expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('fragment not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
- // )
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('fragment too many children', () => {
- // const { container } = mountWithHydration(
- // `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
- // )
- // // fragment ends early and attempts to hydrate the extra <div>bar</div>
- // // as 2nd fragment child.
- // expect(`Hydration text content mismatch`).toHaveBeenWarned()
- // // excessive children removal
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('Teleport target has empty children', () => {
- // const teleportContainer = document.createElement('div')
- // teleportContainer.id = 'teleport'
- // document.body.appendChild(teleportContainer)
- // mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
- // h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
- // )
- // expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
- // expect(`Hydration children mismatch`).toHaveBeenWarned()
- // })
- // test('comment mismatch (element)', () => {
- // const { container } = mountWithHydration(`<div><span></span></div>`, () =>
- // h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe('<div><!--hi--></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- // test('comment mismatch (text)', () => {
- // const { container } = mountWithHydration(`<div>foobar</div>`, () =>
- // h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe('<div><!--hi--></div>')
- // expect(`Hydration node mismatch`).toHaveBeenWarned()
- // })
- test('class mismatch', async () => {
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref(['foo', 'bar']),
- )
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref({ foo: true, bar: true }),
- )
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('foo bar'),
- )
- // svg classes
- await mountWithHydration(
- `<svg class="foo bar"></svg>`,
- `<svg :class="data"></svg>`,
- ref('foo bar'),
- )
- // class with different order
- await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('bar foo'),
- )
- expect(`Hydration class mismatch`).not.toHaveBeenWarned()
- // single root mismatch
- const { container: root } = await mountWithHydration(
- `<div class="foo bar"></div>`,
- `<div :class="data"></div>`,
- ref('baz'),
- )
- expect(root.innerHTML).toBe('<div class="foo bar baz"></div>')
- expect(`Hydration class mismatch`).toHaveBeenWarned()
- // multiple root mismatch
- const { container } = await mountWithHydration(
- `<div class="foo bar"></div><span/>`,
- `<div :class="data"></div><span/>`,
- ref('foo'),
- )
- expect(container.innerHTML).toBe('<div class="foo"></div><span></span>')
- expect(`Hydration class mismatch`).toHaveBeenWarned()
- })
- test('style mismatch', async () => {
- await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'red' }),
- )
- await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref('color:red;'),
- )
- // style with different order
- await mountWithHydration(
- `<div style="color:red; font-size: 12px;"></div>`,
- `<div :style="data"></div>`,
- ref(`font-size: 12px; color:red;`),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- // single root mismatch
- const { container: root } = await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'green' }),
- )
- expect(root.innerHTML).toBe('<div style="color: green;"></div>')
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- // multiple root mismatch
- const { container } = await mountWithHydration(
- `<div style="color:red;"></div><span/>`,
- `<div :style="data"></div><span/>`,
- ref({ color: 'green' }),
- )
- expect(container.innerHTML).toBe(
- '<div style="color: green;"></div><span></span>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- })
- test('style mismatch when no style attribute is present', async () => {
- await mountWithHydration(
- `<div></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'red' }),
- )
- expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
- })
- test('style mismatch w/ v-show', async () => {
- await mountWithHydration(
- `<div style="color:red;display:none"></div>`,
- `<div v-show="data" style="color: red;"></div>`,
- ref(false),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- // mismatch with single root
- const { container: root } = await mountWithHydration(
- `<div style="color:red;"></div>`,
- `<div v-show="data" style="color: red;"></div>`,
- ref(false),
- )
- expect(root.innerHTML).toBe(
- '<div style="color: red; display: none;"></div>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- // mismatch with multiple root
- const { container } = await mountWithHydration(
- `<div style="color:red;"></div><span/>`,
- `<div v-show="data.show" :style="data.style"></div><span/>`,
- ref({ show: false, style: 'color: red' }),
- )
- expect(container.innerHTML).toBe(
- '<div style="color: red; display: none;"></div><span></span>',
- )
- expect(`Hydration style mismatch`).toHaveBeenWarned()
- })
- test('attr mismatch', async () => {
- await mountWithHydration(
- `<div id="foo"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<div spellcheck></div>`,
- `<div :spellcheck="data"></div>`,
- ref(''),
- )
- await mountWithHydration(
- `<div></div>`,
- `<div :id="data"></div>`,
- ref(undefined),
- )
- // boolean
- await mountWithHydration(
- `<select multiple></div>`,
- `<select :multiple="data"></select>`,
- ref(true),
- )
- await mountWithHydration(
- `<select multiple></div>`,
- `<select :multiple="data"></select>`,
- ref('multiple'),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(
- `<div></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
- await mountWithHydration(
- `<div id="bar"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
- })
- test('attr special case: textarea value', async () => {
- await mountWithHydration(
- `<textarea>foo</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<textarea></textarea>`,
- `<textarea :value="data"></textarea>`,
- ref(''),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(
- `<textarea>foo</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('bar'),
- )
- expect(`Hydration attribute mismatch`).toHaveBeenWarned()
- })
- test('<textarea> with newlines at the beginning', async () => {
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea :value="data"></textarea>`,
- ref('\nhello'),
- )
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea v-text="data"></textarea>`,
- ref('\nhello'),
- )
- await mountWithHydration(
- `<textarea>\nhello</textarea>`,
- `<textarea v-bind="data"></textarea>`,
- ref({ textContent: '\nhello' }),
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- test('<pre> with newlines at the beginning', async () => {
- await mountWithHydration(`<pre>\n</pre>`, `<pre>{{data}}</pre>`, ref('\n'))
- await mountWithHydration(
- `<pre>\n</pre>`,
- `<pre v-text="data"></pre>`,
- ref('\n'),
- )
- await mountWithHydration(
- `<pre>\n</pre>`,
- `<pre v-bind="data"></pre>`,
- ref({ textContent: '\n' }),
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- test('boolean attr handling', async () => {
- await mountWithHydration(
- `<input />`,
- `<input :readonly="data" />`,
- ref(false),
- )
- await mountWithHydration(
- `<input readonly />`,
- `<input :readonly="data" />`,
- ref(true),
- )
- await mountWithHydration(
- `<input readonly="readonly" />`,
- `<input :readonly="data" />`,
- ref(true),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('client value is null or undefined', async () => {
- await mountWithHydration(
- `<div></div>`,
- `<div :draggable="data"></div>`,
- ref(undefined),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- await mountWithHydration(`<input />`, `<input :type="data" />`, ref(null))
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn against object values', async () => {
- await mountWithHydration(`<input />`, `<input :from="data" />`, ref({}))
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn on falsy bindings of non-property keys', async () => {
- await mountWithHydration(
- `<button></button>`,
- `<button :href="data"></button>`,
- ref(undefined),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test('should not warn on non-renderable option values', async () => {
- await mountWithHydration(
- `<select><option>hello</option></select>`,
- `<select><option :value="data">hello</option></select>`,
- ref(['foo']),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- test.todo('should not warn css v-bind', () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
- // const app = createSSRApp({
- // setup() {
- // useCssVars(() => ({
- // foo: 'red',
- // }))
- // return () => h('div', { style: { color: 'var(--foo)' } })
- // },
- // })
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test.todo(
- 'css vars should only be added to expected on component root dom',
- () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
- // const app = createSSRApp({
- // setup() {
- // useCssVars(() => ({
- // foo: 'red',
- // }))
- // return () =>
- // h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
- // },
- // })
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- },
- )
- test.todo('css vars support fallthrough', () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
- // const app = createSSRApp({
- // setup() {
- // useCssVars(() => ({
- // foo: 'red',
- // }))
- // return () => h(Child)
- // },
- // })
- // const Child = {
- // setup() {
- // return () => h('div', { style: 'padding: 4px' })
- // },
- // }
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- // vapor directive does not have a created hook
- test('should not warn for directives that mutate DOM in created', () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div class="test red"></div>`
- // const vColor: ObjectDirective = {
- // created(el, binding) {
- // el.classList.add(binding.value)
- // },
- // }
- // const app = createSSRApp({
- // setup() {
- // return () =>
- // withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
- // },
- // })
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test.todo('escape css var name', () => {
- // const container = document.createElement('div')
- // container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
- // const app = createSSRApp({
- // setup() {
- // useCssVars(() => ({
- // 'foo.bar': 'red',
- // }))
- // return () => h(Child)
- // },
- // })
- // const Child = {
- // setup() {
- // return () => h('div', { style: 'padding: 4px' })
- // },
- // }
- // app.mount(container)
- // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- })
- describe('data-allow-mismatch', () => {
- test('element text content', async () => {
- const data = ref({ textContent: 'bar' })
- const { container } = await mountWithHydration(
- `<div data-allow-mismatch="text">foo</div>`,
- `<div v-bind="data"></div>`,
- data,
- )
- expect(container.innerHTML).toBe(
- '<div data-allow-mismatch="text">bar</div>',
- )
- expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- })
- // test('not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"></div>`,
- // () => h('div', [h('span', 'foo'), h('span', 'bar')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
- // )
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- // test('too many children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
- // () => h('div', [h('span', 'foo')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>foo</span></div>',
- // )
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- test('complete mismatch', async () => {
- const { container } = await mountWithHydration(
- `<div data-allow-mismatch="children"><div>foo</div></div>`,
- `<div><component :is="data">foo</component></div>`,
- ref('span'),
- )
- expect(container.innerHTML).toBe(
- '<div data-allow-mismatch="children"><span>foo</span><!--dynamic-component--></div>',
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- })
- // test('fragment mismatch removal', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
- // () => h('div', [h('span', 'replaced')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><span>replaced</span></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('fragment not enough children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('fragment too many children', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
- // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
- // )
- // // fragment ends early and attempts to hydrate the extra <div>bar</div>
- // // as 2nd fragment child.
- // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
- // // excessive children removal
- // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
- // })
- // test('comment mismatch (element)', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children"><span></span></div>`,
- // () => h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--hi--></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- // test('comment mismatch (text)', () => {
- // const { container } = mountWithHydration(
- // `<div data-allow-mismatch="children">foobar</div>`,
- // () => h('div', [createCommentVNode('hi')]),
- // )
- // expect(container.innerHTML).toBe(
- // '<div data-allow-mismatch="children"><!--hi--></div>',
- // )
- // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- // })
- test('class mismatch', async () => {
- await mountWithHydration(
- `<div class="foo bar" data-allow-mismatch="class"></div>`,
- `<div :class="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration class mismatch`).not.toHaveBeenWarned()
- })
- test('style mismatch', async () => {
- await mountWithHydration(
- `<div style="color:red;" data-allow-mismatch="style"></div>`,
- `<div :style="data"></div>`,
- ref({ color: 'green' }),
- )
- expect(`Hydration style mismatch`).not.toHaveBeenWarned()
- })
- test('attr mismatch', async () => {
- await mountWithHydration(
- `<div data-allow-mismatch="attribute"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- await mountWithHydration(
- `<div id="bar" data-allow-mismatch="attribute"></div>`,
- `<div :id="data"></div>`,
- ref('foo'),
- )
- expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
- })
- })
- describe('VDOM interop', () => {
- test('basic render vapor component', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>{{ data }}</template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
- })
- test('nested components (VDOM -> Vapor -> VDOM)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template><components.VdomChild/></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template>{{ data }}</template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"true"`)
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(`"false"`)
- })
- test('nested components (VDOM -> Vapor -> VDOM (with slot fallback))', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template><components.VdomChild/></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><slot><span>{{data}}</span></slot></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false</span><!--]-->
- "
- `,
- )
- })
- test('nested components (VDOM -> Vapor(with slot content) -> VDOM)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>
- <components.VdomChild>
- <template #default>
- <span>{{data}} vapor fallback</span>
- </template>
- </components.VdomChild>
- </template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template><slot><span>vdom fallback</span></slot></template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true vapor fallback</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false vapor fallback</span><!--]-->
- "
- `,
- )
- })
- test('nested components (VDOM -> Vapor(with slot content) -> Vapor)', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild/>
- </template>`,
- {
- VaporChild: {
- code: `<template>
- <components.VaporChild2>
- <template #default>
- <span>{{data}} vapor fallback</span>
- </template>
- </components.VaporChild2>
- </template>`,
- vapor: true,
- },
- VaporChild2: {
- code: `<template><slot><span>vapor fallback2</span></slot></template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>true vapor fallback</span><!--]-->
- "
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "
- <!--[--><span>false vapor fallback</span><!--]-->
- "
- `,
- )
- })
- test('vapor slot render vdom component', async () => {
- const data = ref(true)
- const { container } = await testWithVDOMApp(
- `<script setup>const data = _data; const components = _components;</script>
- <template>
- <components.VaporChild>
- <components.VdomChild/>
- </components.VaporChild>
- </template>`,
- {
- VaporChild: {
- code: `<template><div><slot/></div></template>`,
- vapor: true,
- },
- VdomChild: {
- code: `<script setup>const data = _data;</script>
- <template>{{ data }}</template>`,
- vapor: false,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->true<!--]-->
- </div>"
- `,
- )
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[-->false<!--]-->
- </div>"
- `,
- )
- })
- test('vapor slot render vdom component (render function)', async () => {
- const data = ref(true)
- const { container } = await testWithVaporApp(
- `<script setup>
- import { h } from 'vue'
- const data = _data; const components = _components;
- const VdomChild = {
- setup() {
- return () => h('div', null, [h('div', [String(data.value)])])
- }
- }
- </script>
- <template>
- <components.VaporChild>
- <VdomChild/>
- </components.VaporChild>
- </template>`,
- {
- VaporChild: {
- code: `<template><div><slot/></div></template>`,
- vapor: true,
- },
- },
- data,
- )
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div>true</div></div><!--]-->
- </div>"
- `,
- )
- expect(`Hydration node mismatch`).not.toHaveBeenWarned()
- data.value = false
- await nextTick()
- expect(formatHtml(container.innerHTML)).toMatchInlineSnapshot(
- `
- "<div>
- <!--[--><div><div>false</div></div><!--]-->
- </div>"
- `,
- )
- })
- })
|