componentSlots.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
  2. import {
  3. createComponent,
  4. createVaporApp,
  5. defineComponent,
  6. getCurrentInstance,
  7. nextTick,
  8. ref,
  9. template,
  10. } from '../src'
  11. import { makeRender } from './_utils'
  12. const define = makeRender<any>()
  13. function renderWithSlots(slots: any): any {
  14. let instance: any
  15. const Comp = defineComponent({
  16. render() {
  17. const t0 = template('<div></div>')
  18. const n0 = t0()
  19. instance = getCurrentInstance()
  20. return n0
  21. },
  22. })
  23. const { render } = define({
  24. render() {
  25. return createComponent(Comp, {}, slots)
  26. },
  27. })
  28. render()
  29. return instance
  30. }
  31. describe('component: slots', () => {
  32. test('initSlots: instance.slots should be set correctly', () => {
  33. const { slots } = renderWithSlots({ _: 1 })
  34. expect(slots).toMatchObject({ _: 1 })
  35. })
  36. // NOTE: slot normalization is not supported
  37. test.todo(
  38. 'initSlots: should normalize object slots (when value is null, string, array)',
  39. () => {},
  40. )
  41. test.todo(
  42. 'initSlots: should normalize object slots (when value is function)',
  43. () => {},
  44. )
  45. test('initSlots: instance.slots should be set correctly', () => {
  46. let instance: any
  47. const Comp = defineComponent({
  48. render() {
  49. const t0 = template('<div></div>')
  50. const n0 = t0()
  51. instance = getCurrentInstance()
  52. return n0
  53. },
  54. })
  55. const { render } = define({
  56. render() {
  57. return createComponent(Comp, {}, { header: () => template('header')() })
  58. },
  59. })
  60. render()
  61. expect(instance.slots.header()).toMatchObject(
  62. document.createTextNode('header'),
  63. )
  64. })
  65. // runtime-core's "initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)"
  66. test('initSlots: instance.slots should be set correctly', () => {
  67. const { slots } = renderWithSlots({
  68. default: () => template('<span></span>')(),
  69. })
  70. // expect(
  71. // '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
  72. // ).toHaveBeenWarned()
  73. expect(slots.default()).toMatchObject(document.createElement('span'))
  74. })
  75. test('updateSlots: instance.slots should be updated correctly', async () => {
  76. const flag1 = ref(true)
  77. let instance: any
  78. const Child = () => {
  79. instance = getCurrentInstance()
  80. return template('child')()
  81. }
  82. const { render } = define({
  83. render() {
  84. return createComponent(Child, {}, { _: 2 as any }, () => [
  85. flag1.value
  86. ? { name: 'one', fn: () => template('<span></span>')() }
  87. : { name: 'two', fn: () => template('<div></div>')() },
  88. ])
  89. },
  90. })
  91. render()
  92. expect(instance.slots).toHaveProperty('one')
  93. expect(instance.slots).not.toHaveProperty('two')
  94. flag1.value = false
  95. await nextTick()
  96. expect(instance.slots).not.toHaveProperty('one')
  97. expect(instance.slots).toHaveProperty('two')
  98. })
  99. // NOTE: it is not supported
  100. // test('updateSlots: instance.slots should be updated correctly (when slotType is null)', () => {})
  101. // runtime-core's "updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)"
  102. test('updateSlots: instance.slots should be update correctly', async () => {
  103. const flag1 = ref(true)
  104. let instance: any
  105. const Child = () => {
  106. instance = getCurrentInstance()
  107. return template('child')()
  108. }
  109. const { render } = define({
  110. setup() {
  111. return createComponent(Child, {}, {}, () => [
  112. flag1.value
  113. ? [{ name: 'header', fn: () => template('header')() }]
  114. : [{ name: 'footer', fn: () => template('footer')() }],
  115. ])
  116. },
  117. })
  118. render()
  119. expect(instance.slots).toHaveProperty('header')
  120. flag1.value = false
  121. await nextTick()
  122. // expect(
  123. // '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
  124. // ).toHaveBeenWarned()
  125. expect(instance.slots).toHaveProperty('footer')
  126. })
  127. test.todo('should respect $stable flag', async () => {
  128. // TODO: $stable flag?
  129. })
  130. test.todo('should not warn when mounting another app in setup', () => {
  131. // TODO: warning
  132. const Comp = defineComponent({
  133. render() {
  134. const i = getCurrentInstance()
  135. return i!.slots.default!()
  136. },
  137. })
  138. const mountComp = () => {
  139. createVaporApp({
  140. render() {
  141. return createComponent(
  142. Comp,
  143. {},
  144. { default: () => template('msg')() },
  145. )!
  146. },
  147. })
  148. }
  149. const App = {
  150. setup() {
  151. mountComp()
  152. },
  153. render() {
  154. return null!
  155. },
  156. }
  157. createVaporApp(App).mount(document.createElement('div'))
  158. expect(
  159. 'Slot "default" invoked outside of the render function',
  160. ).not.toHaveBeenWarned()
  161. })
  162. })