hydration.spec.ts 66 KB

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