componentSlots.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
  2. import {
  3. createComponent,
  4. // @ts-expect-error
  5. createForSlots,
  6. createSlot,
  7. createVaporApp,
  8. defineVaporComponent,
  9. insert,
  10. prepend,
  11. renderEffect,
  12. setText,
  13. template,
  14. } from '../src'
  15. import { currentInstance, nextTick, ref } from '@vue/runtime-dom'
  16. import { makeRender } from './_utils'
  17. const define = makeRender<any>()
  18. function renderWithSlots(slots: any): any {
  19. let instance: any
  20. const Comp = defineVaporComponent({
  21. setup() {
  22. const t0 = template('<div></div>')
  23. const n0 = t0()
  24. instance = currentInstance
  25. return n0
  26. },
  27. })
  28. const { render } = define({
  29. render() {
  30. return createComponent(Comp, {}, slots)
  31. },
  32. })
  33. render()
  34. return instance
  35. }
  36. describe('component: slots', () => {
  37. test('initSlots: instance.slots should be set correctly', () => {
  38. const { slots } = renderWithSlots({
  39. default: () => template('<span></span>')(),
  40. })
  41. expect(slots.default()).toMatchObject(document.createElement('span'))
  42. })
  43. test('updateSlots: instance.slots should be updated correctly', async () => {
  44. const flag1 = ref(true)
  45. let instance: any
  46. const Child = () => {
  47. instance = currentInstance
  48. return template('child')()
  49. }
  50. const { render } = define({
  51. render() {
  52. return createComponent(
  53. Child,
  54. {},
  55. {
  56. $: [
  57. () =>
  58. flag1.value
  59. ? { name: 'one', fn: () => template('<span></span>')() }
  60. : { name: 'two', fn: () => template('<div></div>')() },
  61. ],
  62. },
  63. )
  64. },
  65. })
  66. render()
  67. expect(instance.slots).toHaveProperty('one')
  68. expect(instance.slots).not.toHaveProperty('two')
  69. flag1.value = false
  70. await nextTick()
  71. expect(instance.slots).not.toHaveProperty('one')
  72. expect(instance.slots).toHaveProperty('two')
  73. })
  74. test.todo('should work with createFlorSlots', async () => {
  75. const loop = ref([1, 2, 3])
  76. let instance: any
  77. const Child = () => {
  78. instance = currentInstance
  79. return template('child')()
  80. }
  81. const { render } = define({
  82. setup() {
  83. return createComponent(Child, null, {
  84. $: [
  85. () =>
  86. // @ts-expect-error
  87. createForSlots(loop.value, (item, i) => ({
  88. name: item,
  89. fn: () => template(item + i)(),
  90. })),
  91. ],
  92. })
  93. },
  94. })
  95. render()
  96. expect(instance.slots).toHaveProperty('1')
  97. expect(instance.slots).toHaveProperty('2')
  98. expect(instance.slots).toHaveProperty('3')
  99. loop.value.push(4)
  100. await nextTick()
  101. expect(instance.slots).toHaveProperty('4')
  102. loop.value.shift()
  103. await nextTick()
  104. expect(instance.slots).not.toHaveProperty('1')
  105. })
  106. // passes but no warning for slot invocation in vapor currently
  107. test.todo('should not warn when mounting another app in setup', () => {
  108. const Comp = defineVaporComponent({
  109. setup(_, { slots }) {
  110. return slots.default!()
  111. },
  112. })
  113. const mountComp = () => {
  114. createVaporApp({
  115. render() {
  116. return createComponent(
  117. Comp,
  118. {},
  119. { default: () => template('msg')() },
  120. )!
  121. },
  122. })
  123. }
  124. const App = {
  125. setup() {
  126. mountComp()
  127. return []
  128. },
  129. }
  130. createVaporApp(App).mount(document.createElement('div'))
  131. expect(
  132. 'Slot "default" invoked outside of the render function',
  133. ).not.toHaveBeenWarned()
  134. })
  135. describe('createSlot', () => {
  136. test('slot should be rendered correctly', () => {
  137. const Comp = defineVaporComponent(() => {
  138. const n0 = template('<div>')()
  139. insert(createSlot('header'), n0 as any as ParentNode)
  140. return n0
  141. })
  142. const { host } = define(() => {
  143. return createComponent(Comp, null, {
  144. header: () => template('header')(),
  145. })
  146. }).render()
  147. expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
  148. })
  149. test('slot should be rendered correctly with slot props', async () => {
  150. const Comp = defineVaporComponent(() => {
  151. const n0 = template('<div></div>')()
  152. insert(
  153. createSlot('header', { title: () => 'header' }),
  154. n0 as any as ParentNode,
  155. )
  156. return n0
  157. })
  158. const { host } = define(() => {
  159. return createComponent(Comp, null, {
  160. header: props => {
  161. const el = template('<h1></h1>')()
  162. renderEffect(() => {
  163. setText(el, props.title)
  164. })
  165. return el
  166. },
  167. })
  168. }).render()
  169. expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
  170. })
  171. test('dynamic slot props', async () => {
  172. let props: any
  173. const bindObj = ref<Record<string, any>>({ foo: 1, baz: 'qux' })
  174. const Comp = defineVaporComponent(() =>
  175. createSlot('default', { $: [() => bindObj.value] }),
  176. )
  177. define(() =>
  178. createComponent(Comp, null, {
  179. default: _props => ((props = _props), []),
  180. }),
  181. ).render()
  182. expect(props).toEqual({ foo: 1, baz: 'qux' })
  183. bindObj.value.foo = 2
  184. await nextTick()
  185. expect(props).toEqual({ foo: 2, baz: 'qux' })
  186. delete bindObj.value.baz
  187. await nextTick()
  188. expect(props).toEqual({ foo: 2 })
  189. })
  190. test('dynamic slot props with static slot props', async () => {
  191. let props: any
  192. const foo = ref(0)
  193. const bindObj = ref<Record<string, any>>({ foo: 100, baz: 'qux' })
  194. const Comp = defineVaporComponent(() =>
  195. createSlot('default', {
  196. foo: () => foo.value,
  197. $: [() => bindObj.value],
  198. }),
  199. )
  200. define(() =>
  201. createComponent(Comp, null, {
  202. default: _props => ((props = _props), []),
  203. }),
  204. ).render()
  205. expect(props).toEqual({ foo: 100, baz: 'qux' })
  206. foo.value = 2
  207. await nextTick()
  208. expect(props).toEqual({ foo: 100, baz: 'qux' })
  209. delete bindObj.value.foo
  210. await nextTick()
  211. expect(props).toEqual({ foo: 2, baz: 'qux' })
  212. })
  213. test('dynamic slot should be rendered correctly with slot props', async () => {
  214. const val = ref('header')
  215. const Comp = defineVaporComponent(() => {
  216. const n0 = template('<div></div>')()
  217. prepend(
  218. n0 as any as ParentNode,
  219. createSlot('header', { title: () => val.value }),
  220. )
  221. return n0
  222. })
  223. const { host } = define(() => {
  224. // dynamic slot
  225. return createComponent(Comp, null, {
  226. $: [
  227. () => ({
  228. name: 'header',
  229. fn: (props: any) => template(props.title)(),
  230. }),
  231. ],
  232. })
  233. }).render()
  234. expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
  235. val.value = 'footer'
  236. await nextTick()
  237. expect(host.innerHTML).toBe('<div>footer<!--slot--></div>')
  238. })
  239. test('dynamic slot outlet should be render correctly with slot props', async () => {
  240. const val = ref('header')
  241. const Comp = defineVaporComponent(() => {
  242. const n0 = template('<div></div>')()
  243. prepend(
  244. n0 as any as ParentNode,
  245. createSlot(
  246. () => val.value, // dynamic slot outlet name
  247. ),
  248. )
  249. return n0
  250. })
  251. const { host } = define(() => {
  252. return createComponent(Comp, null, {
  253. header: () => template('header')(),
  254. footer: () => template('footer')(),
  255. })
  256. }).render()
  257. expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
  258. val.value = 'footer'
  259. await nextTick()
  260. expect(host.innerHTML).toBe('<div>footer<!--slot--></div>')
  261. })
  262. test('fallback should be render correctly', () => {
  263. const Comp = defineVaporComponent(() => {
  264. const n0 = template('<div></div>')()
  265. insert(
  266. createSlot('header', undefined, () => template('fallback')()),
  267. n0 as any as ParentNode,
  268. )
  269. return n0
  270. })
  271. const { host } = define(() => {
  272. return createComponent(Comp, {}, {})
  273. }).render()
  274. expect(host.innerHTML).toBe('<div>fallback<!--slot--></div>')
  275. })
  276. test('dynamic slot should be updated correctly', async () => {
  277. const flag1 = ref(true)
  278. const Child = defineVaporComponent(() => {
  279. const temp0 = template('<p></p>')
  280. const el0 = temp0()
  281. const el1 = temp0()
  282. const slot1 = createSlot('one', null, () => template('one fallback')())
  283. const slot2 = createSlot('two', null, () => template('two fallback')())
  284. insert(slot1, el0 as any as ParentNode)
  285. insert(slot2, el1 as any as ParentNode)
  286. return [el0, el1]
  287. })
  288. const { host } = define(() => {
  289. return createComponent(Child, null, {
  290. $: [
  291. () =>
  292. flag1.value
  293. ? { name: 'one', fn: () => template('one content')() }
  294. : { name: 'two', fn: () => template('two content')() },
  295. ],
  296. })
  297. }).render()
  298. expect(host.innerHTML).toBe(
  299. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  300. )
  301. flag1.value = false
  302. await nextTick()
  303. expect(host.innerHTML).toBe(
  304. '<p>one fallback<!--slot--></p><p>two content<!--slot--></p>',
  305. )
  306. flag1.value = true
  307. await nextTick()
  308. expect(host.innerHTML).toBe(
  309. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  310. )
  311. })
  312. test('dynamic slot outlet should be updated correctly', async () => {
  313. const slotOutletName = ref('one')
  314. const Child = defineVaporComponent(() => {
  315. const temp0 = template('<p>')
  316. const el0 = temp0()
  317. const slot1 = createSlot(
  318. () => slotOutletName.value,
  319. undefined,
  320. () => template('fallback')(),
  321. )
  322. insert(slot1, el0 as any as ParentNode)
  323. return el0
  324. })
  325. const { host } = define(() => {
  326. return createComponent(
  327. Child,
  328. {},
  329. {
  330. one: () => template('one content')(),
  331. two: () => template('two content')(),
  332. },
  333. )
  334. }).render()
  335. expect(host.innerHTML).toBe('<p>one content<!--slot--></p>')
  336. slotOutletName.value = 'two'
  337. await nextTick()
  338. expect(host.innerHTML).toBe('<p>two content<!--slot--></p>')
  339. slotOutletName.value = 'none'
  340. await nextTick()
  341. expect(host.innerHTML).toBe('<p>fallback<!--slot--></p>')
  342. })
  343. test('non-exist slot', async () => {
  344. const Child = defineVaporComponent(() => {
  345. const el0 = template('<p>')()
  346. const slot = createSlot('not-exist', undefined)
  347. insert(slot, el0 as any as ParentNode)
  348. return el0
  349. })
  350. const { host } = define(() => {
  351. return createComponent(Child)
  352. }).render()
  353. expect(host.innerHTML).toBe('<p><!--slot--></p>')
  354. })
  355. })
  356. })