Skip to content

Commit 05f76b6

Browse files
authored
fix: always provide access to global property (#2386)
createVMProxy function has been fixed to also return global properties. Fixes #2140
1 parent 6aa8226 commit 05f76b6

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

src/vueWrapper.ts

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ function createVMProxy<T extends ComponentPublicInstance>(
3232
} else if (key in setupState) {
3333
// second if the key is acccessible from the setupState
3434
return Reflect.get(setupState, key, receiver)
35+
} else if (key in vm.$.appContext.config.globalProperties) {
36+
// third if the key is a global property
37+
return Reflect.get(
38+
vm.$.appContext.config.globalProperties,
39+
key,
40+
receiver
41+
)
3542
} else {
3643
// vm.$.ctx is the internal context of the vm
3744
// with all variables, methods and props

tests/components/OptionComponent.vue

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
export default {
3+
data() {
4+
return {
5+
message: 'Hello world!'
6+
}
7+
}
8+
}
9+
</script>
10+
11+
<template>
12+
<div>{{ message }} (Options API)</div>
13+
<div>{{ foo }}</div>
14+
</template>
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
export default {
3+
setup() {
4+
const description = "Options API with setup() function";
5+
return { description };
6+
},
7+
data() {
8+
return {
9+
message: "Hello world!",
10+
};
11+
},
12+
};
13+
</script>
14+
15+
<template>
16+
<div>{{ message }} ({{ description }})</div>
17+
<div>{{ foo }}</div>
18+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { onUnmounted } from 'vue'
3+
4+
export default {
5+
setup() {
6+
onUnmounted(() => { console.log('unmounted') })
7+
},
8+
data() {
9+
return {
10+
message: 'Hello world!'
11+
}
12+
}
13+
}
14+
</script>
15+
16+
<template>
17+
<div>{{ message }} (Options API component with a setup() function that does not return)</div>
18+
</template>

tests/mountingOptions/global.plugins.spec.ts

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { describe, expect, test, it, vi } from 'vitest'
22
import { h, App } from 'vue'
33

44
import { mount } from '../../src'
5+
import ScriptSetup from '../components/ScriptSetup.vue'
6+
import Option from '../components/OptionComponent.vue'
7+
import OptionsSetup from '../components/OptionSetupComponent.vue'
8+
import OptionsSetupWithoutReturn from '../components/OptionSetupWithoutReturnComponent.vue'
59

610
describe('mounting options: plugins', () => {
711
it('installs a plugin via `plugins`', () => {
@@ -51,6 +55,41 @@ describe('mounting options: plugins', () => {
5155

5256
expect(installed).toHaveBeenCalledWith(options, testString)
5357
})
58+
59+
describe('provides access to a global property', () => {
60+
class Plugin {
61+
static install(app: App) {
62+
app.config.globalProperties.foo = 'bar'
63+
}
64+
}
65+
it('provides access to a global property from a Composition API component', () => {
66+
const wrapper = mount(ScriptSetup, {
67+
global: { plugins: [Plugin] }
68+
})
69+
expect((wrapper.vm as any).foo).toBeDefined()
70+
})
71+
72+
it('provides access to a global property from an Options API component', () => {
73+
const wrapper = mount(Option, {
74+
global: { plugins: [Plugin] }
75+
})
76+
expect((wrapper.vm as any).foo).toBeDefined()
77+
})
78+
79+
it('provides access to a global property from an Options API component with a setup() function', () => {
80+
const wrapper = mount(OptionsSetup, {
81+
global: { plugins: [Plugin] }
82+
})
83+
expect((wrapper.vm as any).foo).toBeDefined()
84+
})
85+
86+
it('provides access to a global property from an Options API component with a setup() function that does not return', () => {
87+
const wrapper = mount(OptionsSetupWithoutReturn, {
88+
global: { plugins: [Plugin] }
89+
})
90+
expect((wrapper.vm as any).foo).toBeDefined()
91+
})
92+
})
5493
})
5594

5695
test('installs plugins with and without options', () => {

0 commit comments

Comments
 (0)