hydration.spec.ts 76 KB

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