|
1 | 1 | import { describe, expect, it, vi } from 'vitest'
|
2 | 2 | import { nextTick } from 'vue'
|
3 | 3 | import { mount } from '../src'
|
4 |
| -import Hello from './components/Hello.vue' |
| 4 | + |
5 | 5 | import DefineExpose from './components/DefineExpose.vue'
|
6 | 6 | import DefineExposeWithRenderFunction from './components/DefineExposeWithRenderFunction.vue'
|
7 | 7 | import ScriptSetupExpose from './components/ScriptSetup_Expose.vue'
|
8 | 8 | import ScriptSetup from './components/ScriptSetup.vue'
|
9 | 9 | import ScriptSetupWithProps from './components/ScriptSetupWithProps.vue'
|
10 | 10 |
|
11 | 11 | describe('expose', () => {
|
12 |
| - it('access vm on simple components', async () => { |
13 |
| - const wrapper = mount(Hello) |
| 12 | + const commonTests = (vm: any) => { |
| 13 | + // exposedState1 is exposed vie `expose` and aliased to `exposedState1` |
| 14 | + expect(vm.exposedState1).toBe('exposedState1') |
| 15 | + // exposedState2 is exposed vie `expose` and aliased to `exposedState2Alias` |
| 16 | + expect(vm.exposedState2Alias).toBe('exposedState2') |
| 17 | + |
| 18 | + // exposed state can be changed but will not affect the original state |
| 19 | + vm.exposedState2Alias = 'newExposedState2' |
| 20 | + expect(vm.exposedState2Alias).toBe('newExposedState2') |
| 21 | + expect(vm.exposedState2Getter()).toBe('exposedState2') |
| 22 | + |
| 23 | + // exposed ref can be changed and will affect the original ref |
| 24 | + // @ts-ignore upstream issue, see https://github.com/vuejs/vue-next/issues/4397#issuecomment-957613874 |
| 25 | + expect(vm.exposedRef).toBe('exposedRef') |
| 26 | + vm.exposedRef = 'newExposedRef' |
| 27 | + expect(vm.exposedRef).toBe('newExposedRef') |
| 28 | + expect(vm.exposedRefGetter()).toBe('newExposedRef') |
14 | 29 |
|
15 |
| - expect(wrapper.vm.msg).toBe('Hello world') |
16 |
| - }) |
| 30 | + // exposedMethod1 is exposed vie `expose` |
| 31 | + expect(vm.exposedMethod1).not.toBe(undefined) |
| 32 | + expect(vm.exposedMethod1()).toBe('result of exposedMethod1') |
| 33 | + |
| 34 | + // exposedMethod2 is exposed vie `expose` and aliased to `exposedMethod2Alias` |
| 35 | + expect(vm.exposedMethod2Alias).not.toBe(undefined) |
| 36 | + expect(vm.exposedMethod2Alias()).toBe('result of exposedMethod2') |
| 37 | + } |
17 | 38 |
|
18 | 39 | it('access vm on simple components with custom `expose`', async () => {
|
19 | 40 | const wrapper = mount(DefineExpose)
|
| 41 | + const vm = wrapper.vm |
| 42 | + |
| 43 | + commonTests(vm) |
| 44 | + |
| 45 | + // returned state shuold be accessible |
| 46 | + expect(vm.returnedState).toBe('returnedState') |
20 | 47 |
|
21 |
| - // other is exposed vie `expose` |
22 |
| - expect(wrapper.vm.other).toBe('other') |
23 |
| - // can access `msg` even if not exposed |
24 |
| - expect(wrapper.vm.msg).toBe('Hello world') |
| 48 | + // non-exposed and non-returned state should not be accessible |
| 49 | + expect( |
| 50 | + (vm as unknown as { stateNonExposedAndNonReturned: undefined }) |
| 51 | + .stateNonExposedAndNonReturned |
| 52 | + ).toBe(undefined) |
25 | 53 | })
|
26 | 54 |
|
27 | 55 | it('access vm on simple components with custom `expose` and a setup returning a render function', async () => {
|
28 | 56 | const wrapper = mount(DefineExposeWithRenderFunction)
|
| 57 | + const vm = wrapper.vm |
29 | 58 |
|
30 |
| - // other is exposed vie `expose` |
31 |
| - // @ts-ignore upstream issue, see https://github.com/vuejs/vue-next/issues/4397#issuecomment-957613874 |
32 |
| - expect(wrapper.vm.other).toBe('other') |
33 |
| - // can't access `msg` as it is not exposed |
| 59 | + commonTests(vm) |
| 60 | + |
| 61 | + // can't access `refUseByRenderFnButNotExposed` as it is not exposed |
34 | 62 | // and we are in a component with a setup returning a render function
|
35 |
| - expect((wrapper.vm as unknown as { msg: undefined }).msg).toBeUndefined() |
| 63 | + expect( |
| 64 | + (vm as unknown as { refUseByRenderFnButNotExposed: undefined }) |
| 65 | + .refUseByRenderFnButNotExposed |
| 66 | + ).toBeUndefined() |
36 | 67 | })
|
37 | 68 |
|
38 | 69 | it('access vm with <script setup> and defineExpose()', async () => {
|
39 | 70 | const wrapper = mount(ScriptSetupExpose)
|
| 71 | + const vm = wrapper.vm as unknown as { |
| 72 | + inc: () => void |
| 73 | + resetCount: () => void |
| 74 | + count: number |
| 75 | + refNonExposed: string |
| 76 | + refNonExposedGetter: () => string |
| 77 | + } |
| 78 | + |
| 79 | + commonTests(vm) |
40 | 80 |
|
41 | 81 | await wrapper.find('button').trigger('click')
|
42 | 82 | expect(wrapper.html()).toContain('1')
|
43 |
| - // can access `count` as it is exposed via `defineExpose()` |
44 |
| - expect(wrapper.vm.count).toBe(1) |
45 |
| - |
46 |
| - wrapper.vm.resetCount() |
47 | 83 |
|
48 |
| - expect(wrapper.vm.count).toBe(0) |
| 84 | + // can access state/method/ref as it is exposed via `defineExpose()` |
| 85 | + expect(vm.count).toBe(1) |
| 86 | + vm.resetCount() |
| 87 | + expect(vm.count).toBe(0) |
| 88 | + |
| 89 | + // non-exposed state/method/ref should be accessible |
| 90 | + vm.inc() |
| 91 | + expect(vm.count).toBe(1) |
| 92 | + expect(vm.refNonExposed).toBe('refNonExposed') |
| 93 | + vm.refNonExposed = 'newRefNonExposed' |
| 94 | + expect(vm.refNonExposedGetter()).toBe('newRefNonExposed') |
49 | 95 | })
|
50 | 96 |
|
51 | 97 | it('access vm with <script setup> even without defineExpose()', async () => {
|
|
0 commit comments