Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose props on vm for script setup components #1864

Merged
merged 1 commit into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/vueWrapper.ts
Expand Up @@ -35,7 +35,9 @@ function createVMProxy<T extends ComponentPublicInstance>(
if (key in setupState) {
return Reflect.get(setupState, key, receiver)
} else {
return (vm as any)[key]
// vm.$.ctx is the internal context of the vm
// with all variables, methods and props
return (vm as any).$.ctx[key]
}
},
set(vm, key, value, receiver) {
Expand Down
13 changes: 13 additions & 0 deletions tests/components/ScriptSetupWithProps.vue
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { ref } from 'vue';

const props = defineProps<{ count: number }>()
const internalCount = ref(props.count);
const inc = () => {
internalCount.value++
}
</script>

<template>
<button @click="inc">{{ internalCount }}</button>
</template>
15 changes: 15 additions & 0 deletions tests/expose.spec.ts
Expand Up @@ -6,6 +6,7 @@ import DefineExpose from './components/DefineExpose.vue'
import DefineExposeWithRenderFunction from './components/DefineExposeWithRenderFunction.vue'
import ScriptSetupExpose from './components/ScriptSetup_Expose.vue'
import ScriptSetup from './components/ScriptSetup.vue'
import ScriptSetupWithProps from './components/ScriptSetupWithProps.vue'

describe('expose', () => {
it('access vm on simple components', async () => {
Expand Down Expand Up @@ -76,4 +77,18 @@ describe('expose', () => {
expect(spiedIncrement).toHaveBeenCalled()
expect(wrapper.html()).toContain('0')
})

it('access props on vm with <script setup>', async () => {
const wrapper = mount(ScriptSetupWithProps, {
props: {
count: 2
}
})
// make sure that props are accessible on wrapper.vm
expect(wrapper.vm.count).toBe(2)
expect(wrapper.html()).toContain('2')

await wrapper.find('button').trigger('click')
expect(wrapper.html()).toContain('3')
})
})