rendererOptimizedMode.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. createTextVNode,
  12. createVNode,
  13. defineComponent,
  14. h,
  15. serializeInner as inner,
  16. nextTick,
  17. nodeOps,
  18. onBeforeUnmount,
  19. onUnmounted,
  20. openBlock,
  21. ref,
  22. render,
  23. renderList,
  24. renderSlot,
  25. serialize,
  26. withCtx,
  27. } from '@vue/runtime-test'
  28. import { PatchFlags, SlotFlags } from '@vue/shared'
  29. import { SuspenseImpl } from '../src/components/Suspense'
  30. describe('renderer: optimized mode', () => {
  31. let root: TestElement
  32. let block: VNode | null = null
  33. beforeEach(() => {
  34. root = nodeOps.createElement('div')
  35. block = null
  36. })
  37. const renderWithBlock = (renderChildren: () => VNode[]) => {
  38. render(
  39. (openBlock(), (block = createBlock('div', null, renderChildren()))),
  40. root,
  41. )
  42. }
  43. test('basic use of block', () => {
  44. render((openBlock(), (block = createBlock('p', null, 'foo'))), root)
  45. expect(block.dynamicChildren!.length).toBe(0)
  46. expect(inner(root)).toBe('<p>foo</p>')
  47. })
  48. test('block can appear anywhere in the vdom tree', () => {
  49. render(
  50. h('div', (openBlock(), (block = createBlock('p', null, 'foo')))),
  51. root,
  52. )
  53. expect(block.dynamicChildren!.length).toBe(0)
  54. expect(inner(root)).toBe('<div><p>foo</p></div>')
  55. })
  56. test('block should collect dynamic vnodes', () => {
  57. renderWithBlock(() => [
  58. createVNode('p', null, 'foo', PatchFlags.TEXT),
  59. createVNode('i'),
  60. ])
  61. expect(block!.dynamicChildren!.length).toBe(1)
  62. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  63. '<p>foo</p>',
  64. )
  65. })
  66. test('block can disable tracking', () => {
  67. render(
  68. // disable tracking
  69. (openBlock(true),
  70. (block = createBlock('div', null, [
  71. createVNode('p', null, 'foo', PatchFlags.TEXT),
  72. ]))),
  73. root,
  74. )
  75. expect(block.dynamicChildren!.length).toBe(0)
  76. })
  77. test('block as dynamic children', () => {
  78. renderWithBlock(() => [
  79. (openBlock(), createBlock('div', { key: 0 }, [h('p')])),
  80. ])
  81. expect(block!.dynamicChildren!.length).toBe(1)
  82. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(0)
  83. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  84. '<div><p></p></div>',
  85. )
  86. renderWithBlock(() => [
  87. (openBlock(), createBlock('div', { key: 1 }, [h('i')])),
  88. ])
  89. expect(block!.dynamicChildren!.length).toBe(1)
  90. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(0)
  91. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  92. '<div><i></i></div>',
  93. )
  94. })
  95. test('PatchFlags: PatchFlags.TEXT', async () => {
  96. renderWithBlock(() => [createVNode('p', null, 'foo', PatchFlags.TEXT)])
  97. expect(inner(root)).toBe('<div><p>foo</p></div>')
  98. expect(block!.dynamicChildren!.length).toBe(1)
  99. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  100. '<p>foo</p>',
  101. )
  102. renderWithBlock(() => [createVNode('p', null, 'bar', PatchFlags.TEXT)])
  103. expect(inner(root)).toBe('<div><p>bar</p></div>')
  104. expect(block!.dynamicChildren!.length).toBe(1)
  105. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  106. '<p>bar</p>',
  107. )
  108. })
  109. test('PatchFlags: PatchFlags.CLASS', async () => {
  110. renderWithBlock(() => [
  111. createVNode('p', { class: 'foo' }, '', PatchFlags.CLASS),
  112. ])
  113. expect(inner(root)).toBe('<div><p class="foo"></p></div>')
  114. expect(block!.dynamicChildren!.length).toBe(1)
  115. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  116. '<p class="foo"></p>',
  117. )
  118. renderWithBlock(() => [
  119. createVNode('p', { class: 'bar' }, '', PatchFlags.CLASS),
  120. ])
  121. expect(inner(root)).toBe('<div><p class="bar"></p></div>')
  122. expect(block!.dynamicChildren!.length).toBe(1)
  123. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  124. '<p class="bar"></p>',
  125. )
  126. })
  127. test('PatchFlags: PatchFlags.STYLE', async () => {
  128. renderWithBlock(() => [
  129. createVNode('p', { style: 'color: red' }, '', PatchFlags.STYLE),
  130. ])
  131. expect(inner(root)).toBe('<div><p style="color: red"></p></div>')
  132. expect(block!.dynamicChildren!.length).toBe(1)
  133. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  134. '<p style="color: red"></p>',
  135. )
  136. renderWithBlock(() => [
  137. createVNode('p', { style: 'color: green' }, '', PatchFlags.STYLE),
  138. ])
  139. expect(inner(root)).toBe('<div><p style="color: green"></p></div>')
  140. expect(block!.dynamicChildren!.length).toBe(1)
  141. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  142. '<p style="color: green"></p>',
  143. )
  144. })
  145. test('PatchFlags: PatchFlags.PROPS', async () => {
  146. renderWithBlock(() => [
  147. createVNode('p', { id: 'foo' }, '', PatchFlags.PROPS, ['id']),
  148. ])
  149. expect(inner(root)).toBe('<div><p id="foo"></p></div>')
  150. expect(block!.dynamicChildren!.length).toBe(1)
  151. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  152. '<p id="foo"></p>',
  153. )
  154. renderWithBlock(() => [
  155. createVNode('p', { id: 'bar' }, '', PatchFlags.PROPS, ['id']),
  156. ])
  157. expect(inner(root)).toBe('<div><p id="bar"></p></div>')
  158. expect(block!.dynamicChildren!.length).toBe(1)
  159. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  160. '<p id="bar"></p>',
  161. )
  162. })
  163. test('PatchFlags: PatchFlags.FULL_PROPS', async () => {
  164. let propName = 'foo'
  165. renderWithBlock(() => [
  166. createVNode('p', { [propName]: 'dynamic' }, '', PatchFlags.FULL_PROPS),
  167. ])
  168. expect(inner(root)).toBe('<div><p foo="dynamic"></p></div>')
  169. expect(block!.dynamicChildren!.length).toBe(1)
  170. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  171. '<p foo="dynamic"></p>',
  172. )
  173. propName = 'bar'
  174. renderWithBlock(() => [
  175. createVNode('p', { [propName]: 'dynamic' }, '', PatchFlags.FULL_PROPS),
  176. ])
  177. expect(inner(root)).toBe('<div><p bar="dynamic"></p></div>')
  178. expect(block!.dynamicChildren!.length).toBe(1)
  179. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  180. '<p bar="dynamic"></p>',
  181. )
  182. })
  183. // the order and length of the list will not change
  184. test('PatchFlags: PatchFlags.STABLE_FRAGMENT', async () => {
  185. let list = ['foo', 'bar']
  186. render(
  187. (openBlock(),
  188. (block = createBlock(
  189. Fragment,
  190. null,
  191. list.map(item => {
  192. return createVNode('p', null, item, PatchFlags.TEXT)
  193. }),
  194. PatchFlags.STABLE_FRAGMENT,
  195. ))),
  196. root,
  197. )
  198. expect(inner(root)).toBe('<p>foo</p><p>bar</p>')
  199. expect(block.dynamicChildren!.length).toBe(2)
  200. expect(serialize(block.dynamicChildren![0].el as TestElement)).toBe(
  201. '<p>foo</p>',
  202. )
  203. expect(serialize(block.dynamicChildren![1].el as TestElement)).toBe(
  204. '<p>bar</p>',
  205. )
  206. list = list.map(item => item.repeat(2))
  207. render(
  208. (openBlock(),
  209. createBlock(
  210. Fragment,
  211. null,
  212. list.map(item => {
  213. return createVNode('p', null, item, PatchFlags.TEXT)
  214. }),
  215. PatchFlags.STABLE_FRAGMENT,
  216. )),
  217. root,
  218. )
  219. expect(inner(root)).toBe('<p>foofoo</p><p>barbar</p>')
  220. expect(block.dynamicChildren!.length).toBe(2)
  221. expect(serialize(block.dynamicChildren![0].el as TestElement)).toBe(
  222. '<p>foofoo</p>',
  223. )
  224. expect(serialize(block.dynamicChildren![1].el as TestElement)).toBe(
  225. '<p>barbar</p>',
  226. )
  227. })
  228. // A Fragment with `UNKEYED_FRAGMENT` flag will always patch its children,
  229. // so there's no need for tracking dynamicChildren.
  230. test('PatchFlags: PatchFlags.UNKEYED_FRAGMENT', async () => {
  231. const list = [{ tag: 'p', text: 'foo' }]
  232. render(
  233. (openBlock(true),
  234. (block = createBlock(
  235. Fragment,
  236. null,
  237. list.map(item => {
  238. return createVNode(item.tag, null, item.text)
  239. }),
  240. PatchFlags.UNKEYED_FRAGMENT,
  241. ))),
  242. root,
  243. )
  244. expect(inner(root)).toBe('<p>foo</p>')
  245. expect(block.dynamicChildren!.length).toBe(0)
  246. list.unshift({ tag: 'i', text: 'bar' })
  247. render(
  248. (openBlock(true),
  249. createBlock(
  250. Fragment,
  251. null,
  252. list.map(item => {
  253. return createVNode(item.tag, null, item.text)
  254. }),
  255. PatchFlags.UNKEYED_FRAGMENT,
  256. )),
  257. root,
  258. )
  259. expect(inner(root)).toBe('<i>bar</i><p>foo</p>')
  260. expect(block.dynamicChildren!.length).toBe(0)
  261. })
  262. // A Fragment with `KEYED_FRAGMENT` will always patch its children,
  263. // so there's no need for tracking dynamicChildren.
  264. test('PatchFlags: PatchFlags.KEYED_FRAGMENT', async () => {
  265. const list = [{ tag: 'p', text: 'foo' }]
  266. render(
  267. (openBlock(true),
  268. (block = createBlock(
  269. Fragment,
  270. null,
  271. list.map(item => {
  272. return createVNode(item.tag, { key: item.tag }, item.text)
  273. }),
  274. PatchFlags.KEYED_FRAGMENT,
  275. ))),
  276. root,
  277. )
  278. expect(inner(root)).toBe('<p>foo</p>')
  279. expect(block.dynamicChildren!.length).toBe(0)
  280. list.unshift({ tag: 'i', text: 'bar' })
  281. render(
  282. (openBlock(true),
  283. createBlock(
  284. Fragment,
  285. null,
  286. list.map(item => {
  287. return createVNode(item.tag, { key: item.tag }, item.text)
  288. }),
  289. PatchFlags.KEYED_FRAGMENT,
  290. )),
  291. root,
  292. )
  293. expect(inner(root)).toBe('<i>bar</i><p>foo</p>')
  294. expect(block.dynamicChildren!.length).toBe(0)
  295. })
  296. test('PatchFlags: PatchFlags.NEED_PATCH', async () => {
  297. const spyMounted = vi.fn()
  298. const spyUpdated = vi.fn()
  299. const count = ref(0)
  300. const Comp = {
  301. setup() {
  302. return () => {
  303. count.value
  304. return (
  305. openBlock(),
  306. (block = createBlock('div', null, [
  307. createVNode(
  308. 'p',
  309. { onVnodeMounted: spyMounted, onVnodeBeforeUpdate: spyUpdated },
  310. '',
  311. PatchFlags.NEED_PATCH,
  312. ),
  313. ]))
  314. )
  315. }
  316. },
  317. }
  318. render(h(Comp), root)
  319. expect(inner(root)).toBe('<div><p></p></div>')
  320. expect(block!.dynamicChildren!.length).toBe(1)
  321. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  322. '<p></p>',
  323. )
  324. expect(spyMounted).toHaveBeenCalledTimes(1)
  325. expect(spyUpdated).toHaveBeenCalledTimes(0)
  326. count.value++
  327. await nextTick()
  328. expect(inner(root)).toBe('<div><p></p></div>')
  329. expect(block!.dynamicChildren!.length).toBe(1)
  330. expect(serialize(block!.dynamicChildren![0].el as TestElement)).toBe(
  331. '<p></p>',
  332. )
  333. expect(spyMounted).toHaveBeenCalledTimes(1)
  334. expect(spyUpdated).toHaveBeenCalledTimes(1)
  335. })
  336. test('PatchFlags: PatchFlags.BAIL', async () => {
  337. render(
  338. (openBlock(),
  339. (block = createBlock('div', null, [createVNode('p', null, 'foo')]))),
  340. root,
  341. )
  342. expect(inner(root)).toBe('<div><p>foo</p></div>')
  343. expect(block!.dynamicChildren!.length).toBe(0)
  344. render(
  345. (openBlock(),
  346. (block = createBlock(
  347. 'div',
  348. null,
  349. [createVNode('i', null, 'bar')],
  350. PatchFlags.BAIL,
  351. ))),
  352. root,
  353. )
  354. expect(inner(root)).toBe('<div><i>bar</i></div>')
  355. expect(block!.dynamicChildren).toBe(null)
  356. })
  357. // #1980
  358. test('dynamicChildren should be tracked correctly when normalizing slots to plain children', async () => {
  359. let block: VNode
  360. const Comp = defineComponent({
  361. setup(_props, { slots }) {
  362. return () => {
  363. const vnode =
  364. (openBlock(),
  365. (block = createBlock('div', null, {
  366. default: withCtx(() => [renderSlot(slots, 'default')]),
  367. _: SlotFlags.FORWARDED,
  368. })))
  369. return vnode
  370. }
  371. },
  372. })
  373. const foo = ref(0)
  374. const App = {
  375. setup() {
  376. return () => {
  377. return createBlock(Comp, null, {
  378. default: withCtx(() => [
  379. createVNode('p', null, foo.value, PatchFlags.TEXT),
  380. ]),
  381. // Indicates that this is a stable slot to avoid bail out
  382. _: SlotFlags.STABLE,
  383. })
  384. }
  385. },
  386. }
  387. render(h(App), root)
  388. expect(inner(root)).toBe('<div><p>0</p></div>')
  389. expect(block!.dynamicChildren!.length).toBe(1)
  390. expect(block!.dynamicChildren![0].type).toBe(Fragment)
  391. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  392. expect(
  393. serialize(
  394. block!.dynamicChildren![0].dynamicChildren![0].el as TestElement,
  395. ),
  396. ).toBe('<p>0</p>')
  397. foo.value++
  398. await nextTick()
  399. expect(inner(root)).toBe('<div><p>1</p></div>')
  400. })
  401. // #2169
  402. // block
  403. // - dynamic child (1)
  404. // - component (2)
  405. // When unmounting (1), we know we are in optimized mode so no need to further
  406. // traverse unmount its children
  407. test('should not perform unnecessary unmount traversals', () => {
  408. const spy = vi.fn()
  409. const Child = {
  410. setup() {
  411. onBeforeUnmount(spy)
  412. return () => 'child'
  413. },
  414. }
  415. const Parent = () => (
  416. openBlock(),
  417. createBlock('div', null, [
  418. createVNode('div', { style: {} }, [createVNode(Child)], 4 /* STYLE */),
  419. ])
  420. )
  421. render(h(Parent), root)
  422. render(null, root)
  423. expect(spy).toHaveBeenCalledTimes(1)
  424. })
  425. test('should call onUnmounted hook for dynamic components receiving an existing vnode w/ component children', async () => {
  426. const spy = vi.fn()
  427. const show = ref(1)
  428. const Child = {
  429. setup() {
  430. onUnmounted(spy)
  431. return () => 'child'
  432. },
  433. }
  434. const foo = h('div', null, h(Child))
  435. const app = createApp({
  436. render() {
  437. return show.value
  438. ? (openBlock(),
  439. createBlock('div', null, [(openBlock(), createBlock(foo))]))
  440. : createCommentVNode('v-if', true)
  441. },
  442. })
  443. app.mount(root)
  444. show.value = 0
  445. await nextTick()
  446. expect(spy).toHaveBeenCalledTimes(1)
  447. })
  448. // #2444
  449. // `KEYED_FRAGMENT` and `UNKEYED_FRAGMENT` always need to diff its children
  450. test('non-stable Fragment always need to diff its children', () => {
  451. const spyA = vi.fn()
  452. const spyB = vi.fn()
  453. const ChildA = {
  454. setup() {
  455. onBeforeUnmount(spyA)
  456. return () => 'child'
  457. },
  458. }
  459. const ChildB = {
  460. setup() {
  461. onBeforeUnmount(spyB)
  462. return () => 'child'
  463. },
  464. }
  465. const Parent = () => (
  466. openBlock(),
  467. createBlock('div', null, [
  468. (openBlock(true),
  469. createBlock(
  470. Fragment,
  471. null,
  472. [createVNode(ChildA, { key: 0 })],
  473. 128 /* KEYED_FRAGMENT */,
  474. )),
  475. (openBlock(true),
  476. createBlock(
  477. Fragment,
  478. null,
  479. [createVNode(ChildB)],
  480. 256 /* UNKEYED_FRAGMENT */,
  481. )),
  482. ])
  483. )
  484. render(h(Parent), root)
  485. render(null, root)
  486. expect(spyA).toHaveBeenCalledTimes(1)
  487. expect(spyB).toHaveBeenCalledTimes(1)
  488. })
  489. // #2893
  490. test('manually rendering the optimized slots should allow subsequent updates to exit the optimized mode correctly', async () => {
  491. const state = ref(0)
  492. const CompA = {
  493. name: 'A',
  494. setup(props: any, { slots }: SetupContext) {
  495. return () => {
  496. return (
  497. openBlock(),
  498. createBlock('div', null, [renderSlot(slots, 'default')])
  499. )
  500. }
  501. },
  502. }
  503. const Wrapper = {
  504. name: 'Wrapper',
  505. setup(props: any, { slots }: SetupContext) {
  506. // use the manually written render function to rendering the optimized slots,
  507. // which should make subsequent updates exit the optimized mode correctly
  508. return () => {
  509. return slots.default!()[state.value]
  510. }
  511. },
  512. }
  513. const app = createApp({
  514. name: 'App',
  515. setup() {
  516. return () => {
  517. return (
  518. openBlock(),
  519. createBlock(Wrapper, null, {
  520. default: withCtx(() => [
  521. createVNode(CompA, null, {
  522. default: withCtx(() => [createTextVNode('Hello')]),
  523. _: 1 /* STABLE */,
  524. }),
  525. createVNode(CompA, null, {
  526. default: withCtx(() => [createTextVNode('World')]),
  527. _: 1 /* STABLE */,
  528. }),
  529. ]),
  530. _: 1 /* STABLE */,
  531. })
  532. )
  533. }
  534. },
  535. })
  536. app.mount(root)
  537. expect(inner(root)).toBe('<div>Hello</div>')
  538. state.value = 1
  539. await nextTick()
  540. expect(inner(root)).toBe('<div>World</div>')
  541. })
  542. //#3623
  543. test('nested teleport unmount need exit the optimization mode', () => {
  544. const target = nodeOps.createElement('div')
  545. const root = nodeOps.createElement('div')
  546. render(
  547. (openBlock(),
  548. createBlock('div', null, [
  549. (openBlock(),
  550. createBlock(
  551. Teleport as any,
  552. {
  553. to: target,
  554. },
  555. [
  556. createVNode('div', null, [
  557. (openBlock(),
  558. createBlock(
  559. Teleport as any,
  560. {
  561. to: target,
  562. },
  563. [createVNode('div', null, 'foo')],
  564. )),
  565. ]),
  566. ],
  567. )),
  568. ])),
  569. root,
  570. )
  571. expect(inner(target)).toMatchInlineSnapshot(
  572. `"<div><!--teleport start--><!--teleport end--></div><div>foo</div>"`,
  573. )
  574. expect(inner(root)).toMatchInlineSnapshot(
  575. `"<div><!--teleport start--><!--teleport end--></div>"`,
  576. )
  577. render(null, root)
  578. expect(inner(target)).toBe('')
  579. })
  580. // #3548
  581. test('should not track dynamic children when the user calls a compiled slot inside template expression', () => {
  582. const Comp = {
  583. setup(props: any, { slots }: SetupContext) {
  584. return () => {
  585. return (
  586. openBlock(),
  587. (block = createBlock('section', null, [
  588. renderSlot(slots, 'default'),
  589. ]))
  590. )
  591. }
  592. },
  593. }
  594. let dynamicVNode: VNode
  595. const Wrapper = {
  596. setup(props: any, { slots }: SetupContext) {
  597. return () => {
  598. return (
  599. openBlock(),
  600. createBlock(Comp, null, {
  601. default: withCtx(() => {
  602. return [
  603. (dynamicVNode = createVNode(
  604. 'div',
  605. {
  606. class: {
  607. foo: !!slots.default!(),
  608. },
  609. },
  610. null,
  611. PatchFlags.CLASS,
  612. )),
  613. ]
  614. }),
  615. _: 1,
  616. })
  617. )
  618. }
  619. },
  620. }
  621. const app = createApp({
  622. render() {
  623. return (
  624. openBlock(),
  625. createBlock(Wrapper, null, {
  626. default: withCtx(() => {
  627. return [createVNode({}) /* component */]
  628. }),
  629. _: 1,
  630. })
  631. )
  632. },
  633. })
  634. app.mount(root)
  635. expect(inner(root)).toBe('<section><div class="foo"></div></section>')
  636. /**
  637. * Block Tree:
  638. * - block(div)
  639. * - block(Fragment): renderSlots()
  640. * - dynamicVNode
  641. */
  642. expect(block!.dynamicChildren!.length).toBe(1)
  643. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  644. expect(block!.dynamicChildren![0].dynamicChildren![0]).toEqual(
  645. dynamicVNode!,
  646. )
  647. })
  648. // 3569
  649. test('should force bailout when the user manually calls the slot function', async () => {
  650. const index = ref(0)
  651. const Foo = {
  652. setup(props: any, { slots }: SetupContext) {
  653. return () => {
  654. return slots.default!()[index.value]
  655. }
  656. },
  657. }
  658. const app = createApp({
  659. setup() {
  660. return () => {
  661. return (
  662. openBlock(),
  663. createBlock(Foo, null, {
  664. default: withCtx(() => [
  665. true
  666. ? (openBlock(), createBlock('p', { key: 0 }, '1'))
  667. : createCommentVNode('v-if', true),
  668. true
  669. ? (openBlock(), createBlock('p', { key: 0 }, '2'))
  670. : createCommentVNode('v-if', true),
  671. ]),
  672. _: 1 /* STABLE */,
  673. })
  674. )
  675. }
  676. },
  677. })
  678. app.mount(root)
  679. expect(inner(root)).toBe('<p>1</p>')
  680. index.value = 1
  681. await nextTick()
  682. expect(inner(root)).toBe('<p>2</p>')
  683. index.value = 0
  684. await nextTick()
  685. expect(inner(root)).toBe('<p>1</p>')
  686. })
  687. // #3779
  688. test('treat slots manually written by the user as dynamic', async () => {
  689. const Middle = {
  690. setup(props: any, { slots }: any) {
  691. return slots.default!
  692. },
  693. }
  694. const Comp = {
  695. setup(props: any, { slots }: any) {
  696. return () => {
  697. return (
  698. openBlock(),
  699. createBlock('div', null, [
  700. createVNode(Middle, null, {
  701. default: withCtx(
  702. () => [
  703. createVNode('div', null, [renderSlot(slots, 'default')]),
  704. ],
  705. undefined,
  706. ),
  707. _: 3 /* FORWARDED */,
  708. }),
  709. ])
  710. )
  711. }
  712. },
  713. }
  714. const loading = ref(false)
  715. const app = createApp({
  716. setup() {
  717. return () => {
  718. // important: write the slot content here
  719. const content = h('span', loading.value ? 'loading' : 'loaded')
  720. return h(Comp, null, {
  721. default: () => content,
  722. })
  723. }
  724. },
  725. })
  726. app.mount(root)
  727. expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
  728. loading.value = true
  729. await nextTick()
  730. expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
  731. })
  732. // #3828
  733. test('patch Suspense in optimized mode w/ nested dynamic nodes', async () => {
  734. const show = ref(false)
  735. const app = createApp({
  736. render() {
  737. return (
  738. openBlock(),
  739. createBlock(
  740. Fragment,
  741. null,
  742. [
  743. (openBlock(),
  744. createBlock(SuspenseImpl, null, {
  745. default: withCtx(() => [
  746. createVNode('div', null, [
  747. createVNode('div', null, show.value, PatchFlags.TEXT),
  748. ]),
  749. ]),
  750. _: SlotFlags.STABLE,
  751. })),
  752. ],
  753. PatchFlags.STABLE_FRAGMENT,
  754. )
  755. )
  756. },
  757. })
  758. app.mount(root)
  759. expect(inner(root)).toBe('<div><div>false</div></div>')
  760. show.value = true
  761. await nextTick()
  762. expect(inner(root)).toBe('<div><div>true</div></div>')
  763. })
  764. // #4183
  765. test('should not take unmount children fast path /w Suspense', async () => {
  766. const show = ref(true)
  767. const spyUnmounted = vi.fn()
  768. const Parent = {
  769. setup(props: any, { slots }: SetupContext) {
  770. return () => (
  771. openBlock(),
  772. createBlock(SuspenseImpl, null, {
  773. default: withCtx(() => [renderSlot(slots, 'default')]),
  774. _: SlotFlags.FORWARDED,
  775. })
  776. )
  777. },
  778. }
  779. const Child = {
  780. setup() {
  781. onUnmounted(spyUnmounted)
  782. return () => createVNode('div', null, show.value, PatchFlags.TEXT)
  783. },
  784. }
  785. const app = createApp({
  786. render() {
  787. return show.value
  788. ? (openBlock(),
  789. createBlock(
  790. Parent,
  791. { key: 0 },
  792. {
  793. default: withCtx(() => [createVNode(Child)]),
  794. _: SlotFlags.STABLE,
  795. },
  796. ))
  797. : createCommentVNode('v-if', true)
  798. },
  799. })
  800. app.mount(root)
  801. expect(inner(root)).toBe('<div>true</div>')
  802. show.value = false
  803. await nextTick()
  804. expect(inner(root)).toBe('<!--v-if-->')
  805. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  806. })
  807. // #3881
  808. // root cause: fragment inside a compiled slot passed to component which
  809. // programmatically invokes the slot. The entire slot should de-opt but
  810. // the fragment was incorrectly put in optimized mode which causes it to skip
  811. // updates for its inner components.
  812. test('fragments inside programmatically invoked compiled slot should de-opt properly', async () => {
  813. const Parent: FunctionalComponent = (_, { slots }) => slots.default!()
  814. const Dummy = () => 'dummy'
  815. const toggle = ref(true)
  816. const force = ref(0)
  817. const app = createApp({
  818. render() {
  819. if (!toggle.value) {
  820. return null
  821. }
  822. return h(
  823. Parent,
  824. { n: force.value },
  825. {
  826. default: withCtx(
  827. () => [
  828. createVNode('ul', null, [
  829. (openBlock(),
  830. createBlock(
  831. Fragment,
  832. null,
  833. renderList(1, item => {
  834. return createVNode('li', null, [createVNode(Dummy)])
  835. }),
  836. 64 /* STABLE_FRAGMENT */,
  837. )),
  838. ]),
  839. ],
  840. undefined,
  841. true,
  842. ),
  843. _: 1 /* STABLE */,
  844. },
  845. )
  846. },
  847. })
  848. app.mount(root)
  849. // force a patch
  850. force.value++
  851. await nextTick()
  852. expect(inner(root)).toBe(`<ul><li>dummy</li></ul>`)
  853. // unmount
  854. toggle.value = false
  855. await nextTick()
  856. // should successfully unmount without error
  857. expect(inner(root)).toBe(`<!---->`)
  858. })
  859. })