|
1 |
| -import { ref } from '@vue/reactivity' |
| 1 | +import { ref, shallowRef } from '@vue/reactivity' |
2 | 2 | import { createComponent } from '../src/apiCreateComponent'
|
3 | 3 | import { setRef } from '../src/dom/templateRef'
|
4 | 4 | import { makeRender } from './_utils'
|
| 5 | +import { |
| 6 | + type ComponentInternalInstance, |
| 7 | + getCurrentInstance, |
| 8 | +} from '../src/component' |
5 | 9 |
|
6 | 10 | const define = makeRender()
|
7 | 11 | describe('api: expose', () => {
|
8 |
| - test('string ref mount (component)', () => { |
| 12 | + test('via setup context', () => { |
9 | 13 | const { component: Child } = define({
|
10 | 14 | setup(_, { expose }) {
|
11 | 15 | expose({
|
@@ -33,4 +37,61 @@ describe('api: expose', () => {
|
33 | 37 | expect(childRef.value.bar).toBe(2)
|
34 | 38 | expect(childRef.value.baz).toBeUndefined()
|
35 | 39 | })
|
| 40 | + |
| 41 | + test('via setup context (expose empty)', () => { |
| 42 | + let childInstance: ComponentInternalInstance | null = null |
| 43 | + const { component: Child } = define({ |
| 44 | + setup(_) { |
| 45 | + childInstance = getCurrentInstance() |
| 46 | + }, |
| 47 | + }) |
| 48 | + const childRef = shallowRef() |
| 49 | + const { render } = define({ |
| 50 | + render: () => { |
| 51 | + const n0 = createComponent(Child) |
| 52 | + setRef(n0, childRef) |
| 53 | + return n0 |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | + render() |
| 58 | + expect(childInstance!.exposed).toBeUndefined() |
| 59 | + expect(childRef.value).toBe(childInstance!) |
| 60 | + }) |
| 61 | + |
| 62 | + test('warning for ref', () => { |
| 63 | + const { render } = define({ |
| 64 | + setup(_, { expose }) { |
| 65 | + expose(ref(1)) |
| 66 | + }, |
| 67 | + }) |
| 68 | + render() |
| 69 | + expect( |
| 70 | + 'expose() should be passed a plain object, received ref', |
| 71 | + ).toHaveBeenWarned() |
| 72 | + }) |
| 73 | + |
| 74 | + test('warning for array', () => { |
| 75 | + const { render } = define({ |
| 76 | + setup(_, { expose }) { |
| 77 | + expose(['focus']) |
| 78 | + }, |
| 79 | + }) |
| 80 | + render() |
| 81 | + expect( |
| 82 | + 'expose() should be passed a plain object, received array', |
| 83 | + ).toHaveBeenWarned() |
| 84 | + }) |
| 85 | + |
| 86 | + test('warning for function', () => { |
| 87 | + const { render } = define({ |
| 88 | + setup(_, { expose }) { |
| 89 | + expose(() => null) |
| 90 | + }, |
| 91 | + }) |
| 92 | + render() |
| 93 | + expect( |
| 94 | + 'expose() should be passed a plain object, received function', |
| 95 | + ).toHaveBeenWarned() |
| 96 | + }) |
36 | 97 | })
|
0 commit comments