apiSetupHelpers.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { createComponent, defineVaporComponent, template } from '../src'
  2. import { ref, useAttrs, useSlots } from '@vue/runtime-dom'
  3. import { makeRender } from './_utils'
  4. import type { VaporComponentInstance } from '../src/component'
  5. const define = makeRender<any>()
  6. describe.todo('SFC <script setup> helpers', () => {
  7. test.todo('should warn runtime usage', () => {})
  8. test('useSlots / useAttrs (no args)', () => {
  9. let slots: VaporComponentInstance['slots'] | undefined
  10. let attrs: VaporComponentInstance['attrs'] | undefined
  11. const Comp = defineVaporComponent({
  12. setup() {
  13. // @ts-expect-error
  14. slots = useSlots()
  15. attrs = useAttrs()
  16. return []
  17. },
  18. })
  19. const count = ref(0)
  20. const passedAttrs = { id: () => count.value }
  21. const passedSlots = {
  22. default: () => template('')(),
  23. x: () => template('')(),
  24. }
  25. const { render } = define({
  26. render: () => createComponent(Comp, passedAttrs, passedSlots),
  27. })
  28. render()
  29. expect(typeof slots!.default).toBe('function')
  30. expect(typeof slots!.x).toBe('function')
  31. expect(attrs).toMatchObject({ id: 0 })
  32. count.value++
  33. expect(attrs).toMatchObject({ id: 1 })
  34. })
  35. test('useSlots / useAttrs (with args)', () => {
  36. let slots: VaporComponentInstance['slots'] | undefined
  37. let attrs: VaporComponentInstance['attrs'] | undefined
  38. let ctx: VaporComponentInstance | undefined
  39. const Comp = defineVaporComponent({
  40. setup(_, _ctx) {
  41. // @ts-expect-error
  42. slots = useSlots()
  43. attrs = useAttrs()
  44. ctx = _ctx as VaporComponentInstance
  45. return []
  46. },
  47. })
  48. const { render } = define({ render: () => createComponent(Comp) })
  49. render()
  50. expect(slots).toBe(ctx!.slots)
  51. expect(attrs).toBe(ctx!.attrs)
  52. })
  53. describe.todo('mergeDefaults', () => {
  54. test.todo('object syntax', () => {})
  55. test.todo('array syntax', () => {})
  56. test.todo('merging with skipFactory', () => {})
  57. test.todo('should warn missing', () => {})
  58. })
  59. describe('mergeModels', () => {
  60. test.todo('array syntax', () => {})
  61. test.todo('object syntax', () => {})
  62. test.todo('overwrite', () => {})
  63. })
  64. test.todo('createPropsRestProxy', () => {})
  65. describe.todo('withAsyncContext', () => {
  66. test.todo('basic', async () => {})
  67. test.todo('error handling', async () => {})
  68. test.todo('should not leak instance on multiple awaits', async () => {})
  69. test.todo('should not leak on multiple awaits + error', async () => {})
  70. test.todo('race conditions', async () => {})
  71. test.todo('should teardown in-scope effects', async () => {})
  72. })
  73. })