apiExpose.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { nodeOps, render } from '@vue/runtime-test'
  2. import { defineComponent, h, ref } from '../src'
  3. describe('api: expose', () => {
  4. test('via setup context', () => {
  5. const Child = defineComponent({
  6. render() {},
  7. setup(_, { expose }) {
  8. expose({
  9. foo: ref(1),
  10. bar: ref(2)
  11. })
  12. return {
  13. bar: ref(3),
  14. baz: ref(4)
  15. }
  16. }
  17. })
  18. const childRef = ref()
  19. const Parent = {
  20. setup() {
  21. return () => h(Child, { ref: childRef })
  22. }
  23. }
  24. const root = nodeOps.createElement('div')
  25. render(h(Parent), root)
  26. expect(childRef.value).toBeTruthy()
  27. expect(childRef.value.foo).toBe(1)
  28. expect(childRef.value.bar).toBe(2)
  29. expect(childRef.value.baz).toBeUndefined()
  30. })
  31. test('via options', () => {
  32. const Child = defineComponent({
  33. render() {},
  34. data() {
  35. return {
  36. foo: 1
  37. }
  38. },
  39. setup() {
  40. return {
  41. bar: ref(2),
  42. baz: ref(3)
  43. }
  44. },
  45. expose: ['foo', 'bar']
  46. })
  47. const childRef = ref()
  48. const Parent = {
  49. setup() {
  50. return () => h(Child, { ref: childRef })
  51. }
  52. }
  53. const root = nodeOps.createElement('div')
  54. render(h(Parent), root)
  55. expect(childRef.value).toBeTruthy()
  56. expect(childRef.value.foo).toBe(1)
  57. expect(childRef.value.bar).toBe(2)
  58. expect(childRef.value.baz).toBeUndefined()
  59. })
  60. test('options + context', () => {
  61. const Child = defineComponent({
  62. render() {},
  63. expose: ['foo'],
  64. data() {
  65. return {
  66. foo: 1
  67. }
  68. },
  69. setup(_, { expose }) {
  70. expose({
  71. bar: ref(2)
  72. })
  73. return {
  74. bar: ref(3),
  75. baz: ref(4)
  76. }
  77. }
  78. })
  79. const childRef = ref()
  80. const Parent = {
  81. setup() {
  82. return () => h(Child, { ref: childRef })
  83. }
  84. }
  85. const root = nodeOps.createElement('div')
  86. render(h(Parent), root)
  87. expect(childRef.value).toBeTruthy()
  88. expect(childRef.value.foo).toBe(1)
  89. expect(childRef.value.bar).toBe(2)
  90. expect(childRef.value.baz).toBeUndefined()
  91. })
  92. })