hydration.spec.ts 69 KB

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