apiSetupHelpers.spec.ts 2.4 KB

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