2
0

apiExpose.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { ref, shallowRef } from '@vue/reactivity'
  2. import { type VaporComponentInstance, createComponent } from '../src/component'
  3. import { setRef } from '../src/dom/templateRef'
  4. import { makeRender } from './_utils'
  5. import { currentInstance } from '@vue/runtime-dom'
  6. import { defineVaporComponent } from '../src/apiDefineComponent'
  7. const define = makeRender()
  8. describe.todo('api: expose', () => {
  9. test('via setup context', () => {
  10. const Child = defineVaporComponent({
  11. setup(_, { expose }) {
  12. expose({
  13. foo: 1,
  14. bar: ref(2),
  15. })
  16. return {
  17. bar: ref(3),
  18. baz: ref(4),
  19. }
  20. },
  21. })
  22. const childRef = ref()
  23. define({
  24. render: () => {
  25. const n0 = createComponent(Child)
  26. setRef(n0, childRef)
  27. return n0
  28. },
  29. }).render()
  30. expect(childRef.value).toBeTruthy()
  31. expect(childRef.value.foo).toBe(1)
  32. expect(childRef.value.bar).toBe(2)
  33. expect(childRef.value.baz).toBeUndefined()
  34. })
  35. test('via setup context (expose empty)', () => {
  36. let childInstance: VaporComponentInstance | null = null
  37. const Child = defineVaporComponent({
  38. setup(_) {
  39. childInstance = currentInstance as VaporComponentInstance
  40. return []
  41. },
  42. })
  43. const childRef = shallowRef()
  44. define({
  45. render: () => {
  46. const n0 = createComponent(Child)
  47. setRef(n0, childRef)
  48. return n0
  49. },
  50. }).render()
  51. expect(childInstance!.exposed).toBeUndefined()
  52. expect(childRef.value).toBe(childInstance!)
  53. })
  54. test('with mount', () => {
  55. const { instance } = define({
  56. setup(_, { expose }) {
  57. expose({
  58. foo: 1,
  59. })
  60. return {
  61. bar: 2,
  62. }
  63. },
  64. }).render()
  65. expect(instance!.exposed!.foo).toBe(1)
  66. expect(instance!.exposed!.bar).toBe(undefined)
  67. })
  68. test('warning for ref', () => {
  69. define({
  70. setup(_, { expose }) {
  71. expose(ref(1))
  72. return []
  73. },
  74. }).render()
  75. expect(
  76. 'expose() should be passed a plain object, received ref',
  77. ).toHaveBeenWarned()
  78. })
  79. test('warning for array', () => {
  80. define({
  81. setup(_, { expose }) {
  82. expose(['focus'])
  83. return []
  84. },
  85. }).render()
  86. expect(
  87. 'expose() should be passed a plain object, received array',
  88. ).toHaveBeenWarned()
  89. })
  90. test('warning for function', () => {
  91. define({
  92. setup(_, { expose }) {
  93. expose(() => null)
  94. return []
  95. },
  96. }).render()
  97. expect(
  98. 'expose() should be passed a plain object, received function',
  99. ).toHaveBeenWarned()
  100. })
  101. })