componentSlots.spec.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import {
  2. createApp,
  3. getCurrentInstance,
  4. h,
  5. nextTick,
  6. nodeOps,
  7. ref,
  8. render,
  9. } from '@vue/runtime-test'
  10. import { createBlock, normalizeVNode } from '../src/vnode'
  11. import { createSlots } from '../src/helpers/createSlots'
  12. describe('component: slots', () => {
  13. function renderWithSlots(slots: any): any {
  14. let instance: any
  15. const Comp = {
  16. render() {
  17. instance = getCurrentInstance()
  18. return h('div')
  19. },
  20. }
  21. render(h(Comp, null, slots), nodeOps.createElement('div'))
  22. return instance
  23. }
  24. test('initSlots: instance.slots should be set correctly', () => {
  25. let instance: any
  26. const Comp = {
  27. render() {
  28. instance = getCurrentInstance()
  29. return h('div')
  30. },
  31. }
  32. const slots = { foo: () => {}, _: 1 }
  33. render(createBlock(Comp, null, slots), nodeOps.createElement('div'))
  34. expect(instance.slots).toMatchObject(slots)
  35. })
  36. test('initSlots: instance.slots should remove compiler marker if parent is using manual render function', () => {
  37. const { slots } = renderWithSlots({ _: 1 })
  38. expect(slots).toMatchObject({})
  39. })
  40. test('initSlots: should normalize object slots (when value is null, string, array)', () => {
  41. const { slots } = renderWithSlots({
  42. _inner: '_inner',
  43. foo: null,
  44. header: 'header',
  45. footer: ['f1', 'f2'],
  46. })
  47. expect(
  48. '[Vue warn]: Non-function value encountered for slot "header". Prefer function slots for better performance.',
  49. ).toHaveBeenWarned()
  50. expect(
  51. '[Vue warn]: Non-function value encountered for slot "footer". Prefer function slots for better performance.',
  52. ).toHaveBeenWarned()
  53. expect(slots).not.toHaveProperty('_inner')
  54. expect(slots).not.toHaveProperty('foo')
  55. expect(slots.header()).toMatchObject([normalizeVNode('header')])
  56. expect(slots.footer()).toMatchObject([
  57. normalizeVNode('f1'),
  58. normalizeVNode('f2'),
  59. ])
  60. })
  61. test('initSlots: should normalize object slots (when value is function)', () => {
  62. let proxy: any
  63. const Comp = {
  64. render() {
  65. proxy = getCurrentInstance()
  66. return h('div')
  67. },
  68. }
  69. render(
  70. h(Comp, null, {
  71. header: () => 'header',
  72. }),
  73. nodeOps.createElement('div'),
  74. )
  75. expect(proxy.slots.header()).toMatchObject([normalizeVNode('header')])
  76. })
  77. test('initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)', () => {
  78. const { slots } = renderWithSlots([h('span')])
  79. expect(
  80. '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
  81. ).toHaveBeenWarned()
  82. expect(slots.default()).toMatchObject([normalizeVNode(h('span'))])
  83. })
  84. test('updateSlots: instance.slots should be updated correctly (when slotType is number)', async () => {
  85. const flag1 = ref(true)
  86. let instance: any
  87. const Child = () => {
  88. instance = getCurrentInstance()
  89. return 'child'
  90. }
  91. const Comp = {
  92. setup() {
  93. return () => [
  94. h(
  95. Child,
  96. null,
  97. createSlots({ _: 2 as any }, [
  98. flag1.value
  99. ? {
  100. name: 'one',
  101. fn: () => [h('span')],
  102. }
  103. : {
  104. name: 'two',
  105. fn: () => [h('div')],
  106. },
  107. ]),
  108. ),
  109. ]
  110. },
  111. }
  112. render(h(Comp), nodeOps.createElement('div'))
  113. expect(instance.slots).toHaveProperty('one')
  114. expect(instance.slots).not.toHaveProperty('two')
  115. flag1.value = false
  116. await nextTick()
  117. expect(instance.slots).not.toHaveProperty('one')
  118. expect(instance.slots).toHaveProperty('two')
  119. })
  120. test('updateSlots: instance.slots should be updated correctly (when slotType is null)', async () => {
  121. const flag1 = ref(true)
  122. let instance: any
  123. const Child = () => {
  124. instance = getCurrentInstance()
  125. return 'child'
  126. }
  127. const oldSlots = {
  128. header: 'header',
  129. footer: undefined,
  130. }
  131. const newSlots = {
  132. header: undefined,
  133. footer: 'footer',
  134. }
  135. const Comp = {
  136. setup() {
  137. return () => [
  138. h(Child, { n: flag1.value }, flag1.value ? oldSlots : newSlots),
  139. ]
  140. },
  141. }
  142. render(h(Comp), nodeOps.createElement('div'))
  143. expect(instance.slots).toHaveProperty('header')
  144. expect(instance.slots).not.toHaveProperty('footer')
  145. flag1.value = false
  146. await nextTick()
  147. expect(
  148. '[Vue warn]: Non-function value encountered for slot "header". Prefer function slots for better performance.',
  149. ).toHaveBeenWarned()
  150. expect(
  151. '[Vue warn]: Non-function value encountered for slot "footer". Prefer function slots for better performance.',
  152. ).toHaveBeenWarned()
  153. expect(instance.slots).not.toHaveProperty('header')
  154. expect(instance.slots.footer()).toMatchObject([normalizeVNode('footer')])
  155. })
  156. test('updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)', async () => {
  157. const flag1 = ref(true)
  158. let instance: any
  159. const Child = () => {
  160. instance = getCurrentInstance()
  161. return 'child'
  162. }
  163. const Comp = {
  164. setup() {
  165. return () => [
  166. h(Child, { n: flag1.value }, flag1.value ? ['header'] : ['footer']),
  167. ]
  168. },
  169. }
  170. render(h(Comp), nodeOps.createElement('div'))
  171. expect(instance.slots.default()).toMatchObject([normalizeVNode('header')])
  172. flag1.value = false
  173. await nextTick()
  174. expect(
  175. '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
  176. ).toHaveBeenWarned()
  177. expect(instance.slots.default()).toMatchObject([normalizeVNode('footer')])
  178. })
  179. test('should respect $stable flag with a value of true', async () => {
  180. const flag1 = ref(1)
  181. const flag2 = ref(2)
  182. const spy = vi.fn()
  183. const Child = () => {
  184. spy()
  185. return 'child'
  186. }
  187. const App = {
  188. setup() {
  189. return () => [
  190. flag1.value,
  191. h(
  192. Child,
  193. { n: flag2.value },
  194. {
  195. foo: () => 'foo',
  196. $stable: true,
  197. },
  198. ),
  199. ]
  200. },
  201. }
  202. render(h(App), nodeOps.createElement('div'))
  203. expect(spy).toHaveBeenCalledTimes(1)
  204. // parent re-render, props didn't change, slots are stable
  205. // -> child should not update
  206. flag1.value++
  207. await nextTick()
  208. expect(spy).toHaveBeenCalledTimes(1)
  209. // parent re-render, props changed
  210. // -> child should update
  211. flag2.value++
  212. await nextTick()
  213. expect(spy).toHaveBeenCalledTimes(2)
  214. })
  215. test('should respect $stable flag with a value of false', async () => {
  216. const flag1 = ref(1)
  217. const flag2 = ref(2)
  218. const spy = vi.fn()
  219. const Child = () => {
  220. spy()
  221. return 'child'
  222. }
  223. const App = {
  224. setup() {
  225. return () => [
  226. flag1.value,
  227. h(
  228. Child,
  229. { n: flag2.value },
  230. {
  231. foo: () => 'foo',
  232. $stable: false,
  233. },
  234. ),
  235. ]
  236. },
  237. }
  238. render(h(App), nodeOps.createElement('div'))
  239. expect(spy).toHaveBeenCalledTimes(1)
  240. // parent re-render, props didn't change, slots are not stable
  241. // -> child should update
  242. flag1.value++
  243. await nextTick()
  244. expect(spy).toHaveBeenCalledTimes(2)
  245. // parent re-render, props changed
  246. // -> child should update
  247. flag2.value++
  248. await nextTick()
  249. expect(spy).toHaveBeenCalledTimes(3)
  250. })
  251. test('should not warn when mounting another app in setup', () => {
  252. const Comp = {
  253. setup(_: any, { slots }: any) {
  254. return () => slots.default?.()
  255. },
  256. }
  257. const mountComp = () => {
  258. createApp({
  259. setup() {
  260. return () => h(Comp, () => 'msg')
  261. },
  262. }).mount(nodeOps.createElement('div'))
  263. }
  264. const App = {
  265. setup() {
  266. mountComp()
  267. return () => null
  268. },
  269. }
  270. createApp(App).mount(nodeOps.createElement('div'))
  271. expect(
  272. 'Slot "default" invoked outside of the render function',
  273. ).not.toHaveBeenWarned()
  274. })
  275. })