apiExpose.spec.ts 2.7 KB

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