hydration.spec.ts 46 KB

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