hydration.spec.ts 106 KB

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