rendererOptimizedMode.spec.ts 27 KB

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