rendererOptimizedMode.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 createVNode(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. setup(props: any, { slots }: SetupContext) {
  494. return () => {
  495. return (
  496. openBlock(),
  497. createBlock('div', null, [renderSlot(slots, 'default')])
  498. )
  499. }
  500. },
  501. }
  502. const Wrapper = {
  503. setup(props: any, { slots }: SetupContext) {
  504. // use the manually written render function to rendering the optimized slots,
  505. // which should make subsequent updates exit the optimized mode correctly
  506. return () => {
  507. return slots.default!()[state.value]
  508. }
  509. },
  510. }
  511. const app = createApp({
  512. setup() {
  513. return () => {
  514. return (
  515. openBlock(),
  516. createBlock(Wrapper, null, {
  517. default: withCtx(() => [
  518. createVNode(CompA, null, {
  519. default: withCtx(() => [createTextVNode('Hello')]),
  520. _: 1 /* STABLE */,
  521. }),
  522. createVNode(CompA, null, {
  523. default: withCtx(() => [createTextVNode('World')]),
  524. _: 1 /* STABLE */,
  525. }),
  526. ]),
  527. _: 1 /* STABLE */,
  528. })
  529. )
  530. }
  531. },
  532. })
  533. app.mount(root)
  534. expect(inner(root)).toBe('<div>Hello</div>')
  535. state.value = 1
  536. await nextTick()
  537. expect(inner(root)).toBe('<div>World</div>')
  538. })
  539. //#3623
  540. test('nested teleport unmount need exit the optimization mode', () => {
  541. const target = nodeOps.createElement('div')
  542. const root = nodeOps.createElement('div')
  543. render(
  544. (openBlock(),
  545. createBlock('div', null, [
  546. (openBlock(),
  547. createBlock(
  548. Teleport as any,
  549. {
  550. to: target,
  551. },
  552. [
  553. createVNode('div', null, [
  554. (openBlock(),
  555. createBlock(
  556. Teleport as any,
  557. {
  558. to: target,
  559. },
  560. [createVNode('div', null, 'foo')],
  561. )),
  562. ]),
  563. ],
  564. )),
  565. ])),
  566. root,
  567. )
  568. expect(inner(target)).toMatchInlineSnapshot(
  569. `"<div><!--teleport start--><!--teleport end--></div><div>foo</div>"`,
  570. )
  571. expect(inner(root)).toMatchInlineSnapshot(
  572. `"<div><!--teleport start--><!--teleport end--></div>"`,
  573. )
  574. render(null, root)
  575. expect(inner(target)).toBe('')
  576. })
  577. // #3548
  578. test('should not track dynamic children when the user calls a compiled slot inside template expression', () => {
  579. const Comp = {
  580. setup(props: any, { slots }: SetupContext) {
  581. return () => {
  582. return (
  583. openBlock(),
  584. (block = createBlock('section', null, [
  585. renderSlot(slots, 'default'),
  586. ]))
  587. )
  588. }
  589. },
  590. }
  591. let dynamicVNode: VNode
  592. const Wrapper = {
  593. setup(props: any, { slots }: SetupContext) {
  594. return () => {
  595. return (
  596. openBlock(),
  597. createBlock(Comp, null, {
  598. default: withCtx(() => {
  599. return [
  600. (dynamicVNode = createVNode(
  601. 'div',
  602. {
  603. class: {
  604. foo: !!slots.default!(),
  605. },
  606. },
  607. null,
  608. PatchFlags.CLASS,
  609. )),
  610. ]
  611. }),
  612. _: 1,
  613. })
  614. )
  615. }
  616. },
  617. }
  618. const app = createApp({
  619. render() {
  620. return (
  621. openBlock(),
  622. createBlock(Wrapper, null, {
  623. default: withCtx(() => {
  624. return [createVNode({}) /* component */]
  625. }),
  626. _: 1,
  627. })
  628. )
  629. },
  630. })
  631. app.mount(root)
  632. expect(inner(root)).toBe('<section><div class="foo"></div></section>')
  633. /**
  634. * Block Tree:
  635. * - block(div)
  636. * - block(Fragment): renderSlots()
  637. * - dynamicVNode
  638. */
  639. expect(block!.dynamicChildren!.length).toBe(1)
  640. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  641. expect(block!.dynamicChildren![0].dynamicChildren![0]).toEqual(
  642. dynamicVNode!,
  643. )
  644. })
  645. // 3569
  646. test('should force bailout when the user manually calls the slot function', async () => {
  647. const index = ref(0)
  648. const Foo = {
  649. setup(props: any, { slots }: SetupContext) {
  650. return () => {
  651. return slots.default!()[index.value]
  652. }
  653. },
  654. }
  655. const app = createApp({
  656. setup() {
  657. return () => {
  658. return (
  659. openBlock(),
  660. createBlock(Foo, null, {
  661. default: withCtx(() => [
  662. true
  663. ? (openBlock(), createBlock('p', { key: 0 }, '1'))
  664. : createCommentVNode('v-if', true),
  665. true
  666. ? (openBlock(), createBlock('p', { key: 0 }, '2'))
  667. : createCommentVNode('v-if', true),
  668. ]),
  669. _: 1 /* STABLE */,
  670. })
  671. )
  672. }
  673. },
  674. })
  675. app.mount(root)
  676. expect(inner(root)).toBe('<p>1</p>')
  677. index.value = 1
  678. await nextTick()
  679. expect(inner(root)).toBe('<p>2</p>')
  680. index.value = 0
  681. await nextTick()
  682. expect(inner(root)).toBe('<p>1</p>')
  683. })
  684. // #3779
  685. test('treat slots manually written by the user as dynamic', async () => {
  686. const Middle = {
  687. setup(props: any, { slots }: any) {
  688. return slots.default!
  689. },
  690. }
  691. const Comp = {
  692. setup(props: any, { slots }: any) {
  693. return () => {
  694. return (
  695. openBlock(),
  696. createBlock('div', null, [
  697. createVNode(Middle, null, {
  698. default: withCtx(
  699. () => [
  700. createVNode('div', null, [renderSlot(slots, 'default')]),
  701. ],
  702. undefined,
  703. ),
  704. _: 3 /* FORWARDED */,
  705. }),
  706. ])
  707. )
  708. }
  709. },
  710. }
  711. const loading = ref(false)
  712. const app = createApp({
  713. setup() {
  714. return () => {
  715. // important: write the slot content here
  716. const content = h('span', loading.value ? 'loading' : 'loaded')
  717. return h(Comp, null, {
  718. default: () => content,
  719. })
  720. }
  721. },
  722. })
  723. app.mount(root)
  724. expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
  725. loading.value = true
  726. await nextTick()
  727. expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
  728. })
  729. // #3828
  730. test('patch Suspense in optimized mode w/ nested dynamic nodes', async () => {
  731. const show = ref(false)
  732. const app = createApp({
  733. render() {
  734. return (
  735. openBlock(),
  736. createBlock(
  737. Fragment,
  738. null,
  739. [
  740. (openBlock(),
  741. createBlock(SuspenseImpl, null, {
  742. default: withCtx(() => [
  743. createVNode('div', null, [
  744. createVNode('div', null, show.value, PatchFlags.TEXT),
  745. ]),
  746. ]),
  747. _: SlotFlags.STABLE,
  748. })),
  749. ],
  750. PatchFlags.STABLE_FRAGMENT,
  751. )
  752. )
  753. },
  754. })
  755. app.mount(root)
  756. expect(inner(root)).toBe('<div><div>false</div></div>')
  757. show.value = true
  758. await nextTick()
  759. expect(inner(root)).toBe('<div><div>true</div></div>')
  760. })
  761. // #4183
  762. test('should not take unmount children fast path /w Suspense', async () => {
  763. const show = ref(true)
  764. const spyUnmounted = vi.fn()
  765. const Parent = {
  766. setup(props: any, { slots }: SetupContext) {
  767. return () => (
  768. openBlock(),
  769. createBlock(SuspenseImpl, null, {
  770. default: withCtx(() => [renderSlot(slots, 'default')]),
  771. _: SlotFlags.FORWARDED,
  772. })
  773. )
  774. },
  775. }
  776. const Child = {
  777. setup() {
  778. onUnmounted(spyUnmounted)
  779. return () => createVNode('div', null, show.value, PatchFlags.TEXT)
  780. },
  781. }
  782. const app = createApp({
  783. render() {
  784. return show.value
  785. ? (openBlock(),
  786. createBlock(
  787. Parent,
  788. { key: 0 },
  789. {
  790. default: withCtx(() => [createVNode(Child)]),
  791. _: SlotFlags.STABLE,
  792. },
  793. ))
  794. : createCommentVNode('v-if', true)
  795. },
  796. })
  797. app.mount(root)
  798. expect(inner(root)).toBe('<div>true</div>')
  799. show.value = false
  800. await nextTick()
  801. expect(inner(root)).toBe('<!--v-if-->')
  802. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  803. })
  804. // #3881
  805. // root cause: fragment inside a compiled slot passed to component which
  806. // programmatically invokes the slot. The entire slot should de-opt but
  807. // the fragment was incorrectly put in optimized mode which causes it to skip
  808. // updates for its inner components.
  809. test('fragments inside programmatically invoked compiled slot should de-opt properly', async () => {
  810. const Parent: FunctionalComponent = (_, { slots }) => slots.default!()
  811. const Dummy = () => 'dummy'
  812. const toggle = ref(true)
  813. const force = ref(0)
  814. const app = createApp({
  815. render() {
  816. if (!toggle.value) {
  817. return null
  818. }
  819. return h(
  820. Parent,
  821. { n: force.value },
  822. {
  823. default: withCtx(
  824. () => [
  825. createVNode('ul', null, [
  826. (openBlock(),
  827. createBlock(
  828. Fragment,
  829. null,
  830. renderList(1, item => {
  831. return createVNode('li', null, [createVNode(Dummy)])
  832. }),
  833. 64 /* STABLE_FRAGMENT */,
  834. )),
  835. ]),
  836. ],
  837. undefined,
  838. true,
  839. ),
  840. _: 1 /* STABLE */,
  841. },
  842. )
  843. },
  844. })
  845. app.mount(root)
  846. // force a patch
  847. force.value++
  848. await nextTick()
  849. expect(inner(root)).toBe(`<ul><li>dummy</li></ul>`)
  850. // unmount
  851. toggle.value = false
  852. await nextTick()
  853. // should successfully unmount without error
  854. expect(inner(root)).toBe(`<!---->`)
  855. })
  856. })