rendererOptimizedMode.spec.ts 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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', () => {
  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. expect(inner(target)).toMatchInlineSnapshot(
  575. `"<div><!--teleport start--><!--teleport end--></div><div>foo</div>"`,
  576. )
  577. expect(inner(root)).toMatchInlineSnapshot(
  578. `"<div><!--teleport start--><!--teleport end--></div>"`,
  579. )
  580. render(null, root)
  581. expect(inner(target)).toBe('')
  582. })
  583. // #3548
  584. test('should not track dynamic children when the user calls a compiled slot inside template expression', () => {
  585. const Comp = {
  586. setup(props: any, { slots }: SetupContext) {
  587. return () => {
  588. return (
  589. openBlock(),
  590. (block = createBlock('section', null, [
  591. renderSlot(slots, 'default'),
  592. ]))
  593. )
  594. }
  595. },
  596. }
  597. let dynamicVNode: VNode
  598. const Wrapper = {
  599. setup(props: any, { slots }: SetupContext) {
  600. return () => {
  601. return (
  602. openBlock(),
  603. createBlock(Comp, null, {
  604. default: withCtx(() => {
  605. return [
  606. (dynamicVNode = createVNode(
  607. 'div',
  608. {
  609. class: {
  610. foo: !!slots.default!(),
  611. },
  612. },
  613. null,
  614. PatchFlags.CLASS,
  615. )),
  616. ]
  617. }),
  618. _: 1,
  619. })
  620. )
  621. }
  622. },
  623. }
  624. const app = createApp({
  625. render() {
  626. return (
  627. openBlock(),
  628. createBlock(Wrapper, null, {
  629. default: withCtx(() => {
  630. return [createVNode({}) /* component */]
  631. }),
  632. _: 1,
  633. })
  634. )
  635. },
  636. })
  637. app.mount(root)
  638. expect(inner(root)).toBe('<section><div class="foo"></div></section>')
  639. /**
  640. * Block Tree:
  641. * - block(div)
  642. * - block(Fragment): renderSlots()
  643. * - dynamicVNode
  644. */
  645. expect(block!.dynamicChildren!.length).toBe(1)
  646. expect(block!.dynamicChildren![0].dynamicChildren!.length).toBe(1)
  647. expect(block!.dynamicChildren![0].dynamicChildren![0]).toEqual(
  648. dynamicVNode!,
  649. )
  650. })
  651. // 3569
  652. test('should force bailout when the user manually calls the slot function', async () => {
  653. const index = ref(0)
  654. const Foo = {
  655. setup(props: any, { slots }: SetupContext) {
  656. return () => {
  657. return slots.default!()[index.value]
  658. }
  659. },
  660. }
  661. const app = createApp({
  662. setup() {
  663. return () => {
  664. return (
  665. openBlock(),
  666. createBlock(Foo, null, {
  667. default: withCtx(() => [
  668. true
  669. ? (openBlock(), createBlock('p', { key: 0 }, '1'))
  670. : createCommentVNode('v-if', true),
  671. true
  672. ? (openBlock(), createBlock('p', { key: 0 }, '2'))
  673. : createCommentVNode('v-if', true),
  674. ]),
  675. _: 1 /* STABLE */,
  676. })
  677. )
  678. }
  679. },
  680. })
  681. app.mount(root)
  682. expect(inner(root)).toBe('<p>1</p>')
  683. index.value = 1
  684. await nextTick()
  685. expect(inner(root)).toBe('<p>2</p>')
  686. index.value = 0
  687. await nextTick()
  688. expect(inner(root)).toBe('<p>1</p>')
  689. })
  690. // #3779
  691. test('treat slots manually written by the user as dynamic', async () => {
  692. const Middle = {
  693. setup(props: any, { slots }: any) {
  694. return slots.default!
  695. },
  696. }
  697. const Comp = {
  698. setup(props: any, { slots }: any) {
  699. return () => {
  700. return (
  701. openBlock(),
  702. createBlock('div', null, [
  703. createVNode(Middle, null, {
  704. default: withCtx(
  705. () => [
  706. createVNode('div', null, [renderSlot(slots, 'default')]),
  707. ],
  708. undefined,
  709. ),
  710. _: 3 /* FORWARDED */,
  711. }),
  712. ])
  713. )
  714. }
  715. },
  716. }
  717. const loading = ref(false)
  718. const app = createApp({
  719. setup() {
  720. return () => {
  721. // important: write the slot content here
  722. const content = h('span', loading.value ? 'loading' : 'loaded')
  723. return h(Comp, null, {
  724. default: () => content,
  725. })
  726. }
  727. },
  728. })
  729. app.mount(root)
  730. expect(inner(root)).toBe('<div><div><span>loaded</span></div></div>')
  731. loading.value = true
  732. await nextTick()
  733. expect(inner(root)).toBe('<div><div><span>loading</span></div></div>')
  734. })
  735. // #3828
  736. test('patch Suspense in optimized mode w/ nested dynamic nodes', async () => {
  737. const show = ref(false)
  738. const app = createApp({
  739. render() {
  740. return (
  741. openBlock(),
  742. createBlock(
  743. Fragment,
  744. null,
  745. [
  746. (openBlock(),
  747. createBlock(SuspenseImpl, null, {
  748. default: withCtx(() => [
  749. createVNode('div', null, [
  750. createVNode('div', null, show.value, PatchFlags.TEXT),
  751. ]),
  752. ]),
  753. _: SlotFlags.STABLE,
  754. })),
  755. ],
  756. PatchFlags.STABLE_FRAGMENT,
  757. )
  758. )
  759. },
  760. })
  761. app.mount(root)
  762. expect(inner(root)).toBe('<div><div>false</div></div>')
  763. show.value = true
  764. await nextTick()
  765. expect(inner(root)).toBe('<div><div>true</div></div>')
  766. })
  767. // #4183
  768. test('should not take unmount children fast path /w Suspense', async () => {
  769. const show = ref(true)
  770. const spyUnmounted = vi.fn()
  771. const Parent = {
  772. setup(props: any, { slots }: SetupContext) {
  773. return () => (
  774. openBlock(),
  775. createBlock(SuspenseImpl, null, {
  776. default: withCtx(() => [renderSlot(slots, 'default')]),
  777. _: SlotFlags.FORWARDED,
  778. })
  779. )
  780. },
  781. }
  782. const Child = {
  783. setup() {
  784. onUnmounted(spyUnmounted)
  785. return () => createVNode('div', null, show.value, PatchFlags.TEXT)
  786. },
  787. }
  788. const app = createApp({
  789. render() {
  790. return show.value
  791. ? (openBlock(),
  792. createBlock(
  793. Parent,
  794. { key: 0 },
  795. {
  796. default: withCtx(() => [createVNode(Child)]),
  797. _: SlotFlags.STABLE,
  798. },
  799. ))
  800. : createCommentVNode('v-if', true)
  801. },
  802. })
  803. app.mount(root)
  804. expect(inner(root)).toBe('<div>true</div>')
  805. show.value = false
  806. await nextTick()
  807. expect(inner(root)).toBe('<!--v-if-->')
  808. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  809. })
  810. // #3881
  811. // root cause: fragment inside a compiled slot passed to component which
  812. // programmatically invokes the slot. The entire slot should de-opt but
  813. // the fragment was incorrectly put in optimized mode which causes it to skip
  814. // updates for its inner components.
  815. test('fragments inside programmatically invoked compiled slot should de-opt properly', async () => {
  816. const Parent: FunctionalComponent = (_, { slots }) => slots.default!()
  817. const Dummy = () => 'dummy'
  818. const toggle = ref(true)
  819. const force = ref(0)
  820. const app = createApp({
  821. render() {
  822. if (!toggle.value) {
  823. return null
  824. }
  825. return h(
  826. Parent,
  827. { n: force.value },
  828. {
  829. default: withCtx(
  830. () => [
  831. createVNode('ul', null, [
  832. (openBlock(),
  833. createBlock(
  834. Fragment,
  835. null,
  836. renderList(1, item => {
  837. return createVNode('li', null, [createVNode(Dummy)])
  838. }),
  839. 64 /* STABLE_FRAGMENT */,
  840. )),
  841. ]),
  842. ],
  843. undefined,
  844. true,
  845. ),
  846. _: 1 /* STABLE */,
  847. },
  848. )
  849. },
  850. })
  851. app.mount(root)
  852. // force a patch
  853. force.value++
  854. await nextTick()
  855. expect(inner(root)).toBe(`<ul><li>dummy</li></ul>`)
  856. // unmount
  857. toggle.value = false
  858. await nextTick()
  859. // should successfully unmount without error
  860. expect(inner(root)).toBe(`<!---->`)
  861. })
  862. // #10870
  863. test('should bail manually rendered compiler slots for both mount and update', async () => {
  864. // only reproducible in prod
  865. __DEV__ = false
  866. function Outer(_: any, { slots }: any) {
  867. return slots.default()
  868. }
  869. const Mid = {
  870. render(ctx: any) {
  871. return (
  872. openBlock(),
  873. createElementBlock('div', null, [renderSlot(ctx.$slots, 'default')])
  874. )
  875. },
  876. }
  877. const state1 = ref(true)
  878. const state2 = ref(true)
  879. const App = {
  880. render() {
  881. return (
  882. openBlock(),
  883. createBlock(Outer, null, {
  884. default: withCtx(() => [
  885. createVNode(
  886. Mid,
  887. { foo: state2.value },
  888. {
  889. default: withCtx(() => [
  890. createElementVNode('div', null, [
  891. createElementVNode('div', null, [
  892. state2.value
  893. ? (openBlock(),
  894. createElementBlock(
  895. 'div',
  896. {
  897. key: 0,
  898. id: 'if',
  899. foo: state1.value,
  900. },
  901. null,
  902. 8 /* PROPS */,
  903. ['foo'],
  904. ))
  905. : createCommentVNode('v-if', true),
  906. ]),
  907. ]),
  908. ]),
  909. _: 1 /* STABLE */,
  910. },
  911. 8 /* PROPS */,
  912. ['foo'],
  913. ),
  914. ]),
  915. _: 1 /* STABLE */,
  916. })
  917. )
  918. },
  919. }
  920. const app = createApp(App)
  921. app.config.errorHandler = vi.fn()
  922. try {
  923. app.mount(root)
  924. state1.value = false
  925. await nextTick()
  926. state2.value = false
  927. await nextTick()
  928. } finally {
  929. __DEV__ = true
  930. expect(app.config.errorHandler).not.toHaveBeenCalled()
  931. }
  932. })
  933. // #11336
  934. test('should bail manually rendered compiler slots for both mount and update (2)', async () => {
  935. // only reproducible in prod
  936. __DEV__ = false
  937. const n = ref(0)
  938. function Outer(_: any, { slots }: any) {
  939. n.value // track
  940. return slots.default()
  941. }
  942. const Mid = {
  943. render(ctx: any) {
  944. return (
  945. openBlock(),
  946. createElementBlock('div', null, [renderSlot(ctx.$slots, 'default')])
  947. )
  948. },
  949. }
  950. const show = ref(false)
  951. const App = {
  952. render() {
  953. return (
  954. openBlock(),
  955. createBlock(Outer, null, {
  956. default: withCtx(() => [
  957. createVNode(Mid, null, {
  958. default: withCtx(() => [
  959. createElementVNode('div', null, [
  960. show.value
  961. ? (openBlock(),
  962. createElementBlock('div', { key: 0 }, '1'))
  963. : createCommentVNode('v-if', true),
  964. createElementVNode('div', null, '2'),
  965. createElementVNode('div', null, '3'),
  966. ]),
  967. createElementVNode('div', null, '4'),
  968. ]),
  969. _: 1 /* STABLE */,
  970. }),
  971. ]),
  972. _: 1 /* STABLE */,
  973. })
  974. )
  975. },
  976. }
  977. const app = createApp(App)
  978. app.config.errorHandler = vi.fn()
  979. try {
  980. app.mount(root)
  981. // force Outer update, which will assign new slots to Mid
  982. // we want to make sure the compiled slot flag doesn't accidentally
  983. // get assigned again
  984. n.value++
  985. await nextTick()
  986. show.value = true
  987. await nextTick()
  988. } finally {
  989. __DEV__ = true
  990. expect(app.config.errorHandler).not.toHaveBeenCalled()
  991. }
  992. })
  993. test('diff slot and slot fallback node', async () => {
  994. const Comp = {
  995. props: ['show'],
  996. setup(props: any, { slots }: SetupContext) {
  997. return () => {
  998. return (
  999. openBlock(),
  1000. createElementBlock('div', null, [
  1001. renderSlot(slots, 'default', { hide: !props.show }, () => [
  1002. (openBlock(),
  1003. (block = createElementBlock(
  1004. Fragment,
  1005. { key: 0 },
  1006. [createTextVNode('foo')],
  1007. PatchFlags.STABLE_FRAGMENT,
  1008. ))),
  1009. ]),
  1010. ])
  1011. )
  1012. }
  1013. },
  1014. }
  1015. const show = ref(true)
  1016. const app = createApp({
  1017. render() {
  1018. return (
  1019. openBlock(),
  1020. createBlock(
  1021. Comp,
  1022. { show: show.value },
  1023. {
  1024. default: withCtx(({ hide }: { hide: boolean }) => [
  1025. !hide
  1026. ? (openBlock(),
  1027. createElementBlock(
  1028. Fragment,
  1029. { key: 0 },
  1030. [
  1031. createCommentVNode('comment'),
  1032. createElementVNode(
  1033. 'div',
  1034. null,
  1035. 'bar',
  1036. PatchFlags.HOISTED,
  1037. ),
  1038. ],
  1039. PatchFlags.STABLE_FRAGMENT,
  1040. ))
  1041. : createCommentVNode('v-if', true),
  1042. ]),
  1043. _: SlotFlags.STABLE,
  1044. },
  1045. PatchFlags.PROPS,
  1046. ['show'],
  1047. )
  1048. )
  1049. },
  1050. })
  1051. app.mount(root)
  1052. expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
  1053. expect(block).toBe(null)
  1054. show.value = false
  1055. await nextTick()
  1056. expect(inner(root)).toBe('<div>foo</div>')
  1057. show.value = true
  1058. await nextTick()
  1059. expect(inner(root)).toBe('<div><!--comment--><div>bar</div></div>')
  1060. })
  1061. test('should not take unmount children fast path if children contain cached nodes', async () => {
  1062. const show = ref(true)
  1063. const spyUnmounted = vi.fn()
  1064. const Child = {
  1065. setup() {
  1066. onUnmounted(spyUnmounted)
  1067. return () => createVNode('div', null, 'Child')
  1068. },
  1069. }
  1070. const app = createApp({
  1071. render(_: any, cache: any) {
  1072. return show.value
  1073. ? (openBlock(),
  1074. createBlock('div', null, [
  1075. createVNode('div', null, [
  1076. cache[0] ||
  1077. (setBlockTracking(-1),
  1078. ((cache[0] = createVNode('div', null, [
  1079. createVNode(Child),
  1080. ])).cacheIndex = 0),
  1081. setBlockTracking(1),
  1082. cache[0]),
  1083. ]),
  1084. ]))
  1085. : createCommentVNode('v-if', true)
  1086. },
  1087. })
  1088. app.mount(root)
  1089. expect(inner(root)).toBe(
  1090. '<div><div><div><div>Child</div></div></div></div>',
  1091. )
  1092. show.value = false
  1093. await nextTick()
  1094. expect(inner(root)).toBe('<!--v-if-->')
  1095. expect(spyUnmounted).toHaveBeenCalledTimes(1)
  1096. show.value = true
  1097. await nextTick()
  1098. expect(inner(root)).toBe(
  1099. '<div><div><div><div>Child</div></div></div></div>',
  1100. )
  1101. // should unmount again, this verifies previous cache was properly cleared
  1102. show.value = false
  1103. await nextTick()
  1104. expect(inner(root)).toBe('<!--v-if-->')
  1105. expect(spyUnmounted).toHaveBeenCalledTimes(2)
  1106. })
  1107. })