rendererOptimizedMode.spec.ts 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. import {
  2. Fragment,
  3. type FunctionalComponent,
  4. type SetupContext,
  5. Teleport,
  6. type TestElement,
  7. type VNode,
  8. createApp,
  9. createBlock,
  10. createCommentVNode,
  11. createElementBlock,
  12. createElementVNode,
  13. createTextVNode,
  14. createVNode,
  15. defineComponent,
  16. h,
  17. serializeInner as inner,
  18. nextTick,
  19. nodeOps,
  20. onBeforeMount,
  21. onBeforeUnmount,
  22. onUnmounted,
  23. openBlock,
  24. ref,
  25. render,
  26. renderList,
  27. renderSlot,
  28. serialize,
  29. setBlockTracking,
  30. withCtx,
  31. } from '@vue/runtime-test'
  32. import { PatchFlags, SlotFlags } from '@vue/shared'
  33. import { SuspenseImpl } from '../src/components/Suspense'
  34. describe('renderer: optimized mode', () => {
  35. let root: TestElement
  36. let block: VNode | null = null
  37. beforeEach(() => {
  38. root = nodeOps.createElement('div')
  39. block = null
  40. })
  41. const renderWithBlock = (renderChildren: () => VNode[]) => {
  42. render(
  43. (openBlock(), (block = createBlock('div', null, renderChildren()))),
  44. root,
  45. )
  46. }
  47. test('basic use of block', () => {
  48. render((openBlock(), (block = createBlock('p', null, 'foo'))), root)
  49. expect(block.dynamicChildren!.length).toBe(0)
  50. expect(inner(root)).toBe('<p>foo</p>')
  51. })
  52. test('block can appear anywhere in the vdom tree', () => {
  53. render(
  54. h('div', (openBlock(), (block = createBlock('p', null, 'foo')))),
  55. root,
  56. )
  57. expect(block.dynamicChildren!.length).toBe(0)
  58. expect(inner(root)).toBe('<div><p>foo</p></div>')
  59. })
  60. test('block should collect dynamic vnodes', () => {
  61. renderWithBlock(() => [
  62. createVNode('p', null, 'foo', PatchFlags.TEXT),
  63. createVNode('i'),
  64. ])
  65. expect(block!.dynamicChildren!.length).toBe(1)
  66. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  67. '<p>foo</p>',
  68. )
  69. })
  70. test('block can disable tracking', () => {
  71. render(
  72. // disable tracking
  73. (openBlock(true),
  74. (block = createBlock('div', null, [
  75. createVNode('p', null, 'foo', PatchFlags.TEXT),
  76. ]))),
  77. root,
  78. )
  79. expect(block.dynamicChildren!.length).toBe(0)
  80. })
  81. test('block as dynamic children', () => {
  82. renderWithBlock(() => [
  83. (openBlock(), createBlock('div', { key: 0 }, [h('p')])),
  84. ])
  85. expect(block!.dynamicChildren!.length).toBe(1)
  86. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(0)
  87. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  88. '<div><p></p></div>',
  89. )
  90. renderWithBlock(() => [
  91. (openBlock(), createBlock('div', { key: 1 }, [h('i')])),
  92. ])
  93. expect(block!.dynamicChildren!.length).toBe(1)
  94. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(0)
  95. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  96. '<div><i></i></div>',
  97. )
  98. })
  99. test('PatchFlags: PatchFlags.TEXT', async () => {
  100. renderWithBlock(() => [createVNode('p', null, 'foo', PatchFlags.TEXT)])
  101. expect(inner(root)).toBe('<div><p>foo</p></div>')
  102. expect(block!.dynamicChildren!.length).toBe(1)
  103. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  104. '<p>foo</p>',
  105. )
  106. renderWithBlock(() => [createVNode('p', null, 'bar', PatchFlags.TEXT)])
  107. expect(inner(root)).toBe('<div><p>bar</p></div>')
  108. expect(block!.dynamicChildren!.length).toBe(1)
  109. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  110. '<p>bar</p>',
  111. )
  112. })
  113. test('PatchFlags: PatchFlags.CLASS', async () => {
  114. renderWithBlock(() => [
  115. createVNode('p', { class: 'foo' }, '', PatchFlags.CLASS),
  116. ])
  117. expect(inner(root)).toBe('<div><p class="foo"></p></div>')
  118. expect(block!.dynamicChildren!.length).toBe(1)
  119. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  120. '<p class="foo"></p>',
  121. )
  122. renderWithBlock(() => [
  123. createVNode('p', { class: 'bar' }, '', PatchFlags.CLASS),
  124. ])
  125. expect(inner(root)).toBe('<div><p class="bar"></p></div>')
  126. expect(block!.dynamicChildren!.length).toBe(1)
  127. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  128. '<p class="bar"></p>',
  129. )
  130. })
  131. test('PatchFlags: PatchFlags.STYLE', async () => {
  132. renderWithBlock(() => [
  133. createVNode('p', { style: 'color: red' }, '', PatchFlags.STYLE),
  134. ])
  135. expect(inner(root)).toBe('<div><p style="color: red"></p></div>')
  136. expect(block!.dynamicChildren!.length).toBe(1)
  137. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  138. '<p style="color: red"></p>',
  139. )
  140. renderWithBlock(() => [
  141. createVNode('p', { style: 'color: green' }, '', PatchFlags.STYLE),
  142. ])
  143. expect(inner(root)).toBe('<div><p style="color: green"></p></div>')
  144. expect(block!.dynamicChildren!.length).toBe(1)
  145. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  146. '<p style="color: green"></p>',
  147. )
  148. })
  149. test('PatchFlags: PatchFlags.PROPS', async () => {
  150. renderWithBlock(() => [
  151. createVNode('p', { id: 'foo' }, '', PatchFlags.PROPS, ['id']),
  152. ])
  153. expect(inner(root)).toBe('<div><p id="foo"></p></div>')
  154. expect(block!.dynamicChildren!.length).toBe(1)
  155. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  156. '<p id="foo"></p>',
  157. )
  158. renderWithBlock(() => [
  159. createVNode('p', { id: 'bar' }, '', PatchFlags.PROPS, ['id']),
  160. ])
  161. expect(inner(root)).toBe('<div><p id="bar"></p></div>')
  162. expect(block!.dynamicChildren!.length).toBe(1)
  163. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  164. '<p id="bar"></p>',
  165. )
  166. })
  167. test('PatchFlags: PatchFlags.FULL_PROPS', async () => {
  168. let propName = 'foo'
  169. renderWithBlock(() => [
  170. createVNode('p', { [propName]: 'dynamic' }, '', PatchFlags.FULL_PROPS),
  171. ])
  172. expect(inner(root)).toBe('<div><p foo="dynamic"></p></div>')
  173. expect(block!.dynamicChildren!.length).toBe(1)
  174. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  175. '<p foo="dynamic"></p>',
  176. )
  177. propName = 'bar'
  178. renderWithBlock(() => [
  179. createVNode('p', { [propName]: 'dynamic' }, '', PatchFlags.FULL_PROPS),
  180. ])
  181. expect(inner(root)).toBe('<div><p bar="dynamic"></p></div>')
  182. expect(block!.dynamicChildren!.length).toBe(1)
  183. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  184. '<p bar="dynamic"></p>',
  185. )
  186. })
  187. // the order and length of the list will not change
  188. test('PatchFlags: PatchFlags.STABLE_FRAGMENT', async () => {
  189. let list = ['foo', 'bar']
  190. render(
  191. (openBlock(),
  192. (block = createBlock(
  193. Fragment,
  194. null,
  195. list.map(item => {
  196. return createVNode('p', null, item, PatchFlags.TEXT)
  197. }),
  198. PatchFlags.STABLE_FRAGMENT,
  199. ))),
  200. root,
  201. )
  202. expect(inner(root)).toBe('<p>foo</p><p>bar</p>')
  203. expect(block.dynamicChildren!.length).toBe(2)
  204. expect(serialize(block.dynamicChildren![0].el as TestElement)).toBe(
  205. '<p>foo</p>',
  206. )
  207. expect(serialize(block.dynamicChildren![1].el as TestElement)).toBe(
  208. '<p>bar</p>',
  209. )
  210. list = list.map(item => item.repeat(2))
  211. render(
  212. (openBlock(),
  213. createBlock(
  214. Fragment,
  215. null,
  216. list.map(item => {
  217. return createVNode('p', null, item, PatchFlags.TEXT)
  218. }),
  219. PatchFlags.STABLE_FRAGMENT,
  220. )),
  221. root,
  222. )
  223. expect(inner(root)).toBe('<p>foofoo</p><p>barbar</p>')
  224. expect(block.dynamicChildren!.length).toBe(2)
  225. expect(serialize(block.dynamicChildren![0].el as TestElement)).toBe(
  226. '<p>foofoo</p>',
  227. )
  228. expect(serialize(block.dynamicChildren![1].el as TestElement)).toBe(
  229. '<p>barbar</p>',
  230. )
  231. })
  232. // A Fragment with `UNKEYED_FRAGMENT` flag will always patch its children,
  233. // so there's no need for tracking dynamicChildren.
  234. test('PatchFlags: PatchFlags.UNKEYED_FRAGMENT', async () => {
  235. const list = [{ tag: 'p', text: 'foo' }]
  236. render(
  237. (openBlock(true),
  238. (block = createBlock(
  239. Fragment,
  240. null,
  241. list.map(item => {
  242. return createVNode(item.tag, null, item.text)
  243. }),
  244. PatchFlags.UNKEYED_FRAGMENT,
  245. ))),
  246. root,
  247. )
  248. expect(inner(root)).toBe('<p>foo</p>')
  249. expect(block.dynamicChildren!.length).toBe(0)
  250. list.unshift({ tag: 'i', text: 'bar' })
  251. render(
  252. (openBlock(true),
  253. createBlock(
  254. Fragment,
  255. null,
  256. list.map(item => {
  257. return createVNode(item.tag, null, item.text)
  258. }),
  259. PatchFlags.UNKEYED_FRAGMENT,
  260. )),
  261. root,
  262. )
  263. expect(inner(root)).toBe('<i>bar</i><p>foo</p>')
  264. expect(block.dynamicChildren!.length).toBe(0)
  265. })
  266. // A Fragment with `KEYED_FRAGMENT` will always patch its children,
  267. // so there's no need for tracking dynamicChildren.
  268. test('PatchFlags: PatchFlags.KEYED_FRAGMENT', async () => {
  269. const list = [{ tag: 'p', text: 'foo' }]
  270. render(
  271. (openBlock(true),
  272. (block = createBlock(
  273. Fragment,
  274. null,
  275. list.map(item => {
  276. return createVNode(item.tag, { key: item.tag }, item.text)
  277. }),
  278. PatchFlags.KEYED_FRAGMENT,
  279. ))),
  280. root,
  281. )
  282. expect(inner(root)).toBe('<p>foo</p>')
  283. expect(block.dynamicChildren!.length).toBe(0)
  284. list.unshift({ tag: 'i', text: 'bar' })
  285. render(
  286. (openBlock(true),
  287. createBlock(
  288. Fragment,
  289. null,
  290. list.map(item => {
  291. return createVNode(item.tag, { key: item.tag }, item.text)
  292. }),
  293. PatchFlags.KEYED_FRAGMENT,
  294. )),
  295. root,
  296. )
  297. expect(inner(root)).toBe('<i>bar</i><p>foo</p>')
  298. expect(block.dynamicChildren!.length).toBe(0)
  299. })
  300. test('PatchFlags: PatchFlags.NEED_PATCH', async () => {
  301. const spyMounted = vi.fn()
  302. const spyUpdated = vi.fn()
  303. const count = ref(0)
  304. const Comp = {
  305. setup() {
  306. return () => {
  307. count.value
  308. return (
  309. openBlock(),
  310. (block = createBlock('div', null, [
  311. createVNode(
  312. 'p',
  313. { onVnodeMounted: spyMounted, onVnodeBeforeUpdate: spyUpdated },
  314. '',
  315. PatchFlags.NEED_PATCH,
  316. ),
  317. ]))
  318. )
  319. }
  320. },
  321. }
  322. render(h(Comp), root)
  323. expect(inner(root)).toBe('<div><p></p></div>')
  324. expect(block!.dynamicChildren!.length).toBe(1)
  325. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  326. '<p></p>',
  327. )
  328. expect(spyMounted).toHaveBeenCalledTimes(1)
  329. expect(spyUpdated).toHaveBeenCalledTimes(0)
  330. count.value++
  331. await nextTick()
  332. expect(inner(root)).toBe('<div><p></p></div>')
  333. expect(block!.dynamicChildren!.length).toBe(1)
  334. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  335. '<p></p>',
  336. )
  337. expect(spyMounted).toHaveBeenCalledTimes(1)
  338. expect(spyUpdated).toHaveBeenCalledTimes(1)
  339. })
  340. test('PatchFlags: PatchFlags.BAIL', async () => {
  341. render(
  342. (openBlock(),
  343. (block = createBlock('div', null, [createVNode('p', null, 'foo')]))),
  344. root,
  345. )
  346. expect(inner(root)).toBe('<div><p>foo</p></div>')
  347. expect(block!.dynamicChildren!.length).toBe(0)
  348. render(
  349. (openBlock(),
  350. (block = createBlock(
  351. 'div',
  352. null,
  353. [createVNode('i', null, 'bar')],
  354. PatchFlags.BAIL,
  355. ))),
  356. root,
  357. )
  358. expect(inner(root)).toBe('<div><i>bar</i></div>')
  359. expect(block!.dynamicChildren).toBe(null)
  360. })
  361. // #1980
  362. test('dynamicChildren should be tracked correctly when normalizing slots to plain children', async () => {
  363. let block: VNode
  364. const Comp = defineComponent({
  365. setup(_props, { slots }) {
  366. return () => {
  367. const vnode =
  368. (openBlock(),
  369. (block = createBlock('div', null, {
  370. default: withCtx(() => [renderSlot(slots, 'default')]),
  371. _: SlotFlags.FORWARDED,
  372. })))
  373. return vnode
  374. }
  375. },
  376. })
  377. const foo = ref(0)
  378. const App = {
  379. setup() {
  380. return () => {
  381. return createBlock(Comp, null, {
  382. default: withCtx(() => [
  383. createVNode('p', null, foo.value, PatchFlags.TEXT),
  384. ]),
  385. // Indicates that this is a stable slot to avoid bail out
  386. _: SlotFlags.STABLE,
  387. })
  388. }
  389. },
  390. }
  391. render(h(App), root)
  392. expect(inner(root)).toBe('<div><p>0</p></div>')
  393. expect(block!.dynamicChildren!.length).toBe(1)
  394. expect(block!.dynamicChildren![0].type).toBe(Fragment)
  395. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  396. expect(
  397. serialize(
  398. block!.dynamicChildren![0].dynamicChildren![0].el as TestElement,
  399. ),
  400. ).toBe('<p>0</p>')
  401. foo.value++
  402. await nextTick()
  403. expect(inner(root)).toBe('<div><p>1</p></div>')
  404. })
  405. // #2169
  406. // block
  407. // - dynamic child (1)
  408. // - component (2)
  409. // When unmounting (1), we know we are in optimized mode so no need to further
  410. // traverse unmount its children
  411. test('should not perform unnecessary unmount traversals', () => {
  412. const spy = vi.fn()
  413. const Child = {
  414. setup() {
  415. onBeforeUnmount(spy)
  416. return () => 'child'
  417. },
  418. }
  419. const Parent = () => (
  420. openBlock(),
  421. createBlock('div', null, [
  422. createVNode('div', { style: {} }, [createVNode(Child)], 4 /* STYLE */),
  423. ])
  424. )
  425. render(h(Parent), root)
  426. render(null, root)
  427. expect(spy).toHaveBeenCalledTimes(1)
  428. })
  429. test('should call onUnmounted hook for dynamic components receiving an existing vnode w/ component children', async () => {
  430. const spy = vi.fn()
  431. const show = ref(1)
  432. const Child = {
  433. setup() {
  434. onUnmounted(spy)
  435. return () => 'child'
  436. },
  437. }
  438. const foo = h('div', null, h(Child))
  439. const app = createApp({
  440. render() {
  441. return show.value
  442. ? (openBlock(),
  443. createBlock('div', null, [(openBlock(), createBlock(foo))]))
  444. : createCommentVNode('v-if', true)
  445. },
  446. })
  447. app.mount(root)
  448. show.value = 0
  449. await nextTick()
  450. expect(spy).toHaveBeenCalledTimes(1)
  451. })
  452. // #2444
  453. // `KEYED_FRAGMENT` and `UNKEYED_FRAGMENT` always need to diff its children
  454. test('non-stable Fragment always need to diff its children', () => {
  455. const spyA = vi.fn()
  456. const spyB = vi.fn()
  457. const ChildA = {
  458. setup() {
  459. onBeforeUnmount(spyA)
  460. return () => 'child'
  461. },
  462. }
  463. const ChildB = {
  464. setup() {
  465. onBeforeUnmount(spyB)
  466. return () => 'child'
  467. },
  468. }
  469. const Parent = () => (
  470. openBlock(),
  471. createBlock('div', null, [
  472. (openBlock(true),
  473. createBlock(
  474. Fragment,
  475. null,
  476. [createVNode(ChildA, { key: 0 })],
  477. 128 /* KEYED_FRAGMENT */,
  478. )),
  479. (openBlock(true),
  480. createBlock(
  481. Fragment,
  482. null,
  483. [createVNode(ChildB)],
  484. 256 /* UNKEYED_FRAGMENT */,
  485. )),
  486. ])
  487. )
  488. render(h(Parent), root)
  489. render(null, root)
  490. expect(spyA).toHaveBeenCalledTimes(1)
  491. expect(spyB).toHaveBeenCalledTimes(1)
  492. })
  493. // #2893
  494. test('manually rendering the optimized slots should allow subsequent updates to exit the optimized mode correctly', async () => {
  495. const state = ref(0)
  496. const CompA = {
  497. name: 'A',
  498. setup(props: any, { slots }: SetupContext) {
  499. return () => {
  500. return (
  501. openBlock(),
  502. createBlock('div', null, [renderSlot(slots, 'default')])
  503. )
  504. }
  505. },
  506. }
  507. const Wrapper = {
  508. name: 'Wrapper',
  509. setup(props: any, { slots }: SetupContext) {
  510. // use the manually written render function to rendering the optimized slots,
  511. // which should make subsequent updates exit the optimized mode correctly
  512. return () => {
  513. return slots.default!()[state.value]
  514. }
  515. },
  516. }
  517. const app = createApp({
  518. name: 'App',
  519. setup() {
  520. return () => {
  521. return (
  522. openBlock(),
  523. createBlock(Wrapper, null, {
  524. default: withCtx(() => [
  525. createVNode(CompA, null, {
  526. default: withCtx(() => [createTextVNode('Hello')]),
  527. _: 1 /* STABLE */,
  528. }),
  529. createVNode(CompA, null, {
  530. default: withCtx(() => [createTextVNode('World')]),
  531. _: 1 /* STABLE */,
  532. }),
  533. ]),
  534. _: 1 /* STABLE */,
  535. })
  536. )
  537. }
  538. },
  539. })
  540. app.mount(root)
  541. expect(inner(root)).toBe('<div>Hello</div>')
  542. state.value = 1
  543. await nextTick()
  544. expect(inner(root)).toBe('<div>World</div>')
  545. })
  546. //#3623
  547. test('nested teleport unmount need exit the optimization mode', async () => {
  548. const target = nodeOps.createElement('div')
  549. const root = nodeOps.createElement('div')
  550. render(
  551. (openBlock(),
  552. createBlock('div', null, [
  553. (openBlock(),
  554. createBlock(
  555. Teleport as any,
  556. {
  557. to: target,
  558. },
  559. [
  560. createVNode('div', null, [
  561. (openBlock(),
  562. createBlock(
  563. Teleport as any,
  564. {
  565. to: target,
  566. },
  567. [createVNode('div', null, 'foo')],
  568. )),
  569. ]),
  570. ],
  571. )),
  572. ])),
  573. root,
  574. )
  575. await nextTick()
  576. expect(inner(target)).toMatchInlineSnapshot(
  577. `"<div><!--teleport start--><!--teleport end--></div><div>foo</div>"`,
  578. )
  579. expect(inner(root)).toMatchInlineSnapshot(
  580. `"<div><!--teleport start--><!--teleport end--></div>"`,
  581. )
  582. render(null, root)
  583. expect(inner(target)).toBe('')
  584. })
  585. // #3548
  586. test('should not track dynamic children when the user calls a compiled slot inside template expression', () => {
  587. const Comp = {
  588. setup(props: any, { slots }: SetupContext) {
  589. return () => {
  590. return (
  591. openBlock(),
  592. (block = createBlock('section', null, [
  593. renderSlot(slots, 'default'),
  594. ]))
  595. )
  596. }
  597. },
  598. }
  599. let dynamicVNode: VNode
  600. const Wrapper = {
  601. setup(props: any, { slots }: SetupContext) {
  602. return () => {
  603. return (
  604. openBlock(),
  605. createBlock(Comp, null, {
  606. default: withCtx(() => {
  607. return [
  608. (dynamicVNode = createVNode(
  609. 'div',
  610. {
  611. class: {
  612. foo: !!slots.default!(),
  613. },
  614. },
  615. null,
  616. PatchFlags.CLASS,
  617. )),
  618. ]
  619. }),
  620. _: 1,
  621. })
  622. )
  623. }
  624. },
  625. }
  626. const app = createApp({
  627. render() {
  628. return (
  629. openBlock(),
  630. createBlock(Wrapper, null, {
  631. default: withCtx(() => {
  632. return [createVNode({}) /* component */]
  633. }),
  634. _: 1,
  635. })
  636. )
  637. },
  638. })
  639. app.mount(root)
  640. expect(inner(root)).toBe('<section><div class="foo"></div></section>')
  641. /**
  642. * Block Tree:
  643. * - block(div)
  644. * - block(Fragment): renderSlots()
  645. * - dynamicVNode
  646. */
  647. expect(block!.dynamicChildren!.length).toBe(1)
  648. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  649. expect(block!.dynamicChildren![0].dynamicChildren![0]).toEqual(
  650. dynamicVNode!,
  651. )
  652. })
  653. // 3569
  654. test('should force bailout when the user manually calls the slot function', async () => {
  655. const index = ref(0)
  656. const Foo = {
  657. setup(props: any, { slots }: SetupContext) {
  658. return () => {
  659. return slots.default!()[index.value]
  660. }
  661. },
  662. }
  663. const app = createApp({
  664. setup() {
  665. return () => {
  666. return (
  667. openBlock(),
  668. createBlock(Foo, null, {
  669. default: withCtx(() => [
  670. true
  671. ? (openBlock(), createBlock('p', { key: 0 }, '1'))
  672. : createCommentVNode('v-if', true),
  673. true
  674. ? (openBlock(), createBlock('p', { key: 0 }, '2'))
  675. : createCommentVNode('v-if', true),
  676. ]),
  677. _: 1 /* STABLE */,
  678. })
  679. )
  680. }
  681. },
  682. })
  683. app.mount(root)
  684. expect(inner(root)).toBe('<p>1</p>')
  685. index.value = 1
  686. await nextTick()
  687. expect(inner(root)).toBe('<p>2</p>')
  688. index.value = 0
  689. await nextTick()
  690. expect(inner(root)).toBe('<p>1</p>')
  691. })
  692. // #3779
  693. test('treat slots manually written by the user as dynamic', async () => {
  694. const Middle = {
  695. setup(props: any, { slots }: any) {
  696. return slots.default!
  697. },
  698. }
  699. const Comp = {
  700. setup(props: any, { slots }: any) {
  701. return () => {
  702. return (
  703. openBlock(),
  704. createBlock('div', null, [
  705. createVNode(Middle, null, {
  706. default: withCtx(
  707. () => [
  708. createVNode('div', null, [renderSlot(slots, 'default')]),
  709. ],
  710. undefined,
  711. ),
  712. _: 3 /* FORWARDED */,
  713. }),
  714. ])
  715. )
  716. }
  717. },
  718. }
  719. const loading = ref(false)
  720. const app = createApp({
  721. setup() {
  722. return () => {
  723. // important: write the slot content here
  724. const content = h('span', loading.value ? 'loading' : 'loaded')
  725. return h(Comp, null, {
  726. default: () => content,
  727. })
  728. }
  729. },
  730. })
  731. app.mount(root)
  732. expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
  733. loading.value = true
  734. await nextTick()
  735. expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
  736. })
  737. // #3828
  738. test('patch Suspense in optimized mode w/ nested dynamic nodes', async () => {
  739. const show = ref(false)
  740. const app = createApp({
  741. render() {
  742. return (
  743. openBlock(),
  744. createBlock(
  745. Fragment,
  746. null,
  747. [
  748. (openBlock(),
  749. createBlock(SuspenseImpl, null, {
  750. default: withCtx(() => [
  751. createVNode('div', null, [
  752. createVNode('div', null, show.value, PatchFlags.TEXT),
  753. ]),
  754. ]),
  755. _: SlotFlags.STABLE,
  756. })),
  757. ],
  758. PatchFlags.STABLE_FRAGMENT,
  759. )
  760. )
  761. },
  762. })
  763. app.mount(root)
  764. expect(inner(root)).toBe('<div><div>false</div></div>')
  765. show.value = true
  766. await nextTick()
  767. expect(inner(root)).toBe('<div><div>true</div></div>')
  768. })
  769. // #4183
  770. test('should not take unmount children fast path /w Suspense', async () => {
  771. const show = ref(true)
  772. const spyUnmounted = vi.fn()
  773. const Parent = {
  774. setup(props: any, { slots }: SetupContext) {
  775. return () => (
  776. openBlock(),
  777. createBlock(SuspenseImpl, null, {
  778. default: withCtx(() => [renderSlot(slots, 'default')]),
  779. _: SlotFlags.FORWARDED,
  780. })
  781. )
  782. },
  783. }
  784. const Child = {
  785. setup() {
  786. onUnmounted(spyUnmounted)
  787. return () => createVNode('div', null, show.value, PatchFlags.TEXT)
  788. },
  789. }
  790. const app = createApp({
  791. render() {
  792. return show.value
  793. ? (openBlock(),
  794. createBlock(
  795. Parent,
  796. { key: 0 },
  797. {
  798. default: withCtx(() => [createVNode(Child)]),
  799. _: SlotFlags.STABLE,
  800. },
  801. ))
  802. : createCommentVNode('v-if', true)
  803. },
  804. })
  805. app.mount(root)
  806. expect(inner(root)).toBe('<div>true</div>')
  807. show.value = false
  808. await nextTick()
  809. expect(inner(root)).toBe('<!--v-if-->')
  810. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  811. })
  812. // #3881
  813. // root cause: fragment inside a compiled slot passed to component which
  814. // programmatically invokes the slot. The entire slot should de-opt but
  815. // the fragment was incorrectly put in optimized mode which causes it to skip
  816. // updates for its inner components.
  817. test('fragments inside programmatically invoked compiled slot should de-opt properly', async () => {
  818. const Parent: FunctionalComponent = (_, { slots }) => slots.default!()
  819. const Dummy = () => 'dummy'
  820. const toggle = ref(true)
  821. const force = ref(0)
  822. const app = createApp({
  823. render() {
  824. if (!toggle.value) {
  825. return null
  826. }
  827. return h(
  828. Parent,
  829. { n: force.value },
  830. {
  831. default: withCtx(
  832. () => [
  833. createVNode('ul', null, [
  834. (openBlock(),
  835. createBlock(
  836. Fragment,
  837. null,
  838. renderList(1, item => {
  839. return createVNode('li', null, [createVNode(Dummy)])
  840. }),
  841. 64 /* STABLE_FRAGMENT */,
  842. )),
  843. ]),
  844. ],
  845. undefined,
  846. true,
  847. ),
  848. _: 1 /* STABLE */,
  849. },
  850. )
  851. },
  852. })
  853. app.mount(root)
  854. // force a patch
  855. force.value++
  856. await nextTick()
  857. expect(inner(root)).toBe(`<ul><li>dummy</li></ul>`)
  858. // unmount
  859. toggle.value = false
  860. await nextTick()
  861. // should successfully unmount without error
  862. expect(inner(root)).toBe(`<!---->`)
  863. })
  864. // #10870
  865. test('should bail manually rendered compiler slots for both mount and update', async () => {
  866. // only reproducible in prod
  867. __DEV__ = false
  868. function Outer(_: any, { slots }: any) {
  869. return slots.default()
  870. }
  871. const Mid = {
  872. render(ctx: any) {
  873. return (
  874. openBlock(),
  875. createElementBlock('div', null, [renderSlot(ctx.$slots, 'default')])
  876. )
  877. },
  878. }
  879. const state1 = ref(true)
  880. const state2 = ref(true)
  881. const App = {
  882. render() {
  883. return (
  884. openBlock(),
  885. createBlock(Outer, null, {
  886. default: withCtx(() => [
  887. createVNode(
  888. Mid,
  889. { foo: state2.value },
  890. {
  891. default: withCtx(() => [
  892. createElementVNode('div', null, [
  893. createElementVNode('div', null, [
  894. state2.value
  895. ? (openBlock(),
  896. createElementBlock(
  897. 'div',
  898. {
  899. key: 0,
  900. id: 'if',
  901. foo: state1.value,
  902. },
  903. null,
  904. 8 /* PROPS */,
  905. ['foo'],
  906. ))
  907. : createCommentVNode('v-if', true),
  908. ]),
  909. ]),
  910. ]),
  911. _: 1 /* STABLE */,
  912. },
  913. 8 /* PROPS */,
  914. ['foo'],
  915. ),
  916. ]),
  917. _: 1 /* STABLE */,
  918. })
  919. )
  920. },
  921. }
  922. const app = createApp(App)
  923. app.config.errorHandler = vi.fn()
  924. try {
  925. app.mount(root)
  926. state1.value = false
  927. await nextTick()
  928. state2.value = false
  929. await nextTick()
  930. } finally {
  931. __DEV__ = true
  932. expect(app.config.errorHandler).not.toHaveBeenCalled()
  933. }
  934. })
  935. // #11336
  936. test('should bail manually rendered compiler slots for both mount and update (2)', async () => {
  937. // only reproducible in prod
  938. __DEV__ = false
  939. const n = ref(0)
  940. function Outer(_: any, { slots }: any) {
  941. n.value // track
  942. return slots.default()
  943. }
  944. const Mid = {
  945. render(ctx: any) {
  946. return (
  947. openBlock(),
  948. createElementBlock('div', null, [renderSlot(ctx.$slots, 'default')])
  949. )
  950. },
  951. }
  952. const show = ref(false)
  953. const App = {
  954. render() {
  955. return (
  956. openBlock(),
  957. createBlock(Outer, null, {
  958. default: withCtx(() => [
  959. createVNode(Mid, null, {
  960. default: withCtx(() => [
  961. createElementVNode('div', null, [
  962. show.value
  963. ? (openBlock(),
  964. createElementBlock('div', { key: 0 }, '1'))
  965. : createCommentVNode('v-if', true),
  966. createElementVNode('div', null, '2'),
  967. createElementVNode('div', null, '3'),
  968. ]),
  969. createElementVNode('div', null, '4'),
  970. ]),
  971. _: 1 /* STABLE */,
  972. }),
  973. ]),
  974. _: 1 /* STABLE */,
  975. })
  976. )
  977. },
  978. }
  979. const app = createApp(App)
  980. app.config.errorHandler = vi.fn()
  981. try {
  982. app.mount(root)
  983. // force Outer update, which will assign new slots to Mid
  984. // we want to make sure the compiled slot flag doesn't accidentally
  985. // get assigned again
  986. n.value++
  987. await nextTick()
  988. show.value = true
  989. await nextTick()
  990. } finally {
  991. __DEV__ = true
  992. expect(app.config.errorHandler).not.toHaveBeenCalled()
  993. }
  994. })
  995. test('diff slot and slot fallback node', async () => {
  996. const Comp = {
  997. props: ['show'],
  998. setup(props: any, { slots }: SetupContext) {
  999. return () => {
  1000. return (
  1001. openBlock(),
  1002. createElementBlock('div', null, [
  1003. renderSlot(slots, 'default', { hide: !props.show }, () => [
  1004. (openBlock(),
  1005. (block = createElementBlock(
  1006. Fragment,
  1007. { key: 0 },
  1008. [createTextVNode('foo')],
  1009. PatchFlags.STABLE_FRAGMENT,
  1010. ))),
  1011. ]),
  1012. ])
  1013. )
  1014. }
  1015. },
  1016. }
  1017. const show = ref(true)
  1018. const app = createApp({
  1019. render() {
  1020. return (
  1021. openBlock(),
  1022. createBlock(
  1023. Comp,
  1024. { show: show.value },
  1025. {
  1026. default: withCtx(({ hide }: { hide: boolean }) => [
  1027. !hide
  1028. ? (openBlock(),
  1029. createElementBlock(
  1030. Fragment,
  1031. { key: 0 },
  1032. [
  1033. createCommentVNode('comment'),
  1034. createElementVNode(
  1035. 'div',
  1036. null,
  1037. 'bar',
  1038. PatchFlags.CACHED,
  1039. ),
  1040. ],
  1041. PatchFlags.STABLE_FRAGMENT,
  1042. ))
  1043. : createCommentVNode('v-if', true),
  1044. ]),
  1045. _: SlotFlags.STABLE,
  1046. },
  1047. PatchFlags.PROPS,
  1048. ['show'],
  1049. )
  1050. )
  1051. },
  1052. })
  1053. app.mount(root)
  1054. expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
  1055. expect(block).toBe(null)
  1056. show.value = false
  1057. await nextTick()
  1058. expect(inner(root)).toBe('<div>foo</div>')
  1059. show.value = true
  1060. await nextTick()
  1061. expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
  1062. })
  1063. test('should not take unmount children fast path if children contain cached nodes', async () => {
  1064. const show = ref(true)
  1065. const spyUnmounted = vi.fn()
  1066. const Child = {
  1067. setup() {
  1068. onUnmounted(spyUnmounted)
  1069. return () => createVNode('div', null, 'Child')
  1070. },
  1071. }
  1072. const app = createApp({
  1073. render(_: any, cache: any) {
  1074. return show.value
  1075. ? (openBlock(),
  1076. createBlock('div', null, [
  1077. createVNode('div', null, [
  1078. cache[0] ||
  1079. (setBlockTracking(-1, true),
  1080. ((cache[0] = createVNode('div', null, [
  1081. createVNode(Child),
  1082. ])).cacheIndex = 0),
  1083. setBlockTracking(1),
  1084. cache[0]),
  1085. ]),
  1086. ]))
  1087. : createCommentVNode('v-if', true)
  1088. },
  1089. })
  1090. app.mount(root)
  1091. expect(inner(root)).toBe(
  1092. '<div><div><div><div>Child</div></div></div></div>',
  1093. )
  1094. show.value = false
  1095. await nextTick()
  1096. expect(inner(root)).toBe('<!--v-if-->')
  1097. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  1098. show.value = true
  1099. await nextTick()
  1100. expect(inner(root)).toBe(
  1101. '<div><div><div><div>Child</div></div></div></div>',
  1102. )
  1103. // should unmount again, this verifies previous cache was properly cleared
  1104. show.value = false
  1105. await nextTick()
  1106. expect(inner(root)).toBe('<!--v-if-->')
  1107. expect(spyUnmounted).toHaveBeenCalledTimes(2)
  1108. })
  1109. // #12371
  1110. test('unmount children when the user calls a compiled slot', async () => {
  1111. const beforeMountSpy = vi.fn()
  1112. const beforeUnmountSpy = vi.fn()
  1113. const Child = {
  1114. setup() {
  1115. onBeforeMount(beforeMountSpy)
  1116. onBeforeUnmount(beforeUnmountSpy)
  1117. return () => 'child'
  1118. },
  1119. }
  1120. const Wrapper = {
  1121. setup(_: any, { slots }: SetupContext) {
  1122. return () => (
  1123. openBlock(),
  1124. createElementBlock('section', null, [
  1125. (openBlock(),
  1126. createElementBlock('div', { key: 1 }, [
  1127. createTextVNode(slots.header!() ? 'foo' : 'bar', 1 /* TEXT */),
  1128. renderSlot(slots, 'content'),
  1129. ])),
  1130. ])
  1131. )
  1132. },
  1133. }
  1134. const show = ref(false)
  1135. const app = createApp({
  1136. render() {
  1137. return show.value
  1138. ? (openBlock(),
  1139. createBlock(Wrapper, null, {
  1140. header: withCtx(() => [createVNode({})]),
  1141. content: withCtx(() => [createVNode(Child)]),
  1142. _: 1,
  1143. }))
  1144. : createCommentVNode('v-if', true)
  1145. },
  1146. })
  1147. app.mount(root)
  1148. expect(inner(root)).toMatchInlineSnapshot(`"<!--v-if-->"`)
  1149. expect(beforeMountSpy).toHaveBeenCalledTimes(0)
  1150. expect(beforeUnmountSpy).toHaveBeenCalledTimes(0)
  1151. show.value = true
  1152. await nextTick()
  1153. expect(inner(root)).toMatchInlineSnapshot(
  1154. `"<section><div>foochild</div></section>"`,
  1155. )
  1156. expect(beforeMountSpy).toHaveBeenCalledTimes(1)
  1157. show.value = false
  1158. await nextTick()
  1159. expect(inner(root)).toBe('<!--v-if-->')
  1160. expect(beforeUnmountSpy).toHaveBeenCalledTimes(1)
  1161. })
  1162. })