-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose everything on wrapper.vm
This commit changes `wrapper.vm` to actually point to `vm.$.proxy`. This shouldn't change the behaviour of existing tests, but allows components written with `script setup` to be tested as well, without the need to expose everything just for testing purposes. For example a component like: ```vue <script setup lang="ts"> import { ref } from 'vue' const count = ref(0) </script> ``` can now be tested like `expect(wrapper.vm.count).toBe(0)`, whereas you previously had to add `defineExpose({ count })` for this to work. The downside is that you now can't test that something is _not_ exposed by a component, but I don't think this is a problem. This also removes the previous hacks for script setup, as it looks like they are no longer necessary.
- Loading branch information
Showing
7 changed files
with
93 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<template> | ||
<div id="root"> | ||
<div id="msg">{{ msg }}</div> | ||
<div>{{ other }}</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, ref } from 'vue' | ||
export default defineComponent({ | ||
name: 'Hello', | ||
setup(props, { expose }) { | ||
const other = ref('other') | ||
expose({ other }) | ||
return { | ||
msg: ref('Hello world'), | ||
other | ||
} | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script setup lang="ts"> | ||
// imported components are also directly usable in template | ||
import { ref } from 'vue' | ||
import Hello from './Hello.vue' | ||
const count = ref(0) | ||
const inc = () => { | ||
count.value++ | ||
} | ||
defineExpose({ | ||
count | ||
}) | ||
</script> | ||
|
||
<template> | ||
<button @click="inc">{{ count }}</button> | ||
<Hello /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { mount } from '../src' | ||
import Hello from './components/Hello.vue' | ||
import DefineExpose from './components/DefineExpose.vue' | ||
import ScriptSetupExpose from './components/ScriptSetup_Expose.vue' | ||
import ScriptSetup from './components/ScriptSetup.vue' | ||
|
||
describe('expose', () => { | ||
it('access vm on simple components', async () => { | ||
const wrapper = mount(Hello) | ||
|
||
expect(wrapper.vm.msg).toBe('Hello world') | ||
}) | ||
|
||
it('access vm on simple components with custom `expose`', async () => { | ||
const wrapper = mount(DefineExpose) | ||
|
||
// other is exposed vie `expose` | ||
expect(wrapper.vm.other).toBe('other') | ||
// can access `msg` even if not exposed | ||
expect(wrapper.vm.msg).toBe('Hello world') | ||
}) | ||
|
||
it('access vm with <script setup> and defineExpose()', async () => { | ||
const wrapper = mount(ScriptSetupExpose) | ||
|
||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.html()).toContain('1') | ||
// can access `count` as it is exposed via `defineExpose()` | ||
expect(wrapper.vm.count).toBe(1) | ||
}) | ||
|
||
it('access vm with <script setup> even without defineExpose()', async () => { | ||
const wrapper = mount(ScriptSetup) | ||
|
||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.html()).toContain('1') | ||
// can access `count` even if it is _not_ exposed | ||
expect(wrapper.vm.count).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { defineComponent, ref } from 'vue' | ||
|
||
import { mount } from '../src' | ||
|
||
describe('vm', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"compilerOptions": { | ||
"lib": ["DOM", "ES2020"], | ||
"skipLibCheck": true | ||
} | ||
}, | ||
"exclude": ["tests/expose.spec.ts"] | ||
} |