hydration.spec.ts 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /**
  2. * @vitest-environment jsdom
  3. */
  4. import {
  5. Suspense,
  6. Teleport,
  7. Transition,
  8. type VNode,
  9. createBlock,
  10. createCommentVNode,
  11. createElementBlock,
  12. createElementVNode,
  13. createSSRApp,
  14. createStaticVNode,
  15. createTextVNode,
  16. createVNode,
  17. defineAsyncComponent,
  18. defineComponent,
  19. h,
  20. nextTick,
  21. onMounted,
  22. openBlock,
  23. ref,
  24. renderSlot,
  25. useCssVars,
  26. vModelCheckbox,
  27. vShow,
  28. withCtx,
  29. withDirectives,
  30. } from '@vue/runtime-dom'
  31. import { type SSRContext, renderToString } from '@vue/server-renderer'
  32. import { PatchFlags } from '@vue/shared'
  33. import { vShowOriginalDisplay } from '../../runtime-dom/src/directives/vShow'
  34. import { expect } from 'vitest'
  35. function mountWithHydration(html: string, render: () => any) {
  36. const container = document.createElement('div')
  37. container.innerHTML = html
  38. const app = createSSRApp({
  39. render,
  40. })
  41. return {
  42. vnode: app.mount(container).$.subTree as VNode<Node, Element> & {
  43. el: Element
  44. },
  45. container,
  46. }
  47. }
  48. const triggerEvent = (type: string, el: Element) => {
  49. const event = new Event(type)
  50. el.dispatchEvent(event)
  51. }
  52. describe('SSR hydration', () => {
  53. beforeEach(() => {
  54. document.body.innerHTML = ''
  55. })
  56. test('text', async () => {
  57. const msg = ref('foo')
  58. const { vnode, container } = mountWithHydration('foo', () => msg.value)
  59. expect(vnode.el).toBe(container.firstChild)
  60. expect(container.textContent).toBe('foo')
  61. msg.value = 'bar'
  62. await nextTick()
  63. expect(container.textContent).toBe('bar')
  64. })
  65. test('empty text', async () => {
  66. const { container } = mountWithHydration('<div></div>', () =>
  67. h('div', createTextVNode('')),
  68. )
  69. expect(container.textContent).toBe('')
  70. expect(`Hydration children mismatch in <div>`).not.toHaveBeenWarned()
  71. })
  72. test('comment', () => {
  73. const { vnode, container } = mountWithHydration('<!---->', () => null)
  74. expect(vnode.el).toBe(container.firstChild)
  75. expect(vnode.el.nodeType).toBe(8) // comment
  76. })
  77. test('static', () => {
  78. const html = '<div><span>hello</span></div>'
  79. const { vnode, container } = mountWithHydration(html, () =>
  80. createStaticVNode('', 1),
  81. )
  82. expect(vnode.el).toBe(container.firstChild)
  83. expect(vnode.el.outerHTML).toBe(html)
  84. expect(vnode.anchor).toBe(container.firstChild)
  85. expect(vnode.children).toBe(html)
  86. })
  87. test('static (multiple elements)', () => {
  88. const staticContent = '<div></div><span>hello</span>'
  89. const html = `<div><div>hi</div>` + staticContent + `<div>ho</div></div>`
  90. const n1 = h('div', 'hi')
  91. const s = createStaticVNode('', 2)
  92. const n2 = h('div', 'ho')
  93. const { container } = mountWithHydration(html, () => h('div', [n1, s, n2]))
  94. const div = container.firstChild!
  95. expect(n1.el).toBe(div.firstChild)
  96. expect(n2.el).toBe(div.lastChild)
  97. expect(s.el).toBe(div.childNodes[1])
  98. expect(s.anchor).toBe(div.childNodes[2])
  99. expect(s.children).toBe(staticContent)
  100. })
  101. // #6008
  102. test('static (with text node as starting node)', () => {
  103. const html = ` A <span>foo</span> B`
  104. const { vnode, container } = mountWithHydration(html, () =>
  105. createStaticVNode(` A <span>foo</span> B`, 3),
  106. )
  107. expect(vnode.el).toBe(container.firstChild)
  108. expect(vnode.anchor).toBe(container.lastChild)
  109. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  110. })
  111. test('static with content adoption', () => {
  112. const html = ` A <span>foo</span> B`
  113. const { vnode, container } = mountWithHydration(html, () =>
  114. createStaticVNode(``, 3),
  115. )
  116. expect(vnode.el).toBe(container.firstChild)
  117. expect(vnode.anchor).toBe(container.lastChild)
  118. expect(vnode.children).toBe(html)
  119. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  120. })
  121. test('element with text children', async () => {
  122. const msg = ref('foo')
  123. const { vnode, container } = mountWithHydration(
  124. '<div class="foo">foo</div>',
  125. () => h('div', { class: msg.value }, msg.value),
  126. )
  127. expect(vnode.el).toBe(container.firstChild)
  128. expect(container.firstChild!.textContent).toBe('foo')
  129. msg.value = 'bar'
  130. await nextTick()
  131. expect(container.innerHTML).toBe(`<div class="bar">bar</div>`)
  132. })
  133. test('element with elements children', async () => {
  134. const msg = ref('foo')
  135. const fn = vi.fn()
  136. const { vnode, container } = mountWithHydration(
  137. '<div><span>foo</span><span class="foo"></span></div>',
  138. () =>
  139. h('div', [
  140. h('span', msg.value),
  141. h('span', { class: msg.value, onClick: fn }),
  142. ]),
  143. )
  144. expect(vnode.el).toBe(container.firstChild)
  145. expect((vnode.children as VNode[])[0].el).toBe(
  146. container.firstChild!.childNodes[0],
  147. )
  148. expect((vnode.children as VNode[])[1].el).toBe(
  149. container.firstChild!.childNodes[1],
  150. )
  151. // event handler
  152. triggerEvent('click', vnode.el.querySelector('.foo')!)
  153. expect(fn).toHaveBeenCalled()
  154. msg.value = 'bar'
  155. await nextTick()
  156. expect(vnode.el.innerHTML).toBe(`<span>bar</span><span class="bar"></span>`)
  157. })
  158. test('element with ref', () => {
  159. const el = ref()
  160. const { vnode, container } = mountWithHydration('<div></div>', () =>
  161. h('div', { ref: el }),
  162. )
  163. expect(vnode.el).toBe(container.firstChild)
  164. expect(el.value).toBe(vnode.el)
  165. })
  166. test('Fragment', async () => {
  167. const msg = ref('foo')
  168. const fn = vi.fn()
  169. const { vnode, container } = mountWithHydration(
  170. '<div><!--[--><span>foo</span><!--[--><span class="foo"></span><!--]--><!--]--></div>',
  171. () =>
  172. h('div', [
  173. [
  174. h('span', msg.value),
  175. [h('span', { class: msg.value, onClick: fn })],
  176. ],
  177. ]),
  178. )
  179. expect(vnode.el).toBe(container.firstChild)
  180. expect(vnode.el.innerHTML).toBe(
  181. `<!--[--><span>foo</span><!--[--><span class="foo"></span><!--]--><!--]-->`,
  182. )
  183. // start fragment 1
  184. const fragment1 = (vnode.children as VNode[])[0]
  185. expect(fragment1.el).toBe(vnode.el.childNodes[0])
  186. const fragment1Children = fragment1.children as VNode[]
  187. // first <span>
  188. expect(fragment1Children[0].el!.tagName).toBe('SPAN')
  189. expect(fragment1Children[0].el).toBe(vnode.el.childNodes[1])
  190. // start fragment 2
  191. const fragment2 = fragment1Children[1]
  192. expect(fragment2.el).toBe(vnode.el.childNodes[2])
  193. const fragment2Children = fragment2.children as VNode[]
  194. // second <span>
  195. expect(fragment2Children[0].el!.tagName).toBe('SPAN')
  196. expect(fragment2Children[0].el).toBe(vnode.el.childNodes[3])
  197. // end fragment 2
  198. expect(fragment2.anchor).toBe(vnode.el.childNodes[4])
  199. // end fragment 1
  200. expect(fragment1.anchor).toBe(vnode.el.childNodes[5])
  201. // event handler
  202. triggerEvent('click', vnode.el.querySelector('.foo')!)
  203. expect(fn).toHaveBeenCalled()
  204. msg.value = 'bar'
  205. await nextTick()
  206. expect(vnode.el.innerHTML).toBe(
  207. `<!--[--><span>bar</span><!--[--><span class="bar"></span><!--]--><!--]-->`,
  208. )
  209. })
  210. test('Teleport', async () => {
  211. const msg = ref('foo')
  212. const fn = vi.fn()
  213. const teleportContainer = document.createElement('div')
  214. teleportContainer.id = 'teleport'
  215. teleportContainer.innerHTML = `<span>foo</span><span class="foo"></span><!--teleport anchor-->`
  216. document.body.appendChild(teleportContainer)
  217. const { vnode, container } = mountWithHydration(
  218. '<!--teleport start--><!--teleport end-->',
  219. () =>
  220. h(Teleport, { to: '#teleport' }, [
  221. h('span', msg.value),
  222. h('span', { class: msg.value, onClick: fn }),
  223. ]),
  224. )
  225. expect(vnode.el).toBe(container.firstChild)
  226. expect(vnode.anchor).toBe(container.lastChild)
  227. expect(vnode.target).toBe(teleportContainer)
  228. expect((vnode.children as VNode[])[0].el).toBe(
  229. teleportContainer.childNodes[0],
  230. )
  231. expect((vnode.children as VNode[])[1].el).toBe(
  232. teleportContainer.childNodes[1],
  233. )
  234. expect(vnode.targetAnchor).toBe(teleportContainer.childNodes[2])
  235. // event handler
  236. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  237. expect(fn).toHaveBeenCalled()
  238. msg.value = 'bar'
  239. await nextTick()
  240. expect(teleportContainer.innerHTML).toBe(
  241. `<span>bar</span><span class="bar"></span><!--teleport anchor-->`,
  242. )
  243. })
  244. test('Teleport (multiple + integration)', async () => {
  245. const msg = ref('foo')
  246. const fn1 = vi.fn()
  247. const fn2 = vi.fn()
  248. const Comp = () => [
  249. h(Teleport, { to: '#teleport2' }, [
  250. h('span', msg.value),
  251. h('span', { class: msg.value, onClick: fn1 }),
  252. ]),
  253. h(Teleport, { to: '#teleport2' }, [
  254. h('span', msg.value + '2'),
  255. h('span', { class: msg.value + '2', onClick: fn2 }),
  256. ]),
  257. ]
  258. const teleportContainer = document.createElement('div')
  259. teleportContainer.id = 'teleport2'
  260. const ctx: SSRContext = {}
  261. const mainHtml = await renderToString(h(Comp), ctx)
  262. expect(mainHtml).toMatchInlineSnapshot(
  263. `"<!--[--><!--teleport start--><!--teleport end--><!--teleport start--><!--teleport end--><!--]-->"`,
  264. )
  265. const teleportHtml = ctx.teleports!['#teleport2']
  266. expect(teleportHtml).toMatchInlineSnapshot(
  267. `"<span>foo</span><span class="foo"></span><!--teleport anchor--><span>foo2</span><span class="foo2"></span><!--teleport anchor-->"`,
  268. )
  269. teleportContainer.innerHTML = teleportHtml
  270. document.body.appendChild(teleportContainer)
  271. const { vnode, container } = mountWithHydration(mainHtml, Comp)
  272. expect(vnode.el).toBe(container.firstChild)
  273. const teleportVnode1 = (vnode.children as VNode[])[0]
  274. const teleportVnode2 = (vnode.children as VNode[])[1]
  275. expect(teleportVnode1.el).toBe(container.childNodes[1])
  276. expect(teleportVnode1.anchor).toBe(container.childNodes[2])
  277. expect(teleportVnode2.el).toBe(container.childNodes[3])
  278. expect(teleportVnode2.anchor).toBe(container.childNodes[4])
  279. expect(teleportVnode1.target).toBe(teleportContainer)
  280. expect((teleportVnode1 as any).children[0].el).toBe(
  281. teleportContainer.childNodes[0],
  282. )
  283. expect(teleportVnode1.targetAnchor).toBe(teleportContainer.childNodes[2])
  284. expect(teleportVnode2.target).toBe(teleportContainer)
  285. expect((teleportVnode2 as any).children[0].el).toBe(
  286. teleportContainer.childNodes[3],
  287. )
  288. expect(teleportVnode2.targetAnchor).toBe(teleportContainer.childNodes[5])
  289. // // event handler
  290. triggerEvent('click', teleportContainer.querySelector('.foo')!)
  291. expect(fn1).toHaveBeenCalled()
  292. triggerEvent('click', teleportContainer.querySelector('.foo2')!)
  293. expect(fn2).toHaveBeenCalled()
  294. msg.value = 'bar'
  295. await nextTick()
  296. expect(teleportContainer.innerHTML).toMatchInlineSnapshot(
  297. `"<span>bar</span><span class="bar"></span><!--teleport anchor--><span>bar2</span><span class="bar2"></span><!--teleport anchor-->"`,
  298. )
  299. })
  300. test('Teleport (disabled)', async () => {
  301. const msg = ref('foo')
  302. const fn1 = vi.fn()
  303. const fn2 = vi.fn()
  304. const Comp = () => [
  305. h('div', 'foo'),
  306. h(Teleport, { to: '#teleport3', disabled: true }, [
  307. h('span', msg.value),
  308. h('span', { class: msg.value, onClick: fn1 }),
  309. ]),
  310. h('div', { class: msg.value + '2', onClick: fn2 }, 'bar'),
  311. ]
  312. const teleportContainer = document.createElement('div')
  313. teleportContainer.id = 'teleport3'
  314. const ctx: SSRContext = {}
  315. const mainHtml = await renderToString(h(Comp), ctx)
  316. expect(mainHtml).toMatchInlineSnapshot(
  317. `"<!--[--><div>foo</div><!--teleport start--><span>foo</span><span class="foo"></span><!--teleport end--><div class="foo2">bar</div><!--]-->"`,
  318. )
  319. const teleportHtml = ctx.teleports!['#teleport3']
  320. expect(teleportHtml).toMatchInlineSnapshot(`"<!--teleport anchor-->"`)
  321. teleportContainer.innerHTML = teleportHtml
  322. document.body.appendChild(teleportContainer)
  323. const { vnode, container } = mountWithHydration(mainHtml, Comp)
  324. expect(vnode.el).toBe(container.firstChild)
  325. const children = vnode.children as VNode[]
  326. expect(children[0].el).toBe(container.childNodes[1])
  327. const teleportVnode = children[1]
  328. expect(teleportVnode.el).toBe(container.childNodes[2])
  329. expect((teleportVnode.children as VNode[])[0].el).toBe(
  330. container.childNodes[3],
  331. )
  332. expect((teleportVnode.children as VNode[])[1].el).toBe(
  333. container.childNodes[4],
  334. )
  335. expect(teleportVnode.anchor).toBe(container.childNodes[5])
  336. expect(children[2].el).toBe(container.childNodes[6])
  337. expect(teleportVnode.target).toBe(teleportContainer)
  338. expect(teleportVnode.targetAnchor).toBe(teleportContainer.childNodes[0])
  339. // // event handler
  340. triggerEvent('click', container.querySelector('.foo')!)
  341. expect(fn1).toHaveBeenCalled()
  342. triggerEvent('click', container.querySelector('.foo2')!)
  343. expect(fn2).toHaveBeenCalled()
  344. msg.value = 'bar'
  345. await nextTick()
  346. expect(container.innerHTML).toMatchInlineSnapshot(
  347. `"<!--[--><div>foo</div><!--teleport start--><span>bar</span><span class="bar"></span><!--teleport end--><div class="bar2">bar</div><!--]-->"`,
  348. )
  349. })
  350. // #6152
  351. test('Teleport (disabled + as component root)', () => {
  352. const { container } = mountWithHydration(
  353. '<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
  354. () => [
  355. h('div', 'Parent fragment'),
  356. h(() =>
  357. h(Teleport, { to: 'body', disabled: true }, [
  358. h('div', 'Teleport content'),
  359. ]),
  360. ),
  361. ],
  362. )
  363. expect(document.body.innerHTML).toBe('')
  364. expect(container.innerHTML).toBe(
  365. '<!--[--><div>Parent fragment</div><!--teleport start--><div>Teleport content</div><!--teleport end--><!--]-->',
  366. )
  367. expect(
  368. `Hydration completed but contains mismatches.`,
  369. ).not.toHaveBeenWarned()
  370. })
  371. test('Teleport (as component root)', () => {
  372. const teleportContainer = document.createElement('div')
  373. teleportContainer.id = 'teleport4'
  374. teleportContainer.innerHTML = `hello<!--teleport anchor-->`
  375. document.body.appendChild(teleportContainer)
  376. const wrapper = {
  377. render() {
  378. return h(Teleport, { to: '#teleport4' }, ['hello'])
  379. },
  380. }
  381. const { vnode, container } = mountWithHydration(
  382. '<div><!--teleport start--><!--teleport end--><div></div></div>',
  383. () => h('div', [h(wrapper), h('div')]),
  384. )
  385. expect(vnode.el).toBe(container.firstChild)
  386. // component el
  387. const wrapperVNode = (vnode as any).children[0]
  388. const tpStart = container.firstChild?.firstChild
  389. const tpEnd = tpStart?.nextSibling
  390. expect(wrapperVNode.el).toBe(tpStart)
  391. expect(wrapperVNode.component.subTree.el).toBe(tpStart)
  392. expect(wrapperVNode.component.subTree.anchor).toBe(tpEnd)
  393. // next node hydrate properly
  394. const nextVNode = (vnode as any).children[1]
  395. expect(nextVNode.el).toBe(container.firstChild?.lastChild)
  396. })
  397. test('Teleport (nested)', () => {
  398. const teleportContainer = document.createElement('div')
  399. teleportContainer.id = 'teleport5'
  400. teleportContainer.innerHTML = `<div><!--teleport start--><!--teleport end--></div><!--teleport anchor--><div>child</div><!--teleport anchor-->`
  401. document.body.appendChild(teleportContainer)
  402. const { vnode, container } = mountWithHydration(
  403. '<!--teleport start--><!--teleport end-->',
  404. () =>
  405. h(Teleport, { to: '#teleport5' }, [
  406. h('div', [h(Teleport, { to: '#teleport5' }, [h('div', 'child')])]),
  407. ]),
  408. )
  409. expect(vnode.el).toBe(container.firstChild)
  410. expect(vnode.anchor).toBe(container.lastChild)
  411. const childDivVNode = (vnode as any).children[0]
  412. const div = teleportContainer.firstChild
  413. expect(childDivVNode.el).toBe(div)
  414. expect(vnode.targetAnchor).toBe(div?.nextSibling)
  415. const childTeleportVNode = childDivVNode.children[0]
  416. expect(childTeleportVNode.el).toBe(div?.firstChild)
  417. expect(childTeleportVNode.anchor).toBe(div?.lastChild)
  418. expect(childTeleportVNode.targetAnchor).toBe(teleportContainer.lastChild)
  419. expect(childTeleportVNode.children[0].el).toBe(
  420. teleportContainer.lastChild?.previousSibling,
  421. )
  422. })
  423. // compile SSR + client render fn from the same template & hydrate
  424. test('full compiler integration', async () => {
  425. const mounted: string[] = []
  426. const log = vi.fn()
  427. const toggle = ref(true)
  428. const Child = {
  429. data() {
  430. return {
  431. count: 0,
  432. text: 'hello',
  433. style: {
  434. color: 'red',
  435. },
  436. }
  437. },
  438. mounted() {
  439. mounted.push('child')
  440. },
  441. template: `
  442. <div>
  443. <span class="count" :style="style">{{ count }}</span>
  444. <button class="inc" @click="count++">inc</button>
  445. <button class="change" @click="style.color = 'green'" >change color</button>
  446. <button class="emit" @click="$emit('foo')">emit</button>
  447. <span class="text">{{ text }}</span>
  448. <input v-model="text">
  449. </div>
  450. `,
  451. }
  452. const App = {
  453. setup() {
  454. return { toggle }
  455. },
  456. mounted() {
  457. mounted.push('parent')
  458. },
  459. template: `
  460. <div>
  461. <span>hello</span>
  462. <template v-if="toggle">
  463. <Child @foo="log('child')"/>
  464. <template v-if="true">
  465. <button class="parent-click" @click="log('click')">click me</button>
  466. </template>
  467. </template>
  468. <span>hello</span>
  469. </div>`,
  470. components: {
  471. Child,
  472. },
  473. methods: {
  474. log,
  475. },
  476. }
  477. const container = document.createElement('div')
  478. // server render
  479. container.innerHTML = await renderToString(h(App))
  480. // hydrate
  481. createSSRApp(App).mount(container)
  482. // assert interactions
  483. // 1. parent button click
  484. triggerEvent('click', container.querySelector('.parent-click')!)
  485. expect(log).toHaveBeenCalledWith('click')
  486. // 2. child inc click + text interpolation
  487. const count = container.querySelector('.count') as HTMLElement
  488. expect(count.textContent).toBe(`0`)
  489. triggerEvent('click', container.querySelector('.inc')!)
  490. await nextTick()
  491. expect(count.textContent).toBe(`1`)
  492. // 3. child color click + style binding
  493. expect(count.style.color).toBe('red')
  494. triggerEvent('click', container.querySelector('.change')!)
  495. await nextTick()
  496. expect(count.style.color).toBe('green')
  497. // 4. child event emit
  498. triggerEvent('click', container.querySelector('.emit')!)
  499. expect(log).toHaveBeenCalledWith('child')
  500. // 5. child v-model
  501. const text = container.querySelector('.text')!
  502. const input = container.querySelector('input')!
  503. expect(text.textContent).toBe('hello')
  504. input.value = 'bye'
  505. triggerEvent('input', input)
  506. await nextTick()
  507. expect(text.textContent).toBe('bye')
  508. })
  509. test('handle click error in ssr mode', async () => {
  510. const App = {
  511. setup() {
  512. const throwError = () => {
  513. throw new Error('Sentry Error')
  514. }
  515. return { throwError }
  516. },
  517. template: `
  518. <div>
  519. <button class="parent-click" @click="throwError">click me</button>
  520. </div>`,
  521. }
  522. const container = document.createElement('div')
  523. // server render
  524. container.innerHTML = await renderToString(h(App))
  525. // hydrate
  526. const app = createSSRApp(App)
  527. const handler = (app.config.errorHandler = vi.fn())
  528. app.mount(container)
  529. // assert interactions
  530. // parent button click
  531. triggerEvent('click', container.querySelector('.parent-click')!)
  532. expect(handler).toHaveBeenCalled()
  533. })
  534. test('handle blur error in ssr mode', async () => {
  535. const App = {
  536. setup() {
  537. const throwError = () => {
  538. throw new Error('Sentry Error')
  539. }
  540. return { throwError }
  541. },
  542. template: `
  543. <div>
  544. <input class="parent-click" @blur="throwError"/>
  545. </div>`,
  546. }
  547. const container = document.createElement('div')
  548. // server render
  549. container.innerHTML = await renderToString(h(App))
  550. // hydrate
  551. const app = createSSRApp(App)
  552. const handler = (app.config.errorHandler = vi.fn())
  553. app.mount(container)
  554. // assert interactions
  555. // parent blur event
  556. triggerEvent('blur', container.querySelector('.parent-click')!)
  557. expect(handler).toHaveBeenCalled()
  558. })
  559. test('Suspense', async () => {
  560. const AsyncChild = {
  561. async setup() {
  562. const count = ref(0)
  563. return () =>
  564. h(
  565. 'span',
  566. {
  567. onClick: () => {
  568. count.value++
  569. },
  570. },
  571. count.value,
  572. )
  573. },
  574. }
  575. const { vnode, container } = mountWithHydration('<span>0</span>', () =>
  576. h(Suspense, () => h(AsyncChild)),
  577. )
  578. expect(vnode.el).toBe(container.firstChild)
  579. // wait for hydration to finish
  580. await new Promise(r => setTimeout(r))
  581. triggerEvent('click', container.querySelector('span')!)
  582. await nextTick()
  583. expect(container.innerHTML).toBe(`<span>1</span>`)
  584. })
  585. test('Suspense (full integration)', async () => {
  586. const mountedCalls: number[] = []
  587. const asyncDeps: Promise<any>[] = []
  588. const AsyncChild = defineComponent({
  589. props: ['n'],
  590. async setup(props) {
  591. const count = ref(props.n)
  592. onMounted(() => {
  593. mountedCalls.push(props.n)
  594. })
  595. const p = new Promise(r => setTimeout(r, props.n * 10))
  596. asyncDeps.push(p)
  597. await p
  598. return () =>
  599. h(
  600. 'span',
  601. {
  602. onClick: () => {
  603. count.value++
  604. },
  605. },
  606. count.value,
  607. )
  608. },
  609. })
  610. const done = vi.fn()
  611. const App = {
  612. template: `
  613. <Suspense @resolve="done">
  614. <div>
  615. <AsyncChild :n="1" />
  616. <AsyncChild :n="2" />
  617. </div>
  618. </Suspense>`,
  619. components: {
  620. AsyncChild,
  621. },
  622. methods: {
  623. done,
  624. },
  625. }
  626. const container = document.createElement('div')
  627. // server render
  628. container.innerHTML = await renderToString(h(App))
  629. expect(container.innerHTML).toMatchInlineSnapshot(
  630. `"<div><span>1</span><span>2</span></div>"`,
  631. )
  632. // reset asyncDeps from ssr
  633. asyncDeps.length = 0
  634. // hydrate
  635. createSSRApp(App).mount(container)
  636. expect(mountedCalls.length).toBe(0)
  637. expect(asyncDeps.length).toBe(2)
  638. // wait for hydration to complete
  639. await Promise.all(asyncDeps)
  640. await new Promise(r => setTimeout(r))
  641. // should flush buffered effects
  642. expect(mountedCalls).toMatchObject([1, 2])
  643. expect(container.innerHTML).toMatch(
  644. `<div><span>1</span><span>2</span></div>`,
  645. )
  646. const span1 = container.querySelector('span')!
  647. triggerEvent('click', span1)
  648. await nextTick()
  649. expect(container.innerHTML).toMatch(
  650. `<div><span>2</span><span>2</span></div>`,
  651. )
  652. const span2 = span1.nextSibling as Element
  653. triggerEvent('click', span2)
  654. await nextTick()
  655. expect(container.innerHTML).toMatch(
  656. `<div><span>2</span><span>3</span></div>`,
  657. )
  658. })
  659. test('async component', async () => {
  660. const spy = vi.fn()
  661. const Comp = () =>
  662. h(
  663. 'button',
  664. {
  665. onClick: spy,
  666. },
  667. 'hello!',
  668. )
  669. let serverResolve: any
  670. let AsyncComp = defineAsyncComponent(
  671. () =>
  672. new Promise(r => {
  673. serverResolve = r
  674. }),
  675. )
  676. const App = {
  677. render() {
  678. return ['hello', h(AsyncComp), 'world']
  679. },
  680. }
  681. // server render
  682. const htmlPromise = renderToString(h(App))
  683. serverResolve(Comp)
  684. const html = await htmlPromise
  685. expect(html).toMatchInlineSnapshot(
  686. `"<!--[-->hello<button>hello!</button>world<!--]-->"`,
  687. )
  688. // hydration
  689. let clientResolve: any
  690. AsyncComp = defineAsyncComponent(
  691. () =>
  692. new Promise(r => {
  693. clientResolve = r
  694. }),
  695. )
  696. const container = document.createElement('div')
  697. container.innerHTML = html
  698. createSSRApp(App).mount(container)
  699. // hydration not complete yet
  700. triggerEvent('click', container.querySelector('button')!)
  701. expect(spy).not.toHaveBeenCalled()
  702. // resolve
  703. clientResolve(Comp)
  704. await new Promise(r => setTimeout(r))
  705. // should be hydrated now
  706. triggerEvent('click', container.querySelector('button')!)
  707. expect(spy).toHaveBeenCalled()
  708. })
  709. test('update async wrapper before resolve', async () => {
  710. const Comp = {
  711. render() {
  712. return h('h1', 'Async component')
  713. },
  714. }
  715. let serverResolve: any
  716. let AsyncComp = defineAsyncComponent(
  717. () =>
  718. new Promise(r => {
  719. serverResolve = r
  720. }),
  721. )
  722. const toggle = ref(true)
  723. const App = {
  724. setup() {
  725. onMounted(() => {
  726. // change state, this makes updateComponent(AsyncComp) execute before
  727. // the async component is resolved
  728. toggle.value = false
  729. })
  730. return () => {
  731. return [toggle.value ? 'hello' : 'world', h(AsyncComp)]
  732. }
  733. },
  734. }
  735. // server render
  736. const htmlPromise = renderToString(h(App))
  737. serverResolve(Comp)
  738. const html = await htmlPromise
  739. expect(html).toMatchInlineSnapshot(
  740. `"<!--[-->hello<h1>Async component</h1><!--]-->"`,
  741. )
  742. // hydration
  743. let clientResolve: any
  744. AsyncComp = defineAsyncComponent(
  745. () =>
  746. new Promise(r => {
  747. clientResolve = r
  748. }),
  749. )
  750. const container = document.createElement('div')
  751. container.innerHTML = html
  752. createSSRApp(App).mount(container)
  753. // resolve
  754. clientResolve(Comp)
  755. await new Promise(r => setTimeout(r))
  756. // should be hydrated now
  757. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  758. expect(container.innerHTML).toMatchInlineSnapshot(
  759. `"<!--[-->world<h1>Async component</h1><!--]-->"`,
  760. )
  761. })
  762. test('hydrate safely when property used by async setup changed before render', async () => {
  763. const toggle = ref(true)
  764. const AsyncComp = {
  765. async setup() {
  766. await new Promise<void>(r => setTimeout(r, 10))
  767. return () => h('h1', 'Async component')
  768. },
  769. }
  770. const AsyncWrapper = {
  771. render() {
  772. return h(AsyncComp)
  773. },
  774. }
  775. const SiblingComp = {
  776. setup() {
  777. toggle.value = false
  778. return () => h('span')
  779. },
  780. }
  781. const App = {
  782. setup() {
  783. return () =>
  784. h(
  785. Suspense,
  786. {},
  787. {
  788. default: () => [
  789. h('main', {}, [
  790. h(AsyncWrapper, {
  791. prop: toggle.value ? 'hello' : 'world',
  792. }),
  793. h(SiblingComp),
  794. ]),
  795. ],
  796. },
  797. )
  798. },
  799. }
  800. // server render
  801. const html = await renderToString(h(App))
  802. expect(html).toMatchInlineSnapshot(
  803. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  804. )
  805. expect(toggle.value).toBe(false)
  806. // hydration
  807. // reset the value
  808. toggle.value = true
  809. expect(toggle.value).toBe(true)
  810. const container = document.createElement('div')
  811. container.innerHTML = html
  812. createSSRApp(App).mount(container)
  813. await new Promise(r => setTimeout(r, 10))
  814. expect(toggle.value).toBe(false)
  815. // should be hydrated now
  816. expect(container.innerHTML).toMatchInlineSnapshot(
  817. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  818. )
  819. })
  820. test('hydrate safely when property used by deep nested async setup changed before render', async () => {
  821. const toggle = ref(true)
  822. const AsyncComp = {
  823. async setup() {
  824. await new Promise<void>(r => setTimeout(r, 10))
  825. return () => h('h1', 'Async component')
  826. },
  827. }
  828. const AsyncWrapper = { render: () => h(AsyncComp) }
  829. const AsyncWrapperWrapper = { render: () => h(AsyncWrapper) }
  830. const SiblingComp = {
  831. setup() {
  832. toggle.value = false
  833. return () => h('span')
  834. },
  835. }
  836. const App = {
  837. setup() {
  838. return () =>
  839. h(
  840. Suspense,
  841. {},
  842. {
  843. default: () => [
  844. h('main', {}, [
  845. h(AsyncWrapperWrapper, {
  846. prop: toggle.value ? 'hello' : 'world',
  847. }),
  848. h(SiblingComp),
  849. ]),
  850. ],
  851. },
  852. )
  853. },
  854. }
  855. // server render
  856. const html = await renderToString(h(App))
  857. expect(html).toMatchInlineSnapshot(
  858. `"<main><h1 prop="hello">Async component</h1><span></span></main>"`,
  859. )
  860. expect(toggle.value).toBe(false)
  861. // hydration
  862. // reset the value
  863. toggle.value = true
  864. expect(toggle.value).toBe(true)
  865. const container = document.createElement('div')
  866. container.innerHTML = html
  867. createSSRApp(App).mount(container)
  868. await new Promise(r => setTimeout(r, 10))
  869. expect(toggle.value).toBe(false)
  870. // should be hydrated now
  871. expect(container.innerHTML).toMatchInlineSnapshot(
  872. `"<main><h1 prop="world">Async component</h1><span></span></main>"`,
  873. )
  874. })
  875. // #3787
  876. test('unmount async wrapper before load', async () => {
  877. let resolve: any
  878. const AsyncComp = defineAsyncComponent(
  879. () =>
  880. new Promise(r => {
  881. resolve = r
  882. }),
  883. )
  884. const show = ref(true)
  885. const root = document.createElement('div')
  886. root.innerHTML = '<div><div>async</div></div>'
  887. createSSRApp({
  888. render() {
  889. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  890. },
  891. }).mount(root)
  892. show.value = false
  893. await nextTick()
  894. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  895. resolve({})
  896. })
  897. test('unmount async wrapper before load (fragment)', async () => {
  898. let resolve: any
  899. const AsyncComp = defineAsyncComponent(
  900. () =>
  901. new Promise(r => {
  902. resolve = r
  903. }),
  904. )
  905. const show = ref(true)
  906. const root = document.createElement('div')
  907. root.innerHTML = '<div><!--[-->async<!--]--></div>'
  908. createSSRApp({
  909. render() {
  910. return h('div', [show.value ? h(AsyncComp) : h('div', 'hi')])
  911. },
  912. }).mount(root)
  913. show.value = false
  914. await nextTick()
  915. expect(root.innerHTML).toBe('<div><div>hi</div></div>')
  916. resolve({})
  917. })
  918. test('elements with camel-case in svg ', () => {
  919. const { vnode, container } = mountWithHydration(
  920. '<animateTransform></animateTransform>',
  921. () => h('animateTransform'),
  922. )
  923. expect(vnode.el).toBe(container.firstChild)
  924. expect(`Hydration node mismatch`).not.toHaveBeenWarned()
  925. })
  926. test('SVG as a mount container', () => {
  927. const svgContainer = document.createElement('svg')
  928. svgContainer.innerHTML = '<g></g>'
  929. const app = createSSRApp({
  930. render: () => h('g'),
  931. })
  932. expect(
  933. (
  934. app.mount(svgContainer).$.subTree as VNode<Node, Element> & {
  935. el: Element
  936. }
  937. ).el instanceof SVGElement,
  938. )
  939. })
  940. test('force hydrate prop with `.prop` modifier', () => {
  941. const { container } = mountWithHydration('<input type="checkbox">', () =>
  942. h('input', {
  943. type: 'checkbox',
  944. '.indeterminate': true,
  945. }),
  946. )
  947. expect((container.firstChild! as any).indeterminate).toBe(true)
  948. })
  949. test('force hydrate input v-model with non-string value bindings', () => {
  950. const { container } = mountWithHydration(
  951. '<input type="checkbox" value="true">',
  952. () =>
  953. withDirectives(
  954. createVNode(
  955. 'input',
  956. { type: 'checkbox', 'true-value': true },
  957. null,
  958. PatchFlags.PROPS,
  959. ['true-value'],
  960. ),
  961. [[vModelCheckbox, true]],
  962. ),
  963. )
  964. expect((container.firstChild as any)._trueValue).toBe(true)
  965. })
  966. test('force hydrate checkbox with indeterminate', () => {
  967. const { container } = mountWithHydration(
  968. '<input type="checkbox" indeterminate>',
  969. () =>
  970. createVNode(
  971. 'input',
  972. { type: 'checkbox', indeterminate: '' },
  973. null,
  974. PatchFlags.HOISTED,
  975. ),
  976. )
  977. expect((container.firstChild as any).indeterminate).toBe(true)
  978. })
  979. test('force hydrate select option with non-string value bindings', () => {
  980. const { container } = mountWithHydration(
  981. '<select><option value="true">ok</option></select>',
  982. () =>
  983. h('select', [
  984. // hoisted because bound value is a constant...
  985. createVNode('option', { value: true }, null, -1 /* HOISTED */),
  986. ]),
  987. )
  988. expect((container.firstChild!.firstChild as any)._value).toBe(true)
  989. })
  990. // #5728
  991. test('empty text node in slot', () => {
  992. const Comp = {
  993. render(this: any) {
  994. return renderSlot(this.$slots, 'default', {}, () => [
  995. createTextVNode(''),
  996. ])
  997. },
  998. }
  999. const { container, vnode } = mountWithHydration('<!--[--><!--]-->', () =>
  1000. h(Comp),
  1001. )
  1002. expect(container.childNodes.length).toBe(3)
  1003. const text = container.childNodes[1]
  1004. expect(text.nodeType).toBe(3)
  1005. expect(vnode.el).toBe(container.childNodes[0])
  1006. // component => slot fragment => text node
  1007. expect((vnode as any).component?.subTree.children[0].el).toBe(text)
  1008. })
  1009. test('app.unmount()', async () => {
  1010. const container = document.createElement('DIV')
  1011. container.innerHTML = '<button></button>'
  1012. const App = defineComponent({
  1013. setup(_, { expose }) {
  1014. const count = ref(0)
  1015. expose({ count })
  1016. return () =>
  1017. h('button', {
  1018. onClick: () => count.value++,
  1019. })
  1020. },
  1021. })
  1022. const app = createSSRApp(App)
  1023. const vm = app.mount(container)
  1024. await nextTick()
  1025. expect((container as any)._vnode).toBeDefined()
  1026. // @ts-expect-error - expose()'d properties are not available on vm type
  1027. expect(vm.count).toBe(0)
  1028. app.unmount()
  1029. expect((container as any)._vnode).toBe(null)
  1030. })
  1031. // #6637
  1032. test('stringified root fragment', () => {
  1033. mountWithHydration(`<!--[--><div></div><!--]-->`, () =>
  1034. createStaticVNode(`<div></div>`, 1),
  1035. )
  1036. expect(`mismatch`).not.toHaveBeenWarned()
  1037. })
  1038. test('transition appear', () => {
  1039. const { vnode, container } = mountWithHydration(
  1040. `<template><div>foo</div></template>`,
  1041. () =>
  1042. h(
  1043. Transition,
  1044. { appear: true },
  1045. {
  1046. default: () => h('div', 'foo'),
  1047. },
  1048. ),
  1049. )
  1050. expect(container.firstChild).toMatchInlineSnapshot(`
  1051. <div
  1052. class="v-enter-from v-enter-active"
  1053. >
  1054. foo
  1055. </div>
  1056. `)
  1057. expect(vnode.el).toBe(container.firstChild)
  1058. expect(`mismatch`).not.toHaveBeenWarned()
  1059. })
  1060. test('transition appear with v-if', () => {
  1061. const show = false
  1062. const { vnode, container } = mountWithHydration(
  1063. `<template><!----></template>`,
  1064. () =>
  1065. h(
  1066. Transition,
  1067. { appear: true },
  1068. {
  1069. default: () => (show ? h('div', 'foo') : createCommentVNode('')),
  1070. },
  1071. ),
  1072. )
  1073. expect(container.firstChild).toMatchInlineSnapshot('<!---->')
  1074. expect(vnode.el).toBe(container.firstChild)
  1075. expect(`mismatch`).not.toHaveBeenWarned()
  1076. })
  1077. test('transition appear with v-show', () => {
  1078. const show = false
  1079. const { vnode, container } = mountWithHydration(
  1080. `<template><div style="display: none;">foo</div></template>`,
  1081. () =>
  1082. h(
  1083. Transition,
  1084. { appear: true },
  1085. {
  1086. default: () =>
  1087. withDirectives(createVNode('div', null, 'foo'), [[vShow, show]]),
  1088. },
  1089. ),
  1090. )
  1091. expect(container.firstChild).toMatchInlineSnapshot(`
  1092. <div
  1093. class="v-enter-from v-enter-active"
  1094. style="display: none;"
  1095. >
  1096. foo
  1097. </div>
  1098. `)
  1099. expect((container.firstChild as any)[vShowOriginalDisplay]).toBe('')
  1100. expect(vnode.el).toBe(container.firstChild)
  1101. expect(`mismatch`).not.toHaveBeenWarned()
  1102. })
  1103. test('transition appear w/ event listener', async () => {
  1104. const container = document.createElement('div')
  1105. container.innerHTML = `<template><button>0</button></template>`
  1106. createSSRApp({
  1107. data() {
  1108. return {
  1109. count: 0,
  1110. }
  1111. },
  1112. template: `
  1113. <Transition appear>
  1114. <button @click="count++">{{count}}</button>
  1115. </Transition>
  1116. `,
  1117. }).mount(container)
  1118. expect(container.firstChild).toMatchInlineSnapshot(`
  1119. <button
  1120. class="v-enter-from v-enter-active"
  1121. >
  1122. 0
  1123. </button>
  1124. `)
  1125. triggerEvent('click', container.querySelector('button')!)
  1126. await nextTick()
  1127. expect(container.firstChild).toMatchInlineSnapshot(`
  1128. <button
  1129. class="v-enter-from v-enter-active"
  1130. >
  1131. 1
  1132. </button>
  1133. `)
  1134. })
  1135. // #10607
  1136. test('update component stable slot (prod + optimized mode)', async () => {
  1137. __DEV__ = false
  1138. const container = document.createElement('div')
  1139. container.innerHTML = `<template><div show="false"><!--[--><div><div><!----></div></div><div>0</div><!--]--></div></template>`
  1140. const Comp = {
  1141. render(this: any) {
  1142. return (
  1143. openBlock(),
  1144. createElementBlock('div', null, [renderSlot(this.$slots, 'default')])
  1145. )
  1146. },
  1147. }
  1148. const show = ref(false)
  1149. const clicked = ref(false)
  1150. const Wrapper = {
  1151. setup() {
  1152. const items = ref<number[]>([])
  1153. onMounted(() => {
  1154. items.value = [1]
  1155. })
  1156. return () => {
  1157. return (
  1158. openBlock(),
  1159. createBlock(Comp, null, {
  1160. default: withCtx(() => [
  1161. createElementVNode('div', null, [
  1162. createElementVNode('div', null, [
  1163. clicked.value
  1164. ? (openBlock(),
  1165. createElementBlock('div', { key: 0 }, 'foo'))
  1166. : createCommentVNode('v-if', true),
  1167. ]),
  1168. ]),
  1169. createElementVNode(
  1170. 'div',
  1171. null,
  1172. items.value.length,
  1173. 1 /* TEXT */,
  1174. ),
  1175. ]),
  1176. _: 1 /* STABLE */,
  1177. })
  1178. )
  1179. }
  1180. },
  1181. }
  1182. createSSRApp({
  1183. components: { Wrapper },
  1184. data() {
  1185. return { show }
  1186. },
  1187. template: `<Wrapper :show="show"/>`,
  1188. }).mount(container)
  1189. await nextTick()
  1190. expect(container.innerHTML).toBe(
  1191. `<div show="false"><!--[--><div><div><!----></div></div><div>1</div><!--]--></div>`,
  1192. )
  1193. show.value = true
  1194. await nextTick()
  1195. expect(async () => {
  1196. clicked.value = true
  1197. await nextTick()
  1198. }).not.toThrow("Cannot read properties of null (reading 'insertBefore')")
  1199. await nextTick()
  1200. expect(container.innerHTML).toBe(
  1201. `<div show="true"><!--[--><div><div><div>foo</div></div></div><div>1</div><!--]--></div>`,
  1202. )
  1203. __DEV__ = true
  1204. })
  1205. describe('mismatch handling', () => {
  1206. test('text node', () => {
  1207. const { container } = mountWithHydration(`foo`, () => 'bar')
  1208. expect(container.textContent).toBe('bar')
  1209. expect(`Hydration text mismatch`).toHaveBeenWarned()
  1210. })
  1211. test('element text content', () => {
  1212. const { container } = mountWithHydration(`<div>foo</div>`, () =>
  1213. h('div', 'bar'),
  1214. )
  1215. expect(container.innerHTML).toBe('<div>bar</div>')
  1216. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1217. })
  1218. test('not enough children', () => {
  1219. const { container } = mountWithHydration(`<div></div>`, () =>
  1220. h('div', [h('span', 'foo'), h('span', 'bar')]),
  1221. )
  1222. expect(container.innerHTML).toBe(
  1223. '<div><span>foo</span><span>bar</span></div>',
  1224. )
  1225. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1226. })
  1227. test('too many children', () => {
  1228. const { container } = mountWithHydration(
  1229. `<div><span>foo</span><span>bar</span></div>`,
  1230. () => h('div', [h('span', 'foo')]),
  1231. )
  1232. expect(container.innerHTML).toBe('<div><span>foo</span></div>')
  1233. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1234. })
  1235. test('complete mismatch', () => {
  1236. const { container } = mountWithHydration(
  1237. `<div><span>foo</span><span>bar</span></div>`,
  1238. () => h('div', [h('div', 'foo'), h('p', 'bar')]),
  1239. )
  1240. expect(container.innerHTML).toBe('<div><div>foo</div><p>bar</p></div>')
  1241. expect(`Hydration node mismatch`).toHaveBeenWarnedTimes(2)
  1242. })
  1243. test('fragment mismatch removal', () => {
  1244. const { container } = mountWithHydration(
  1245. `<div><!--[--><div>foo</div><div>bar</div><!--]--></div>`,
  1246. () => h('div', [h('span', 'replaced')]),
  1247. )
  1248. expect(container.innerHTML).toBe('<div><span>replaced</span></div>')
  1249. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1250. })
  1251. test('fragment not enough children', () => {
  1252. const { container } = mountWithHydration(
  1253. `<div><!--[--><div>foo</div><!--]--><div>baz</div></div>`,
  1254. () => h('div', [[h('div', 'foo'), h('div', 'bar')], h('div', 'baz')]),
  1255. )
  1256. expect(container.innerHTML).toBe(
  1257. '<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>',
  1258. )
  1259. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1260. })
  1261. test('fragment too many children', () => {
  1262. const { container } = mountWithHydration(
  1263. `<div><!--[--><div>foo</div><div>bar</div><!--]--><div>baz</div></div>`,
  1264. () => h('div', [[h('div', 'foo')], h('div', 'baz')]),
  1265. )
  1266. expect(container.innerHTML).toBe(
  1267. '<div><!--[--><div>foo</div><!--]--><div>baz</div></div>',
  1268. )
  1269. // fragment ends early and attempts to hydrate the extra <div>bar</div>
  1270. // as 2nd fragment child.
  1271. expect(`Hydration text content mismatch`).toHaveBeenWarned()
  1272. // excessive children removal
  1273. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1274. })
  1275. test('Teleport target has empty children', () => {
  1276. const teleportContainer = document.createElement('div')
  1277. teleportContainer.id = 'teleport'
  1278. document.body.appendChild(teleportContainer)
  1279. mountWithHydration('<!--teleport start--><!--teleport end-->', () =>
  1280. h(Teleport, { to: '#teleport' }, [h('span', 'value')]),
  1281. )
  1282. expect(teleportContainer.innerHTML).toBe(`<span>value</span>`)
  1283. expect(`Hydration children mismatch`).toHaveBeenWarned()
  1284. })
  1285. test('comment mismatch (element)', () => {
  1286. const { container } = mountWithHydration(`<div><span></span></div>`, () =>
  1287. h('div', [createCommentVNode('hi')]),
  1288. )
  1289. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1290. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1291. })
  1292. test('comment mismatch (text)', () => {
  1293. const { container } = mountWithHydration(`<div>foobar</div>`, () =>
  1294. h('div', [createCommentVNode('hi')]),
  1295. )
  1296. expect(container.innerHTML).toBe('<div><!--hi--></div>')
  1297. expect(`Hydration node mismatch`).toHaveBeenWarned()
  1298. })
  1299. test('class mismatch', () => {
  1300. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1301. h('div', { class: ['foo', 'bar'] }),
  1302. )
  1303. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1304. h('div', { class: { foo: true, bar: true } }),
  1305. )
  1306. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1307. h('div', { class: 'foo bar' }),
  1308. )
  1309. // SVG classes
  1310. mountWithHydration(`<svg class="foo bar"></svg>`, () =>
  1311. h('svg', { class: 'foo bar' }),
  1312. )
  1313. // class with different order
  1314. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1315. h('div', { class: 'bar foo' }),
  1316. )
  1317. expect(`Hydration class mismatch`).not.toHaveBeenWarned()
  1318. mountWithHydration(`<div class="foo bar"></div>`, () =>
  1319. h('div', { class: 'foo' }),
  1320. )
  1321. expect(`Hydration class mismatch`).toHaveBeenWarned()
  1322. })
  1323. test('style mismatch', () => {
  1324. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1325. h('div', { style: { color: 'red' } }),
  1326. )
  1327. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1328. h('div', { style: `color:red;` }),
  1329. )
  1330. mountWithHydration(
  1331. `<div style="color:red; font-size: 12px;"></div>`,
  1332. () => h('div', { style: `font-size: 12px; color:red;` }),
  1333. )
  1334. mountWithHydration(`<div style="color:red;display:none;"></div>`, () =>
  1335. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1336. [vShow, false],
  1337. ]),
  1338. )
  1339. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1340. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1341. h('div', { style: { color: 'green' } }),
  1342. )
  1343. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1344. })
  1345. test('style mismatch w/ v-show', () => {
  1346. mountWithHydration(`<div style="color:red;display:none"></div>`, () =>
  1347. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1348. [vShow, false],
  1349. ]),
  1350. )
  1351. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1352. mountWithHydration(`<div style="color:red;"></div>`, () =>
  1353. withDirectives(createVNode('div', { style: 'color: red' }, ''), [
  1354. [vShow, false],
  1355. ]),
  1356. )
  1357. expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(1)
  1358. })
  1359. test('attr mismatch', () => {
  1360. mountWithHydration(`<div id="foo"></div>`, () => h('div', { id: 'foo' }))
  1361. mountWithHydration(`<div spellcheck></div>`, () =>
  1362. h('div', { spellcheck: '' }),
  1363. )
  1364. mountWithHydration(`<div></div>`, () => h('div', { id: undefined }))
  1365. // boolean
  1366. mountWithHydration(`<select multiple></div>`, () =>
  1367. h('select', { multiple: true }),
  1368. )
  1369. mountWithHydration(`<select multiple></div>`, () =>
  1370. h('select', { multiple: 'multiple' }),
  1371. )
  1372. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1373. mountWithHydration(`<div></div>`, () => h('div', { id: 'foo' }))
  1374. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(1)
  1375. mountWithHydration(`<div id="bar"></div>`, () => h('div', { id: 'foo' }))
  1376. expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)
  1377. })
  1378. test('attr special case: textarea value', () => {
  1379. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1380. h('textarea', { value: 'foo' }),
  1381. )
  1382. mountWithHydration(`<textarea></textarea>`, () =>
  1383. h('textarea', { value: '' }),
  1384. )
  1385. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1386. mountWithHydration(`<textarea>foo</textarea>`, () =>
  1387. h('textarea', { value: 'bar' }),
  1388. )
  1389. expect(`Hydration attribute mismatch`).toHaveBeenWarned()
  1390. })
  1391. test('boolean attr handling', () => {
  1392. mountWithHydration(`<input />`, () => h('input', { readonly: false }))
  1393. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1394. mountWithHydration(`<input readonly />`, () =>
  1395. h('input', { readonly: true }),
  1396. )
  1397. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1398. mountWithHydration(`<input readonly="readonly" />`, () =>
  1399. h('input', { readonly: true }),
  1400. )
  1401. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1402. })
  1403. test('client value is null or undefined', () => {
  1404. mountWithHydration(`<div></div>`, () =>
  1405. h('div', { draggable: undefined }),
  1406. )
  1407. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1408. mountWithHydration(`<input />`, () => h('input', { type: null }))
  1409. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1410. })
  1411. test('should not warn against object values', () => {
  1412. mountWithHydration(`<input />`, () => h('input', { from: {} }))
  1413. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1414. })
  1415. test('should not warn on falsy bindings of non-property keys', () => {
  1416. mountWithHydration(`<button />`, () => h('button', { href: undefined }))
  1417. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1418. })
  1419. test('should not warn on non-renderable option values', () => {
  1420. mountWithHydration(`<select><option>hello</option></select>`, () =>
  1421. h('select', [h('option', { value: ['foo'] }, 'hello')]),
  1422. )
  1423. expect(`Hydration attribute mismatch`).not.toHaveBeenWarned()
  1424. })
  1425. test('should not warn css v-bind', () => {
  1426. const container = document.createElement('div')
  1427. container.innerHTML = `<div style="--foo:red;color:var(--foo);" />`
  1428. const app = createSSRApp({
  1429. setup() {
  1430. useCssVars(() => ({
  1431. foo: 'red',
  1432. }))
  1433. return () => h('div', { style: { color: 'var(--foo)' } })
  1434. },
  1435. })
  1436. app.mount(container)
  1437. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1438. })
  1439. // #10317 - test case from #10325
  1440. test('css vars should only be added to expected on component root dom', () => {
  1441. const container = document.createElement('div')
  1442. container.innerHTML = `<div style="--foo:red;"><div style="color:var(--foo);" /></div>`
  1443. const app = createSSRApp({
  1444. setup() {
  1445. useCssVars(() => ({
  1446. foo: 'red',
  1447. }))
  1448. return () =>
  1449. h('div', null, [h('div', { style: { color: 'var(--foo)' } })])
  1450. },
  1451. })
  1452. app.mount(container)
  1453. expect(`Hydration style mismatch`).not.toHaveBeenWarned()
  1454. })
  1455. })
  1456. })