hydration.spec.ts 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757
  1. import { createVaporSSRApp, delegateEvents } from '../src'
  2. import { nextTick, reactive, ref } from '@vue/runtime-dom'
  3. import { compileScript, parse } from '@vue/compiler-sfc'
  4. import * as runtimeVapor from '../src'
  5. import * as runtimeDom from '@vue/runtime-dom'
  6. import * as VueServerRenderer from '@vue/server-renderer'
  7. import {
  8. DYNAMIC_COMPONENT_ANCHOR_LABEL,
  9. FOR_ANCHOR_LABEL,
  10. IF_ANCHOR_LABEL,
  11. SLOT_ANCHOR_LABEL,
  12. } from '@vue/shared'
  13. const Vue = { ...runtimeDom, ...runtimeVapor }
  14. function compile(
  15. sfc: string,
  16. data: runtimeDom.Ref<any>,
  17. components: Record<string, any> = {},
  18. ssr = false,
  19. ) {
  20. if (!sfc.includes(`<script`)) {
  21. sfc =
  22. `<script vapor>const data = _data; const components = _components;</script>` +
  23. sfc
  24. }
  25. const descriptor = parse(sfc).descriptor
  26. const script = compileScript(descriptor, {
  27. id: 'x',
  28. isProd: true,
  29. inlineTemplate: true,
  30. genDefaultAs: '__sfc__',
  31. vapor: true,
  32. templateOptions: {
  33. ssr,
  34. },
  35. })
  36. const code =
  37. script.content
  38. .replace(/\bimport {/g, 'const {')
  39. .replace(/ as _/g, ': _')
  40. .replace(/} from ['"]vue['"]/g, `} = Vue`)
  41. .replace(/} from "vue\/server-renderer"/g, '} = VueServerRenderer') +
  42. '\nreturn __sfc__'
  43. return new Function('Vue', 'VueServerRenderer', '_data', '_components', code)(
  44. Vue,
  45. VueServerRenderer,
  46. data,
  47. components,
  48. )
  49. }
  50. async function testHydration(
  51. code: string,
  52. components: Record<string, string> = {},
  53. data: any = ref('foo'),
  54. ) {
  55. const ssrComponents: any = {}
  56. const clientComponents: any = {}
  57. for (const key in components) {
  58. clientComponents[key] = compile(components[key], data, clientComponents)
  59. ssrComponents[key] = compile(components[key], data, ssrComponents, true)
  60. }
  61. const serverComp = compile(code, data, ssrComponents, true)
  62. const html = await VueServerRenderer.renderToString(
  63. runtimeDom.createSSRApp(serverComp),
  64. )
  65. const container = document.createElement('div')
  66. document.body.appendChild(container)
  67. container.innerHTML = html
  68. const clientComp = compile(code, data, clientComponents)
  69. const app = createVaporSSRApp(clientComp)
  70. app.mount(container)
  71. return { data, container }
  72. }
  73. const triggerEvent = (type: string, el: Element) => {
  74. const event = new Event(type, { bubbles: true })
  75. el.dispatchEvent(event)
  76. }
  77. describe('Vapor Mode hydration', () => {
  78. delegateEvents('click')
  79. beforeEach(() => {
  80. document.body.innerHTML = ''
  81. })
  82. describe('text', () => {
  83. test('root text', async () => {
  84. const { data, container } = await testHydration(`
  85. <template>{{ data }}</template>
  86. `)
  87. expect(container.innerHTML).toMatchInlineSnapshot(`"foo"`)
  88. data.value = 'bar'
  89. await nextTick()
  90. expect(container.innerHTML).toMatchInlineSnapshot(`"bar"`)
  91. })
  92. test('consecutive text nodes', async () => {
  93. const { data, container } = await testHydration(`
  94. <template>{{ data }}{{ data }}</template>
  95. `)
  96. expect(container.innerHTML).toMatchInlineSnapshot(`"foofoo"`)
  97. data.value = 'bar'
  98. await nextTick()
  99. expect(container.innerHTML).toMatchInlineSnapshot(`"barbar"`)
  100. })
  101. test('consecutive text nodes with anchor insertion', async () => {
  102. const { data, container } = await testHydration(`
  103. <template><span/>{{ data }}{{ data }}<span/></template>
  104. `)
  105. expect(container.innerHTML).toMatchInlineSnapshot(
  106. `"<!--[--><span></span>foofoo<span></span><!--]-->"`,
  107. )
  108. data.value = 'bar'
  109. await nextTick()
  110. expect(container.innerHTML).toMatchInlineSnapshot(
  111. `"<!--[--><span></span>barbar<span></span><!--]-->"`,
  112. )
  113. })
  114. test('mixed text nodes', async () => {
  115. const { data, container } = await testHydration(`
  116. <template>{{ data }}A{{ data }}B{{ data }}</template>
  117. `)
  118. expect(container.innerHTML).toMatchInlineSnapshot(`"fooAfooBfoo"`)
  119. data.value = 'bar'
  120. await nextTick()
  121. expect(container.innerHTML).toMatchInlineSnapshot(`"barAbarBbar"`)
  122. })
  123. test('mixed text nodes with anchor insertion', async () => {
  124. const { data, container } = await testHydration(`
  125. <template><span/>{{ data }}A{{ data }}B{{ data }}<span/></template>
  126. `)
  127. expect(container.innerHTML).toMatchInlineSnapshot(
  128. `"<!--[--><span></span>fooAfooBfoo<span></span><!--]-->"`,
  129. )
  130. data.value = 'bar'
  131. await nextTick()
  132. expect(container.innerHTML).toMatchInlineSnapshot(
  133. `"<!--[--><span></span>barAbarBbar<span></span><!--]-->"`,
  134. )
  135. })
  136. })
  137. describe('element', () => {
  138. test('root comment', async () => {
  139. const { container } = await testHydration(`
  140. <template><!----></template>
  141. `)
  142. expect(container.innerHTML).toBe('<!---->')
  143. expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
  144. })
  145. test('root with mixed element and text', async () => {
  146. const { container, data } = await testHydration(`
  147. <template> A<span>{{ data }}</span>{{ data }}</template>
  148. `)
  149. expect(container.innerHTML).toMatchInlineSnapshot(
  150. `"<!--[--> A<span>foo</span>foo<!--]-->"`,
  151. )
  152. data.value = 'bar'
  153. await nextTick()
  154. expect(container.innerHTML).toMatchInlineSnapshot(
  155. `"<!--[--> A<span>bar</span>bar<!--]-->"`,
  156. )
  157. })
  158. test('empty element', async () => {
  159. const { container } = await testHydration(`
  160. <template><div/></template>
  161. `)
  162. expect(container.innerHTML).toBe('<div></div>')
  163. expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
  164. })
  165. test('element with binding and text children', async () => {
  166. const { container, data } = await testHydration(`
  167. <template><div :class="data">{{ data }}</div></template>
  168. `)
  169. expect(container.innerHTML).toMatchInlineSnapshot(
  170. `"<div class="foo">foo</div>"`,
  171. )
  172. data.value = 'bar'
  173. await nextTick()
  174. expect(container.innerHTML).toMatchInlineSnapshot(
  175. `"<div class="bar">bar</div>"`,
  176. )
  177. })
  178. test('element with elements children', async () => {
  179. const { container } = await testHydration(`
  180. <template>
  181. <div>
  182. <span>{{ data }}</span>
  183. <span :class="data" @click="data = 'bar'"/>
  184. </div>
  185. </template>
  186. `)
  187. expect(container.innerHTML).toMatchInlineSnapshot(
  188. `"<div><span>foo</span><span class="foo"></span></div>"`,
  189. )
  190. // event handler
  191. triggerEvent('click', container.querySelector('.foo')!)
  192. await nextTick()
  193. expect(container.innerHTML).toMatchInlineSnapshot(
  194. `"<div><span>bar</span><span class="bar"></span></div>"`,
  195. )
  196. })
  197. })
  198. describe('component', () => {
  199. test('basic component', async () => {
  200. const { container, data } = await testHydration(
  201. `
  202. <template><div><span></span><components.Child/></div></template>
  203. `,
  204. { Child: `<template>{{ data }}</template>` },
  205. )
  206. expect(container.innerHTML).toMatchInlineSnapshot(
  207. `"<div><span></span>foo</div>"`,
  208. )
  209. data.value = 'bar'
  210. await nextTick()
  211. expect(container.innerHTML).toMatchInlineSnapshot(
  212. `"<div><span></span>bar</div>"`,
  213. )
  214. })
  215. test('fragment component', async () => {
  216. const { container, data } = await testHydration(
  217. `
  218. <template><div><span></span><components.Child/></div></template>
  219. `,
  220. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  221. )
  222. expect(container.innerHTML).toMatchInlineSnapshot(
  223. `"<div><span></span><!--[--><div>foo</div>-foo-<!--]--></div>"`,
  224. )
  225. data.value = 'bar'
  226. await nextTick()
  227. expect(container.innerHTML).toMatchInlineSnapshot(
  228. `"<div><span></span><!--[--><div>bar</div>-bar-<!--]--></div>"`,
  229. )
  230. })
  231. test('fragment component with prepend', async () => {
  232. const { container, data } = await testHydration(
  233. `
  234. <template><div><components.Child/><span></span></div></template>
  235. `,
  236. { Child: `<template><div>{{ data }}</div>-{{ data }}-</template>` },
  237. )
  238. expect(container.innerHTML).toMatchInlineSnapshot(
  239. `"<div><!--[--><div>foo</div>-foo-<!--]--><span></span></div>"`,
  240. )
  241. data.value = 'bar'
  242. await nextTick()
  243. expect(container.innerHTML).toMatchInlineSnapshot(
  244. `"<div><!--[--><div>bar</div>-bar-<!--]--><span></span></div>"`,
  245. )
  246. })
  247. test('nested fragment components', async () => {
  248. const { container, data } = await testHydration(
  249. `
  250. <template><div><components.Parent/><span></span></div></template>
  251. `,
  252. {
  253. Parent: `<template><div/><components.Child/><div/></template>`,
  254. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  255. },
  256. )
  257. expect(container.innerHTML).toBe(
  258. `<div>` +
  259. `<!--[-->` +
  260. `<div></div>` +
  261. `<!--[--><div>foo</div>-foo-<!--]-->` +
  262. `<div></div>` +
  263. `<!--]-->` +
  264. `<span></span>` +
  265. `</div>`,
  266. )
  267. data.value = 'bar'
  268. await nextTick()
  269. expect(container.innerHTML).toBe(
  270. `<div>` +
  271. `<!--[-->` +
  272. `<div></div>` +
  273. `<!--[--><div>bar</div>-bar-<!--]-->` +
  274. `<div></div>` +
  275. `<!--]-->` +
  276. `<span></span>` +
  277. `</div>`,
  278. )
  279. })
  280. test('component with anchor insertion', async () => {
  281. const { container, data } = await testHydration(
  282. `<template>
  283. <div>
  284. <span/>
  285. <components.Child/>
  286. <span/>
  287. </div>
  288. </template>
  289. `,
  290. {
  291. Child: `<template>{{ data }}</template>`,
  292. },
  293. )
  294. expect(container.innerHTML).toMatchInlineSnapshot(
  295. `"<div><span></span>foo<span></span></div>"`,
  296. )
  297. data.value = 'bar'
  298. await nextTick()
  299. expect(container.innerHTML).toMatchInlineSnapshot(
  300. `"<div><span></span>bar<span></span></div>"`,
  301. )
  302. })
  303. test('nested components with anchor insertion', async () => {
  304. const { container, data } = await testHydration(
  305. `
  306. <template><components.Parent/></template>
  307. `,
  308. {
  309. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  310. Child: `<template><div>{{ data }}</div></template>`,
  311. },
  312. )
  313. expect(container.innerHTML).toBe(
  314. `<div><span></span><div>foo</div><span></span></div>`,
  315. )
  316. data.value = 'bar'
  317. await nextTick()
  318. expect(container.innerHTML).toBe(
  319. `<div><span></span><div>bar</div><span></span></div>`,
  320. )
  321. })
  322. test('nested components with multi level anchor insertion', async () => {
  323. const { container, data } = await testHydration(
  324. `
  325. <template><div><span></span><components.Parent/><span></span></div></template>
  326. `,
  327. {
  328. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  329. Child: `<template><div>{{ data }}</div></template>`,
  330. },
  331. )
  332. expect(container.innerHTML).toBe(
  333. `<div>` +
  334. `<span></span>` +
  335. `<div>` +
  336. `<span></span>` +
  337. `<div>foo</div>` +
  338. `<span></span>` +
  339. `</div>` +
  340. `<span></span>` +
  341. `</div>`,
  342. )
  343. data.value = 'bar'
  344. await nextTick()
  345. expect(container.innerHTML).toBe(
  346. `<div>` +
  347. `<span></span>` +
  348. `<div>` +
  349. `<span></span>` +
  350. `<div>bar</div>` +
  351. `<span></span>` +
  352. `</div>` +
  353. `<span></span>` +
  354. `</div>`,
  355. )
  356. })
  357. test('consecutive components with anchor insertion', async () => {
  358. const { container, data } = await testHydration(
  359. `<template>
  360. <div>
  361. <span/>
  362. <components.Child/>
  363. <components.Child/>
  364. <span/>
  365. </div>
  366. </template>
  367. `,
  368. {
  369. Child: `<template>{{ data }}</template>`,
  370. },
  371. )
  372. expect(container.innerHTML).toBe(
  373. `<div>` +
  374. `<span></span>` +
  375. `foo` +
  376. `<!--[[-->foo<!--]]-->` +
  377. `<span></span>` +
  378. `</div>`,
  379. )
  380. data.value = 'bar'
  381. await nextTick()
  382. expect(container.innerHTML).toBe(
  383. `<div>` +
  384. `<span></span>` +
  385. `bar` +
  386. `<!--[[-->bar<!--]]-->` +
  387. `<span></span>` +
  388. `</div>`,
  389. )
  390. })
  391. test('nested consecutive components with anchor insertion', async () => {
  392. const { container, data } = await testHydration(
  393. `
  394. <template><components.Parent/></template>
  395. `,
  396. {
  397. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  398. Child: `<template><div>{{ data }}</div></template>`,
  399. },
  400. )
  401. expect(container.innerHTML).toBe(
  402. `<div>` +
  403. `<span></span>` +
  404. `<div>foo</div>` +
  405. `<!--[[--><div>foo</div><!--]]-->` +
  406. `<span></span>` +
  407. `</div>`,
  408. )
  409. data.value = 'bar'
  410. await nextTick()
  411. expect(container.innerHTML).toBe(
  412. `<div>` +
  413. `<span></span>` +
  414. `<div>bar</div>` +
  415. `<!--[[--><div>bar</div><!--]]-->` +
  416. `<span></span>` +
  417. `</div>`,
  418. )
  419. })
  420. test('nested consecutive components with multi level anchor insertion', async () => {
  421. const { container, data } = await testHydration(
  422. `
  423. <template><div><span></span><components.Parent/><span></span></div></template>
  424. `,
  425. {
  426. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  427. Child: `<template><div>{{ data }}</div></template>`,
  428. },
  429. )
  430. expect(container.innerHTML).toBe(
  431. `<div>` +
  432. `<span></span>` +
  433. `<div>` +
  434. `<span></span>` +
  435. `<div>foo</div>` +
  436. `<!--[[--><div>foo</div><!--]]-->` +
  437. `<span></span>` +
  438. `</div>` +
  439. `<span></span>` +
  440. `</div>`,
  441. )
  442. data.value = 'bar'
  443. await nextTick()
  444. expect(container.innerHTML).toBe(
  445. `<div>` +
  446. `<span></span>` +
  447. `<div>` +
  448. `<span></span>` +
  449. `<div>bar</div>` +
  450. `<!--[[--><div>bar</div><!--]]-->` +
  451. `<span></span>` +
  452. `</div>` +
  453. `<span></span>` +
  454. `</div>`,
  455. )
  456. })
  457. test('mixed component and element with anchor insertion', async () => {
  458. const { container, data } = await testHydration(
  459. `<template>
  460. <div>
  461. <span/>
  462. <components.Child/>
  463. <span/>
  464. <components.Child/>
  465. <span/>
  466. </div>
  467. </template>
  468. `,
  469. {
  470. Child: `<template>{{ data }}</template>`,
  471. },
  472. )
  473. expect(container.innerHTML).toBe(
  474. `<div>` +
  475. `<span></span>` +
  476. `foo` +
  477. `<span></span>` +
  478. `foo` +
  479. `<span></span>` +
  480. `</div>`,
  481. )
  482. data.value = 'bar'
  483. await nextTick()
  484. expect(container.innerHTML).toBe(
  485. `<div>` +
  486. `<span></span>` +
  487. `bar` +
  488. `<span></span>` +
  489. `bar` +
  490. `<span></span>` +
  491. `</div>`,
  492. )
  493. })
  494. test('mixed component and text with anchor insertion', async () => {
  495. const { container, data } = await testHydration(
  496. `<template>
  497. <div>
  498. <span/>
  499. <components.Child/>
  500. {{ data }}
  501. <components.Child/>
  502. <span/>
  503. </div>
  504. </template>
  505. `,
  506. {
  507. Child: `<template>{{ data }}</template>`,
  508. },
  509. )
  510. expect(container.innerHTML).toBe(
  511. `<div>` +
  512. `<span></span>` +
  513. `foo` +
  514. `<!--[[--> foo <!--]]-->` +
  515. `foo` +
  516. `<span></span>` +
  517. `</div>`,
  518. )
  519. data.value = 'bar'
  520. await nextTick()
  521. expect(container.innerHTML).toBe(
  522. `<div>` +
  523. `<span></span>` +
  524. `bar` +
  525. `<!--[[--> bar <!--]]-->` +
  526. `bar` +
  527. `<span></span>` +
  528. `</div>`,
  529. )
  530. })
  531. test('fragment component with anchor insertion', async () => {
  532. const { container, data } = await testHydration(
  533. `<template>
  534. <div>
  535. <span/>
  536. <components.Child/>
  537. <span/>
  538. </div>
  539. </template>
  540. `,
  541. {
  542. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  543. },
  544. )
  545. expect(container.innerHTML).toBe(
  546. `<div>` +
  547. `<span></span>` +
  548. `<!--[--><div>foo</div>-foo<!--]-->` +
  549. `<span></span>` +
  550. `</div>`,
  551. )
  552. data.value = 'bar'
  553. await nextTick()
  554. expect(container.innerHTML).toBe(
  555. `<div>` +
  556. `<span></span>` +
  557. `<!--[--><div>bar</div>-bar<!--]-->` +
  558. `<span></span>` +
  559. `</div>`,
  560. )
  561. })
  562. test('nested fragment component with anchor insertion', async () => {
  563. const { container, data } = await testHydration(
  564. `
  565. <template><components.Parent/></template>
  566. `,
  567. {
  568. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  569. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  570. },
  571. )
  572. expect(container.innerHTML).toBe(
  573. `<div>` +
  574. `<span></span>` +
  575. `<!--[--><div>foo</div>-foo-<!--]-->` +
  576. `<span></span>` +
  577. `</div>`,
  578. )
  579. data.value = 'bar'
  580. await nextTick()
  581. expect(container.innerHTML).toBe(
  582. `<div>` +
  583. `<span></span>` +
  584. `<!--[--><div>bar</div>-bar-<!--]-->` +
  585. `<span></span>` +
  586. `</div>`,
  587. )
  588. })
  589. test('nested fragment component with multi level anchor insertion', async () => {
  590. const { container, data } = await testHydration(
  591. `
  592. <template><div><span/><components.Parent/><span/></div></template>
  593. `,
  594. {
  595. Parent: `<template><div><span/><components.Child/><span/></div></template>`,
  596. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  597. },
  598. )
  599. expect(container.innerHTML).toBe(
  600. `<div>` +
  601. `<span></span>` +
  602. `<div>` +
  603. `<span></span>` +
  604. `<!--[--><div>foo</div>-foo-<!--]-->` +
  605. `<span></span>` +
  606. `</div>` +
  607. `<span></span>` +
  608. `</div>`,
  609. )
  610. data.value = 'bar'
  611. await nextTick()
  612. expect(container.innerHTML).toBe(
  613. `<div>` +
  614. `<span></span>` +
  615. `<div>` +
  616. `<span></span>` +
  617. `<!--[--><div>bar</div>-bar-<!--]-->` +
  618. `<span></span>` +
  619. `</div>` +
  620. `<span></span>` +
  621. `</div>`,
  622. )
  623. })
  624. test('consecutive fragment components with anchor insertion', async () => {
  625. const { container, data } = await testHydration(
  626. `<template>
  627. <div>
  628. <span/>
  629. <components.Child/>
  630. <components.Child/>
  631. <span/>
  632. </div>
  633. </template>
  634. `,
  635. {
  636. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  637. },
  638. )
  639. expect(container.innerHTML).toBe(
  640. `<div>` +
  641. `<span></span>` +
  642. `<!--[--><div>foo</div>-foo<!--]-->` +
  643. `<!--[[-->` +
  644. `<!--[--><div>foo</div>-foo<!--]-->` +
  645. `<!--]]-->` +
  646. `<span></span>` +
  647. `</div>`,
  648. )
  649. data.value = 'bar'
  650. await nextTick()
  651. expect(container.innerHTML).toBe(
  652. `<div>` +
  653. `<span></span>` +
  654. `<!--[--><div>bar</div>-bar<!--]-->` +
  655. `<!--[[-->` +
  656. `<!--[--><div>bar</div>-bar<!--]-->` +
  657. `<!--]]-->` +
  658. `<span></span>` +
  659. `</div>`,
  660. )
  661. })
  662. test('nested consecutive fragment components with anchor insertion', async () => {
  663. const { container, data } = await testHydration(
  664. `
  665. <template><components.Parent/></template>
  666. `,
  667. {
  668. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  669. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  670. },
  671. )
  672. expect(container.innerHTML).toBe(
  673. `<div>` +
  674. `<span></span>` +
  675. `<!--[--><div>foo</div>-foo-<!--]-->` +
  676. `<!--[[-->` +
  677. `<!--[--><div>foo</div>-foo-<!--]-->` +
  678. `<!--]]-->` +
  679. `<span></span>` +
  680. `</div>`,
  681. )
  682. data.value = 'bar'
  683. await nextTick()
  684. expect(container.innerHTML).toBe(
  685. `<div>` +
  686. `<span></span>` +
  687. `<!--[--><div>bar</div>-bar-<!--]-->` +
  688. `<!--[[-->` +
  689. `<!--[--><div>bar</div>-bar-<!--]-->` +
  690. `<!--]]-->` +
  691. `<span></span>` +
  692. `</div>`,
  693. )
  694. })
  695. test('nested consecutive fragment components with multi level anchor insertion', async () => {
  696. const { container, data } = await testHydration(
  697. `
  698. <template><div><span></span><components.Parent/><span></span></div></template>
  699. `,
  700. {
  701. Parent: `<template><div><span/><components.Child/><components.Child/><span/></div></template>`,
  702. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  703. },
  704. )
  705. expect(container.innerHTML).toBe(
  706. `<div>` +
  707. `<span></span>` +
  708. `<div>` +
  709. `<span></span>` +
  710. `<!--[--><div>foo</div>-foo-<!--]-->` +
  711. `<!--[[-->` +
  712. `<!--[--><div>foo</div>-foo-<!--]-->` +
  713. `<!--]]-->` +
  714. `<span></span>` +
  715. `</div>` +
  716. `<span></span>` +
  717. `</div>`,
  718. )
  719. data.value = 'bar'
  720. await nextTick()
  721. expect(container.innerHTML).toBe(
  722. `<div>` +
  723. `<span></span>` +
  724. `<div>` +
  725. `<span></span>` +
  726. `<!--[--><div>bar</div>-bar-<!--]-->` +
  727. `<!--[[-->` +
  728. `<!--[--><div>bar</div>-bar-<!--]-->` +
  729. `<!--]]-->` +
  730. `<span></span>` +
  731. `</div>` +
  732. `<span></span>` +
  733. `</div>`,
  734. )
  735. })
  736. test('nested consecutive fragment components with root level anchor insertion', async () => {
  737. const { container, data } = await testHydration(
  738. `
  739. <template><div><span></span><components.Parent/><span></span></div></template>
  740. `,
  741. {
  742. Parent: `<template><components.Child/><components.Child/></template>`,
  743. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  744. },
  745. )
  746. expect(container.innerHTML).toBe(
  747. `<div>` +
  748. `<span></span>` +
  749. `<!--[-->` +
  750. `<!--[--><div>foo</div>-foo-<!--]-->` +
  751. `<!--[--><div>foo</div>-foo-<!--]-->` +
  752. `<!--]-->` +
  753. `<span></span>` +
  754. `</div>`,
  755. )
  756. data.value = 'bar'
  757. await nextTick()
  758. expect(container.innerHTML).toBe(
  759. `<div>` +
  760. `<span></span>` +
  761. `<!--[-->` +
  762. `<!--[--><div>bar</div>-bar-<!--]-->` +
  763. `<!--[--><div>bar</div>-bar-<!--]-->` +
  764. `<!--]-->` +
  765. `<span></span>` +
  766. `</div>`,
  767. )
  768. })
  769. test('mixed fragment component and element with anchor insertion', async () => {
  770. const { container, data } = await testHydration(
  771. `<template>
  772. <div>
  773. <span/>
  774. <components.Child/>
  775. <span/>
  776. <components.Child/>
  777. <span/>
  778. </div>
  779. </template>
  780. `,
  781. {
  782. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  783. },
  784. )
  785. expect(container.innerHTML).toBe(
  786. `<div>` +
  787. `<span></span>` +
  788. `<!--[--><div>foo</div>-foo<!--]-->` +
  789. `<span></span>` +
  790. `<!--[--><div>foo</div>-foo<!--]-->` +
  791. `<span></span>` +
  792. `</div>`,
  793. )
  794. data.value = 'bar'
  795. await nextTick()
  796. expect(container.innerHTML).toBe(
  797. `<div>` +
  798. `<span></span>` +
  799. `<!--[--><div>bar</div>-bar<!--]-->` +
  800. `<span></span>` +
  801. `<!--[--><div>bar</div>-bar<!--]-->` +
  802. `<span></span>` +
  803. `</div>`,
  804. )
  805. })
  806. test('mixed fragment component and text with anchor insertion', async () => {
  807. const { container, data } = await testHydration(
  808. `<template>
  809. <div>
  810. <span/>
  811. <components.Child/>
  812. {{ data }}
  813. <components.Child/>
  814. <span/>
  815. </div>
  816. </template>
  817. `,
  818. {
  819. Child: `<template><div>{{ data }}</div>-{{ data }}</template>`,
  820. },
  821. )
  822. expect(container.innerHTML).toBe(
  823. `<div>` +
  824. `<span></span>` +
  825. `<!--[--><div>foo</div>-foo<!--]-->` +
  826. ` <!--[[--> foo <!--]]--> ` +
  827. `<!--[--><div>foo</div>-foo<!--]-->` +
  828. `<span></span>` +
  829. `</div>`,
  830. )
  831. data.value = 'bar'
  832. await nextTick()
  833. expect(container.innerHTML).toBe(
  834. `<div>` +
  835. `<span></span>` +
  836. `<!--[--><div>bar</div>-bar<!--]-->` +
  837. ` <!--[[--> bar <!--]]--> ` +
  838. `<!--[--><div>bar</div>-bar<!--]-->` +
  839. `<span></span>` +
  840. `</div>`,
  841. )
  842. })
  843. })
  844. describe('dynamic component', () => {
  845. const anchorLabel = DYNAMIC_COMPONENT_ANCHOR_LABEL
  846. test('basic dynamic component', async () => {
  847. const { container, data } = await testHydration(
  848. `<template>
  849. <component :is="components[data]"/>
  850. </template>`,
  851. {
  852. foo: `<template><div>foo</div></template>`,
  853. bar: `<template><div>bar</div></template>`,
  854. },
  855. ref('foo'),
  856. )
  857. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  858. data.value = 'bar'
  859. await nextTick()
  860. expect(container.innerHTML).toBe(`<div>bar</div><!--${anchorLabel}-->`)
  861. })
  862. test('dynamic component with anchor insertion', async () => {
  863. const { container, data } = await testHydration(
  864. `<template>
  865. <div>
  866. <span/>
  867. <component :is="components[data]"/>
  868. <span/>
  869. </div>
  870. </template>`,
  871. {
  872. foo: `<template><div>foo</div></template>`,
  873. bar: `<template><div>bar</div></template>`,
  874. },
  875. ref('foo'),
  876. )
  877. expect(container.innerHTML).toBe(
  878. `<div>` +
  879. `<span></span>` +
  880. `<div>foo</div><!--${anchorLabel}-->` +
  881. `<span></span>` +
  882. `</div>`,
  883. )
  884. data.value = 'bar'
  885. await nextTick()
  886. expect(container.innerHTML).toBe(
  887. `<div>` +
  888. `<span></span>` +
  889. `<div>bar</div><!--${anchorLabel}-->` +
  890. `<span></span>` +
  891. `</div>`,
  892. )
  893. })
  894. test('consecutive dynamic components with anchor insertion', async () => {
  895. const { container, data } = await testHydration(
  896. `<template>
  897. <div>
  898. <span/>
  899. <component :is="components[data]"/>
  900. <component :is="components[data]"/>
  901. <span/>
  902. </div>
  903. </template>`,
  904. {
  905. foo: `<template><div>foo</div></template>`,
  906. bar: `<template><div>bar</div></template>`,
  907. },
  908. ref('foo'),
  909. )
  910. expect(container.innerHTML).toBe(
  911. `<div>` +
  912. `<span></span>` +
  913. `<div>foo</div><!--${anchorLabel}-->` +
  914. `<!--[[--><div>foo</div><!--${anchorLabel}--><!--]]-->` +
  915. `<span></span>` +
  916. `</div>`,
  917. )
  918. data.value = 'bar'
  919. await nextTick()
  920. expect(container.innerHTML).toBe(
  921. `<div>` +
  922. `<span></span>` +
  923. `<div>bar</div><!--${anchorLabel}-->` +
  924. `<!--[[--><div>bar</div><!--${anchorLabel}--><!--]]-->` +
  925. `<span></span>` +
  926. `</div>`,
  927. )
  928. })
  929. })
  930. describe('if', () => {
  931. const anchorLabel = IF_ANCHOR_LABEL
  932. test('basic toggle - true -> false', async () => {
  933. const data = ref(true)
  934. const { container } = await testHydration(
  935. `<template>
  936. <div v-if="data">foo</div>
  937. </template>`,
  938. undefined,
  939. data,
  940. )
  941. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  942. data.value = false
  943. await nextTick()
  944. expect(container.innerHTML).toBe(`<!--${anchorLabel}-->`)
  945. })
  946. test('basic toggle - false -> true', async () => {
  947. const data = ref(false)
  948. const { container } = await testHydration(
  949. `<template>
  950. <div v-if="data">foo</div>
  951. </template>`,
  952. undefined,
  953. data,
  954. )
  955. // v-if="false" is rendered as <!----> in the server-rendered HTML
  956. // it reused as anchor, so the anchor label is empty
  957. expect(container.innerHTML).toBe(`<!---->`)
  958. data.value = true
  959. await nextTick()
  960. expect(container.innerHTML).toBe(`<div>foo</div><!---->`)
  961. })
  962. test('v-if/else-if/else chain - switch branches', async () => {
  963. const data = ref('a')
  964. const { container } = await testHydration(
  965. `<template>
  966. <div v-if="data === 'a'">foo</div>
  967. <div v-else-if="data === 'b'">bar</div>
  968. <div v-else>baz</div>
  969. </template>`,
  970. undefined,
  971. data,
  972. )
  973. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  974. data.value = 'b'
  975. await nextTick()
  976. expect(container.innerHTML).toBe(
  977. `<div>bar</div><!--${anchorLabel}--><!--${anchorLabel}-->`,
  978. )
  979. data.value = 'c'
  980. await nextTick()
  981. expect(container.innerHTML).toBe(
  982. `<div>baz</div><!--${anchorLabel}--><!--${anchorLabel}-->`,
  983. )
  984. data.value = 'a'
  985. await nextTick()
  986. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  987. })
  988. test('v-if/else-if/else chain - switch branches (PROD)', async () => {
  989. try {
  990. __DEV__ = false
  991. const data = ref('a')
  992. const { container } = await testHydration(
  993. `<template>
  994. <div v-if="data === 'a'">foo</div>
  995. <div v-else-if="data === 'b'">bar</div>
  996. <div v-else>baz</div>
  997. </template>`,
  998. undefined,
  999. data,
  1000. )
  1001. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  1002. data.value = 'b'
  1003. await nextTick()
  1004. // In PROD, the anchor of v-else-if (DynamicFragment) is an empty text node,
  1005. // so it won't be rendered
  1006. expect(container.innerHTML).toBe(`<div>bar</div><!--${anchorLabel}-->`)
  1007. data.value = 'c'
  1008. await nextTick()
  1009. // same as above
  1010. expect(container.innerHTML).toBe(`<div>baz</div><!--${anchorLabel}-->`)
  1011. data.value = 'a'
  1012. await nextTick()
  1013. expect(container.innerHTML).toBe(`<div>foo</div><!--${anchorLabel}-->`)
  1014. } finally {
  1015. __DEV__ = true
  1016. }
  1017. })
  1018. test('nested if', async () => {
  1019. const data = reactive({ outer: true, inner: true })
  1020. const { container } = await testHydration(
  1021. `<template>
  1022. <div v-if="data.outer">
  1023. <span>outer</span>
  1024. <div v-if="data.inner">inner</div>
  1025. </div>
  1026. </template>`,
  1027. undefined,
  1028. data,
  1029. )
  1030. expect(container.innerHTML).toBe(
  1031. `<div>` +
  1032. `<span>outer</span>` +
  1033. `<div>inner</div><!--${anchorLabel}-->` +
  1034. `</div><!--${anchorLabel}-->`,
  1035. )
  1036. data.inner = false
  1037. await nextTick()
  1038. expect(container.innerHTML).toBe(
  1039. `<div>` +
  1040. `<span>outer</span>` +
  1041. `<!--${anchorLabel}-->` +
  1042. `</div><!--${anchorLabel}-->`,
  1043. )
  1044. data.outer = false
  1045. await nextTick()
  1046. expect(container.innerHTML).toBe(`<!--${anchorLabel}-->`)
  1047. })
  1048. test('on component', async () => {
  1049. const data = ref(true)
  1050. const { container } = await testHydration(
  1051. `<template>
  1052. <components.Child v-if="data"/>
  1053. </template>`,
  1054. { Child: `<template>foo</template>` },
  1055. data,
  1056. )
  1057. expect(container.innerHTML).toBe(`foo<!--${anchorLabel}-->`)
  1058. data.value = false
  1059. await nextTick()
  1060. expect(container.innerHTML).toBe(`<!--${anchorLabel}-->`)
  1061. })
  1062. test('v-if/else-if/else chain on component - switch branches', async () => {
  1063. const data = ref('a')
  1064. const { container } = await testHydration(
  1065. `<template>
  1066. <components.Child1 v-if="data === 'a'"/>
  1067. <components.Child2 v-else-if="data === 'b'"/>
  1068. <components.Child3 v-else/>
  1069. </template>`,
  1070. {
  1071. Child1: `<template><span>{{data}} child1</span></template>`,
  1072. Child2: `<template><span>{{data}} child2</span></template>`,
  1073. Child3: `<template><span>{{data}} child3</span></template>`,
  1074. },
  1075. data,
  1076. )
  1077. expect(container.innerHTML).toBe(
  1078. `<span>a child1</span><!--${anchorLabel}-->`,
  1079. )
  1080. data.value = 'b'
  1081. await nextTick()
  1082. expect(container.innerHTML).toBe(
  1083. `<span>b child2</span><!--${anchorLabel}--><!--${anchorLabel}-->`,
  1084. )
  1085. data.value = 'c'
  1086. await nextTick()
  1087. expect(container.innerHTML).toBe(
  1088. `<span>c child3</span><!--${anchorLabel}--><!--${anchorLabel}-->`,
  1089. )
  1090. data.value = 'a'
  1091. await nextTick()
  1092. expect(container.innerHTML).toBe(
  1093. `<span>a child1</span><!--${anchorLabel}-->`,
  1094. )
  1095. })
  1096. test('v-if/else-if/else chain on component - switch branches (PROD)', async () => {
  1097. try {
  1098. __DEV__ = false
  1099. const data = ref('a')
  1100. const { container } = await testHydration(
  1101. `<template>
  1102. <components.Child1 v-if="data === 'a'"/>
  1103. <components.Child2 v-else-if="data === 'b'"/>
  1104. <components.Child3 v-else/>
  1105. </template>`,
  1106. {
  1107. Child1: `<template><span>{{data}} child1</span></template>`,
  1108. Child2: `<template><span>{{data}} child2</span></template>`,
  1109. Child3: `<template><span>{{data}} child3</span></template>`,
  1110. },
  1111. data,
  1112. )
  1113. expect(container.innerHTML).toBe(
  1114. `<span>a child1</span><!--${anchorLabel}-->`,
  1115. )
  1116. data.value = 'b'
  1117. await nextTick()
  1118. // In PROD, the anchor of v-else-if (DynamicFragment) is an empty text node,
  1119. // so it won't be rendered
  1120. expect(container.innerHTML).toBe(
  1121. `<span>b child2</span><!--${anchorLabel}-->`,
  1122. )
  1123. data.value = 'c'
  1124. await nextTick()
  1125. // same as above
  1126. expect(container.innerHTML).toBe(
  1127. `<span>c child3</span><!--${anchorLabel}-->`,
  1128. )
  1129. data.value = 'a'
  1130. await nextTick()
  1131. expect(container.innerHTML).toBe(
  1132. `<span>a child1</span><!--${anchorLabel}-->`,
  1133. )
  1134. } finally {
  1135. __DEV__ = true
  1136. }
  1137. })
  1138. test('on component with anchor insertion', async () => {
  1139. const data = ref(true)
  1140. const { container } = await testHydration(
  1141. `<template>
  1142. <div>
  1143. <span/>
  1144. <components.Child v-if="data"/>
  1145. <span/>
  1146. </div>
  1147. </template>`,
  1148. { Child: `<template>foo</template>` },
  1149. data,
  1150. )
  1151. expect(container.innerHTML).toBe(
  1152. `<div>` +
  1153. `<span></span>` +
  1154. `foo<!--${anchorLabel}-->` +
  1155. `<span></span>` +
  1156. `</div>`,
  1157. )
  1158. data.value = false
  1159. await nextTick()
  1160. expect(container.innerHTML).toBe(
  1161. `<div>` +
  1162. `<span></span>` +
  1163. `<!--${anchorLabel}-->` +
  1164. `<span></span>` +
  1165. `</div>`,
  1166. )
  1167. })
  1168. test('consecutive v-if on component with anchor insertion', async () => {
  1169. const data = ref(true)
  1170. const { container } = await testHydration(
  1171. `<template>
  1172. <div>
  1173. <span/>
  1174. <components.Child v-if="data"/>
  1175. <components.Child v-if="data"/>
  1176. <span/>
  1177. </div>
  1178. </template>`,
  1179. { Child: `<template>foo</template>` },
  1180. data,
  1181. )
  1182. expect(container.innerHTML).toBe(
  1183. `<div>` +
  1184. `<span></span>` +
  1185. `foo<!--${anchorLabel}-->` +
  1186. `foo<!--${anchorLabel}-->` +
  1187. `<span></span>` +
  1188. `</div>`,
  1189. )
  1190. data.value = false
  1191. await nextTick()
  1192. expect(container.innerHTML).toBe(
  1193. `<div>` +
  1194. `<span></span>` +
  1195. `<!--${anchorLabel}-->` +
  1196. `<!--${anchorLabel}-->` +
  1197. `<span></span>` +
  1198. `</div>`,
  1199. )
  1200. })
  1201. test('on fragment component', async () => {
  1202. const data = ref(true)
  1203. const { container } = await testHydration(
  1204. `<template>
  1205. <div>
  1206. <components.Child v-if="data"/>
  1207. </div>
  1208. </template>`,
  1209. {
  1210. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1211. },
  1212. data,
  1213. )
  1214. expect(container.innerHTML).toBe(
  1215. `<div>` +
  1216. `<!--[--><div>true</div>-true-<!--]-->` +
  1217. `<!--if-->` +
  1218. `</div>`,
  1219. )
  1220. data.value = false
  1221. await nextTick()
  1222. expect(container.innerHTML).toBe(
  1223. `<div><!--[--><!--]--><!--${anchorLabel}--></div>`,
  1224. )
  1225. })
  1226. test('on fragment component with anchor insertion', async () => {
  1227. const data = ref(true)
  1228. const { container } = await testHydration(
  1229. `<template>
  1230. <div>
  1231. <span/>
  1232. <components.Child v-if="data"/>
  1233. <span/>
  1234. </div>
  1235. </template>`,
  1236. {
  1237. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1238. },
  1239. data,
  1240. )
  1241. expect(container.innerHTML).toBe(
  1242. `<div>` +
  1243. `<span></span>` +
  1244. `<!--[--><div>true</div>-true-<!--]-->` +
  1245. `<!--if-->` +
  1246. `<span></span>` +
  1247. `</div>`,
  1248. )
  1249. data.value = false
  1250. await nextTick()
  1251. expect(container.innerHTML).toBe(
  1252. `<div>` +
  1253. `<span></span>` +
  1254. `<!--[--><!--]-->` +
  1255. `<!--${anchorLabel}-->` +
  1256. `<span></span>` +
  1257. `</div>`,
  1258. )
  1259. })
  1260. test('consecutive v-if on fragment component with anchor insertion', async () => {
  1261. const data = ref(true)
  1262. const { container } = await testHydration(
  1263. `<template>
  1264. <div>
  1265. <span/>
  1266. <components.Child v-if="data"/>
  1267. <components.Child v-if="data"/>
  1268. <span/>
  1269. </div>
  1270. </template>`,
  1271. {
  1272. Child: `<template><div>{{ data }}</div>-{{ data }}-</template>`,
  1273. },
  1274. data,
  1275. )
  1276. expect(container.innerHTML).toBe(
  1277. `<div>` +
  1278. `<span></span>` +
  1279. `<!--[--><div>true</div>-true-<!--]--><!--${anchorLabel}-->` +
  1280. `<!--[--><div>true</div>-true-<!--]--><!--${anchorLabel}-->` +
  1281. `<span></span>` +
  1282. `</div>`,
  1283. )
  1284. data.value = false
  1285. await nextTick()
  1286. expect(container.innerHTML).toBe(
  1287. `<div>` +
  1288. `<span></span>` +
  1289. `<!--[--><!--]--><!--${anchorLabel}-->` +
  1290. `<!--[--><!--]--><!--${anchorLabel}-->` +
  1291. `<span></span>` +
  1292. `</div>`,
  1293. )
  1294. })
  1295. test('on dynamic component with anchor insertion', async () => {
  1296. const dynamicComponentAnchorLabel = DYNAMIC_COMPONENT_ANCHOR_LABEL
  1297. const data = ref(true)
  1298. const { container } = await testHydration(
  1299. `<template>
  1300. <div>
  1301. <span/>
  1302. <component :is="components.Child" v-if="data"/>
  1303. <span/>
  1304. </div>
  1305. </template>`,
  1306. { Child: `<template>foo</template>` },
  1307. data,
  1308. )
  1309. expect(container.innerHTML).toBe(
  1310. `<div>` +
  1311. `<span></span>` +
  1312. `foo<!--${dynamicComponentAnchorLabel}--><!--${anchorLabel}-->` +
  1313. `<span></span>` +
  1314. `</div>`,
  1315. )
  1316. data.value = false
  1317. await nextTick()
  1318. expect(container.innerHTML).toBe(
  1319. `<div>` +
  1320. `<span></span>` +
  1321. `<!--${anchorLabel}-->` +
  1322. `<span></span>` +
  1323. `</div>`,
  1324. )
  1325. })
  1326. })
  1327. describe('for', () => {
  1328. const forAnchorLabel = FOR_ANCHOR_LABEL
  1329. const slotAnchorLabel = SLOT_ANCHOR_LABEL
  1330. test('basic v-for', async () => {
  1331. const { container, data } = await testHydration(
  1332. `<template>
  1333. <div>
  1334. <span v-for="item in data" :key="item">{{ item }}</span>
  1335. </div>
  1336. </template>`,
  1337. undefined,
  1338. ref(['a', 'b', 'c']),
  1339. )
  1340. expect(container.innerHTML).toBe(
  1341. `<div>` +
  1342. `<!--[-->` +
  1343. `<span>a</span>` +
  1344. `<span>b</span>` +
  1345. `<span>c</span>` +
  1346. `<!--]--><!--${forAnchorLabel}-->` +
  1347. `</div>`,
  1348. )
  1349. data.value.push('d')
  1350. await nextTick()
  1351. expect(container.innerHTML).toBe(
  1352. `<div>` +
  1353. `<!--[-->` +
  1354. `<span>a</span>` +
  1355. `<span>b</span>` +
  1356. `<span>c</span>` +
  1357. `<!--]-->` +
  1358. `<span>d</span>` +
  1359. `<!--${forAnchorLabel}-->` +
  1360. `</div>`,
  1361. )
  1362. })
  1363. test('v-for with text node', async () => {
  1364. const { container, data } = await testHydration(
  1365. `<template>
  1366. <div>
  1367. <span v-for="item in data" :key="item">{{ item }}</span>
  1368. </div>
  1369. </template>`,
  1370. undefined,
  1371. ref(['a', 'b', 'c']),
  1372. )
  1373. expect(container.innerHTML).toBe(
  1374. `<div>` +
  1375. `<!--[-->` +
  1376. `<span>a</span><span>b</span><span>c</span>` +
  1377. `<!--]--><!--${forAnchorLabel}-->` +
  1378. `</div>`,
  1379. )
  1380. data.value.push('d')
  1381. await nextTick()
  1382. expect(container.innerHTML).toBe(
  1383. `<div>` +
  1384. `<!--[-->` +
  1385. `<span>a</span><span>b</span><span>c</span>` +
  1386. `<!--]-->` +
  1387. `<span>d</span>` +
  1388. `<!--${forAnchorLabel}-->` +
  1389. `</div>`,
  1390. )
  1391. })
  1392. test('v-for with anchor insertion', async () => {
  1393. const { container, data } = await testHydration(
  1394. `<template>
  1395. <div>
  1396. <span/>
  1397. <span v-for="item in data" :key="item">{{ item }}</span>
  1398. <span/>
  1399. </div>
  1400. </template>`,
  1401. undefined,
  1402. ref(['a', 'b', 'c']),
  1403. )
  1404. expect(container.innerHTML).toBe(
  1405. `<div>` +
  1406. `<span></span>` +
  1407. `<!--[-->` +
  1408. `<span>a</span>` +
  1409. `<span>b</span>` +
  1410. `<span>c</span>` +
  1411. `<!--]--><!--${forAnchorLabel}-->` +
  1412. `<span></span>` +
  1413. `</div>`,
  1414. )
  1415. data.value.push('d')
  1416. await nextTick()
  1417. expect(container.innerHTML).toBe(
  1418. `<div>` +
  1419. `<span></span>` +
  1420. `<!--[-->` +
  1421. `<span>a</span>` +
  1422. `<span>b</span>` +
  1423. `<span>c</span>` +
  1424. `<!--]-->` +
  1425. `<span>d</span>` +
  1426. `<!--${forAnchorLabel}-->` +
  1427. `<span></span>` +
  1428. `</div>`,
  1429. )
  1430. data.value.splice(0, 1)
  1431. await nextTick()
  1432. expect(container.innerHTML).toBe(
  1433. `<div>` +
  1434. `<span></span>` +
  1435. `<!--[-->` +
  1436. `<span>b</span>` +
  1437. `<span>c</span>` +
  1438. `<!--]-->` +
  1439. `<span>d</span>` +
  1440. `<!--${forAnchorLabel}-->` +
  1441. `<span></span>` +
  1442. `</div>`,
  1443. )
  1444. })
  1445. test('consecutive v-for with anchor insertion', async () => {
  1446. const { container, data } = await testHydration(
  1447. `<template>
  1448. <div>
  1449. <span/>
  1450. <span v-for="item in data" :key="item">{{ item }}</span>
  1451. <span v-for="item in data" :key="item">{{ item }}</span>
  1452. <span/>
  1453. </div>
  1454. </template>`,
  1455. undefined,
  1456. ref(['a', 'b', 'c']),
  1457. )
  1458. expect(container.innerHTML).toBe(
  1459. `<div>` +
  1460. `<span></span>` +
  1461. `<!--[-->` +
  1462. `<span>a</span>` +
  1463. `<span>b</span>` +
  1464. `<span>c</span>` +
  1465. `<!--]--><!--${forAnchorLabel}-->` +
  1466. `<!--[-->` +
  1467. `<span>a</span>` +
  1468. `<span>b</span>` +
  1469. `<span>c</span>` +
  1470. `<!--]--><!--${forAnchorLabel}-->` +
  1471. `<span></span>` +
  1472. `</div>`,
  1473. )
  1474. data.value.push('d')
  1475. await nextTick()
  1476. expect(container.innerHTML).toBe(
  1477. `<div>` +
  1478. `<span></span>` +
  1479. `<!--[-->` +
  1480. `<span>a</span>` +
  1481. `<span>b</span>` +
  1482. `<span>c</span>` +
  1483. `<!--]-->` +
  1484. `<span>d</span>` +
  1485. `<!--${forAnchorLabel}-->` +
  1486. `<!--[-->` +
  1487. `<span>a</span>` +
  1488. `<span>b</span>` +
  1489. `<span>c</span>` +
  1490. `<!--]-->` +
  1491. `<span>d</span>` +
  1492. `<!--${forAnchorLabel}-->` +
  1493. `<span></span>` +
  1494. `</div>`,
  1495. )
  1496. data.value.splice(0, 2)
  1497. await nextTick()
  1498. expect(container.innerHTML).toBe(
  1499. `<div>` +
  1500. `<span></span>` +
  1501. `<!--[-->` +
  1502. `<span>c</span>` +
  1503. `<!--]-->` +
  1504. `<span>d</span>` +
  1505. `<!--${forAnchorLabel}-->` +
  1506. `<!--[-->` +
  1507. `<span>c</span>` +
  1508. `<!--]-->` +
  1509. `<span>d</span>` +
  1510. `<!--${forAnchorLabel}-->` +
  1511. `<span></span>` +
  1512. `</div>`,
  1513. )
  1514. })
  1515. test('v-for on component', async () => {
  1516. const { container, data } = await testHydration(
  1517. `<template>
  1518. <div>
  1519. <components.Child v-for="item in data" :key="item"/>
  1520. </div>
  1521. </template>`,
  1522. {
  1523. Child: `<template><div>comp</div></template>`,
  1524. },
  1525. ref(['a', 'b', 'c']),
  1526. )
  1527. expect(container.innerHTML).toBe(
  1528. `<div>` +
  1529. `<!--[-->` +
  1530. `<div>comp</div>` +
  1531. `<div>comp</div>` +
  1532. `<div>comp</div>` +
  1533. `<!--]--><!--${forAnchorLabel}-->` +
  1534. `</div>`,
  1535. )
  1536. data.value.push('d')
  1537. await nextTick()
  1538. expect(container.innerHTML).toBe(
  1539. `<div>` +
  1540. `<!--[-->` +
  1541. `<div>comp</div>` +
  1542. `<div>comp</div>` +
  1543. `<div>comp</div>` +
  1544. `<!--]-->` +
  1545. `<div>comp</div>` +
  1546. `<!--${forAnchorLabel}-->` +
  1547. `</div>`,
  1548. )
  1549. })
  1550. test('v-for on component with slots', async () => {
  1551. const { container, data } = await testHydration(
  1552. `<template>
  1553. <div>
  1554. <components.Child v-for="item in data" :key="item">
  1555. <span>{{ item }}</span>
  1556. </components.Child>
  1557. </div>
  1558. </template>`,
  1559. {
  1560. Child: `<template><slot/></template>`,
  1561. },
  1562. ref(['a', 'b', 'c']),
  1563. )
  1564. expect(container.innerHTML).toBe(
  1565. `<div>` +
  1566. `<!--[-->` +
  1567. `<!--[--><span>a</span><!--]--><!--${slotAnchorLabel}-->` +
  1568. `<!--[--><span>b</span><!--]--><!--${slotAnchorLabel}-->` +
  1569. `<!--[--><span>c</span><!--]--><!--${slotAnchorLabel}-->` +
  1570. `<!--]--><!--${forAnchorLabel}-->` +
  1571. `</div>`,
  1572. )
  1573. data.value.push('d')
  1574. await nextTick()
  1575. expect(container.innerHTML).toBe(
  1576. `<div>` +
  1577. `<!--[-->` +
  1578. `<!--[--><span>a</span><!--]--><!--${slotAnchorLabel}-->` +
  1579. `<!--[--><span>b</span><!--]--><!--${slotAnchorLabel}-->` +
  1580. `<!--[--><span>c</span><!--]--><!--${slotAnchorLabel}-->` +
  1581. `<!--]-->` +
  1582. `<span>d</span><!--${slotAnchorLabel}-->` +
  1583. `<!--${forAnchorLabel}-->` +
  1584. `</div>`,
  1585. )
  1586. })
  1587. test('on fragment component', async () => {
  1588. const { container, data } = await testHydration(
  1589. `<template>
  1590. <div>
  1591. <components.Child v-for="item in data" :key="item"/>
  1592. </div>
  1593. </template>`,
  1594. {
  1595. Child: `<template><div>foo</div>-bar-</template>`,
  1596. },
  1597. ref(['a', 'b', 'c']),
  1598. )
  1599. expect(container.innerHTML).toBe(
  1600. `<div>` +
  1601. `<!--[-->` +
  1602. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1603. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1604. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1605. `<!--]--><!--${forAnchorLabel}-->` +
  1606. `</div>`,
  1607. )
  1608. data.value.push('d')
  1609. await nextTick()
  1610. expect(container.innerHTML).toBe(
  1611. `<div>` +
  1612. `<!--[-->` +
  1613. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1614. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1615. `<!--[--><div>foo</div>-bar-<!--]-->` +
  1616. `<!--]-->` +
  1617. `<div>foo</div>-bar-` +
  1618. `<!--${forAnchorLabel}-->` +
  1619. `</div>`,
  1620. )
  1621. })
  1622. })
  1623. describe('slots', () => {
  1624. const slotAnchorLabel = SLOT_ANCHOR_LABEL
  1625. const forAnchorLabel = FOR_ANCHOR_LABEL
  1626. test('basic slot', async () => {
  1627. const { data, container } = await testHydration(
  1628. `<template>
  1629. <components.Child>
  1630. <span>{{data}}</span>
  1631. </components.Child>
  1632. </template>`,
  1633. {
  1634. Child: `<template><slot/></template>`,
  1635. },
  1636. )
  1637. expect(container.innerHTML).toBe(
  1638. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->`,
  1639. )
  1640. data.value = 'bar'
  1641. await nextTick()
  1642. expect(container.innerHTML).toBe(
  1643. `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->`,
  1644. )
  1645. })
  1646. test('named slot', async () => {
  1647. const { data, container } = await testHydration(
  1648. `<template>
  1649. <components.Child>
  1650. <template #foo>
  1651. <span>{{data}}</span>
  1652. </template>
  1653. </components.Child>
  1654. </template>`,
  1655. {
  1656. Child: `<template><slot name="foo"/></template>`,
  1657. },
  1658. )
  1659. expect(container.innerHTML).toBe(
  1660. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->`,
  1661. )
  1662. data.value = 'bar'
  1663. await nextTick()
  1664. expect(container.innerHTML).toBe(
  1665. `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->`,
  1666. )
  1667. })
  1668. test('named slot with v-if', async () => {
  1669. const { data, container } = await testHydration(
  1670. `<template>
  1671. <components.Child>
  1672. <template #foo v-if="data">
  1673. <span>{{data}}</span>
  1674. </template>
  1675. </components.Child>
  1676. </template>`,
  1677. {
  1678. Child: `<template><slot name="foo"/></template>`,
  1679. },
  1680. )
  1681. expect(container.innerHTML).toBe(
  1682. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->`,
  1683. )
  1684. data.value = false
  1685. await nextTick()
  1686. expect(container.innerHTML).toBe(
  1687. `<!--[--><!--]--><!--${slotAnchorLabel}-->`,
  1688. )
  1689. })
  1690. test('named slot with v-if and v-for', async () => {
  1691. const data = reactive({
  1692. show: true,
  1693. items: ['a', 'b', 'c'],
  1694. })
  1695. const { container } = await testHydration(
  1696. `<template>
  1697. <components.Child>
  1698. <template #foo v-if="data.show">
  1699. <span v-for="item in data.items" :key="item">{{item}}</span>
  1700. </template>
  1701. </components.Child>
  1702. </template>`,
  1703. {
  1704. Child: `<template><slot name="foo"/></template>`,
  1705. },
  1706. data,
  1707. )
  1708. expect(container.innerHTML).toBe(
  1709. `<!--[-->` +
  1710. `<!--[--><span>a</span><span>b</span><span>c</span><!--]--><!--${forAnchorLabel}-->` +
  1711. `<!--]-->` +
  1712. `<!--${slotAnchorLabel}-->`,
  1713. )
  1714. data.show = false
  1715. await nextTick()
  1716. expect(container.innerHTML).toBe(
  1717. `<!--[--><!--[--><!--]--><!--]--><!--${slotAnchorLabel}-->`,
  1718. )
  1719. })
  1720. test('with anchor insertion', async () => {
  1721. const { data, container } = await testHydration(
  1722. `<template>
  1723. <components.Child>
  1724. <span/>
  1725. <span>{{data}}</span>
  1726. <span/>
  1727. </components.Child>
  1728. </template>`,
  1729. {
  1730. Child: `<template><slot/></template>`,
  1731. },
  1732. )
  1733. expect(container.innerHTML).toBe(
  1734. `<!--[-->` +
  1735. `<span></span>` +
  1736. `<span>foo</span>` +
  1737. `<span></span>` +
  1738. `<!--]-->` +
  1739. `<!--${slotAnchorLabel}-->`,
  1740. )
  1741. data.value = 'bar'
  1742. await nextTick()
  1743. expect(container.innerHTML).toBe(
  1744. `<!--[-->` +
  1745. `<span></span>` +
  1746. `<span>bar</span>` +
  1747. `<span></span>` +
  1748. `<!--]-->` +
  1749. `<!--${slotAnchorLabel}-->`,
  1750. )
  1751. })
  1752. test('with multi level anchor insertion', async () => {
  1753. const { data, container } = await testHydration(
  1754. `<template>
  1755. <components.Child>
  1756. <span/>
  1757. <span>{{data}}</span>
  1758. <span/>
  1759. </components.Child>
  1760. </template>`,
  1761. {
  1762. Child: `
  1763. <template>
  1764. <div/>
  1765. <div/>
  1766. <slot/>
  1767. <div/>
  1768. </div>
  1769. </template>`,
  1770. },
  1771. )
  1772. expect(container.innerHTML).toBe(
  1773. `<!--[-->` +
  1774. `<div></div>` +
  1775. `<div></div>` +
  1776. `<!--[-->` +
  1777. `<span></span>` +
  1778. `<span>foo</span>` +
  1779. `<span></span>` +
  1780. `<!--]-->` +
  1781. `<!--${slotAnchorLabel}-->` +
  1782. `<div></div>` +
  1783. `<!--]-->`,
  1784. )
  1785. data.value = 'bar'
  1786. await nextTick()
  1787. expect(container.innerHTML).toBe(
  1788. `<!--[-->` +
  1789. `<div></div>` +
  1790. `<div></div>` +
  1791. `<!--[-->` +
  1792. `<span></span>` +
  1793. `<span>bar</span>` +
  1794. `<span></span>` +
  1795. `<!--]-->` +
  1796. `<!--${slotAnchorLabel}-->` +
  1797. `<div></div>` +
  1798. `<!--]-->`,
  1799. )
  1800. })
  1801. test('mixed slot and text node', async () => {
  1802. const data = reactive({
  1803. text: 'foo',
  1804. msg: 'hi',
  1805. })
  1806. const { container } = await testHydration(
  1807. `<template>
  1808. <components.Child>
  1809. <span>{{data.text}}</span>
  1810. </components.Child>
  1811. </template>`,
  1812. {
  1813. Child: `<template><div><slot/>{{data.msg}}</div></template>`,
  1814. },
  1815. data,
  1816. )
  1817. expect(container.innerHTML).toMatchInlineSnapshot(
  1818. `"<div><!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->hi</div>"`,
  1819. )
  1820. })
  1821. test('mixed slot and element', async () => {
  1822. const data = reactive({
  1823. text: 'foo',
  1824. msg: 'hi',
  1825. })
  1826. const { container } = await testHydration(
  1827. `<template>
  1828. <components.Child>
  1829. <span>{{data.text}}</span>
  1830. </components.Child>
  1831. </template>`,
  1832. {
  1833. Child: `<template><div><slot/><div>{{data.msg}}</div></div></template>`,
  1834. },
  1835. data,
  1836. )
  1837. expect(container.innerHTML).toMatchInlineSnapshot(
  1838. `"<div><!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}--><div>hi</div></div>"`,
  1839. )
  1840. })
  1841. test('mixed slot and component', async () => {
  1842. const data = reactive({
  1843. msg1: 'foo',
  1844. msg2: 'bar',
  1845. })
  1846. const { container } = await testHydration(
  1847. `<template>
  1848. <components.Child>
  1849. <span>{{data.msg1}}</span>
  1850. </components.Child>
  1851. </template>`,
  1852. {
  1853. Child: `
  1854. <template>
  1855. <div>
  1856. <components.Child2/>
  1857. <slot/>
  1858. <components.Child2/>
  1859. </div>
  1860. </template>`,
  1861. Child2: `
  1862. <template>
  1863. <div>{{data.msg2}}</div>
  1864. </template>`,
  1865. },
  1866. data,
  1867. )
  1868. expect(container.innerHTML).toBe(
  1869. `<div>` +
  1870. `<div>bar</div>` +
  1871. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1872. `<div>bar</div>` +
  1873. `</div>`,
  1874. )
  1875. data.msg2 = 'hello'
  1876. await nextTick()
  1877. expect(container.innerHTML).toBe(
  1878. `<div>` +
  1879. `<div>hello</div>` +
  1880. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1881. `<div>hello</div>` +
  1882. `</div>`,
  1883. )
  1884. })
  1885. test('mixed slot and fragment component', async () => {
  1886. const data = reactive({
  1887. msg1: 'foo',
  1888. msg2: 'bar',
  1889. })
  1890. const { container } = await testHydration(
  1891. `<template>
  1892. <components.Child>
  1893. <span>{{data.msg1}}</span>
  1894. </components.Child>
  1895. </template>`,
  1896. {
  1897. Child: `
  1898. <template>
  1899. <div>
  1900. <components.Child2/>
  1901. <slot/>
  1902. <components.Child2/>
  1903. </div>
  1904. </template>`,
  1905. Child2: `
  1906. <template>
  1907. <div>{{data.msg1}}</div> {{data.msg2}}
  1908. </template>`,
  1909. },
  1910. data,
  1911. )
  1912. expect(container.innerHTML).toBe(
  1913. `<div>` +
  1914. `<!--[--><div>foo</div> bar<!--]-->` +
  1915. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1916. `<!--[--><div>foo</div> bar<!--]-->` +
  1917. `</div>`,
  1918. )
  1919. data.msg1 = 'hello'
  1920. data.msg2 = 'vapor'
  1921. await nextTick()
  1922. expect(container.innerHTML).toBe(
  1923. `<div>` +
  1924. `<!--[--><div>hello</div> vapor<!--]-->` +
  1925. `<!--[--><span>hello</span><!--]--><!--${slotAnchorLabel}-->` +
  1926. `<!--[--><div>hello</div> vapor<!--]-->` +
  1927. `</div>`,
  1928. )
  1929. })
  1930. test('mixed slot and v-if', async () => {
  1931. const data = reactive({
  1932. show: true,
  1933. msg: 'foo',
  1934. })
  1935. const { container } = await testHydration(
  1936. `<template>
  1937. <components.Child>
  1938. <span>{{data.msg}}</span>
  1939. </components.Child>
  1940. </template>`,
  1941. {
  1942. Child: `
  1943. <template>
  1944. <div v-if="data.show">{{data.msg}}</div>
  1945. <slot/>
  1946. <div v-if="data.show">{{data.msg}}</div>
  1947. </template>`,
  1948. },
  1949. data,
  1950. )
  1951. expect(container.innerHTML).toBe(
  1952. `<!--[-->` +
  1953. `<div>foo</div><!--if-->` +
  1954. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1955. `<div>foo</div><!--if-->` +
  1956. `<!--]-->`,
  1957. )
  1958. data.show = false
  1959. await nextTick()
  1960. expect(container.innerHTML).toBe(
  1961. `<!--[-->` +
  1962. `<!--if-->` +
  1963. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1964. `<!--if-->` +
  1965. `<!--]-->`,
  1966. )
  1967. })
  1968. test('mixed slot and v-for', async () => {
  1969. const data = reactive({
  1970. items: ['a', 'b', 'c'],
  1971. msg: 'foo',
  1972. })
  1973. const { container } = await testHydration(
  1974. `<template>
  1975. <components.Child>
  1976. <span>{{data.msg}}</span>
  1977. </components.Child>
  1978. </template>`,
  1979. {
  1980. Child: `
  1981. <template>
  1982. <div v-for="item in data.items" :key="item">{{item}}</div>
  1983. <slot/>
  1984. <div v-for="item in data.items" :key="item">{{item}}</div>
  1985. </template>`,
  1986. },
  1987. data,
  1988. )
  1989. expect(container.innerHTML).toBe(
  1990. `<!--[-->` +
  1991. `<!--[--><div>a</div><div>b</div><div>c</div><!--]--><!--${forAnchorLabel}-->` +
  1992. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  1993. `<!--[--><div>a</div><div>b</div><div>c</div><!--]--><!--${forAnchorLabel}-->` +
  1994. `<!--]-->`,
  1995. )
  1996. data.items.push('d')
  1997. await nextTick()
  1998. expect(container.innerHTML).toBe(
  1999. `<!--[-->` +
  2000. `<!--[--><div>a</div><div>b</div><div>c</div><!--]--><div>d</div><!--${forAnchorLabel}-->` +
  2001. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  2002. `<!--[--><div>a</div><div>b</div><div>c</div><!--]--><div>d</div><!--${forAnchorLabel}-->` +
  2003. `<!--]-->`,
  2004. )
  2005. })
  2006. test('consecutive slots', async () => {
  2007. const data = reactive({
  2008. msg1: 'foo',
  2009. msg2: 'bar',
  2010. })
  2011. const { container } = await testHydration(
  2012. `<template>
  2013. <components.Child>
  2014. <span>{{data.msg1}}</span>
  2015. <template #bar>
  2016. <span>{{data.msg2}}</span>
  2017. </template>
  2018. </components.Child>
  2019. </template>`,
  2020. {
  2021. Child: `<template><slot/><slot name="bar"/></template>`,
  2022. },
  2023. data,
  2024. )
  2025. expect(container.innerHTML).toBe(
  2026. `<!--[-->` +
  2027. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  2028. `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->` +
  2029. `<!--]-->`,
  2030. )
  2031. data.msg1 = 'hello'
  2032. data.msg2 = 'vapor'
  2033. await nextTick()
  2034. expect(container.innerHTML).toBe(
  2035. `<!--[-->` +
  2036. `<!--[--><span>hello</span><!--]--><!--${slotAnchorLabel}-->` +
  2037. `<!--[--><span>vapor</span><!--]--><!--${slotAnchorLabel}-->` +
  2038. `<!--]-->`,
  2039. )
  2040. })
  2041. test('consecutive slots with anchor insertion', async () => {
  2042. const data = reactive({
  2043. msg1: 'foo',
  2044. msg2: 'bar',
  2045. })
  2046. const { container } = await testHydration(
  2047. `<template>
  2048. <components.Child>
  2049. <span>{{data.msg1}}</span>
  2050. <template #bar>
  2051. <span>{{data.msg2}}</span>
  2052. </template>
  2053. </components.Child>
  2054. </template>`,
  2055. {
  2056. Child: `<template>
  2057. <div>
  2058. <span/>
  2059. <slot/>
  2060. <slot name="bar"/>
  2061. <span/>
  2062. </div>
  2063. </template>`,
  2064. },
  2065. data,
  2066. )
  2067. expect(container.innerHTML).toBe(
  2068. `<div>` +
  2069. `<span></span>` +
  2070. `<!--[--><span>foo</span><!--]--><!--${slotAnchorLabel}-->` +
  2071. `<!--[--><span>bar</span><!--]--><!--${slotAnchorLabel}-->` +
  2072. `<span></span>` +
  2073. `</div>`,
  2074. )
  2075. data.msg1 = 'hello'
  2076. data.msg2 = 'vapor'
  2077. await nextTick()
  2078. expect(container.innerHTML).toBe(
  2079. `<div>` +
  2080. `<span></span>` +
  2081. `<!--[--><span>hello</span><!--]--><!--${slotAnchorLabel}-->` +
  2082. `<!--[--><span>vapor</span><!--]--><!--${slotAnchorLabel}-->` +
  2083. `<span></span>` +
  2084. `</div>`,
  2085. )
  2086. })
  2087. })
  2088. // test('element with ref', () => {
  2089. // const el = ref()
  2090. // const { vnode, container } = mountWithHydration('<div></div>', () =>
  2091. // h('div', { ref: el }),
  2092. // )
  2093. // expect(vnode.el).toBe(container.firstChild)
  2094. // expect(el.value).toBe(vnode.el)
  2095. // })
  2096. // test('with data-allow-mismatch component when using onServerPrefetch', async () => {
  2097. // const Comp = {
  2098. // template: `
  2099. // <div>Comp2</div>
  2100. // `,
  2101. // }
  2102. // let foo: any
  2103. // const App = {
  2104. // setup() {
  2105. // const flag = ref(true)
  2106. // foo = () => {
  2107. // flag.value = false
  2108. // }
  2109. // onServerPrefetch(() => (flag.value = false))
  2110. // return { flag }
  2111. // },
  2112. // components: {
  2113. // Comp,
  2114. // },
  2115. // template: `
  2116. // <span data-allow-mismatch>
  2117. // <Comp v-if="flag"></Comp>
  2118. // </span>
  2119. // `,
  2120. // }
  2121. // // hydrate
  2122. // const container = document.createElement('div')
  2123. // container.innerHTML = await renderToString(h(App))
  2124. // createSSRApp(App).mount(container)
  2125. // expect(container.innerHTML).toBe(
  2126. // '<span data-allow-mismatch=""><div>Comp2</div></span>',
  2127. // )
  2128. // foo()
  2129. // await nextTick()
  2130. // expect(container.innerHTML).toBe(
  2131. // '<span data-allow-mismatch=""><!--v-if--></span>',
  2132. // )
  2133. // })
  2134. // // compile SSR + client render fn from the same template & hydrate
  2135. // test('full compiler integration', async () => {
  2136. // const mounted: string[] = []
  2137. // const log = vi.fn()
  2138. // const toggle = ref(true)
  2139. // const Child = {
  2140. // data() {
  2141. // return {
  2142. // count: 0,
  2143. // text: 'hello',
  2144. // style: {
  2145. // color: 'red',
  2146. // },
  2147. // }
  2148. // },
  2149. // mounted() {
  2150. // mounted.push('child')
  2151. // },
  2152. // template: `
  2153. // <div>
  2154. // <span class="count" :style="style">{{ count }}</span>
  2155. // <button class="inc" @click="count++">inc</button>
  2156. // <button class="change" @click="style.color = 'green'" >change color</button>
  2157. // <button class="emit" @click="$emit('foo')">emit</button>
  2158. // <span class="text">{{ text }}</span>
  2159. // <input v-model="text">
  2160. // </div>
  2161. // `,
  2162. // }
  2163. // const App = {
  2164. // setup() {
  2165. // return { toggle }
  2166. // },
  2167. // mounted() {
  2168. // mounted.push('parent')
  2169. // },
  2170. // template: `
  2171. // <div>
  2172. // <span>hello</span>
  2173. // <template v-if="toggle">
  2174. // <Child @foo="log('child')"/>
  2175. // <template v-if="true">
  2176. // <button class="parent-click" @click="log('click')">click me</button>
  2177. // </template>
  2178. // </template>
  2179. // <span>hello</span>
  2180. // </div>`,
  2181. // components: {
  2182. // Child,
  2183. // },
  2184. // methods: {
  2185. // log,
  2186. // },
  2187. // }
  2188. // const container = document.createElement('div')
  2189. // // server render
  2190. // container.innerHTML = await renderToString(h(App))
  2191. // // hydrate
  2192. // createSSRApp(App).mount(container)
  2193. // // assert interactions
  2194. // // 1. parent button click
  2195. // triggerEvent('click', container.querySelector('.parent-click')!)
  2196. // expect(log).toHaveBeenCalledWith('click')
  2197. // // 2. child inc click + text interpolation
  2198. // const count = container.querySelector('.count') as HTMLElement
  2199. // expect(count.textContent).toBe(`0`)
  2200. // triggerEvent('click', container.querySelector('.inc')!)
  2201. // await nextTick()
  2202. // expect(count.textContent).toBe(`1`)
  2203. // // 3. child color click + style binding
  2204. // expect(count.style.color).toBe('red')
  2205. // triggerEvent('click', container.querySelector('.change')!)
  2206. // await nextTick()
  2207. // expect(count.style.color).toBe('green')
  2208. // // 4. child event emit
  2209. // triggerEvent('click', container.querySelector('.emit')!)
  2210. // expect(log).toHaveBeenCalledWith('child')
  2211. // // 5. child v-model
  2212. // const text = container.querySelector('.text')!
  2213. // const input = container.querySelector('input')!
  2214. // expect(text.textContent).toBe('hello')
  2215. // input.value = 'bye'
  2216. // triggerEvent('input', input)
  2217. // await nextTick()
  2218. // expect(text.textContent).toBe('bye')
  2219. // })
  2220. // test('handle click error in ssr mode', async () => {
  2221. // const App = {
  2222. // setup() {
  2223. // const throwError = () => {
  2224. // throw new Error('Sentry Error')
  2225. // }
  2226. // return { throwError }
  2227. // },
  2228. // template: `
  2229. // <div>
  2230. // <button class="parent-click" @click="throwError">click me</button>
  2231. // </div>`,
  2232. // }
  2233. // const container = document.createElement('div')
  2234. // // server render
  2235. // container.innerHTML = await renderToString(h(App))
  2236. // // hydrate
  2237. // const app = createSSRApp(App)
  2238. // const handler = (app.config.errorHandler = vi.fn())
  2239. // app.mount(container)
  2240. // // assert interactions
  2241. // // parent button click
  2242. // triggerEvent('click', container.querySelector('.parent-click')!)
  2243. // expect(handler).toHaveBeenCalled()
  2244. // })
  2245. // test('handle blur error in ssr mode', async () => {
  2246. // const App = {
  2247. // setup() {
  2248. // const throwError = () => {
  2249. // throw new Error('Sentry Error')
  2250. // }
  2251. // return { throwError }
  2252. // },
  2253. // template: `
  2254. // <div>
  2255. // <input class="parent-click" @blur="throwError"/>
  2256. // </div>`,
  2257. // }
  2258. // const container = document.createElement('div')
  2259. // // server render
  2260. // container.innerHTML = await renderToString(h(App))
  2261. // // hydrate
  2262. // const app = createSSRApp(App)
  2263. // const handler = (app.config.errorHandler = vi.fn())
  2264. // app.mount(container)
  2265. // // assert interactions
  2266. // // parent blur event
  2267. // triggerEvent('blur', container.querySelector('.parent-click')!)
  2268. // expect(handler).toHaveBeenCalled()
  2269. // })
  2270. // test('async component', async () => {
  2271. // const spy = vi.fn()
  2272. // const Comp = () =>
  2273. // h(
  2274. // 'button',
  2275. // {
  2276. // onClick: spy,
  2277. // },
  2278. // 'hello!',
  2279. // )
  2280. // let serverResolve: any
  2281. // let AsyncComp = defineAsyncComponent(
  2282. // () =>
  2283. // new Promise(r => {
  2284. // serverResolve = r
  2285. // }),
  2286. // )
  2287. // const App = {
  2288. // render() {
  2289. // return ['hello', h(AsyncComp), 'world']
  2290. // },
  2291. // }
  2292. // // server render
  2293. // const htmlPromise = renderToString(h(App))
  2294. // serverResolve(Comp)
  2295. // const html = await htmlPromise
  2296. // expect(html).toMatchInlineSnapshot(
  2297. // `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  2298. // )
  2299. // // hydration
  2300. // let clientResolve: any
  2301. // AsyncComp = defineAsyncComponent(
  2302. // () =>
  2303. // new Promise(r => {
  2304. // clientResolve = r
  2305. // }),
  2306. // )
  2307. // const container = document.createElement('div')
  2308. // container.innerHTML = html
  2309. // createSSRApp(App).mount(container)
  2310. // // hydration not complete yet
  2311. // triggerEvent('click', container.querySelector('button')!)
  2312. // expect(spy).not.toHaveBeenCalled()
  2313. // // resolve
  2314. // clientResolve(Comp)
  2315. // await new Promise(r => setTimeout(r))
  2316. // // should be hydrated now
  2317. // triggerEvent('click', container.querySelector('button')!)
  2318. // expect(spy).toHaveBeenCalled()
  2319. // })
  2320. // test('update async wrapper before resolve', async () => {
  2321. // const Comp = {
  2322. // render() {
  2323. // return h('h1', 'Async component')
  2324. // },
  2325. // }
  2326. // let serverResolve: any
  2327. // let AsyncComp = defineAsyncComponent(
  2328. // () =>
  2329. // new Promise(r => {
  2330. // serverResolve = r
  2331. // }),
  2332. // )
  2333. // const toggle = ref(true)
  2334. // const App = {
  2335. // setup() {
  2336. // onMounted(() => {
  2337. // // change state, this makes updateComponent(AsyncComp) execute before
  2338. // // the async component is resolved
  2339. // toggle.value = false
  2340. // })
  2341. // return () => {
  2342. // return [toggle.value ? 'hello' : 'world', h(AsyncComp)]
  2343. // }
  2344. // },
  2345. // }
  2346. // // server render
  2347. // const htmlPromise = renderToString(h(App))
  2348. // serverResolve(Comp)
  2349. // const html = await htmlPromise
  2350. // expect(html).toMatchInlineSnapshot(
  2351. // `"<!--[-->hello<h1>Async component</h1><!--]-->"`,
  2352. // )
  2353. // // hydration
  2354. // let clientResolve: any
  2355. // AsyncComp = defineAsyncComponent(
  2356. // () =>
  2357. // new Promise(r => {
  2358. // clientResolve = r
  2359. // }),
  2360. // )
  2361. // const container = document.createElement('div')
  2362. // container.innerHTML = html
  2363. // createSSRApp(App).mount(container)
  2364. // // resolve
  2365. // clientResolve(Comp)
  2366. // await new Promise(r => setTimeout(r))
  2367. // // should be hydrated now
  2368. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2369. // expect(container.innerHTML).toMatchInlineSnapshot(
  2370. // `"<!--[-->world<h1>Async component</h1><!--]-->"`,
  2371. // )
  2372. // })
  2373. // test('hydrate safely when property used by async setup changed before render', async () => {
  2374. // const toggle = ref(true)
  2375. // const AsyncComp = {
  2376. // async setup() {
  2377. // await new Promise<void>(r => setTimeout(r, 10))
  2378. // return () => h('h1', 'Async component')
  2379. // },
  2380. // }
  2381. // const AsyncWrapper = {
  2382. // render() {
  2383. // return h(AsyncComp)
  2384. // },
  2385. // }
  2386. // const SiblingComp = {
  2387. // setup() {
  2388. // toggle.value = false
  2389. // return () => h('span')
  2390. // },
  2391. // }
  2392. // const App = {
  2393. // setup() {
  2394. // return () =>
  2395. // h(
  2396. // Suspense,
  2397. // {},
  2398. // {
  2399. // default: () => [
  2400. // h('main', {}, [
  2401. // h(AsyncWrapper, {
  2402. // prop: toggle.value ? 'hello' : 'world',
  2403. // }),
  2404. // h(SiblingComp),
  2405. // ]),
  2406. // ],
  2407. // },
  2408. // )
  2409. // },
  2410. // }
  2411. // // server render
  2412. // const html = await renderToString(h(App))
  2413. // expect(html).toMatchInlineSnapshot(
  2414. // `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  2415. // )
  2416. // expect(toggle.value).toBe(false)
  2417. // // hydration
  2418. // // reset the value
  2419. // toggle.value = true
  2420. // expect(toggle.value).toBe(true)
  2421. // const container = document.createElement('div')
  2422. // container.innerHTML = html
  2423. // createSSRApp(App).mount(container)
  2424. // await new Promise(r => setTimeout(r, 10))
  2425. // expect(toggle.value).toBe(false)
  2426. // // should be hydrated now
  2427. // expect(container.innerHTML).toMatchInlineSnapshot(
  2428. // `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  2429. // )
  2430. // })
  2431. // test('hydrate safely when property used by deep nested async setup changed before render', async () => {
  2432. // const toggle = ref(true)
  2433. // const AsyncComp = {
  2434. // async setup() {
  2435. // await new Promise<void>(r => setTimeout(r, 10))
  2436. // return () => h('h1', 'Async component')
  2437. // },
  2438. // }
  2439. // const AsyncWrapper = { render: () => h(AsyncComp) }
  2440. // const AsyncWrapperWrapper = { render: () => h(AsyncWrapper) }
  2441. // const SiblingComp = {
  2442. // setup() {
  2443. // toggle.value = false
  2444. // return () => h('span')
  2445. // },
  2446. // }
  2447. // const App = {
  2448. // setup() {
  2449. // return () =>
  2450. // h(
  2451. // Suspense,
  2452. // {},
  2453. // {
  2454. // default: () => [
  2455. // h('main', {}, [
  2456. // h(AsyncWrapperWrapper, {
  2457. // prop: toggle.value ? 'hello' : 'world',
  2458. // }),
  2459. // h(SiblingComp),
  2460. // ]),
  2461. // ],
  2462. // },
  2463. // )
  2464. // },
  2465. // }
  2466. // // server render
  2467. // const html = await renderToString(h(App))
  2468. // expect(html).toMatchInlineSnapshot(
  2469. // `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  2470. // )
  2471. // expect(toggle.value).toBe(false)
  2472. // // hydration
  2473. // // reset the value
  2474. // toggle.value = true
  2475. // expect(toggle.value).toBe(true)
  2476. // const container = document.createElement('div')
  2477. // container.innerHTML = html
  2478. // createSSRApp(App).mount(container)
  2479. // await new Promise(r => setTimeout(r, 10))
  2480. // expect(toggle.value).toBe(false)
  2481. // // should be hydrated now
  2482. // expect(container.innerHTML).toMatchInlineSnapshot(
  2483. // `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  2484. // )
  2485. // })
  2486. // // #3787
  2487. // test('unmount async wrapper before load', async () => {
  2488. // let resolve: any
  2489. // const AsyncComp = defineAsyncComponent(
  2490. // () =>
  2491. // new Promise(r => {
  2492. // resolve = r
  2493. // }),
  2494. // )
  2495. // const show = ref(true)
  2496. // const root = document.createElement('div')
  2497. // root.innerHTML = '<div><div>async</div></div>'
  2498. // createSSRApp({
  2499. // render() {
  2500. // return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  2501. // },
  2502. // }).mount(root)
  2503. // show.value = false
  2504. // await nextTick()
  2505. // expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  2506. // resolve({})
  2507. // })
  2508. // //#12362
  2509. // test('nested async wrapper', async () => {
  2510. // const Toggle = defineAsyncComponent(
  2511. // () =>
  2512. // new Promise(r => {
  2513. // r(
  2514. // defineComponent({
  2515. // setup(_, { slots }) {
  2516. // const show = ref(false)
  2517. // onMounted(() => {
  2518. // nextTick(() => {
  2519. // show.value = true
  2520. // })
  2521. // })
  2522. // return () =>
  2523. // withDirectives(
  2524. // h('div', null, [renderSlot(slots, 'default')]),
  2525. // [[vShow, show.value]],
  2526. // )
  2527. // },
  2528. // }) as any,
  2529. // )
  2530. // }),
  2531. // )
  2532. // const Wrapper = defineAsyncComponent(() => {
  2533. // return new Promise(r => {
  2534. // r(
  2535. // defineComponent({
  2536. // render(this: any) {
  2537. // return renderSlot(this.$slots, 'default')
  2538. // },
  2539. // }) as any,
  2540. // )
  2541. // })
  2542. // })
  2543. // const count = ref(0)
  2544. // const fn = vi.fn()
  2545. // const Child = {
  2546. // setup() {
  2547. // onMounted(() => {
  2548. // fn()
  2549. // count.value++
  2550. // })
  2551. // return () => h('div', count.value)
  2552. // },
  2553. // }
  2554. // const App = {
  2555. // render() {
  2556. // return h(Toggle, null, {
  2557. // default: () =>
  2558. // h(Wrapper, null, {
  2559. // default: () =>
  2560. // h(Wrapper, null, {
  2561. // default: () => h(Child),
  2562. // }),
  2563. // }),
  2564. // })
  2565. // },
  2566. // }
  2567. // const root = document.createElement('div')
  2568. // root.innerHTML = await renderToString(h(App))
  2569. // expect(root.innerHTML).toMatchInlineSnapshot(
  2570. // `"<div style="display:none;"><!--[--><!--[--><!--[--><div>0</div><!--]--><!--]--><!--]--></div>"`,
  2571. // )
  2572. // createSSRApp(App).mount(root)
  2573. // await nextTick()
  2574. // await nextTick()
  2575. // expect(root.innerHTML).toMatchInlineSnapshot(
  2576. // `"<div style=""><!--[--><!--[--><!--[--><div>1</div><!--]--><!--]--><!--]--></div>"`,
  2577. // )
  2578. // expect(fn).toBeCalledTimes(1)
  2579. // })
  2580. // test('unmount async wrapper before load (fragment)', async () => {
  2581. // let resolve: any
  2582. // const AsyncComp = defineAsyncComponent(
  2583. // () =>
  2584. // new Promise(r => {
  2585. // resolve = r
  2586. // }),
  2587. // )
  2588. // const show = ref(true)
  2589. // const root = document.createElement('div')
  2590. // root.innerHTML = '<div><!--[-->async<!--]--></div>'
  2591. // createSSRApp({
  2592. // render() {
  2593. // return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  2594. // },
  2595. // }).mount(root)
  2596. // show.value = false
  2597. // await nextTick()
  2598. // expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  2599. // resolve({})
  2600. // })
  2601. // test('elements with camel-case in svg ', () => {
  2602. // const { vnode, container } = mountWithHydration(
  2603. // '<animateTransform></animateTransform>',
  2604. // () => h('animateTransform'),
  2605. // )
  2606. // expect(vnode.el).toBe(container.firstChild)
  2607. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2608. // })
  2609. // test('SVG as a mount container', () => {
  2610. // const svgContainer = document.createElement('svg')
  2611. // svgContainer.innerHTML = '<g></g>'
  2612. // const app = createSSRApp({
  2613. // render: () => h('g'),
  2614. // })
  2615. // expect(
  2616. // (
  2617. // app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
  2618. // el: Element
  2619. // }
  2620. // ).el instanceof SVGElement,
  2621. // )
  2622. // })
  2623. // test('force hydrate prop with `.prop` modifier', () => {
  2624. // const { container } = mountWithHydration('<input type="checkbox">', () =>
  2625. // h('input', {
  2626. // type: 'checkbox',
  2627. // '.indeterminate': true,
  2628. // }),
  2629. // )
  2630. // expect((container.firstChild! as any).indeterminate).toBe(true)
  2631. // })
  2632. // test('force hydrate input v-model with non-string value bindings', () => {
  2633. // const { container } = mountWithHydration(
  2634. // '<input type="checkbox" value="true">',
  2635. // () =>
  2636. // withDirectives(
  2637. // createVNode(
  2638. // 'input',
  2639. // { type: 'checkbox', 'true-value': true },
  2640. // null,
  2641. // PatchFlags.PROPS,
  2642. // ['true-value'],
  2643. // ),
  2644. // [[vModelCheckbox, true]],
  2645. // ),
  2646. // )
  2647. // expect((container.firstChild as any)._trueValue).toBe(true)
  2648. // })
  2649. // test('force hydrate checkbox with indeterminate', () => {
  2650. // const { container } = mountWithHydration(
  2651. // '<input type="checkbox" indeterminate>',
  2652. // () =>
  2653. // createVNode(
  2654. // 'input',
  2655. // { type: 'checkbox', indeterminate: '' },
  2656. // null,
  2657. // PatchFlags.CACHED,
  2658. // ),
  2659. // )
  2660. // expect((container.firstChild as any).indeterminate).toBe(true)
  2661. // })
  2662. // test('force hydrate select option with non-string value bindings', () => {
  2663. // const { container } = mountWithHydration(
  2664. // '<select><option value="true">ok</option></select>',
  2665. // () =>
  2666. // h('select', [
  2667. // // hoisted because bound value is a constant...
  2668. // createVNode('option', { value: true }, null, -1 /* HOISTED */),
  2669. // ]),
  2670. // )
  2671. // expect((container.firstChild!.firstChild as any)._value).toBe(true)
  2672. // })
  2673. // // #7203
  2674. // test('force hydrate custom element with dynamic props', () => {
  2675. // class MyElement extends HTMLElement {
  2676. // foo = ''
  2677. // constructor() {
  2678. // super()
  2679. // }
  2680. // }
  2681. // customElements.define('my-element-7203', MyElement)
  2682. // const msg = ref('bar')
  2683. // const container = document.createElement('div')
  2684. // container.innerHTML = '<my-element-7203></my-element-7203>'
  2685. // const app = createSSRApp({
  2686. // render: () => h('my-element-7203', { foo: msg.value }),
  2687. // })
  2688. // app.mount(container)
  2689. // expect((container.firstChild as any).foo).toBe(msg.value)
  2690. // })
  2691. // // #5728
  2692. // test('empty text node in slot', () => {
  2693. // const Comp = {
  2694. // render(this: any) {
  2695. // return renderSlot(this.$slots, 'default', {}, () => [
  2696. // createTextVNode(''),
  2697. // ])
  2698. // },
  2699. // }
  2700. // const { container, vnode } = mountWithHydration('<!--[--><!--]-->', () =>
  2701. // h(Comp),
  2702. // )
  2703. // expect(container.childNodes.length).toBe(3)
  2704. // const text = container.childNodes[1]
  2705. // expect(text.nodeType).toBe(3)
  2706. // expect(vnode.el).toBe(container.childNodes[0])
  2707. // // component => slot fragment => text node
  2708. // expect((vnode as any).component?.subTree.children[0].el).toBe(text)
  2709. // })
  2710. // // #7215
  2711. // test('empty text node', () => {
  2712. // const Comp = {
  2713. // render(this: any) {
  2714. // return h('p', [''])
  2715. // },
  2716. // }
  2717. // const { container } = mountWithHydration('<p></p>', () => h(Comp))
  2718. // expect(container.childNodes.length).toBe(1)
  2719. // const p = container.childNodes[0]
  2720. // expect(p.childNodes.length).toBe(1)
  2721. // const text = p.childNodes[0]
  2722. // expect(text.nodeType).toBe(3)
  2723. // })
  2724. // // #11372
  2725. // test('object style value tracking in prod', async () => {
  2726. // __DEV__ = false
  2727. // try {
  2728. // const style = reactive({ color: 'red' })
  2729. // const Comp = {
  2730. // render(this: any) {
  2731. // return (
  2732. // openBlock(),
  2733. // createElementBlock(
  2734. // 'div',
  2735. // {
  2736. // style: normalizeStyle(style),
  2737. // },
  2738. // null,
  2739. // 4 /* STYLE */,
  2740. // )
  2741. // )
  2742. // },
  2743. // }
  2744. // const { container } = mountWithHydration(
  2745. // `<div style="color: red;"></div>`,
  2746. // () => h(Comp),
  2747. // )
  2748. // style.color = 'green'
  2749. // await nextTick()
  2750. // expect(container.innerHTML).toBe(`<div style="color: green;"></div>`)
  2751. // } finally {
  2752. // __DEV__ = true
  2753. // }
  2754. // })
  2755. // test('app.unmount()', async () => {
  2756. // const container = document.createElement('DIV')
  2757. // container.innerHTML = '<button></button>'
  2758. // const App = defineComponent({
  2759. // setup(_, { expose }) {
  2760. // const count = ref(0)
  2761. // expose({ count })
  2762. // return () =>
  2763. // h('button', {
  2764. // onClick: () => count.value++,
  2765. // })
  2766. // },
  2767. // })
  2768. // const app = createSSRApp(App)
  2769. // const vm = app.mount(container)
  2770. // await nextTick()
  2771. // expect((container as any)._vnode).toBeDefined()
  2772. // // @ts-expect-error - expose()'d properties are not available on vm type
  2773. // expect(vm.count).toBe(0)
  2774. // app.unmount()
  2775. // expect((container as any)._vnode).toBe(null)
  2776. // })
  2777. // // #6637
  2778. // test('stringified root fragment', () => {
  2779. // mountWithHydration(`<!--[--><div></div><!--]-->`, () =>
  2780. // createStaticVNode(`<div></div>`, 1),
  2781. // )
  2782. // expect(`mismatch`).not.toHaveBeenWarned()
  2783. // })
  2784. // test('transition appear', () => {
  2785. // const { vnode, container } = mountWithHydration(
  2786. // `<template><div>foo</div></template>`,
  2787. // () =>
  2788. // h(
  2789. // Transition,
  2790. // { appear: true },
  2791. // {
  2792. // default: () => h('div', 'foo'),
  2793. // },
  2794. // ),
  2795. // )
  2796. // expect(container.firstChild).toMatchInlineSnapshot(`
  2797. // <div
  2798. // class="v-enter-from v-enter-active"
  2799. // >
  2800. // foo
  2801. // </div>
  2802. // `)
  2803. // expect(vnode.el).toBe(container.firstChild)
  2804. // expect(`mismatch`).not.toHaveBeenWarned()
  2805. // })
  2806. // test('transition appear with v-if', () => {
  2807. // const show = false
  2808. // const { vnode, container } = mountWithHydration(
  2809. // `<template><!----></template>`,
  2810. // () =>
  2811. // h(
  2812. // Transition,
  2813. // { appear: true },
  2814. // {
  2815. // default: () => (show ? h('div', 'foo') : createCommentVNode('')),
  2816. // },
  2817. // ),
  2818. // )
  2819. // expect(container.firstChild).toMatchInlineSnapshot('<!---->')
  2820. // expect(vnode.el).toBe(container.firstChild)
  2821. // expect(`mismatch`).not.toHaveBeenWarned()
  2822. // })
  2823. // test('transition appear with v-show', () => {
  2824. // const show = false
  2825. // const { vnode, container } = mountWithHydration(
  2826. // `<template><div style="display: none;">foo</div></template>`,
  2827. // () =>
  2828. // h(
  2829. // Transition,
  2830. // { appear: true },
  2831. // {
  2832. // default: () =>
  2833. // withDirectives(createVNode('div', null, 'foo'), [[vShow, show]]),
  2834. // },
  2835. // ),
  2836. // )
  2837. // expect(container.firstChild).toMatchInlineSnapshot(`
  2838. // <div
  2839. // class="v-enter-from v-enter-active"
  2840. // style="display: none;"
  2841. // >
  2842. // foo
  2843. // </div>
  2844. // `)
  2845. // expect((container.firstChild as any)[vShowOriginalDisplay]).toBe('')
  2846. // expect(vnode.el).toBe(container.firstChild)
  2847. // expect(`mismatch`).not.toHaveBeenWarned()
  2848. // })
  2849. // test('transition appear w/ event listener', async () => {
  2850. // const container = document.createElement('div')
  2851. // container.innerHTML = `<template><button>0</button></template>`
  2852. // createSSRApp({
  2853. // data() {
  2854. // return {
  2855. // count: 0,
  2856. // }
  2857. // },
  2858. // template: `
  2859. // <Transition appear>
  2860. // <button @click="count++">{{count}}</button>
  2861. // </Transition>
  2862. // `,
  2863. // }).mount(container)
  2864. // expect(container.firstChild).toMatchInlineSnapshot(`
  2865. // <button
  2866. // class="v-enter-from v-enter-active"
  2867. // >
  2868. // 0
  2869. // </button>
  2870. // `)
  2871. // triggerEvent('click', container.querySelector('button')!)
  2872. // await nextTick()
  2873. // expect(container.firstChild).toMatchInlineSnapshot(`
  2874. // <button
  2875. // class="v-enter-from v-enter-active"
  2876. // >
  2877. // 1
  2878. // </button>
  2879. // `)
  2880. // })
  2881. // test('Suspense + transition appear', async () => {
  2882. // const { vnode, container } = mountWithHydration(
  2883. // `<template><div>foo</div></template>`,
  2884. // () =>
  2885. // h(Suspense, {}, () =>
  2886. // h(
  2887. // Transition,
  2888. // { appear: true },
  2889. // {
  2890. // default: () => h('div', 'foo'),
  2891. // },
  2892. // ),
  2893. // ),
  2894. // )
  2895. // expect(vnode.el).toBe(container.firstChild)
  2896. // // wait for hydration to finish
  2897. // await new Promise(r => setTimeout(r))
  2898. // expect(container.firstChild).toMatchInlineSnapshot(`
  2899. // <div
  2900. // class="v-enter-from v-enter-active"
  2901. // >
  2902. // foo
  2903. // </div>
  2904. // `)
  2905. // await nextTick()
  2906. // expect(vnode.el).toBe(container.firstChild)
  2907. // })
  2908. // // #10607
  2909. // test('update component stable slot (prod + optimized mode)', async () => {
  2910. // __DEV__ = false
  2911. // try {
  2912. // const container = document.createElement('div')
  2913. // container.innerHTML = `<template><div show="false"><!--[--><div><div><!----></div></div><div>0</div><!--]--></div></template>`
  2914. // const Comp = {
  2915. // render(this: any) {
  2916. // return (
  2917. // openBlock(),
  2918. // createElementBlock('div', null, [
  2919. // renderSlot(this.$slots, 'default'),
  2920. // ])
  2921. // )
  2922. // },
  2923. // }
  2924. // const show = ref(false)
  2925. // const clicked = ref(false)
  2926. // const Wrapper = {
  2927. // setup() {
  2928. // const items = ref<number[]>([])
  2929. // onMounted(() => {
  2930. // items.value = [1]
  2931. // })
  2932. // return () => {
  2933. // return (
  2934. // openBlock(),
  2935. // createBlock(Comp, null, {
  2936. // default: withCtx(() => [
  2937. // createElementVNode('div', null, [
  2938. // createElementVNode('div', null, [
  2939. // clicked.value
  2940. // ? (openBlock(),
  2941. // createElementBlock('div', { key: 0 }, 'foo'))
  2942. // : createCommentVNode('v-if', true),
  2943. // ]),
  2944. // ]),
  2945. // createElementVNode(
  2946. // 'div',
  2947. // null,
  2948. // items.value.length,
  2949. // 1 /* TEXT */,
  2950. // ),
  2951. // ]),
  2952. // _: 1 /* STABLE */,
  2953. // })
  2954. // )
  2955. // }
  2956. // },
  2957. // }
  2958. // createSSRApp({
  2959. // components: { Wrapper },
  2960. // data() {
  2961. // return { show }
  2962. // },
  2963. // template: `<Wrapper :show="show"/>`,
  2964. // }).mount(container)
  2965. // await nextTick()
  2966. // expect(container.innerHTML).toBe(
  2967. // `<div show="false"><!--[--><div><div><!----></div></div><div>1</div><!--]--></div>`,
  2968. // )
  2969. // show.value = true
  2970. // await nextTick()
  2971. // expect(async () => {
  2972. // clicked.value = true
  2973. // await nextTick()
  2974. // }).not.toThrow("Cannot read properties of null (reading 'insertBefore')")
  2975. // await nextTick()
  2976. // expect(container.innerHTML).toBe(
  2977. // `<div show="true"><!--[--><div><div><div>foo</div></div></div><div>1</div><!--]--></div>`,
  2978. // )
  2979. // } catch (e) {
  2980. // throw e
  2981. // } finally {
  2982. // __DEV__ = true
  2983. // }
  2984. // })
  2985. // describe('mismatch handling', () => {
  2986. // test('text node', () => {
  2987. // const { container } = mountWithHydration(`foo`, () => 'bar')
  2988. // expect(container.textContent).toBe('bar')
  2989. // expect(`Hydration text mismatch`).toHaveBeenWarned()
  2990. // })
  2991. // test('element text content', () => {
  2992. // const { container } = mountWithHydration(`<div>foo</div>`, () =>
  2993. // h('div', 'bar'),
  2994. // )
  2995. // expect(container.innerHTML).toBe('<div>bar</div>')
  2996. // expect(`Hydration text content mismatch`).toHaveBeenWarned()
  2997. // })
  2998. // test('not enough children', () => {
  2999. // const { container } = mountWithHydration(`<div></div>`, () =>
  3000. // h('div', [h('span', 'foo'), h('span', 'bar')]),
  3001. // )
  3002. // expect(container.innerHTML).toBe(
  3003. // '<div><span>foo</span><span>bar</span></div>',
  3004. // )
  3005. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3006. // })
  3007. // test('too many children', () => {
  3008. // const { container } = mountWithHydration(
  3009. // `<div><span>foo</span><span>bar</span></div>`,
  3010. // () => h('div', [h('span', 'foo')]),
  3011. // )
  3012. // expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  3013. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3014. // })
  3015. // test('complete mismatch', () => {
  3016. // const { container } = mountWithHydration(
  3017. // `<div><span>foo</span><span>bar</span></div>`,
  3018. // () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  3019. // )
  3020. // expect(container.innerHTML).toBe('<div><div>foo</div><p>bar</p></div>')
  3021. // expect(`Hydration node mismatch`).toHaveBeenWarnedTimes(2)
  3022. // })
  3023. // test('fragment mismatch removal', () => {
  3024. // const { container } = mountWithHydration(
  3025. // `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  3026. // () => h('div', [h('span', 'replaced')]),
  3027. // )
  3028. // expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  3029. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3030. // })
  3031. // test('fragment not enough children', () => {
  3032. // const { container } = mountWithHydration(
  3033. // `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  3034. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  3035. // )
  3036. // expect(container.innerHTML).toBe(
  3037. // '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  3038. // )
  3039. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3040. // })
  3041. // test('fragment too many children', () => {
  3042. // const { container } = mountWithHydration(
  3043. // `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  3044. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  3045. // )
  3046. // expect(container.innerHTML).toBe(
  3047. // '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  3048. // )
  3049. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  3050. // // as 2nd fragment child.
  3051. // expect(`Hydration text content mismatch`).toHaveBeenWarned()
  3052. // // excessive children removal
  3053. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3054. // })
  3055. // test('Teleport target has empty children', () => {
  3056. // const teleportContainer = document.createElement('div')
  3057. // teleportContainer.id = 'teleport'
  3058. // document.body.appendChild(teleportContainer)
  3059. // mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  3060. // h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  3061. // )
  3062. // expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  3063. // expect(`Hydration children mismatch`).toHaveBeenWarned()
  3064. // })
  3065. // test('comment mismatch (element)', () => {
  3066. // const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  3067. // h('div', [createCommentVNode('hi')]),
  3068. // )
  3069. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  3070. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3071. // })
  3072. // test('comment mismatch (text)', () => {
  3073. // const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  3074. // h('div', [createCommentVNode('hi')]),
  3075. // )
  3076. // expect(container.innerHTML).toBe('<div><!--hi--></div>')
  3077. // expect(`Hydration node mismatch`).toHaveBeenWarned()
  3078. // })
  3079. // test('class mismatch', () => {
  3080. // mountWithHydration(`<div class="foo bar"></div>`, () =>
  3081. // h('div', { class: ['foo', 'bar'] }),
  3082. // )
  3083. // mountWithHydration(`<div class="foo bar"></div>`, () =>
  3084. // h('div', { class: { foo: true, bar: true } }),
  3085. // )
  3086. // mountWithHydration(`<div class="foo bar"></div>`, () =>
  3087. // h('div', { class: 'foo bar' }),
  3088. // )
  3089. // // SVG classes
  3090. // mountWithHydration(`<svg class="foo bar"></svg>`, () =>
  3091. // h('svg', { class: 'foo bar' }),
  3092. // )
  3093. // // class with different order
  3094. // mountWithHydration(`<div class="foo bar"></div>`, () =>
  3095. // h('div', { class: 'bar foo' }),
  3096. // )
  3097. // expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  3098. // mountWithHydration(`<div class="foo bar"></div>`, () =>
  3099. // h('div', { class: 'foo' }),
  3100. // )
  3101. // expect(`Hydration class mismatch`).toHaveBeenWarned()
  3102. // })
  3103. // test('style mismatch', () => {
  3104. // mountWithHydration(`<div style="color:red;"></div>`, () =>
  3105. // h('div', { style: { color: 'red' } }),
  3106. // )
  3107. // mountWithHydration(`<div style="color:red;"></div>`, () =>
  3108. // h('div', { style: `color:red;` }),
  3109. // )
  3110. // mountWithHydration(
  3111. // `<div style="color:red; font-size: 12px;"></div>`,
  3112. // () => h('div', { style: `font-size: 12px; color:red;` }),
  3113. // )
  3114. // mountWithHydration(`<div style="color:red;display:none;"></div>`, () =>
  3115. // withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  3116. // [vShow, false],
  3117. // ]),
  3118. // )
  3119. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3120. // mountWithHydration(`<div style="color:red;"></div>`, () =>
  3121. // h('div', { style: { color: 'green' } }),
  3122. // )
  3123. // expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  3124. // })
  3125. // test('style mismatch when no style attribute is present', () => {
  3126. // mountWithHydration(`<div></div>`, () =>
  3127. // h('div', { style: { color: 'red' } }),
  3128. // )
  3129. // expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  3130. // })
  3131. // test('style mismatch w/ v-show', () => {
  3132. // mountWithHydration(`<div style="color:red;display:none"></div>`, () =>
  3133. // withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  3134. // [vShow, false],
  3135. // ]),
  3136. // )
  3137. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3138. // mountWithHydration(`<div style="color:red;"></div>`, () =>
  3139. // withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  3140. // [vShow, false],
  3141. // ]),
  3142. // )
  3143. // expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  3144. // })
  3145. // test('attr mismatch', () => {
  3146. // mountWithHydration(`<div id="foo"></div>`, () => h('div', { id: 'foo' }))
  3147. // mountWithHydration(`<div spellcheck></div>`, () =>
  3148. // h('div', { spellcheck: '' }),
  3149. // )
  3150. // mountWithHydration(`<div></div>`, () => h('div', { id: undefined }))
  3151. // // boolean
  3152. // mountWithHydration(`<select multiple></div>`, () =>
  3153. // h('select', { multiple: true }),
  3154. // )
  3155. // mountWithHydration(`<select multiple></div>`, () =>
  3156. // h('select', { multiple: 'multiple' }),
  3157. // )
  3158. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3159. // mountWithHydration(`<div></div>`, () => h('div', { id: 'foo' }))
  3160. // expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  3161. // mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
  3162. // expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  3163. // })
  3164. // test('attr special case: textarea value', () => {
  3165. // mountWithHydration(`<textarea>foo</textarea>`, () =>
  3166. // h('textarea', { value: 'foo' }),
  3167. // )
  3168. // mountWithHydration(`<textarea></textarea>`, () =>
  3169. // h('textarea', { value: '' }),
  3170. // )
  3171. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3172. // mountWithHydration(`<textarea>foo</textarea>`, () =>
  3173. // h('textarea', { value: 'bar' }),
  3174. // )
  3175. // expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  3176. // })
  3177. // // #11873
  3178. // test('<textarea> with newlines at the beginning', async () => {
  3179. // const render = () => h('textarea', null, '\nhello')
  3180. // const html = await renderToString(createSSRApp({ render }))
  3181. // mountWithHydration(html, render)
  3182. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  3183. // })
  3184. // test('<pre> with newlines at the beginning', async () => {
  3185. // const render = () => h('pre', null, '\n')
  3186. // const html = await renderToString(createSSRApp({ render }))
  3187. // mountWithHydration(html, render)
  3188. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  3189. // })
  3190. // test('boolean attr handling', () => {
  3191. // mountWithHydration(`<input />`, () => h('input', { readonly: false }))
  3192. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3193. // mountWithHydration(`<input readonly />`, () =>
  3194. // h('input', { readonly: true }),
  3195. // )
  3196. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3197. // mountWithHydration(`<input readonly="readonly" />`, () =>
  3198. // h('input', { readonly: true }),
  3199. // )
  3200. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3201. // })
  3202. // test('client value is null or undefined', () => {
  3203. // mountWithHydration(`<div></div>`, () =>
  3204. // h('div', { draggable: undefined }),
  3205. // )
  3206. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3207. // mountWithHydration(`<input />`, () => h('input', { type: null }))
  3208. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3209. // })
  3210. // test('should not warn against object values', () => {
  3211. // mountWithHydration(`<input />`, () => h('input', { from: {} }))
  3212. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3213. // })
  3214. // test('should not warn on falsy bindings of non-property keys', () => {
  3215. // mountWithHydration(`<button />`, () => h('button', { href: undefined }))
  3216. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3217. // })
  3218. // test('should not warn on non-renderable option values', () => {
  3219. // mountWithHydration(`<select><option>hello</option></select>`, () =>
  3220. // h('select', [h('option', { value: ['foo'] }, 'hello')]),
  3221. // )
  3222. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3223. // })
  3224. // test('should not warn css v-bind', () => {
  3225. // const container = document.createElement('div')
  3226. // container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  3227. // const app = createSSRApp({
  3228. // setup() {
  3229. // useCssVars(() => ({
  3230. // foo: 'red',
  3231. // }))
  3232. // return () => h('div', { style: { color: 'var(--foo)' } })
  3233. // },
  3234. // })
  3235. // app.mount(container)
  3236. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3237. // })
  3238. // // #10317 - test case from #10325
  3239. // test('css vars should only be added to expected on component root dom', () => {
  3240. // const container = document.createElement('div')
  3241. // container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  3242. // const app = createSSRApp({
  3243. // setup() {
  3244. // useCssVars(() => ({
  3245. // foo: 'red',
  3246. // }))
  3247. // return () =>
  3248. // h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
  3249. // },
  3250. // })
  3251. // app.mount(container)
  3252. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3253. // })
  3254. // // #11188
  3255. // test('css vars support fallthrough', () => {
  3256. // const container = document.createElement('div')
  3257. // container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
  3258. // const app = createSSRApp({
  3259. // setup() {
  3260. // useCssVars(() => ({
  3261. // foo: 'red',
  3262. // }))
  3263. // return () => h(Child)
  3264. // },
  3265. // })
  3266. // const Child = {
  3267. // setup() {
  3268. // return () => h('div', { style: 'padding: 4px' })
  3269. // },
  3270. // }
  3271. // app.mount(container)
  3272. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3273. // })
  3274. // // #11189
  3275. // test('should not warn for directives that mutate DOM in created', () => {
  3276. // const container = document.createElement('div')
  3277. // container.innerHTML = `<div class="test red"></div>`
  3278. // const vColor: ObjectDirective = {
  3279. // created(el, binding) {
  3280. // el.classList.add(binding.value)
  3281. // },
  3282. // }
  3283. // const app = createSSRApp({
  3284. // setup() {
  3285. // return () =>
  3286. // withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
  3287. // },
  3288. // })
  3289. // app.mount(container)
  3290. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3291. // })
  3292. // test('escape css var name', () => {
  3293. // const container = document.createElement('div')
  3294. // container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
  3295. // const app = createSSRApp({
  3296. // setup() {
  3297. // useCssVars(() => ({
  3298. // 'foo.bar': 'red',
  3299. // }))
  3300. // return () => h(Child)
  3301. // },
  3302. // })
  3303. // const Child = {
  3304. // setup() {
  3305. // return () => h('div', { style: 'padding: 4px' })
  3306. // },
  3307. // }
  3308. // app.mount(container)
  3309. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3310. // })
  3311. // })
  3312. // describe('data-allow-mismatch', () => {
  3313. // test('element text content', () => {
  3314. // const { container } = mountWithHydration(
  3315. // `<div data-allow-mismatch="text">foo</div>`,
  3316. // () => h('div', 'bar'),
  3317. // )
  3318. // expect(container.innerHTML).toBe(
  3319. // '<div data-allow-mismatch="text">bar</div>',
  3320. // )
  3321. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  3322. // })
  3323. // test('not enough children', () => {
  3324. // const { container } = mountWithHydration(
  3325. // `<div data-allow-mismatch="children"></div>`,
  3326. // () => h('div', [h('span', 'foo'), h('span', 'bar')]),
  3327. // )
  3328. // expect(container.innerHTML).toBe(
  3329. // '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
  3330. // )
  3331. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  3332. // })
  3333. // test('too many children', () => {
  3334. // const { container } = mountWithHydration(
  3335. // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  3336. // () => h('div', [h('span', 'foo')]),
  3337. // )
  3338. // expect(container.innerHTML).toBe(
  3339. // '<div data-allow-mismatch="children"><span>foo</span></div>',
  3340. // )
  3341. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  3342. // })
  3343. // test('complete mismatch', () => {
  3344. // const { container } = mountWithHydration(
  3345. // `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  3346. // () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  3347. // )
  3348. // expect(container.innerHTML).toBe(
  3349. // '<div data-allow-mismatch="children"><div>foo</div><p>bar</p></div>',
  3350. // )
  3351. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3352. // })
  3353. // test('fragment mismatch removal', () => {
  3354. // const { container } = mountWithHydration(
  3355. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  3356. // () => h('div', [h('span', 'replaced')]),
  3357. // )
  3358. // expect(container.innerHTML).toBe(
  3359. // '<div data-allow-mismatch="children"><span>replaced</span></div>',
  3360. // )
  3361. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3362. // })
  3363. // test('fragment not enough children', () => {
  3364. // const { container } = mountWithHydration(
  3365. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  3366. // () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  3367. // )
  3368. // expect(container.innerHTML).toBe(
  3369. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  3370. // )
  3371. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3372. // })
  3373. // test('fragment too many children', () => {
  3374. // const { container } = mountWithHydration(
  3375. // `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  3376. // () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  3377. // )
  3378. // expect(container.innerHTML).toBe(
  3379. // '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  3380. // )
  3381. // // fragment ends early and attempts to hydrate the extra <div>bar</div>
  3382. // // as 2nd fragment child.
  3383. // expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  3384. // // excessive children removal
  3385. // expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  3386. // })
  3387. // test('comment mismatch (element)', () => {
  3388. // const { container } = mountWithHydration(
  3389. // `<div data-allow-mismatch="children"><span></span></div>`,
  3390. // () => h('div', [createCommentVNode('hi')]),
  3391. // )
  3392. // expect(container.innerHTML).toBe(
  3393. // '<div data-allow-mismatch="children"><!--hi--></div>',
  3394. // )
  3395. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3396. // })
  3397. // test('comment mismatch (text)', () => {
  3398. // const { container } = mountWithHydration(
  3399. // `<div data-allow-mismatch="children">foobar</div>`,
  3400. // () => h('div', [createCommentVNode('hi')]),
  3401. // )
  3402. // expect(container.innerHTML).toBe(
  3403. // '<div data-allow-mismatch="children"><!--hi--></div>',
  3404. // )
  3405. // expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  3406. // })
  3407. // test('class mismatch', () => {
  3408. // mountWithHydration(
  3409. // `<div class="foo bar" data-allow-mismatch="class"></div>`,
  3410. // () => h('div', { class: 'foo' }),
  3411. // )
  3412. // expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  3413. // })
  3414. // test('style mismatch', () => {
  3415. // mountWithHydration(
  3416. // `<div style="color:red;" data-allow-mismatch="style"></div>`,
  3417. // () => h('div', { style: { color: 'green' } }),
  3418. // )
  3419. // expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  3420. // })
  3421. // test('attr mismatch', () => {
  3422. // mountWithHydration(`<div data-allow-mismatch="attribute"></div>`, () =>
  3423. // h('div', { id: 'foo' }),
  3424. // )
  3425. // mountWithHydration(
  3426. // `<div id="bar" data-allow-mismatch="attribute"></div>`,
  3427. // () => h('div', { id: 'foo' }),
  3428. // )
  3429. // expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  3430. // })
  3431. // })
  3432. test.todo('Teleport')
  3433. test.todo('Suspense')
  3434. })