rendererOptimizedMode.spec.ts 32 KB

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