apiSetupHelpers.spec.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {
  2. defineComponent,
  3. h,
  4. nodeOps,
  5. render,
  6. SetupContext
  7. } from '@vue/runtime-test'
  8. import { defineEmits, defineProps, useContext } from '../src/apiSetupHelpers'
  9. describe('SFC <script setup> helpers', () => {
  10. test('should warn runtime usage', () => {
  11. defineProps()
  12. expect(`defineProps() is a compiler-hint`).toHaveBeenWarned()
  13. defineEmits()
  14. expect(`defineEmits() is a compiler-hint`).toHaveBeenWarned()
  15. })
  16. test('useContext (no args)', () => {
  17. let ctx: SetupContext | undefined
  18. const Comp = {
  19. setup() {
  20. ctx = useContext()
  21. return () => {}
  22. }
  23. }
  24. render(h(Comp), nodeOps.createElement('div'))
  25. expect(ctx).toMatchObject({
  26. attrs: {},
  27. slots: {}
  28. })
  29. expect(typeof ctx!.emit).toBe('function')
  30. })
  31. test('useContext (with args)', () => {
  32. let ctx: SetupContext | undefined
  33. let ctxArg: SetupContext | undefined
  34. const Comp = defineComponent({
  35. setup(_, _ctxArg) {
  36. ctx = useContext()
  37. ctxArg = _ctxArg
  38. return () => {}
  39. }
  40. })
  41. render(h(Comp), nodeOps.createElement('div'))
  42. expect(ctx).toBeDefined()
  43. expect(ctx).toBe(ctxArg)
  44. })
  45. })