2
0

hydration.spec.ts 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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>fooo</div>', () =>
  139. h('div', ['fo', createTextVNode('o'), 'o']),
  140. )
  141. expect(container.textContent).toBe('fooo')
  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. test('Suspense (full integration)', async () => {
  766. const mountedCalls: number[] = []
  767. const asyncDeps: Promise<any>[] = []
  768. const AsyncChild = defineComponent({
  769. props: ['n'],
  770. async setup(props) {
  771. const count = ref(props.n)
  772. onMounted(() => {
  773. mountedCalls.push(props.n)
  774. })
  775. const p = new Promise(r => setTimeout(r, props.n * 10))
  776. asyncDeps.push(p)
  777. await p
  778. return () =>
  779. h(
  780. 'span',
  781. {
  782. onClick: () => {
  783. count.value++
  784. },
  785. },
  786. count.value,
  787. )
  788. },
  789. })
  790. const done = vi.fn()
  791. const App = {
  792. template: `
  793. <Suspense @resolve="done">
  794. <div>
  795. <AsyncChild :n="1" />
  796. <AsyncChild :n="2" />
  797. </div>
  798. </Suspense>`,
  799. components: {
  800. AsyncChild,
  801. },
  802. methods: {
  803. done,
  804. },
  805. }
  806. const container = document.createElement('div')
  807. // server render
  808. container.innerHTML = await renderToString(h(App))
  809. expect(container.innerHTML).toMatchInlineSnapshot(
  810. `"<div><span>1</span><span>2</span></div>"`,
  811. )
  812. // reset asyncDeps from ssr
  813. asyncDeps.length = 0
  814. // hydrate
  815. createSSRApp(App).mount(container)
  816. expect(mountedCalls.length).toBe(0)
  817. expect(asyncDeps.length).toBe(2)
  818. // wait for hydration to complete
  819. await Promise.all(asyncDeps)
  820. await new Promise(r => setTimeout(r))
  821. // should flush buffered effects
  822. expect(mountedCalls).toMatchObject([1, 2])
  823. expect(container.innerHTML).toMatch(
  824. `<div><span>1</span><span>2</span></div>`,
  825. )
  826. const span1 = container.querySelector('span')!
  827. triggerEvent('click', span1)
  828. await nextTick()
  829. expect(container.innerHTML).toMatch(
  830. `<div><span>2</span><span>2</span></div>`,
  831. )
  832. const span2 = span1.nextSibling as Element
  833. triggerEvent('click', span2)
  834. await nextTick()
  835. expect(container.innerHTML).toMatch(
  836. `<div><span>2</span><span>3</span></div>`,
  837. )
  838. })
  839. test('async component', async () => {
  840. const spy = vi.fn()
  841. const Comp = () =>
  842. h(
  843. 'button',
  844. {
  845. onClick: spy,
  846. },
  847. 'hello!',
  848. )
  849. let serverResolve: any
  850. let AsyncComp = defineAsyncComponent(
  851. () =>
  852. new Promise(r => {
  853. serverResolve = r
  854. }),
  855. )
  856. const App = {
  857. render() {
  858. return ['hello', h(AsyncComp), 'world']
  859. },
  860. }
  861. // server render
  862. const htmlPromise = renderToString(h(App))
  863. serverResolve(Comp)
  864. const html = await htmlPromise
  865. expect(html).toMatchInlineSnapshot(
  866. `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  867. )
  868. // hydration
  869. let clientResolve: any
  870. AsyncComp = defineAsyncComponent(
  871. () =>
  872. new Promise(r => {
  873. clientResolve = r
  874. }),
  875. )
  876. const container = document.createElement('div')
  877. container.innerHTML = html
  878. createSSRApp(App).mount(container)
  879. // hydration not complete yet
  880. triggerEvent('click', container.querySelector('button')!)
  881. expect(spy).not.toHaveBeenCalled()
  882. // resolve
  883. clientResolve(Comp)
  884. await new Promise(r => setTimeout(r))
  885. // should be hydrated now
  886. triggerEvent('click', container.querySelector('button')!)
  887. expect(spy).toHaveBeenCalled()
  888. })
  889. test('update async wrapper before resolve', async () => {
  890. const Comp = {
  891. render() {
  892. return h('h1', 'Async component')
  893. },
  894. }
  895. let serverResolve: any
  896. let AsyncComp = defineAsyncComponent(
  897. () =>
  898. new Promise(r => {
  899. serverResolve = r
  900. }),
  901. )
  902. const toggle = ref(true)
  903. const App = {
  904. setup() {
  905. onMounted(() => {
  906. // change state, this makes updateComponent(AsyncComp) execute before
  907. // the async component is resolved
  908. toggle.value = false
  909. })
  910. return () => {
  911. return [toggle.value ? 'hello' : 'world', h(AsyncComp)]
  912. }
  913. },
  914. }
  915. // server render
  916. const htmlPromise = renderToString(h(App))
  917. serverResolve(Comp)
  918. const html = await htmlPromise
  919. expect(html).toMatchInlineSnapshot(
  920. `"<!--[-->hello<h1>Async component</h1><!--]-->"`,
  921. )
  922. // hydration
  923. let clientResolve: any
  924. AsyncComp = defineAsyncComponent(
  925. () =>
  926. new Promise(r => {
  927. clientResolve = r
  928. }),
  929. )
  930. const container = document.createElement('div')
  931. container.innerHTML = html
  932. createSSRApp(App).mount(container)
  933. // resolve
  934. clientResolve(Comp)
  935. await new Promise(r => setTimeout(r))
  936. // should be hydrated now
  937. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  938. expect(container.innerHTML).toMatchInlineSnapshot(
  939. `"<!--[-->world<h1>Async component</h1><!--]-->"`,
  940. )
  941. })
  942. test('hydrate safely when property used by async setup changed before render', async () => {
  943. const toggle = ref(true)
  944. const AsyncComp = {
  945. async setup() {
  946. await new Promise<void>(r => setTimeout(r, 10))
  947. return () => h('h1', 'Async component')
  948. },
  949. }
  950. const AsyncWrapper = {
  951. render() {
  952. return h(AsyncComp)
  953. },
  954. }
  955. const SiblingComp = {
  956. setup() {
  957. toggle.value = false
  958. return () => h('span')
  959. },
  960. }
  961. const App = {
  962. setup() {
  963. return () =>
  964. h(
  965. Suspense,
  966. {},
  967. {
  968. default: () => [
  969. h('main', {}, [
  970. h(AsyncWrapper, {
  971. prop: toggle.value ? 'hello' : 'world',
  972. }),
  973. h(SiblingComp),
  974. ]),
  975. ],
  976. },
  977. )
  978. },
  979. }
  980. // server render
  981. const html = await renderToString(h(App))
  982. expect(html).toMatchInlineSnapshot(
  983. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  984. )
  985. expect(toggle.value).toBe(false)
  986. // hydration
  987. // reset the value
  988. toggle.value = true
  989. expect(toggle.value).toBe(true)
  990. const container = document.createElement('div')
  991. container.innerHTML = html
  992. createSSRApp(App).mount(container)
  993. await new Promise(r => setTimeout(r, 10))
  994. expect(toggle.value).toBe(false)
  995. // should be hydrated now
  996. expect(container.innerHTML).toMatchInlineSnapshot(
  997. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  998. )
  999. })
  1000. test('hydrate safely when property used by deep nested async setup changed before render', async () => {
  1001. const toggle = ref(true)
  1002. const AsyncComp = {
  1003. async setup() {
  1004. await new Promise<void>(r => setTimeout(r, 10))
  1005. return () => h('h1', 'Async component')
  1006. },
  1007. }
  1008. const AsyncWrapper = { render: () => h(AsyncComp) }
  1009. const AsyncWrapperWrapper = { render: () => h(AsyncWrapper) }
  1010. const SiblingComp = {
  1011. setup() {
  1012. toggle.value = false
  1013. return () => h('span')
  1014. },
  1015. }
  1016. const App = {
  1017. setup() {
  1018. return () =>
  1019. h(
  1020. Suspense,
  1021. {},
  1022. {
  1023. default: () => [
  1024. h('main', {}, [
  1025. h(AsyncWrapperWrapper, {
  1026. prop: toggle.value ? 'hello' : 'world',
  1027. }),
  1028. h(SiblingComp),
  1029. ]),
  1030. ],
  1031. },
  1032. )
  1033. },
  1034. }
  1035. // server render
  1036. const html = await renderToString(h(App))
  1037. expect(html).toMatchInlineSnapshot(
  1038. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  1039. )
  1040. expect(toggle.value).toBe(false)
  1041. // hydration
  1042. // reset the value
  1043. toggle.value = true
  1044. expect(toggle.value).toBe(true)
  1045. const container = document.createElement('div')
  1046. container.innerHTML = html
  1047. createSSRApp(App).mount(container)
  1048. await new Promise(r => setTimeout(r, 10))
  1049. expect(toggle.value).toBe(false)
  1050. // should be hydrated now
  1051. expect(container.innerHTML).toMatchInlineSnapshot(
  1052. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  1053. )
  1054. })
  1055. // #3787
  1056. test('unmount async wrapper before load', async () => {
  1057. let resolve: any
  1058. const AsyncComp = defineAsyncComponent(
  1059. () =>
  1060. new Promise(r => {
  1061. resolve = r
  1062. }),
  1063. )
  1064. const show = ref(true)
  1065. const root = document.createElement('div')
  1066. root.innerHTML = '<div><div>async</div></div>'
  1067. createSSRApp({
  1068. render() {
  1069. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  1070. },
  1071. }).mount(root)
  1072. show.value = false
  1073. await nextTick()
  1074. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  1075. resolve({})
  1076. })
  1077. test('unmount async wrapper before load (fragment)', async () => {
  1078. let resolve: any
  1079. const AsyncComp = defineAsyncComponent(
  1080. () =>
  1081. new Promise(r => {
  1082. resolve = r
  1083. }),
  1084. )
  1085. const show = ref(true)
  1086. const root = document.createElement('div')
  1087. root.innerHTML = '<div><!--[-->async<!--]--></div>'
  1088. createSSRApp({
  1089. render() {
  1090. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  1091. },
  1092. }).mount(root)
  1093. show.value = false
  1094. await nextTick()
  1095. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  1096. resolve({})
  1097. })
  1098. test('elements with camel-case in svg ', () => {
  1099. const { vnode, container } = mountWithHydration(
  1100. '<animateTransform></animateTransform>',
  1101. () => h('animateTransform'),
  1102. )
  1103. expect(vnode.el).toBe(container.firstChild)
  1104. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  1105. })
  1106. test('SVG as a mount container', () => {
  1107. const svgContainer = document.createElement('svg')
  1108. svgContainer.innerHTML = '<g></g>'
  1109. const app = createSSRApp({
  1110. render: () => h('g'),
  1111. })
  1112. expect(
  1113. (
  1114. app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
  1115. el: Element
  1116. }
  1117. ).el instanceof SVGElement,
  1118. )
  1119. })
  1120. test('force hydrate prop with `.prop` modifier', () => {
  1121. const { container } = mountWithHydration('<input type="checkbox">', () =>
  1122. h('input', {
  1123. type: 'checkbox',
  1124. '.indeterminate': true,
  1125. }),
  1126. )
  1127. expect((container.firstChild! as any).indeterminate).toBe(true)
  1128. })
  1129. test('force hydrate input v-model with non-string value bindings', () => {
  1130. const { container } = mountWithHydration(
  1131. '<input type="checkbox" value="true">',
  1132. () =>
  1133. withDirectives(
  1134. createVNode(
  1135. 'input',
  1136. { type: 'checkbox', 'true-value': true },
  1137. null,
  1138. PatchFlags.PROPS,
  1139. ['true-value'],
  1140. ),
  1141. [[vModelCheckbox, true]],
  1142. ),
  1143. )
  1144. expect((container.firstChild as any)._trueValue).toBe(true)
  1145. })
  1146. test('force hydrate checkbox with indeterminate', () => {
  1147. const { container } = mountWithHydration(
  1148. '<input type="checkbox" indeterminate>',
  1149. () =>
  1150. createVNode(
  1151. 'input',
  1152. { type: 'checkbox', indeterminate: '' },
  1153. null,
  1154. PatchFlags.HOISTED,
  1155. ),
  1156. )
  1157. expect((container.firstChild as any).indeterminate).toBe(true)
  1158. })
  1159. test('force hydrate select option with non-string value bindings', () => {
  1160. const { container } = mountWithHydration(
  1161. '<select><option value="true">ok</option></select>',
  1162. () =>
  1163. h('select', [
  1164. // hoisted because bound value is a constant...
  1165. createVNode('option', { value: true }, null, -1 /* HOISTED */),
  1166. ]),
  1167. )
  1168. expect((container.firstChild!.firstChild as any)._value).toBe(true)
  1169. })
  1170. // #5728
  1171. test('empty text node in slot', () => {
  1172. const Comp = {
  1173. render(this: any) {
  1174. return renderSlot(this.$slots, 'default', {}, () => [
  1175. createTextVNode(''),
  1176. ])
  1177. },
  1178. }
  1179. const { container, vnode } = mountWithHydration('<!--[--><!--]-->', () =>
  1180. h(Comp),
  1181. )
  1182. expect(container.childNodes.length).toBe(3)
  1183. const text = container.childNodes[1]
  1184. expect(text.nodeType).toBe(3)
  1185. expect(vnode.el).toBe(container.childNodes[0])
  1186. // component => slot fragment => text node
  1187. expect((vnode as any).component?.subTree.children[0].el).toBe(text)
  1188. })
  1189. // #7215
  1190. test('empty text node', () => {
  1191. const Comp = {
  1192. render(this: any) {
  1193. return h('p', [''])
  1194. },
  1195. }
  1196. const { container } = mountWithHydration('<p></p>', () => h(Comp))
  1197. expect(container.childNodes.length).toBe(1)
  1198. const p = container.childNodes[0]
  1199. expect(p.childNodes.length).toBe(1)
  1200. const text = p.childNodes[0]
  1201. expect(text.nodeType).toBe(3)
  1202. })
  1203. // #11372
  1204. test('object style value tracking in prod', async () => {
  1205. __DEV__ = false
  1206. try {
  1207. const style = reactive({ color: 'red' })
  1208. const Comp = {
  1209. render(this: any) {
  1210. return (
  1211. openBlock(),
  1212. createElementBlock(
  1213. 'div',
  1214. {
  1215. style: normalizeStyle(style),
  1216. },
  1217. null,
  1218. 4 /* STYLE */,
  1219. )
  1220. )
  1221. },
  1222. }
  1223. const { container } = mountWithHydration(
  1224. `<div style="color: red;"></div>`,
  1225. () => h(Comp),
  1226. )
  1227. style.color = 'green'
  1228. await nextTick()
  1229. expect(container.innerHTML).toBe(`<div style="color: green;"></div>`)
  1230. } finally {
  1231. __DEV__ = true
  1232. }
  1233. })
  1234. test('app.unmount()', async () => {
  1235. const container = document.createElement('DIV')
  1236. container.innerHTML = '<button></button>'
  1237. const App = defineComponent({
  1238. setup(_, { expose }) {
  1239. const count = ref(0)
  1240. expose({ count })
  1241. return () =>
  1242. h('button', {
  1243. onClick: () => count.value++,
  1244. })
  1245. },
  1246. })
  1247. const app = createSSRApp(App)
  1248. const vm = app.mount(container)
  1249. await nextTick()
  1250. expect((container as any)._vnode).toBeDefined()
  1251. // @ts-expect-error - expose()'d properties are not available on vm type
  1252. expect(vm.count).toBe(0)
  1253. app.unmount()
  1254. expect((container as any)._vnode).toBe(null)
  1255. })
  1256. // #6637
  1257. test('stringified root fragment', () => {
  1258. mountWithHydration(`<!--[--><div></div><!--]-->`, () =>
  1259. createStaticVNode(`<div></div>`, 1),
  1260. )
  1261. expect(`mismatch`).not.toHaveBeenWarned()
  1262. })
  1263. test('transition appear', () => {
  1264. const { vnode, container } = mountWithHydration(
  1265. `<template><div>foo</div></template>`,
  1266. () =>
  1267. h(
  1268. Transition,
  1269. { appear: true },
  1270. {
  1271. default: () => h('div', 'foo'),
  1272. },
  1273. ),
  1274. )
  1275. expect(container.firstChild).toMatchInlineSnapshot(`
  1276. <div
  1277. class="v-enter-from v-enter-active"
  1278. >
  1279. foo
  1280. </div>
  1281. `)
  1282. expect(vnode.el).toBe(container.firstChild)
  1283. expect(`mismatch`).not.toHaveBeenWarned()
  1284. })
  1285. test('transition appear with v-if', () => {
  1286. const show = false
  1287. const { vnode, container } = mountWithHydration(
  1288. `<template><!----></template>`,
  1289. () =>
  1290. h(
  1291. Transition,
  1292. { appear: true },
  1293. {
  1294. default: () => (show ? h('div', 'foo') : createCommentVNode('')),
  1295. },
  1296. ),
  1297. )
  1298. expect(container.firstChild).toMatchInlineSnapshot('<!---->')
  1299. expect(vnode.el).toBe(container.firstChild)
  1300. expect(`mismatch`).not.toHaveBeenWarned()
  1301. })
  1302. test('transition appear with v-show', () => {
  1303. const show = false
  1304. const { vnode, container } = mountWithHydration(
  1305. `<template><div style="display: none;">foo</div></template>`,
  1306. () =>
  1307. h(
  1308. Transition,
  1309. { appear: true },
  1310. {
  1311. default: () =>
  1312. withDirectives(createVNode('div', null, 'foo'), [[vShow, show]]),
  1313. },
  1314. ),
  1315. )
  1316. expect(container.firstChild).toMatchInlineSnapshot(`
  1317. <div
  1318. class="v-enter-from v-enter-active"
  1319. style="display: none;"
  1320. >
  1321. foo
  1322. </div>
  1323. `)
  1324. expect((container.firstChild as any)[vShowOriginalDisplay]).toBe('')
  1325. expect(vnode.el).toBe(container.firstChild)
  1326. expect(`mismatch`).not.toHaveBeenWarned()
  1327. })
  1328. test('transition appear w/ event listener', async () => {
  1329. const container = document.createElement('div')
  1330. container.innerHTML = `<template><button>0</button></template>`
  1331. createSSRApp({
  1332. data() {
  1333. return {
  1334. count: 0,
  1335. }
  1336. },
  1337. template: `
  1338. <Transition appear>
  1339. <button @click="count++">{{count}}</button>
  1340. </Transition>
  1341. `,
  1342. }).mount(container)
  1343. expect(container.firstChild).toMatchInlineSnapshot(`
  1344. <button
  1345. class="v-enter-from v-enter-active"
  1346. >
  1347. 0
  1348. </button>
  1349. `)
  1350. triggerEvent('click', container.querySelector('button')!)
  1351. await nextTick()
  1352. expect(container.firstChild).toMatchInlineSnapshot(`
  1353. <button
  1354. class="v-enter-from v-enter-active"
  1355. >
  1356. 1
  1357. </button>
  1358. `)
  1359. })
  1360. // #10607
  1361. test('update component stable slot (prod + optimized mode)', async () => {
  1362. __DEV__ = false
  1363. try {
  1364. const container = document.createElement('div')
  1365. container.innerHTML = `<template><div show="false"><!--[--><div><div><!----></div></div><div>0</div><!--]--></div></template>`
  1366. const Comp = {
  1367. render(this: any) {
  1368. return (
  1369. openBlock(),
  1370. createElementBlock('div', null, [
  1371. renderSlot(this.$slots, 'default'),
  1372. ])
  1373. )
  1374. },
  1375. }
  1376. const show = ref(false)
  1377. const clicked = ref(false)
  1378. const Wrapper = {
  1379. setup() {
  1380. const items = ref<number[]>([])
  1381. onMounted(() => {
  1382. items.value = [1]
  1383. })
  1384. return () => {
  1385. return (
  1386. openBlock(),
  1387. createBlock(Comp, null, {
  1388. default: withCtx(() => [
  1389. createElementVNode('div', null, [
  1390. createElementVNode('div', null, [
  1391. clicked.value
  1392. ? (openBlock(),
  1393. createElementBlock('div', { key: 0 }, 'foo'))
  1394. : createCommentVNode('v-if', true),
  1395. ]),
  1396. ]),
  1397. createElementVNode(
  1398. 'div',
  1399. null,
  1400. items.value.length,
  1401. 1 /* TEXT */,
  1402. ),
  1403. ]),
  1404. _: 1 /* STABLE */,
  1405. })
  1406. )
  1407. }
  1408. },
  1409. }
  1410. createSSRApp({
  1411. components: { Wrapper },
  1412. data() {
  1413. return { show }
  1414. },
  1415. template: `<Wrapper :show="show"/>`,
  1416. }).mount(container)
  1417. await nextTick()
  1418. expect(container.innerHTML).toBe(
  1419. `<div show="false"><!--[--><div><div><!----></div></div><div>1</div><!--]--></div>`,
  1420. )
  1421. show.value = true
  1422. await nextTick()
  1423. expect(async () => {
  1424. clicked.value = true
  1425. await nextTick()
  1426. }).not.toThrow("Cannot read properties of null (reading 'insertBefore')")
  1427. await nextTick()
  1428. expect(container.innerHTML).toBe(
  1429. `<div show="true"><!--[--><div><div><div>foo</div></div></div><div>1</div><!--]--></div>`,
  1430. )
  1431. } catch (e) {
  1432. throw e
  1433. } finally {
  1434. __DEV__ = true
  1435. }
  1436. })
  1437. describe('mismatch handling', () => {
  1438. test('text node', () => {
  1439. const { container } = mountWithHydration(`foo`, () => 'bar')
  1440. expect(container.textContent).toBe('bar')
  1441. expect(`Hydration text mismatch`).toHaveBeenWarned()
  1442. })
  1443. test('element text content', () => {
  1444. const { container } = mountWithHydration(`<div>foo</div>`, () =>
  1445. h('div', 'bar'),
  1446. )
  1447. expect(container.innerHTML).toBe('<div>bar</div>')
  1448. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1449. })
  1450. test('not enough children', () => {
  1451. const { container } = mountWithHydration(`<div></div>`, () =>
  1452. h('div', [h('span', 'foo'), h('span', 'bar')]),
  1453. )
  1454. expect(container.innerHTML).toBe(
  1455. '<div><span>foo</span><span>bar</span></div>',
  1456. )
  1457. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1458. })
  1459. test('too many children', () => {
  1460. const { container } = mountWithHydration(
  1461. `<div><span>foo</span><span>bar</span></div>`,
  1462. () => h('div', [h('span', 'foo')]),
  1463. )
  1464. expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  1465. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1466. })
  1467. test('complete mismatch', () => {
  1468. const { container } = mountWithHydration(
  1469. `<div><span>foo</span><span>bar</span></div>`,
  1470. () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  1471. )
  1472. expect(container.innerHTML).toBe('<div><div>foo</div><p>bar</p></div>')
  1473. expect(`Hydration node mismatch`).toHaveBeenWarnedTimes(2)
  1474. })
  1475. test('fragment mismatch removal', () => {
  1476. const { container } = mountWithHydration(
  1477. `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  1478. () => h('div', [h('span', 'replaced')]),
  1479. )
  1480. expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  1481. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1482. })
  1483. test('fragment not enough children', () => {
  1484. const { container } = mountWithHydration(
  1485. `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  1486. () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  1487. )
  1488. expect(container.innerHTML).toBe(
  1489. '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  1490. )
  1491. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1492. })
  1493. test('fragment too many children', () => {
  1494. const { container } = mountWithHydration(
  1495. `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  1496. () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  1497. )
  1498. expect(container.innerHTML).toBe(
  1499. '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  1500. )
  1501. // fragment ends early and attempts to hydrate the extra <div>bar</div>
  1502. // as 2nd fragment child.
  1503. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1504. // excessive children removal
  1505. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1506. })
  1507. test('Teleport target has empty children', () => {
  1508. const teleportContainer = document.createElement('div')
  1509. teleportContainer.id = 'teleport'
  1510. document.body.appendChild(teleportContainer)
  1511. mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  1512. h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  1513. )
  1514. expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  1515. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1516. })
  1517. test('comment mismatch (element)', () => {
  1518. const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  1519. h('div', [createCommentVNode('hi')]),
  1520. )
  1521. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1522. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1523. })
  1524. test('comment mismatch (text)', () => {
  1525. const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  1526. h('div', [createCommentVNode('hi')]),
  1527. )
  1528. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1529. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1530. })
  1531. test('class mismatch', () => {
  1532. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1533. h('div', { class: ['foo', 'bar'] }),
  1534. )
  1535. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1536. h('div', { class: { foo: true, bar: true } }),
  1537. )
  1538. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1539. h('div', { class: 'foo bar' }),
  1540. )
  1541. // SVG classes
  1542. mountWithHydration(`<svg class="foo bar"></svg>`, () =>
  1543. h('svg', { class: 'foo bar' }),
  1544. )
  1545. // class with different order
  1546. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1547. h('div', { class: 'bar foo' }),
  1548. )
  1549. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  1550. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1551. h('div', { class: 'foo' }),
  1552. )
  1553. expect(`Hydration class mismatch`).toHaveBeenWarned()
  1554. })
  1555. test('style mismatch', () => {
  1556. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1557. h('div', { style: { color: 'red' } }),
  1558. )
  1559. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1560. h('div', { style: `color:red;` }),
  1561. )
  1562. mountWithHydration(
  1563. `<div style="color:red; font-size: 12px;"></div>`,
  1564. () => h('div', { style: `font-size: 12px; color:red;` }),
  1565. )
  1566. mountWithHydration(`<div style="color:red;display:none;"></div>`, () =>
  1567. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1568. [vShow, false],
  1569. ]),
  1570. )
  1571. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1572. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1573. h('div', { style: { color: 'green' } }),
  1574. )
  1575. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1576. })
  1577. test('style mismatch when no style attribute is present', () => {
  1578. mountWithHydration(`<div></div>`, () =>
  1579. h('div', { style: { color: 'red' } }),
  1580. )
  1581. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1582. })
  1583. test('style mismatch w/ v-show', () => {
  1584. mountWithHydration(`<div style="color:red;display:none"></div>`, () =>
  1585. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1586. [vShow, false],
  1587. ]),
  1588. )
  1589. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1590. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1591. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1592. [vShow, false],
  1593. ]),
  1594. )
  1595. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1596. })
  1597. test('attr mismatch', () => {
  1598. mountWithHydration(`<div id="foo"></div>`, () => h('div', { id: 'foo' }))
  1599. mountWithHydration(`<div spellcheck></div>`, () =>
  1600. h('div', { spellcheck: '' }),
  1601. )
  1602. mountWithHydration(`<div></div>`, () => h('div', { id: undefined }))
  1603. // boolean
  1604. mountWithHydration(`<select multiple></div>`, () =>
  1605. h('select', { multiple: true }),
  1606. )
  1607. mountWithHydration(`<select multiple></div>`, () =>
  1608. h('select', { multiple: 'multiple' }),
  1609. )
  1610. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1611. mountWithHydration(`<div></div>`, () => h('div', { id: 'foo' }))
  1612. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  1613. mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
  1614. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  1615. })
  1616. test('attr special case: textarea value', () => {
  1617. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1618. h('textarea', { value: 'foo' }),
  1619. )
  1620. mountWithHydration(`<textarea></textarea>`, () =>
  1621. h('textarea', { value: '' }),
  1622. )
  1623. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1624. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1625. h('textarea', { value: 'bar' }),
  1626. )
  1627. expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  1628. })
  1629. test('boolean attr handling', () => {
  1630. mountWithHydration(`<input />`, () => h('input', { readonly: false }))
  1631. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1632. mountWithHydration(`<input readonly />`, () =>
  1633. h('input', { readonly: true }),
  1634. )
  1635. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1636. mountWithHydration(`<input readonly="readonly" />`, () =>
  1637. h('input', { readonly: true }),
  1638. )
  1639. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1640. })
  1641. test('client value is null or undefined', () => {
  1642. mountWithHydration(`<div></div>`, () =>
  1643. h('div', { draggable: undefined }),
  1644. )
  1645. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1646. mountWithHydration(`<input />`, () => h('input', { type: null }))
  1647. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1648. })
  1649. test('should not warn against object values', () => {
  1650. mountWithHydration(`<input />`, () => h('input', { from: {} }))
  1651. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1652. })
  1653. test('should not warn on falsy bindings of non-property keys', () => {
  1654. mountWithHydration(`<button />`, () => h('button', { href: undefined }))
  1655. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1656. })
  1657. test('should not warn on non-renderable option values', () => {
  1658. mountWithHydration(`<select><option>hello</option></select>`, () =>
  1659. h('select', [h('option', { value: ['foo'] }, 'hello')]),
  1660. )
  1661. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1662. })
  1663. test('should not warn css v-bind', () => {
  1664. const container = document.createElement('div')
  1665. container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  1666. const app = createSSRApp({
  1667. setup() {
  1668. useCssVars(() => ({
  1669. foo: 'red',
  1670. }))
  1671. return () => h('div', { style: { color: 'var(--foo)' } })
  1672. },
  1673. })
  1674. app.mount(container)
  1675. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1676. })
  1677. // #10317 - test case from #10325
  1678. test('css vars should only be added to expected on component root dom', () => {
  1679. const container = document.createElement('div')
  1680. container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  1681. const app = createSSRApp({
  1682. setup() {
  1683. useCssVars(() => ({
  1684. foo: 'red',
  1685. }))
  1686. return () =>
  1687. h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
  1688. },
  1689. })
  1690. app.mount(container)
  1691. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1692. })
  1693. // #11188
  1694. test('css vars support fallthrough', () => {
  1695. const container = document.createElement('div')
  1696. container.innerHTML = `<div style="padding: 4px;--foo:red;"></div>`
  1697. const app = createSSRApp({
  1698. setup() {
  1699. useCssVars(() => ({
  1700. foo: 'red',
  1701. }))
  1702. return () => h(Child)
  1703. },
  1704. })
  1705. const Child = {
  1706. setup() {
  1707. return () => h('div', { style: 'padding: 4px' })
  1708. },
  1709. }
  1710. app.mount(container)
  1711. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1712. })
  1713. // #11189
  1714. test('should not warn for directives that mutate DOM in created', () => {
  1715. const container = document.createElement('div')
  1716. container.innerHTML = `<div class="test red"></div>`
  1717. const vColor: ObjectDirective = {
  1718. created(el, binding) {
  1719. el.classList.add(binding.value)
  1720. },
  1721. }
  1722. const app = createSSRApp({
  1723. setup() {
  1724. return () =>
  1725. withDirectives(h('div', { class: 'test' }), [[vColor, 'red']])
  1726. },
  1727. })
  1728. app.mount(container)
  1729. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1730. })
  1731. })
  1732. })