hydration.spec.ts 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  1. /**
  2. * @vitest-environment jsdom
  3. */
  4. import {
  5. type ObjectDirective,
  6. Suspense,
  7. Teleport,
  8. Transition,
  9. type VNode,
  10. createBlock,
  11. createCommentVNode,
  12. createElementBlock,
  13. createElementVNode,
  14. createSSRApp,
  15. createStaticVNode,
  16. createTextVNode,
  17. createVNode,
  18. defineAsyncComponent,
  19. defineComponent,
  20. h,
  21. nextTick,
  22. onMounted,
  23. onServerPrefetch,
  24. openBlock,
  25. reactive,
  26. ref,
  27. renderSlot,
  28. useCssVars,
  29. vModelCheckbox,
  30. vShow,
  31. withCtx,
  32. withDirectives,
  33. } from '@vue/runtime-dom'
  34. import type { HMRRuntime } from '../src/hmr'
  35. import { type SSRContext, renderToString } from '@vue/server-renderer'
  36. import { PatchFlags, normalizeStyle } from '@vue/shared'
  37. import { vShowOriginalDisplay } from '../../runtime-dom/src/directives/vShow'
  38. declare var __VUE_HMR_RUNTIME__: HMRRuntime
  39. const { createRecord, reload } = __VUE_HMR_RUNTIME__
  40. function mountWithHydration(html: string, render: () => any) {
  41. const container = document.createElement('div')
  42. container.innerHTML = html
  43. const app = createSSRApp({
  44. render,
  45. })
  46. return {
  47. vnode: app.mount(container).$.subTree as VNode<Node, Element> & {
  48. el: Element
  49. },
  50. container,
  51. }
  52. }
  53. const triggerEvent = (type: string, el: Element) => {
  54. const event = new Event(type)
  55. el.dispatchEvent(event)
  56. }
  57. describe('SSR hydration', () => {
  58. beforeEach(() => {
  59. document.body.innerHTML = ''
  60. })
  61. test('text', async () => {
  62. const msg = ref('foo')
  63. const { vnode, container } = mountWithHydration('foo', () => msg.value)
  64. expect(vnode.el).toBe(container.firstChild)
  65. expect(container.textContent).toBe('foo')
  66. msg.value = 'bar'
  67. await nextTick()
  68. expect(container.textContent).toBe('bar')
  69. })
  70. test('empty text', async () => {
  71. const { container } = mountWithHydration('<div></div>', () =>
  72. h('div', createTextVNode('')),
  73. )
  74. expect(container.textContent).toBe('')
  75. expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
  76. })
  77. test('comment', () => {
  78. const { vnode, container } = mountWithHydration('<!---->', () => null)
  79. expect(vnode.el).toBe(container.firstChild)
  80. expect(vnode.el.nodeType).toBe(8) // comment
  81. })
  82. test('static', () => {
  83. const html = '<div><span>hello</span></div>'
  84. const { vnode, container } = mountWithHydration(html, () =>
  85. createStaticVNode('', 1),
  86. )
  87. expect(vnode.el).toBe(container.firstChild)
  88. expect(vnode.el.outerHTML).toBe(html)
  89. expect(vnode.anchor).toBe(container.firstChild)
  90. expect(vnode.children).toBe(html)
  91. })
  92. test('static (multiple elements)', () => {
  93. const staticContent = '<div></div><span>hello</span>'
  94. const html = `<div><div>hi</div>` + staticContent + `<div>ho</div></div>`
  95. const n1 = h('div', 'hi')
  96. const s = createStaticVNode('', 2)
  97. const n2 = h('div', 'ho')
  98. const { container } = mountWithHydration(html, () => h('div', [n1, s, n2]))
  99. const div = container.firstChild!
  100. expect(n1.el).toBe(div.firstChild)
  101. expect(n2.el).toBe(div.lastChild)
  102. expect(s.el).toBe(div.childNodes[1])
  103. expect(s.anchor).toBe(div.childNodes[2])
  104. expect(s.children).toBe(staticContent)
  105. })
  106. // #6008
  107. test('static (with text node as starting node)', () => {
  108. const html = ` A <span>foo</span> B`
  109. const { vnode, container } = mountWithHydration(html, () =>
  110. createStaticVNode(` A <span>foo</span> B`, 3),
  111. )
  112. expect(vnode.el).toBe(container.firstChild)
  113. expect(vnode.anchor).toBe(container.lastChild)
  114. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  115. })
  116. test('static with content adoption', () => {
  117. const html = ` A <span>foo</span> B`
  118. const { vnode, container } = mountWithHydration(html, () =>
  119. createStaticVNode(``, 3),
  120. )
  121. expect(vnode.el).toBe(container.firstChild)
  122. expect(vnode.anchor).toBe(container.lastChild)
  123. expect(vnode.children).toBe(html)
  124. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  125. })
  126. test('element with text children', async () => {
  127. const msg = ref('foo')
  128. const { vnode, container } = mountWithHydration(
  129. '<div class="foo">foo</div>',
  130. () => h('div', { class: msg.value }, msg.value),
  131. )
  132. expect(vnode.el).toBe(container.firstChild)
  133. expect(container.firstChild!.textContent).toBe('foo')
  134. msg.value = 'bar'
  135. await nextTick()
  136. expect(container.innerHTML).toBe(`<div class="bar">bar</div>`)
  137. })
  138. // #7285
  139. test('element with multiple continuous text vnodes', async () => {
  140. // should no mismatch warning
  141. const { container } = mountWithHydration('<div>foo0o</div>', () =>
  142. h('div', ['fo', createTextVNode('o'), 0, 'o']),
  143. )
  144. expect(container.textContent).toBe('foo0o')
  145. })
  146. test('element with elements children', async () => {
  147. const msg = ref('foo')
  148. const fn = vi.fn()
  149. const { vnode, container } = mountWithHydration(
  150. '<div><span>foo</span><span class="foo"></span></div>',
  151. () =>
  152. h('div', [
  153. h('span', msg.value),
  154. h('span', { class: msg.value, onClick: fn }),
  155. ]),
  156. )
  157. expect(vnode.el).toBe(container.firstChild)
  158. expect((vnode.children as VNode[])[0].el).toBe(
  159. container.firstChild!.childNodes[0],
  160. )
  161. expect((vnode.children as VNode[])[1].el).toBe(
  162. container.firstChild!.childNodes[1],
  163. )
  164. // event handler
  165. triggerEvent('click', vnode.el.querySelector('.foo')!)
  166. expect(fn).toHaveBeenCalled()
  167. msg.value = 'bar'
  168. await nextTick()
  169. expect(vnode.el.innerHTML).toBe(`<span>bar</span><span class="bar"></span>`)
  170. })
  171. test('element with ref', () => {
  172. const el = ref()
  173. const { vnode, container } = mountWithHydration('<div></div>', () =>
  174. h('div', { ref: el }),
  175. )
  176. expect(vnode.el).toBe(container.firstChild)
  177. expect(el.value).toBe(vnode.el)
  178. })
  179. test('Fragment', async () => {
  180. const msg = ref('foo')
  181. const fn = vi.fn()
  182. const { vnode, container } = mountWithHydration(
  183. '<div><!--[--><span>foo</span><!--[--><span class="foo"></span><!--]--><!--]--></div>',
  184. () =>
  185. h('div', [
  186. [
  187. h('span', msg.value),
  188. [h('span', { class: msg.value, onClick: fn })],
  189. ],
  190. ]),
  191. )
  192. expect(vnode.el).toBe(container.firstChild)
  193. expect(vnode.el.innerHTML).toBe(
  194. `<!--[--><span>foo</span><!--[--><span class="foo"></span><!--]--><!--]-->`,
  195. )
  196. // start fragment 1
  197. const fragment1 = (vnode.children as VNode[])[0]
  198. expect(fragment1.el).toBe(vnode.el.childNodes[0])
  199. const fragment1Children = fragment1.children as VNode[]
  200. // first <span>
  201. expect(fragment1Children[0].el!.tagName).toBe('SPAN')
  202. expect(fragment1Children[0].el).toBe(vnode.el.childNodes[1])
  203. // start fragment 2
  204. const fragment2 = fragment1Children[1]
  205. expect(fragment2.el).toBe(vnode.el.childNodes[2])
  206. const fragment2Children = fragment2.children as VNode[]
  207. // second <span>
  208. expect(fragment2Children[0].el!.tagName).toBe('SPAN')
  209. expect(fragment2Children[0].el).toBe(vnode.el.childNodes[3])
  210. // end fragment 2
  211. expect(fragment2.anchor).toBe(vnode.el.childNodes[4])
  212. // end fragment 1
  213. expect(fragment1.anchor).toBe(vnode.el.childNodes[5])
  214. // event handler
  215. triggerEvent('click', vnode.el.querySelector('.foo')!)
  216. expect(fn).toHaveBeenCalled()
  217. msg.value = 'bar'
  218. await nextTick()
  219. expect(vnode.el.innerHTML).toBe(
  220. `<!--[--><span>bar</span><!--[--><span class="bar"></span><!--]--><!--]-->`,
  221. )
  222. })
  223. // #7285
  224. test('Fragment (multiple continuous text vnodes)', async () => {
  225. // should no mismatch warning
  226. const { container } = mountWithHydration('<!--[-->fooo<!--]-->', () => [
  227. 'fo',
  228. createTextVNode('o'),
  229. 'o',
  230. ])
  231. expect(container.textContent).toBe('fooo')
  232. })
  233. test('Teleport', async () => {
  234. const msg = ref('foo')
  235. const fn = vi.fn()
  236. const teleportContainer = document.createElement('div')
  237. teleportContainer.id = 'teleport'
  238. teleportContainer.innerHTML = `<!--teleport start anchor--><span>foo</span><span class="foo"></span><!--teleport anchor-->`
  239. document.body.appendChild(teleportContainer)
  240. const { vnode, container } = mountWithHydration(
  241. '<!--teleport start--><!--teleport end-->',
  242. () =>
  243. h(Teleport, { to: '#teleport' }, [
  244. h('span', msg.value),
  245. h('span', { class: msg.value, onClick: fn }),
  246. ]),
  247. )
  248. expect(vnode.el).toBe(container.firstChild)
  249. expect(vnode.anchor).toBe(container.lastChild)
  250. expect(vnode.target).toBe(teleportContainer)
  251. expect(vnode.targetStart).toBe(teleportContainer.childNodes[0])
  252. expect((vnode.children as VNode[])[0].el).toBe(
  253. teleportContainer.childNodes[1],
  254. )
  255. expect((vnode.children as VNode[])[1].el).toBe(
  256. teleportContainer.childNodes[2],
  257. )
  258. expect(vnode.targetAnchor).toBe(teleportContainer.childNodes[3])
  259. // event handler
  260. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  261. expect(fn).toHaveBeenCalled()
  262. msg.value = 'bar'
  263. await nextTick()
  264. expect(teleportContainer.innerHTML).toBe(
  265. `<!--teleport start anchor--><span>bar</span><span class="bar"></span><!--teleport anchor-->`,
  266. )
  267. })
  268. test('Teleport (multiple + integration)', async () => {
  269. const msg = ref('foo')
  270. const fn1 = vi.fn()
  271. const fn2 = vi.fn()
  272. const Comp = () => [
  273. h(Teleport, { to: '#teleport2' }, [
  274. h('span', msg.value),
  275. h('span', { class: msg.value, onClick: fn1 }),
  276. ]),
  277. h(Teleport, { to: '#teleport2' }, [
  278. h('span', msg.value + '2'),
  279. h('span', { class: msg.value + '2', onClick: fn2 }),
  280. ]),
  281. ]
  282. const teleportContainer = document.createElement('div')
  283. teleportContainer.id = 'teleport2'
  284. const ctx: SSRContext = {}
  285. const mainHtml = await renderToString(h(Comp), ctx)
  286. expect(mainHtml).toMatchInlineSnapshot(
  287. `"<!--[--><!--teleport start--><!--teleport end--><!--teleport start--><!--teleport end--><!--]-->"`,
  288. )
  289. const teleportHtml = ctx.teleports!['#teleport2']
  290. expect(teleportHtml).toMatchInlineSnapshot(
  291. `"<!--teleport start anchor--><span>foo</span><span class="foo"></span><!--teleport anchor--><!--teleport start anchor--><span>foo2</span><span class="foo2"></span><!--teleport anchor-->"`,
  292. )
  293. teleportContainer.innerHTML = teleportHtml
  294. document.body.appendChild(teleportContainer)
  295. const { vnode, container } = mountWithHydration(mainHtml, Comp)
  296. expect(vnode.el).toBe(container.firstChild)
  297. const teleportVnode1 = (vnode.children as VNode[])[0]
  298. const teleportVnode2 = (vnode.children as VNode[])[1]
  299. expect(teleportVnode1.el).toBe(container.childNodes[1])
  300. expect(teleportVnode1.anchor).toBe(container.childNodes[2])
  301. expect(teleportVnode2.el).toBe(container.childNodes[3])
  302. expect(teleportVnode2.anchor).toBe(container.childNodes[4])
  303. expect(teleportVnode1.target).toBe(teleportContainer)
  304. expect(teleportVnode1.targetStart).toBe(teleportContainer.childNodes[0])
  305. expect((teleportVnode1 as any).children[0].el).toBe(
  306. teleportContainer.childNodes[1],
  307. )
  308. expect(teleportVnode1.targetAnchor).toBe(teleportContainer.childNodes[3])
  309. expect(teleportVnode2.target).toBe(teleportContainer)
  310. expect(teleportVnode2.targetStart).toBe(teleportContainer.childNodes[4])
  311. expect((teleportVnode2 as any).children[0].el).toBe(
  312. teleportContainer.childNodes[5],
  313. )
  314. expect(teleportVnode2.targetAnchor).toBe(teleportContainer.childNodes[7])
  315. // // event handler
  316. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  317. expect(fn1).toHaveBeenCalled()
  318. triggerEvent('click', teleportContainer.querySelector('.foo2')!)
  319. expect(fn2).toHaveBeenCalled()
  320. msg.value = 'bar'
  321. await nextTick()
  322. expect(teleportContainer.innerHTML).toMatchInlineSnapshot(
  323. `"<!--teleport start anchor--><span>bar</span><span class="bar"></span><!--teleport anchor--><!--teleport start anchor--><span>bar2</span><span class="bar2"></span><!--teleport anchor-->"`,
  324. )
  325. })
  326. test('Teleport (disabled)', async () => {
  327. const msg = ref('foo')
  328. const fn1 = vi.fn()
  329. const fn2 = vi.fn()
  330. const Comp = () => [
  331. h('div', 'foo'),
  332. h(Teleport, { to: '#teleport3', disabled: true }, [
  333. h('span', msg.value),
  334. h('span', { class: msg.value, onClick: fn1 }),
  335. ]),
  336. h('div', { class: msg.value + '2', onClick: fn2 }, 'bar'),
  337. ]
  338. const teleportContainer = document.createElement('div')
  339. teleportContainer.id = 'teleport3'
  340. const ctx: SSRContext = {}
  341. const mainHtml = await renderToString(h(Comp), ctx)
  342. expect(mainHtml).toMatchInlineSnapshot(
  343. `"<!--[--><div>foo</div><!--teleport start--><span>foo</span><span class="foo"></span><!--teleport end--><div class="foo2">bar</div><!--]-->"`,
  344. )
  345. const teleportHtml = ctx.teleports!['#teleport3']
  346. expect(teleportHtml).toMatchInlineSnapshot(
  347. `"<!--teleport start anchor--><!--teleport anchor-->"`,
  348. )
  349. teleportContainer.innerHTML = teleportHtml
  350. document.body.appendChild(teleportContainer)
  351. const { vnode, container } = mountWithHydration(mainHtml, Comp)
  352. expect(vnode.el).toBe(container.firstChild)
  353. const children = vnode.children as VNode[]
  354. expect(children[0].el).toBe(container.childNodes[1])
  355. const teleportVnode = children[1]
  356. expect(teleportVnode.el).toBe(container.childNodes[2])
  357. expect((teleportVnode.children as VNode[])[0].el).toBe(
  358. container.childNodes[3],
  359. )
  360. expect((teleportVnode.children as VNode[])[1].el).toBe(
  361. container.childNodes[4],
  362. )
  363. expect(teleportVnode.anchor).toBe(container.childNodes[5])
  364. expect(children[2].el).toBe(container.childNodes[6])
  365. expect(teleportVnode.target).toBe(teleportContainer)
  366. expect(teleportVnode.targetStart).toBe(teleportContainer.childNodes[0])
  367. expect(teleportVnode.targetAnchor).toBe(teleportContainer.childNodes[1])
  368. // // event handler
  369. triggerEvent('click', container.querySelector('.foo')!)
  370. expect(fn1).toHaveBeenCalled()
  371. triggerEvent('click', container.querySelector('.foo2')!)
  372. expect(fn2).toHaveBeenCalled()
  373. msg.value = 'bar'
  374. await nextTick()
  375. expect(container.innerHTML).toMatchInlineSnapshot(
  376. `"<!--[--><div>foo</div><!--teleport start--><span>bar</span><span class="bar"></span><!--teleport end--><div class="bar2">bar</div><!--]-->"`,
  377. )
  378. })
  379. // #6152
  380. test('Teleport (disabled + as component root)', () => {
  381. const { container } = mountWithHydration(
  382. '<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
  383. () => [
  384. h('div', 'Parent fragment'),
  385. h(() =>
  386. h(Teleport, { to: 'body', disabled: true }, [
  387. h('div', 'Teleport content'),
  388. ]),
  389. ),
  390. ],
  391. )
  392. expect(document.body.innerHTML).toBe('')
  393. expect(container.innerHTML).toBe(
  394. '<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
  395. )
  396. expect(
  397. `Hydration completed but contains mismatches.`,
  398. ).not.toHaveBeenWarned()
  399. })
  400. test('Teleport (as component root)', () => {
  401. const teleportContainer = document.createElement('div')
  402. teleportContainer.id = 'teleport4'
  403. teleportContainer.innerHTML = `<!--teleport start anchor-->hello<!--teleport anchor-->`
  404. document.body.appendChild(teleportContainer)
  405. const wrapper = {
  406. render() {
  407. return h(Teleport, { to: '#teleport4' }, ['hello'])
  408. },
  409. }
  410. const { vnode, container } = mountWithHydration(
  411. '<div><!--teleport start--><!--teleport end--><div></div></div>',
  412. () => h('div', [h(wrapper), h('div')]),
  413. )
  414. expect(vnode.el).toBe(container.firstChild)
  415. // component el
  416. const wrapperVNode = (vnode as any).children[0]
  417. const tpStart = container.firstChild?.firstChild
  418. const tpEnd = tpStart?.nextSibling
  419. expect(wrapperVNode.el).toBe(tpStart)
  420. expect(wrapperVNode.component.subTree.el).toBe(tpStart)
  421. expect(wrapperVNode.component.subTree.anchor).toBe(tpEnd)
  422. // next node hydrate properly
  423. const nextVNode = (vnode as any).children[1]
  424. expect(nextVNode.el).toBe(container.firstChild?.lastChild)
  425. })
  426. test('Teleport (nested)', () => {
  427. const teleportContainer = document.createElement('div')
  428. teleportContainer.id = 'teleport5'
  429. teleportContainer.innerHTML = `<!--teleport start anchor--><div><!--teleport start--><!--teleport end--></div><!--teleport anchor--><!--teleport start anchor--><div>child</div><!--teleport anchor-->`
  430. document.body.appendChild(teleportContainer)
  431. const { vnode, container } = mountWithHydration(
  432. '<!--teleport start--><!--teleport end-->',
  433. () =>
  434. h(Teleport, { to: '#teleport5' }, [
  435. h('div', [h(Teleport, { to: '#teleport5' }, [h('div', 'child')])]),
  436. ]),
  437. )
  438. expect(vnode.el).toBe(container.firstChild)
  439. expect(vnode.anchor).toBe(container.lastChild)
  440. const childDivVNode = (vnode as any).children[0]
  441. const div = teleportContainer.childNodes[1]
  442. expect(childDivVNode.el).toBe(div)
  443. expect(vnode.targetAnchor).toBe(div?.nextSibling)
  444. const childTeleportVNode = childDivVNode.children[0]
  445. expect(childTeleportVNode.el).toBe(div?.firstChild)
  446. expect(childTeleportVNode.anchor).toBe(div?.lastChild)
  447. expect(childTeleportVNode.targetAnchor).toBe(teleportContainer.lastChild)
  448. expect(childTeleportVNode.children[0].el).toBe(
  449. teleportContainer.lastChild?.previousSibling,
  450. )
  451. })
  452. test('with data-allow-mismatch component when using onServerPrefetch', async () => {
  453. const Comp = {
  454. template: `
  455. <div>Comp2</div>
  456. `,
  457. }
  458. let foo: any
  459. const App = {
  460. setup() {
  461. const flag = ref(true)
  462. foo = () => {
  463. flag.value = false
  464. }
  465. onServerPrefetch(() => (flag.value = false))
  466. return { flag }
  467. },
  468. components: {
  469. Comp,
  470. },
  471. template: `
  472. <span data-allow-mismatch>
  473. <Comp v-if="flag"></Comp>
  474. </span>
  475. `,
  476. }
  477. // hydrate
  478. const container = document.createElement('div')
  479. container.innerHTML = await renderToString(h(App))
  480. createSSRApp(App).mount(container)
  481. expect(container.innerHTML).toBe(
  482. '<span data-allow-mismatch=""><div>Comp2</div></span>',
  483. )
  484. foo()
  485. await nextTick()
  486. expect(container.innerHTML).toBe(
  487. '<span data-allow-mismatch=""><!--v-if--></span>',
  488. )
  489. })
  490. test('Teleport unmount (full integration)', async () => {
  491. const Comp1 = {
  492. template: `
  493. <Teleport to="#target">
  494. <span>Teleported Comp1</span>
  495. </Teleport>
  496. `,
  497. }
  498. const Comp2 = {
  499. template: `
  500. <div>Comp2</div>
  501. `,
  502. }
  503. const toggle = ref(true)
  504. const App = {
  505. template: `
  506. <div>
  507. <Comp1 v-if="toggle"/>
  508. <Comp2 v-else/>
  509. </div>
  510. `,
  511. components: {
  512. Comp1,
  513. Comp2,
  514. },
  515. setup() {
  516. return { toggle }
  517. },
  518. }
  519. const container = document.createElement('div')
  520. const teleportContainer = document.createElement('div')
  521. teleportContainer.id = 'target'
  522. document.body.appendChild(teleportContainer)
  523. // server render
  524. const ctx: SSRContext = {}
  525. container.innerHTML = await renderToString(h(App), ctx)
  526. expect(container.innerHTML).toBe(
  527. '<div><!--teleport start--><!--teleport end--></div>',
  528. )
  529. teleportContainer.innerHTML = ctx.teleports!['#target']
  530. // hydrate
  531. createSSRApp(App).mount(container)
  532. expect(container.innerHTML).toBe(
  533. '<div><!--teleport start--><!--teleport end--></div>',
  534. )
  535. expect(teleportContainer.innerHTML).toBe(
  536. '<!--teleport start anchor--><span>Teleported Comp1</span><!--teleport anchor-->',
  537. )
  538. expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  539. toggle.value = false
  540. await nextTick()
  541. expect(container.innerHTML).toBe('<div><div>Comp2</div></div>')
  542. expect(teleportContainer.innerHTML).toBe('')
  543. })
  544. test('Teleport unmount (mismatch + full integration)', async () => {
  545. const Comp1 = {
  546. template: `
  547. <Teleport to="#target">
  548. <span>Teleported Comp1</span>
  549. </Teleport>
  550. `,
  551. }
  552. const Comp2 = {
  553. template: `
  554. <div>Comp2</div>
  555. `,
  556. }
  557. const toggle = ref(true)
  558. const App = {
  559. template: `
  560. <div>
  561. <Comp1 v-if="toggle"/>
  562. <Comp2 v-else/>
  563. </div>
  564. `,
  565. components: {
  566. Comp1,
  567. Comp2,
  568. },
  569. setup() {
  570. return { toggle }
  571. },
  572. }
  573. const container = document.createElement('div')
  574. const teleportContainer = document.createElement('div')
  575. teleportContainer.id = 'target'
  576. document.body.appendChild(teleportContainer)
  577. // server render
  578. container.innerHTML = await renderToString(h(App))
  579. expect(container.innerHTML).toBe(
  580. '<div><!--teleport start--><!--teleport end--></div>',
  581. )
  582. expect(teleportContainer.innerHTML).toBe('')
  583. // hydrate
  584. createSSRApp(App).mount(container)
  585. expect(container.innerHTML).toBe(
  586. '<div><!--teleport start--><!--teleport end--></div>',
  587. )
  588. expect(teleportContainer.innerHTML).toBe('<span>Teleported Comp1</span>')
  589. expect(`Hydration children mismatch`).toHaveBeenWarned()
  590. toggle.value = false
  591. await nextTick()
  592. expect(container.innerHTML).toBe('<div><div>Comp2</div></div>')
  593. expect(teleportContainer.innerHTML).toBe('')
  594. })
  595. test('Teleport target change (mismatch + full integration)', async () => {
  596. const target = ref('#target1')
  597. const Comp = {
  598. template: `
  599. <Teleport :to="target">
  600. <span>Teleported</span>
  601. </Teleport>
  602. `,
  603. setup() {
  604. return { target }
  605. },
  606. }
  607. const App = {
  608. template: `
  609. <div>
  610. <Comp />
  611. </div>
  612. `,
  613. components: {
  614. Comp,
  615. },
  616. }
  617. const container = document.createElement('div')
  618. const teleportContainer1 = document.createElement('div')
  619. teleportContainer1.id = 'target1'
  620. const teleportContainer2 = document.createElement('div')
  621. teleportContainer2.id = 'target2'
  622. document.body.appendChild(teleportContainer1)
  623. document.body.appendChild(teleportContainer2)
  624. // server render
  625. container.innerHTML = await renderToString(h(App))
  626. expect(container.innerHTML).toBe(
  627. '<div><!--teleport start--><!--teleport end--></div>',
  628. )
  629. expect(teleportContainer1.innerHTML).toBe('')
  630. expect(teleportContainer2.innerHTML).toBe('')
  631. // hydrate
  632. createSSRApp(App).mount(container)
  633. expect(container.innerHTML).toBe(
  634. '<div><!--teleport start--><!--teleport end--></div>',
  635. )
  636. expect(teleportContainer1.innerHTML).toBe('<span>Teleported</span>')
  637. expect(teleportContainer2.innerHTML).toBe('')
  638. expect(`Hydration children mismatch`).toHaveBeenWarned()
  639. target.value = '#target2'
  640. await nextTick()
  641. expect(teleportContainer1.innerHTML).toBe('')
  642. expect(teleportContainer2.innerHTML).toBe('<span>Teleported</span>')
  643. })
  644. // compile SSR + client render fn from the same template & hydrate
  645. test('full compiler integration', async () => {
  646. const mounted: string[] = []
  647. const log = vi.fn()
  648. const toggle = ref(true)
  649. const Child = {
  650. data() {
  651. return {
  652. count: 0,
  653. text: 'hello',
  654. style: {
  655. color: 'red',
  656. },
  657. }
  658. },
  659. mounted() {
  660. mounted.push('child')
  661. },
  662. template: `
  663. <div>
  664. <span class="count" :style="style">{{ count }}</span>
  665. <button class="inc" @click="count++">inc</button>
  666. <button class="change" @click="style.color = 'green'" >change color</button>
  667. <button class="emit" @click="$emit('foo')">emit</button>
  668. <span class="text">{{ text }}</span>
  669. <input v-model="text">
  670. </div>
  671. `,
  672. }
  673. const App = {
  674. setup() {
  675. return { toggle }
  676. },
  677. mounted() {
  678. mounted.push('parent')
  679. },
  680. template: `
  681. <div>
  682. <span>hello</span>
  683. <template v-if="toggle">
  684. <Child @foo="log('child')"/>
  685. <template v-if="true">
  686. <button class="parent-click" @click="log('click')">click me</button>
  687. </template>
  688. </template>
  689. <span>hello</span>
  690. </div>`,
  691. components: {
  692. Child,
  693. },
  694. methods: {
  695. log,
  696. },
  697. }
  698. const container = document.createElement('div')
  699. // server render
  700. container.innerHTML = await renderToString(h(App))
  701. // hydrate
  702. createSSRApp(App).mount(container)
  703. // assert interactions
  704. // 1. parent button click
  705. triggerEvent('click', container.querySelector('.parent-click')!)
  706. expect(log).toHaveBeenCalledWith('click')
  707. // 2. child inc click + text interpolation
  708. const count = container.querySelector('.count') as HTMLElement
  709. expect(count.textContent).toBe(`0`)
  710. triggerEvent('click', container.querySelector('.inc')!)
  711. await nextTick()
  712. expect(count.textContent).toBe(`1`)
  713. // 3. child color click + style binding
  714. expect(count.style.color).toBe('red')
  715. triggerEvent('click', container.querySelector('.change')!)
  716. await nextTick()
  717. expect(count.style.color).toBe('green')
  718. // 4. child event emit
  719. triggerEvent('click', container.querySelector('.emit')!)
  720. expect(log).toHaveBeenCalledWith('child')
  721. // 5. child v-model
  722. const text = container.querySelector('.text')!
  723. const input = container.querySelector('input')!
  724. expect(text.textContent).toBe('hello')
  725. input.value = 'bye'
  726. triggerEvent('input', input)
  727. await nextTick()
  728. expect(text.textContent).toBe('bye')
  729. })
  730. test('handle click error in ssr mode', async () => {
  731. const App = {
  732. setup() {
  733. const throwError = () => {
  734. throw new Error('Sentry Error')
  735. }
  736. return { throwError }
  737. },
  738. template: `
  739. <div>
  740. <button class="parent-click" @click="throwError">click me</button>
  741. </div>`,
  742. }
  743. const container = document.createElement('div')
  744. // server render
  745. container.innerHTML = await renderToString(h(App))
  746. // hydrate
  747. const app = createSSRApp(App)
  748. const handler = (app.config.errorHandler = vi.fn())
  749. app.mount(container)
  750. // assert interactions
  751. // parent button click
  752. triggerEvent('click', container.querySelector('.parent-click')!)
  753. expect(handler).toHaveBeenCalled()
  754. })
  755. test('handle blur error in ssr mode', async () => {
  756. const App = {
  757. setup() {
  758. const throwError = () => {
  759. throw new Error('Sentry Error')
  760. }
  761. return { throwError }
  762. },
  763. template: `
  764. <div>
  765. <input class="parent-click" @blur="throwError"/>
  766. </div>`,
  767. }
  768. const container = document.createElement('div')
  769. // server render
  770. container.innerHTML = await renderToString(h(App))
  771. // hydrate
  772. const app = createSSRApp(App)
  773. const handler = (app.config.errorHandler = vi.fn())
  774. app.mount(container)
  775. // assert interactions
  776. // parent blur event
  777. triggerEvent('blur', container.querySelector('.parent-click')!)
  778. expect(handler).toHaveBeenCalled()
  779. })
  780. test('Suspense', async () => {
  781. const AsyncChild = {
  782. async setup() {
  783. const count = ref(0)
  784. return () =>
  785. h(
  786. 'span',
  787. {
  788. onClick: () => {
  789. count.value++
  790. },
  791. },
  792. count.value,
  793. )
  794. },
  795. }
  796. const { vnode, container } = mountWithHydration('<span>0</span>', () =>
  797. h(Suspense, () => h(AsyncChild)),
  798. )
  799. expect(vnode.el).toBe(container.firstChild)
  800. // wait for hydration to finish
  801. await new Promise(r => setTimeout(r))
  802. triggerEvent('click', container.querySelector('span')!)
  803. await nextTick()
  804. expect(container.innerHTML).toBe(`<span>1</span>`)
  805. })
  806. // #6638
  807. test('Suspense + async component', async () => {
  808. let isSuspenseResolved = false
  809. let isSuspenseResolvedInChild: any
  810. const AsyncChild = defineAsyncComponent(() =>
  811. Promise.resolve(
  812. defineComponent({
  813. setup() {
  814. isSuspenseResolvedInChild = isSuspenseResolved
  815. const count = ref(0)
  816. return () =>
  817. h(
  818. 'span',
  819. {
  820. onClick: () => {
  821. count.value++
  822. },
  823. },
  824. count.value,
  825. )
  826. },
  827. }),
  828. ),
  829. )
  830. const { vnode, container } = mountWithHydration('<span>0</span>', () =>
  831. h(
  832. Suspense,
  833. {
  834. onResolve() {
  835. isSuspenseResolved = true
  836. },
  837. },
  838. () => h(AsyncChild),
  839. ),
  840. )
  841. expect(vnode.el).toBe(container.firstChild)
  842. // wait for hydration to finish
  843. await new Promise(r => setTimeout(r))
  844. expect(isSuspenseResolvedInChild).toBe(false)
  845. expect(isSuspenseResolved).toBe(true)
  846. // assert interaction
  847. triggerEvent('click', container.querySelector('span')!)
  848. await nextTick()
  849. expect(container.innerHTML).toBe(`<span>1</span>`)
  850. })
  851. test('Suspense (full integration)', async () => {
  852. const mountedCalls: number[] = []
  853. const asyncDeps: Promise<any>[] = []
  854. const AsyncChild = defineComponent({
  855. props: ['n'],
  856. async setup(props) {
  857. const count = ref(props.n)
  858. onMounted(() => {
  859. mountedCalls.push(props.n)
  860. })
  861. const p = new Promise(r => setTimeout(r, props.n * 10))
  862. asyncDeps.push(p)
  863. await p
  864. return () =>
  865. h(
  866. 'span',
  867. {
  868. onClick: () => {
  869. count.value++
  870. },
  871. },
  872. count.value,
  873. )
  874. },
  875. })
  876. const done = vi.fn()
  877. const App = {
  878. template: `
  879. <Suspense @resolve="done">
  880. <div>
  881. <AsyncChild :n="1" />
  882. <AsyncChild :n="2" />
  883. </div>
  884. </Suspense>`,
  885. components: {
  886. AsyncChild,
  887. },
  888. methods: {
  889. done,
  890. },
  891. }
  892. const container = document.createElement('div')
  893. // server render
  894. container.innerHTML = await renderToString(h(App))
  895. expect(container.innerHTML).toMatchInlineSnapshot(
  896. `"<div><span>1</span><span>2</span></div>"`,
  897. )
  898. // reset asyncDeps from ssr
  899. asyncDeps.length = 0
  900. // hydrate
  901. createSSRApp(App).mount(container)
  902. expect(mountedCalls.length).toBe(0)
  903. expect(asyncDeps.length).toBe(2)
  904. // wait for hydration to complete
  905. await Promise.all(asyncDeps)
  906. await new Promise(r => setTimeout(r))
  907. // should flush buffered effects
  908. expect(mountedCalls).toMatchObject([1, 2])
  909. expect(container.innerHTML).toMatch(
  910. `<div><span>1</span><span>2</span></div>`,
  911. )
  912. const span1 = container.querySelector('span')!
  913. triggerEvent('click', span1)
  914. await nextTick()
  915. expect(container.innerHTML).toMatch(
  916. `<div><span>2</span><span>2</span></div>`,
  917. )
  918. const span2 = span1.nextSibling as Element
  919. triggerEvent('click', span2)
  920. await nextTick()
  921. expect(container.innerHTML).toMatch(
  922. `<div><span>2</span><span>3</span></div>`,
  923. )
  924. })
  925. test('async component', async () => {
  926. const spy = vi.fn()
  927. const Comp = () =>
  928. h(
  929. 'button',
  930. {
  931. onClick: spy,
  932. },
  933. 'hello!',
  934. )
  935. let serverResolve: any
  936. let AsyncComp = defineAsyncComponent(
  937. () =>
  938. new Promise(r => {
  939. serverResolve = r
  940. }),
  941. )
  942. const App = {
  943. render() {
  944. return ['hello', h(AsyncComp), 'world']
  945. },
  946. }
  947. // server render
  948. const htmlPromise = renderToString(h(App))
  949. serverResolve(Comp)
  950. const html = await htmlPromise
  951. expect(html).toMatchInlineSnapshot(
  952. `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  953. )
  954. // hydration
  955. let clientResolve: any
  956. AsyncComp = defineAsyncComponent(
  957. () =>
  958. new Promise(r => {
  959. clientResolve = r
  960. }),
  961. )
  962. const container = document.createElement('div')
  963. container.innerHTML = html
  964. createSSRApp(App).mount(container)
  965. // hydration not complete yet
  966. triggerEvent('click', container.querySelector('button')!)
  967. expect(spy).not.toHaveBeenCalled()
  968. // resolve
  969. clientResolve(Comp)
  970. await new Promise(r => setTimeout(r))
  971. // should be hydrated now
  972. triggerEvent('click', container.querySelector('button')!)
  973. expect(spy).toHaveBeenCalled()
  974. })
  975. test('update async wrapper before resolve', async () => {
  976. const Comp = {
  977. render() {
  978. return h('h1', 'Async component')
  979. },
  980. }
  981. let serverResolve: any
  982. let AsyncComp = defineAsyncComponent(
  983. () =>
  984. new Promise(r => {
  985. serverResolve = r
  986. }),
  987. )
  988. const toggle = ref(true)
  989. const App = {
  990. setup() {
  991. onMounted(() => {
  992. // change state, this makes updateComponent(AsyncComp) execute before
  993. // the async component is resolved
  994. toggle.value = false
  995. })
  996. return () => {
  997. return [toggle.value ? 'hello' : 'world', h(AsyncComp)]
  998. }
  999. },
  1000. }
  1001. // server render
  1002. const htmlPromise = renderToString(h(App))
  1003. serverResolve(Comp)
  1004. const html = await htmlPromise
  1005. expect(html).toMatchInlineSnapshot(
  1006. `"<!--[-->hello<h1>Async component</h1><!--]-->"`,
  1007. )
  1008. // hydration
  1009. let clientResolve: any
  1010. AsyncComp = defineAsyncComponent(
  1011. () =>
  1012. new Promise(r => {
  1013. clientResolve = r
  1014. }),
  1015. )
  1016. const container = document.createElement('div')
  1017. container.innerHTML = html
  1018. createSSRApp(App).mount(container)
  1019. // resolve
  1020. clientResolve(Comp)
  1021. await new Promise(r => setTimeout(r))
  1022. // should be hydrated now
  1023. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  1024. expect(container.innerHTML).toMatchInlineSnapshot(
  1025. `"<!--[-->world<h1>Async component</h1><!--]-->"`,
  1026. )
  1027. })
  1028. test('hydrate safely when property used by async setup changed before render', async () => {
  1029. const toggle = ref(true)
  1030. const AsyncComp = {
  1031. async setup() {
  1032. await new Promise<void>(r => setTimeout(r, 10))
  1033. return () => h('h1', 'Async component')
  1034. },
  1035. }
  1036. const AsyncWrapper = {
  1037. render() {
  1038. return h(AsyncComp)
  1039. },
  1040. }
  1041. const SiblingComp = {
  1042. setup() {
  1043. toggle.value = false
  1044. return () => h('span')
  1045. },
  1046. }
  1047. const App = {
  1048. setup() {
  1049. return () =>
  1050. h(
  1051. Suspense,
  1052. {},
  1053. {
  1054. default: () => [
  1055. h('main', {}, [
  1056. h(AsyncWrapper, {
  1057. prop: toggle.value ? 'hello' : 'world',
  1058. }),
  1059. h(SiblingComp),
  1060. ]),
  1061. ],
  1062. },
  1063. )
  1064. },
  1065. }
  1066. // server render
  1067. const html = await renderToString(h(App))
  1068. expect(html).toMatchInlineSnapshot(
  1069. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  1070. )
  1071. expect(toggle.value).toBe(false)
  1072. // hydration
  1073. // reset the value
  1074. toggle.value = true
  1075. expect(toggle.value).toBe(true)
  1076. const container = document.createElement('div')
  1077. container.innerHTML = html
  1078. createSSRApp(App).mount(container)
  1079. await new Promise(r => setTimeout(r, 10))
  1080. expect(toggle.value).toBe(false)
  1081. // should be hydrated now
  1082. expect(container.innerHTML).toMatchInlineSnapshot(
  1083. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  1084. )
  1085. })
  1086. test('hydrate safely when property used by deep nested async setup changed before render', async () => {
  1087. const toggle = ref(true)
  1088. const AsyncComp = {
  1089. async setup() {
  1090. await new Promise<void>(r => setTimeout(r, 10))
  1091. return () => h('h1', 'Async component')
  1092. },
  1093. }
  1094. const AsyncWrapper = { render: () => h(AsyncComp) }
  1095. const AsyncWrapperWrapper = { render: () => h(AsyncWrapper) }
  1096. const SiblingComp = {
  1097. setup() {
  1098. toggle.value = false
  1099. return () => h('span')
  1100. },
  1101. }
  1102. const App = {
  1103. setup() {
  1104. return () =>
  1105. h(
  1106. Suspense,
  1107. {},
  1108. {
  1109. default: () => [
  1110. h('main', {}, [
  1111. h(AsyncWrapperWrapper, {
  1112. prop: toggle.value ? 'hello' : 'world',
  1113. }),
  1114. h(SiblingComp),
  1115. ]),
  1116. ],
  1117. },
  1118. )
  1119. },
  1120. }
  1121. // server render
  1122. const html = await renderToString(h(App))
  1123. expect(html).toMatchInlineSnapshot(
  1124. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  1125. )
  1126. expect(toggle.value).toBe(false)
  1127. // hydration
  1128. // reset the value
  1129. toggle.value = true
  1130. expect(toggle.value).toBe(true)
  1131. const container = document.createElement('div')
  1132. container.innerHTML = html
  1133. createSSRApp(App).mount(container)
  1134. await new Promise(r => setTimeout(r, 10))
  1135. expect(toggle.value).toBe(false)
  1136. // should be hydrated now
  1137. expect(container.innerHTML).toMatchInlineSnapshot(
  1138. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  1139. )
  1140. })
  1141. // #3787
  1142. test('unmount async wrapper before load', async () => {
  1143. let resolve: any
  1144. const AsyncComp = defineAsyncComponent(
  1145. () =>
  1146. new Promise(r => {
  1147. resolve = r
  1148. }),
  1149. )
  1150. const show = ref(true)
  1151. const root = document.createElement('div')
  1152. root.innerHTML = '<div><div>async</div></div>'
  1153. createSSRApp({
  1154. render() {
  1155. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  1156. },
  1157. }).mount(root)
  1158. show.value = false
  1159. await nextTick()
  1160. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  1161. resolve({})
  1162. })
  1163. //#12362
  1164. test('nested async wrapper', async () => {
  1165. const Toggle = defineAsyncComponent(
  1166. () =>
  1167. new Promise(r => {
  1168. r(
  1169. defineComponent({
  1170. setup(_, { slots }) {
  1171. const show = ref(false)
  1172. onMounted(() => {
  1173. nextTick(() => {
  1174. show.value = true
  1175. })
  1176. })
  1177. return () =>
  1178. withDirectives(
  1179. h('div', null, [renderSlot(slots, 'default')]),
  1180. [[vShow, show.value]],
  1181. )
  1182. },
  1183. }) as any,
  1184. )
  1185. }),
  1186. )
  1187. const Wrapper = defineAsyncComponent(() => {
  1188. return new Promise(r => {
  1189. r(
  1190. defineComponent({
  1191. render(this: any) {
  1192. return renderSlot(this.$slots, 'default')
  1193. },
  1194. }) as any,
  1195. )
  1196. })
  1197. })
  1198. const count = ref(0)
  1199. const fn = vi.fn()
  1200. const Child = {
  1201. setup() {
  1202. onMounted(() => {
  1203. fn()
  1204. count.value++
  1205. })
  1206. return () => h('div', count.value)
  1207. },
  1208. }
  1209. const App = {
  1210. render() {
  1211. return h(Toggle, null, {
  1212. default: () =>
  1213. h(Wrapper, null, {
  1214. default: () =>
  1215. h(Wrapper, null, {
  1216. default: () => h(Child),
  1217. }),
  1218. }),
  1219. })
  1220. },
  1221. }
  1222. const root = document.createElement('div')
  1223. root.innerHTML = await renderToString(h(App))
  1224. expect(root.innerHTML).toMatchInlineSnapshot(
  1225. `"<div style="display:none;"><!--[--><!--[--><!--[--><div>0</div><!--]--><!--]--><!--]--></div>"`,
  1226. )
  1227. createSSRApp(App).mount(root)
  1228. await nextTick()
  1229. await nextTick()
  1230. expect(root.innerHTML).toMatchInlineSnapshot(
  1231. `"<div style=""><!--[--><!--[--><!--[--><div>1</div><!--]--><!--]--><!--]--></div>"`,
  1232. )
  1233. expect(fn).toBeCalledTimes(1)
  1234. })
  1235. test('unmount async wrapper before load (fragment)', async () => {
  1236. let resolve: any
  1237. const AsyncComp = defineAsyncComponent(
  1238. () =>
  1239. new Promise(r => {
  1240. resolve = r
  1241. }),
  1242. )
  1243. const show = ref(true)
  1244. const root = document.createElement('div')
  1245. root.innerHTML = '<div><!--[-->async<!--]--></div>'
  1246. createSSRApp({
  1247. render() {
  1248. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  1249. },
  1250. }).mount(root)
  1251. show.value = false
  1252. await nextTick()
  1253. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  1254. resolve({})
  1255. })
  1256. test('elements with camel-case in svg ', () => {
  1257. const { vnode, container } = mountWithHydration(
  1258. '<animateTransform></animateTransform>',
  1259. () => h('animateTransform'),
  1260. )
  1261. expect(vnode.el).toBe(container.firstChild)
  1262. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  1263. })
  1264. test('SVG as a mount container', () => {
  1265. const svgContainer = document.createElement('svg')
  1266. svgContainer.innerHTML = '<g></g>'
  1267. const app = createSSRApp({
  1268. render: () => h('g'),
  1269. })
  1270. expect(
  1271. (
  1272. app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
  1273. el: Element
  1274. }
  1275. ).el instanceof SVGElement,
  1276. )
  1277. })
  1278. test('force hydrate prop with `.prop` modifier', () => {
  1279. const { container } = mountWithHydration('<input type="checkbox">', () =>
  1280. h('input', {
  1281. type: 'checkbox',
  1282. '.indeterminate': true,
  1283. }),
  1284. )
  1285. expect((container.firstChild! as any).indeterminate).toBe(true)
  1286. })
  1287. test('force hydrate input v-model with non-string value bindings', () => {
  1288. const { container } = mountWithHydration(
  1289. '<input type="checkbox" value="true">',
  1290. () =>
  1291. withDirectives(
  1292. createVNode(
  1293. 'input',
  1294. { type: 'checkbox', 'true-value': true },
  1295. null,
  1296. PatchFlags.PROPS,
  1297. ['true-value'],
  1298. ),
  1299. [[vModelCheckbox, true]],
  1300. ),
  1301. )
  1302. expect((container.firstChild as any)._trueValue).toBe(true)
  1303. })
  1304. test('force hydrate checkbox with indeterminate', () => {
  1305. const { container } = mountWithHydration(
  1306. '<input type="checkbox" indeterminate>',
  1307. () =>
  1308. createVNode(
  1309. 'input',
  1310. { type: 'checkbox', indeterminate: '' },
  1311. null,
  1312. PatchFlags.CACHED,
  1313. ),
  1314. )
  1315. expect((container.firstChild as any).indeterminate).toBe(true)
  1316. })
  1317. test('force hydrate select option with non-string value bindings', () => {
  1318. const { container } = mountWithHydration(
  1319. '<select><option value="true">ok</option></select>',
  1320. () =>
  1321. h('select', [
  1322. // hoisted because bound value is a constant...
  1323. createVNode('option', { value: true }, null, -1 /* HOISTED */),
  1324. ]),
  1325. )
  1326. expect((container.firstChild!.firstChild as any)._value).toBe(true)
  1327. })
  1328. // #7203
  1329. test('force hydrate custom element with dynamic props', () => {
  1330. class MyElement extends HTMLElement {
  1331. foo = ''
  1332. constructor() {
  1333. super()
  1334. }
  1335. }
  1336. customElements.define('my-element-7203', MyElement)
  1337. const msg = ref('bar')
  1338. const container = document.createElement('div')
  1339. container.innerHTML = '<my-element-7203></my-element-7203>'
  1340. const app = createSSRApp({
  1341. render: () => h('my-element-7203', { foo: msg.value }),
  1342. })
  1343. app.mount(container)
  1344. expect((container.firstChild as any).foo).toBe(msg.value)
  1345. })
  1346. // #5728
  1347. test('empty text node in slot', () => {
  1348. const Comp = {
  1349. render(this: any) {
  1350. return renderSlot(this.$slots, 'default', {}, () => [
  1351. createTextVNode(''),
  1352. ])
  1353. },
  1354. }
  1355. const { container, vnode } = mountWithHydration('<!--[--><!--]-->', () =>
  1356. h(Comp),
  1357. )
  1358. expect(container.childNodes.length).toBe(3)
  1359. const text = container.childNodes[1]
  1360. expect(text.nodeType).toBe(3)
  1361. expect(vnode.el).toBe(container.childNodes[0])
  1362. // component => slot fragment => text node
  1363. expect((vnode as any).component?.subTree.children[0].el).toBe(text)
  1364. })
  1365. // #7215
  1366. test('empty text node', () => {
  1367. const Comp = {
  1368. render(this: any) {
  1369. return h('p', [''])
  1370. },
  1371. }
  1372. const { container } = mountWithHydration('<p></p>', () => h(Comp))
  1373. expect(container.childNodes.length).toBe(1)
  1374. const p = container.childNodes[0]
  1375. expect(p.childNodes.length).toBe(1)
  1376. const text = p.childNodes[0]
  1377. expect(text.nodeType).toBe(3)
  1378. })
  1379. // #11372
  1380. test('object style value tracking in prod', async () => {
  1381. __DEV__ = false
  1382. try {
  1383. const style = reactive({ color: 'red' })
  1384. const Comp = {
  1385. render(this: any) {
  1386. return (
  1387. openBlock(),
  1388. createElementBlock(
  1389. 'div',
  1390. {
  1391. style: normalizeStyle(style),
  1392. },
  1393. null,
  1394. 4 /* STYLE */,
  1395. )
  1396. )
  1397. },
  1398. }
  1399. const { container } = mountWithHydration(
  1400. `<div style="color: red;"></div>`,
  1401. () => h(Comp),
  1402. )
  1403. style.color = 'green'
  1404. await nextTick()
  1405. expect(container.innerHTML).toBe(`<div style="color: green;"></div>`)
  1406. } finally {
  1407. __DEV__ = true
  1408. }
  1409. })
  1410. test('app.unmount()', async () => {
  1411. const container = document.createElement('DIV')
  1412. container.innerHTML = '<button></button>'
  1413. const App = defineComponent({
  1414. setup(_, { expose }) {
  1415. const count = ref(0)
  1416. expose({ count })
  1417. return () =>
  1418. h('button', {
  1419. onClick: () => count.value++,
  1420. })
  1421. },
  1422. })
  1423. const app = createSSRApp(App)
  1424. const vm = app.mount(container)
  1425. await nextTick()
  1426. expect((container as any)._vnode).toBeDefined()
  1427. // @ts-expect-error - expose()'d properties are not available on vm type
  1428. expect(vm.count).toBe(0)
  1429. app.unmount()
  1430. expect((container as any)._vnode).toBe(null)
  1431. })
  1432. // #6637
  1433. test('stringified root fragment', () => {
  1434. mountWithHydration(`<!--[--><div></div><!--]-->`, () =>
  1435. createStaticVNode(`<div></div>`, 1),
  1436. )
  1437. expect(`mismatch`).not.toHaveBeenWarned()
  1438. })
  1439. test('transition appear', () => {
  1440. const { vnode, container } = mountWithHydration(
  1441. `<template><div>foo</div></template>`,
  1442. () =>
  1443. h(
  1444. Transition,
  1445. { appear: true },
  1446. {
  1447. default: () => h('div', 'foo'),
  1448. },
  1449. ),
  1450. )
  1451. expect(container.firstChild).toMatchInlineSnapshot(`
  1452. <div
  1453. class="v-enter-from v-enter-active"
  1454. >
  1455. foo
  1456. </div>
  1457. `)
  1458. expect(vnode.el).toBe(container.firstChild)
  1459. expect(`mismatch`).not.toHaveBeenWarned()
  1460. })
  1461. test('transition appear with v-if', () => {
  1462. const show = false
  1463. const { vnode, container } = mountWithHydration(
  1464. `<template><!----></template>`,
  1465. () =>
  1466. h(
  1467. Transition,
  1468. { appear: true },
  1469. {
  1470. default: () => (show ? h('div', 'foo') : createCommentVNode('')),
  1471. },
  1472. ),
  1473. )
  1474. expect(container.firstChild).toMatchInlineSnapshot('<!---->')
  1475. expect(vnode.el).toBe(container.firstChild)
  1476. expect(`mismatch`).not.toHaveBeenWarned()
  1477. })
  1478. test('transition appear with v-show', () => {
  1479. const show = false
  1480. const { vnode, container } = mountWithHydration(
  1481. `<template><div style="display: none;">foo</div></template>`,
  1482. () =>
  1483. h(
  1484. Transition,
  1485. { appear: true },
  1486. {
  1487. default: () =>
  1488. withDirectives(createVNode('div', null, 'foo'), [[vShow, show]]),
  1489. },
  1490. ),
  1491. )
  1492. expect(container.firstChild).toMatchInlineSnapshot(`
  1493. <div
  1494. class="v-enter-from v-enter-active"
  1495. style="display: none;"
  1496. >
  1497. foo
  1498. </div>
  1499. `)
  1500. expect((container.firstChild as any)[vShowOriginalDisplay]).toBe('')
  1501. expect(vnode.el).toBe(container.firstChild)
  1502. expect(`mismatch`).not.toHaveBeenWarned()
  1503. })
  1504. test('transition appear w/ event listener', async () => {
  1505. const container = document.createElement('div')
  1506. container.innerHTML = `<template><button>0</button></template>`
  1507. createSSRApp({
  1508. data() {
  1509. return {
  1510. count: 0,
  1511. }
  1512. },
  1513. template: `
  1514. <Transition appear>
  1515. <button @click="count++">{{count}}</button>
  1516. </Transition>
  1517. `,
  1518. }).mount(container)
  1519. expect(container.firstChild).toMatchInlineSnapshot(`
  1520. <button
  1521. class="v-enter-from v-enter-active"
  1522. >
  1523. 0
  1524. </button>
  1525. `)
  1526. triggerEvent('click', container.querySelector('button')!)
  1527. await nextTick()
  1528. expect(container.firstChild).toMatchInlineSnapshot(`
  1529. <button
  1530. class="v-enter-from v-enter-active"
  1531. >
  1532. 1
  1533. </button>
  1534. `)
  1535. })
  1536. test('Suspense + transition appear', async () => {
  1537. const { vnode, container } = mountWithHydration(
  1538. `<template><div>foo</div></template>`,
  1539. () =>
  1540. h(Suspense, {}, () =>
  1541. h(
  1542. Transition,
  1543. { appear: true },
  1544. {
  1545. default: () => h('div', 'foo'),
  1546. },
  1547. ),
  1548. ),
  1549. )
  1550. expect(vnode.el).toBe(container.firstChild)
  1551. // wait for hydration to finish
  1552. await new Promise(r => setTimeout(r))
  1553. expect(container.firstChild).toMatchInlineSnapshot(`
  1554. <div
  1555. class="v-enter-from v-enter-active"
  1556. >
  1557. foo
  1558. </div>
  1559. `)
  1560. await nextTick()
  1561. expect(vnode.el).toBe(container.firstChild)
  1562. })
  1563. // #10607
  1564. test('update component stable slot (prod + optimized mode)', async () => {
  1565. __DEV__ = false
  1566. try {
  1567. const container = document.createElement('div')
  1568. container.innerHTML = `<template><div show="false"><!--[--><div><div><!----></div></div><div>0</div><!--]--></div></template>`
  1569. const Comp = {
  1570. render(this: any) {
  1571. return (
  1572. openBlock(),
  1573. createElementBlock('div', null, [
  1574. renderSlot(this.$slots, 'default'),
  1575. ])
  1576. )
  1577. },
  1578. }
  1579. const show = ref(false)
  1580. const clicked = ref(false)
  1581. const Wrapper = {
  1582. setup() {
  1583. const items = ref<number[]>([])
  1584. onMounted(() => {
  1585. items.value = [1]
  1586. })
  1587. return () => {
  1588. return (
  1589. openBlock(),
  1590. createBlock(Comp, null, {
  1591. default: withCtx(() => [
  1592. createElementVNode('div', null, [
  1593. createElementVNode('div', null, [
  1594. clicked.value
  1595. ? (openBlock(),
  1596. createElementBlock('div', { key: 0 }, 'foo'))
  1597. : createCommentVNode('v-if', true),
  1598. ]),
  1599. ]),
  1600. createElementVNode(
  1601. 'div',
  1602. null,
  1603. items.value.length,
  1604. 1 /* TEXT */,
  1605. ),
  1606. ]),
  1607. _: 1 /* STABLE */,
  1608. })
  1609. )
  1610. }
  1611. },
  1612. }
  1613. createSSRApp({
  1614. components: { Wrapper },
  1615. data() {
  1616. return { show }
  1617. },
  1618. template: `<Wrapper :show="show"/>`,
  1619. }).mount(container)
  1620. await nextTick()
  1621. expect(container.innerHTML).toBe(
  1622. `<div show="false"><!--[--><div><div><!----></div></div><div>1</div><!--]--></div>`,
  1623. )
  1624. show.value = true
  1625. await nextTick()
  1626. expect(async () => {
  1627. clicked.value = true
  1628. await nextTick()
  1629. }).not.toThrow("Cannot read properties of null (reading 'insertBefore')")
  1630. await nextTick()
  1631. expect(container.innerHTML).toBe(
  1632. `<div show="true"><!--[--><div><div><div>foo</div></div></div><div>1</div><!--]--></div>`,
  1633. )
  1634. } catch (e) {
  1635. throw e
  1636. } finally {
  1637. __DEV__ = true
  1638. }
  1639. })
  1640. test('hmr reload child wrapped in KeepAlive', async () => {
  1641. const id = 'child-reload'
  1642. const Child = {
  1643. __hmrId: id,
  1644. template: `<div>foo</div>`,
  1645. }
  1646. createRecord(id, Child)
  1647. const appId = 'test-app-id'
  1648. const App = {
  1649. __hmrId: appId,
  1650. components: { Child },
  1651. template: `
  1652. <div>
  1653. <KeepAlive>
  1654. <Child />
  1655. </KeepAlive>
  1656. </div>
  1657. `,
  1658. }
  1659. const root = document.createElement('div')
  1660. root.innerHTML = await renderToString(h(App))
  1661. createSSRApp(App).mount(root)
  1662. expect(root.innerHTML).toBe('<div><div>foo</div></div>')
  1663. reload(id, {
  1664. __hmrId: id,
  1665. template: `<div>bar</div>`,
  1666. })
  1667. await nextTick()
  1668. expect(root.innerHTML).toBe('<div><div>bar</div></div>')
  1669. })
  1670. test('hmr root reload', async () => {
  1671. const appId = 'test-app-id'
  1672. const App = {
  1673. __hmrId: appId,
  1674. template: `<div>foo</div>`,
  1675. }
  1676. const root = document.createElement('div')
  1677. root.innerHTML = await renderToString(h(App))
  1678. createSSRApp(App).mount(root)
  1679. expect(root.innerHTML).toBe('<div>foo</div>')
  1680. reload(appId, {
  1681. __hmrId: appId,
  1682. template: `<div>bar</div>`,
  1683. })
  1684. await nextTick()
  1685. expect(root.innerHTML).toBe('<div>bar</div>')
  1686. })
  1687. describe('mismatch handling', () => {
  1688. test('text node', () => {
  1689. const { container } = mountWithHydration(`foo`, () => 'bar')
  1690. expect(container.textContent).toBe('bar')
  1691. expect(`Hydration text mismatch`).toHaveBeenWarned()
  1692. })
  1693. test('element text content', () => {
  1694. const { container } = mountWithHydration(`<div>foo</div>`, () =>
  1695. h('div', 'bar'),
  1696. )
  1697. expect(container.innerHTML).toBe('<div>bar</div>')
  1698. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1699. })
  1700. test('not enough children', () => {
  1701. const { container } = mountWithHydration(`<div></div>`, () =>
  1702. h('div', [h('span', 'foo'), h('span', 'bar')]),
  1703. )
  1704. expect(container.innerHTML).toBe(
  1705. '<div><span>foo</span><span>bar</span></div>',
  1706. )
  1707. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1708. })
  1709. test('too many children', () => {
  1710. const { container } = mountWithHydration(
  1711. `<div><span>foo</span><span>bar</span></div>`,
  1712. () => h('div', [h('span', 'foo')]),
  1713. )
  1714. expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  1715. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1716. })
  1717. test('complete mismatch', () => {
  1718. const { container } = mountWithHydration(
  1719. `<div><span>foo</span><span>bar</span></div>`,
  1720. () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  1721. )
  1722. expect(container.innerHTML).toBe('<div><div>foo</div><p>bar</p></div>')
  1723. expect(`Hydration node mismatch`).toHaveBeenWarnedTimes(2)
  1724. })
  1725. test('fragment mismatch removal', () => {
  1726. const { container } = mountWithHydration(
  1727. `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  1728. () => h('div', [h('span', 'replaced')]),
  1729. )
  1730. expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  1731. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1732. })
  1733. test('fragment not enough children', () => {
  1734. const { container } = mountWithHydration(
  1735. `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  1736. () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  1737. )
  1738. expect(container.innerHTML).toBe(
  1739. '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  1740. )
  1741. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1742. })
  1743. test('fragment too many children', () => {
  1744. const { container } = mountWithHydration(
  1745. `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  1746. () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  1747. )
  1748. expect(container.innerHTML).toBe(
  1749. '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  1750. )
  1751. // fragment ends early and attempts to hydrate the extra <div>bar</div>
  1752. // as 2nd fragment child.
  1753. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1754. // excessive children removal
  1755. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1756. })
  1757. test('Teleport target has empty children', () => {
  1758. const teleportContainer = document.createElement('div')
  1759. teleportContainer.id = 'teleport'
  1760. document.body.appendChild(teleportContainer)
  1761. mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  1762. h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  1763. )
  1764. expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  1765. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1766. })
  1767. test('comment mismatch (element)', () => {
  1768. const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  1769. h('div', [createCommentVNode('hi')]),
  1770. )
  1771. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1772. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1773. })
  1774. test('comment mismatch (text)', () => {
  1775. const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  1776. h('div', [createCommentVNode('hi')]),
  1777. )
  1778. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1779. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1780. })
  1781. test('class mismatch', () => {
  1782. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1783. h('div', { class: ['foo', 'bar'] }),
  1784. )
  1785. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1786. h('div', { class: { foo: true, bar: true } }),
  1787. )
  1788. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1789. h('div', { class: 'foo bar' }),
  1790. )
  1791. // SVG classes
  1792. mountWithHydration(`<svg class="foo bar"></svg>`, () =>
  1793. h('svg', { class: 'foo bar' }),
  1794. )
  1795. // class with different order
  1796. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1797. h('div', { class: 'bar foo' }),
  1798. )
  1799. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  1800. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1801. h('div', { class: 'foo' }),
  1802. )
  1803. expect(`Hydration class mismatch`).toHaveBeenWarned()
  1804. })
  1805. test('style mismatch', () => {
  1806. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1807. h('div', { style: { color: 'red' } }),
  1808. )
  1809. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1810. h('div', { style: `color:red;` }),
  1811. )
  1812. mountWithHydration(
  1813. `<div style="color:red; font-size: 12px;"></div>`,
  1814. () => h('div', { style: `font-size: 12px; color:red;` }),
  1815. )
  1816. mountWithHydration(`<div style="color:red;display:none;"></div>`, () =>
  1817. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1818. [vShow, false],
  1819. ]),
  1820. )
  1821. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1822. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1823. h('div', { style: { color: 'green' } }),
  1824. )
  1825. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1826. })
  1827. test('style mismatch when no style attribute is present', () => {
  1828. mountWithHydration(`<div></div>`, () =>
  1829. h('div', { style: { color: 'red' } }),
  1830. )
  1831. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1832. })
  1833. test('style mismatch w/ v-show', () => {
  1834. mountWithHydration(`<div style="color:red;display:none"></div>`, () =>
  1835. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1836. [vShow, false],
  1837. ]),
  1838. )
  1839. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1840. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1841. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1842. [vShow, false],
  1843. ]),
  1844. )
  1845. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1846. })
  1847. test('attr mismatch', () => {
  1848. mountWithHydration(`<div id="foo"></div>`, () => h('div', { id: 'foo' }))
  1849. mountWithHydration(`<div spellcheck></div>`, () =>
  1850. h('div', { spellcheck: '' }),
  1851. )
  1852. mountWithHydration(`<div></div>`, () => h('div', { id: undefined }))
  1853. // boolean
  1854. mountWithHydration(`<select multiple></div>`, () =>
  1855. h('select', { multiple: true }),
  1856. )
  1857. mountWithHydration(`<select multiple></div>`, () =>
  1858. h('select', { multiple: 'multiple' }),
  1859. )
  1860. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1861. mountWithHydration(`<div></div>`, () => h('div', { id: 'foo' }))
  1862. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  1863. mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
  1864. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  1865. })
  1866. test('attr special case: textarea value', () => {
  1867. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1868. h('textarea', { value: 'foo' }),
  1869. )
  1870. mountWithHydration(`<textarea></textarea>`, () =>
  1871. h('textarea', { value: '' }),
  1872. )
  1873. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1874. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1875. h('textarea', { value: 'bar' }),
  1876. )
  1877. expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  1878. })
  1879. // #11873
  1880. test('<textarea> with newlines at the beginning', async () => {
  1881. const render = () => h('textarea', null, '\nhello')
  1882. const html = await renderToString(createSSRApp({ render }))
  1883. mountWithHydration(html, render)
  1884. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  1885. })
  1886. test('<pre> with newlines at the beginning', async () => {
  1887. const render = () => h('pre', null, '\n')
  1888. const html = await renderToString(createSSRApp({ render }))
  1889. mountWithHydration(html, render)
  1890. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  1891. })
  1892. test('boolean attr handling', () => {
  1893. mountWithHydration(`<input />`, () => h('input', { readonly: false }))
  1894. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1895. mountWithHydration(`<input readonly />`, () =>
  1896. h('input', { readonly: true }),
  1897. )
  1898. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1899. mountWithHydration(`<input readonly="readonly" />`, () =>
  1900. h('input', { readonly: true }),
  1901. )
  1902. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1903. })
  1904. test('client value is null or undefined', () => {
  1905. mountWithHydration(`<div></div>`, () =>
  1906. h('div', { draggable: undefined }),
  1907. )
  1908. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1909. mountWithHydration(`<input />`, () => h('input', { type: null }))
  1910. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1911. })
  1912. test('should not warn against object values', () => {
  1913. mountWithHydration(`<input />`, () => h('input', { from: {} }))
  1914. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1915. })
  1916. test('should not warn on falsy bindings of non-property keys', () => {
  1917. mountWithHydration(`<button />`, () => h('button', { href: undefined }))
  1918. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1919. })
  1920. test('should not warn on non-renderable option values', () => {
  1921. mountWithHydration(`<select><option>hello</option></select>`, () =>
  1922. h('select', [h('option', { value: ['foo'] }, 'hello')]),
  1923. )
  1924. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1925. })
  1926. test('should not warn css v-bind', () => {
  1927. const container = document.createElement('div')
  1928. container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  1929. const app = createSSRApp({
  1930. setup() {
  1931. useCssVars(() => ({
  1932. foo: 'red',
  1933. }))
  1934. return () => h('div', { style: { color: 'var(--foo)' } })
  1935. },
  1936. })
  1937. app.mount(container)
  1938. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1939. })
  1940. // #10317 - test case from #10325
  1941. test('css vars should only be added to expected on component root dom', () => {
  1942. const container = document.createElement('div')
  1943. container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  1944. const app = createSSRApp({
  1945. setup() {
  1946. useCssVars(() => ({
  1947. foo: 'red',
  1948. }))
  1949. return () =>
  1950. h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
  1951. },
  1952. })
  1953. app.mount(container)
  1954. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1955. })
  1956. // #11188
  1957. test('css vars support fallthrough', () => {
  1958. const container = document.createElement('div')
  1959. container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
  1960. const app = createSSRApp({
  1961. setup() {
  1962. useCssVars(() => ({
  1963. foo: 'red',
  1964. }))
  1965. return () => h(Child)
  1966. },
  1967. })
  1968. const Child = {
  1969. setup() {
  1970. return () => h('div', { style: 'padding: 4px' })
  1971. },
  1972. }
  1973. app.mount(container)
  1974. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1975. })
  1976. // #11189
  1977. test('should not warn for directives that mutate DOM in created', () => {
  1978. const container = document.createElement('div')
  1979. container.innerHTML = `<div class="test red"></div>`
  1980. const vColor: ObjectDirective = {
  1981. created(el, binding) {
  1982. el.classList.add(binding.value)
  1983. },
  1984. }
  1985. const app = createSSRApp({
  1986. setup() {
  1987. return () =>
  1988. withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
  1989. },
  1990. })
  1991. app.mount(container)
  1992. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1993. })
  1994. test('escape css var name', () => {
  1995. const container = document.createElement('div')
  1996. container.innerHTML = `<div style="padding: 4px;--foo\\.bar:red;"></div>`
  1997. const app = createSSRApp({
  1998. setup() {
  1999. useCssVars(() => ({
  2000. 'foo.bar': 'red',
  2001. }))
  2002. return () => h(Child)
  2003. },
  2004. })
  2005. const Child = {
  2006. setup() {
  2007. return () => h('div', { style: 'padding: 4px' })
  2008. },
  2009. }
  2010. app.mount(container)
  2011. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  2012. })
  2013. })
  2014. describe('data-allow-mismatch', () => {
  2015. test('element text content', () => {
  2016. const { container } = mountWithHydration(
  2017. `<div data-allow-mismatch="text">foo</div>`,
  2018. () => h('div', 'bar'),
  2019. )
  2020. expect(container.innerHTML).toBe(
  2021. '<div data-allow-mismatch="text">bar</div>',
  2022. )
  2023. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  2024. })
  2025. test('not enough children', () => {
  2026. const { container } = mountWithHydration(
  2027. `<div data-allow-mismatch="children"></div>`,
  2028. () => h('div', [h('span', 'foo'), h('span', 'bar')]),
  2029. )
  2030. expect(container.innerHTML).toBe(
  2031. '<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>',
  2032. )
  2033. expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  2034. })
  2035. test('too many children', () => {
  2036. const { container } = mountWithHydration(
  2037. `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  2038. () => h('div', [h('span', 'foo')]),
  2039. )
  2040. expect(container.innerHTML).toBe(
  2041. '<div data-allow-mismatch="children"><span>foo</span></div>',
  2042. )
  2043. expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  2044. })
  2045. test('complete mismatch', () => {
  2046. const { container } = mountWithHydration(
  2047. `<div data-allow-mismatch="children"><span>foo</span><span>bar</span></div>`,
  2048. () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  2049. )
  2050. expect(container.innerHTML).toBe(
  2051. '<div data-allow-mismatch="children"><div>foo</div><p>bar</p></div>',
  2052. )
  2053. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2054. })
  2055. test('fragment mismatch removal', () => {
  2056. const { container } = mountWithHydration(
  2057. `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  2058. () => h('div', [h('span', 'replaced')]),
  2059. )
  2060. expect(container.innerHTML).toBe(
  2061. '<div data-allow-mismatch="children"><span>replaced</span></div>',
  2062. )
  2063. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2064. })
  2065. test('fragment not enough children', () => {
  2066. const { container } = mountWithHydration(
  2067. `<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  2068. () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  2069. )
  2070. expect(container.innerHTML).toBe(
  2071. '<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  2072. )
  2073. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2074. })
  2075. test('fragment too many children', () => {
  2076. const { container } = mountWithHydration(
  2077. `<div data-allow-mismatch="children"><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  2078. () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  2079. )
  2080. expect(container.innerHTML).toBe(
  2081. '<div data-allow-mismatch="children"><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  2082. )
  2083. // fragment ends early and attempts to hydrate the extra <div>bar</div>
  2084. // as 2nd fragment child.
  2085. expect(`Hydration text content mismatch`).not.toHaveBeenWarned()
  2086. // excessive children removal
  2087. expect(`Hydration children mismatch`).not.toHaveBeenWarned()
  2088. })
  2089. test('comment mismatch (element)', () => {
  2090. const { container } = mountWithHydration(
  2091. `<div data-allow-mismatch="children"><span></span></div>`,
  2092. () => h('div', [createCommentVNode('hi')]),
  2093. )
  2094. expect(container.innerHTML).toBe(
  2095. '<div data-allow-mismatch="children"><!--hi--></div>',
  2096. )
  2097. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2098. })
  2099. test('comment mismatch (text)', () => {
  2100. const { container } = mountWithHydration(
  2101. `<div data-allow-mismatch="children">foobar</div>`,
  2102. () => h('div', [createCommentVNode('hi')]),
  2103. )
  2104. expect(container.innerHTML).toBe(
  2105. '<div data-allow-mismatch="children"><!--hi--></div>',
  2106. )
  2107. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  2108. })
  2109. test('class mismatch', () => {
  2110. mountWithHydration(
  2111. `<div class="foo bar" data-allow-mismatch="class"></div>`,
  2112. () => h('div', { class: 'foo' }),
  2113. )
  2114. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  2115. })
  2116. test('style mismatch', () => {
  2117. mountWithHydration(
  2118. `<div style="color:red;" data-allow-mismatch="style"></div>`,
  2119. () => h('div', { style: { color: 'green' } }),
  2120. )
  2121. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  2122. })
  2123. test('attr mismatch', () => {
  2124. mountWithHydration(`<div data-allow-mismatch="attribute"></div>`, () =>
  2125. h('div', { id: 'foo' }),
  2126. )
  2127. mountWithHydration(
  2128. `<div id="bar" data-allow-mismatch="attribute"></div>`,
  2129. () => h('div', { id: 'foo' }),
  2130. )
  2131. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  2132. })
  2133. })
  2134. })