Skip to content

Commit

Permalink
fix: always provide access to global property (vuejs#2386)
Browse files Browse the repository at this point in the history
createVMProxy function has been fixed to also return global properties.

Fixes vuejs#2140
  • Loading branch information
taku-y-9308 authored and nazarepiedady committed Jul 18, 2024
1 parent ac19a7e commit e44cd93
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ function createVMProxy<T extends ComponentPublicInstance>(
} else if (key in setupState) {
// second if the key is acccessible from the setupState
return Reflect.get(setupState, key, receiver)
} else if (key in vm.$.appContext.config.globalProperties) {
// third if the key is a global property
return Reflect.get(
vm.$.appContext.config.globalProperties,
key,
receiver
)
} else {
// vm.$.ctx is the internal context of the vm
// with all variables, methods and props
Expand Down
14 changes: 14 additions & 0 deletions tests/components/OptionComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
export default {
data() {
return {
message: 'Hello world!'
}
}
}
</script>

<template>
<div>{{ message }} (Options API)</div>
<div>{{ foo }}</div>
</template>
18 changes: 18 additions & 0 deletions tests/components/OptionSetupComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
export default {
setup() {
const description = "Options API with setup() function";
return { description };
},
data() {
return {
message: "Hello world!",
};
},
};
</script>

<template>
<div>{{ message }} ({{ description }})</div>
<div>{{ foo }}</div>
</template>
18 changes: 18 additions & 0 deletions tests/components/OptionSetupWithoutReturnComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import { onUnmounted } from 'vue'
export default {
setup() {
onUnmounted(() => { console.log('unmounted') })
},
data() {
return {
message: 'Hello world!'
}
}
}
</script>

<template>
<div>{{ message }} (Options API component with a setup() function that does not return)</div>
</template>
39 changes: 39 additions & 0 deletions tests/mountingOptions/global.plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { describe, expect, test, it, vi } from 'vitest'
import { h, App } from 'vue'

import { mount } from '../../src'
import ScriptSetup from '../components/ScriptSetup.vue'
import Option from '../components/OptionComponent.vue'
import OptionsSetup from '../components/OptionSetupComponent.vue'
import OptionsSetupWithoutReturn from '../components/OptionSetupWithoutReturnComponent.vue'

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

expect(installed).toHaveBeenCalledWith(options, testString)
})

describe('provides access to a global property', () => {
class Plugin {
static install(app: App) {
app.config.globalProperties.foo = 'bar'
}
}
it('provides access to a global property from a Composition API component', () => {
const wrapper = mount(ScriptSetup, {
global: { plugins: [Plugin] }
})
expect((wrapper.vm as any).foo).toBeDefined()
})

it('provides access to a global property from an Options API component', () => {
const wrapper = mount(Option, {
global: { plugins: [Plugin] }
})
expect((wrapper.vm as any).foo).toBeDefined()
})

it('provides access to a global property from an Options API component with a setup() function', () => {
const wrapper = mount(OptionsSetup, {
global: { plugins: [Plugin] }
})
expect((wrapper.vm as any).foo).toBeDefined()
})

it('provides access to a global property from an Options API component with a setup() function that does not return', () => {
const wrapper = mount(OptionsSetupWithoutReturn, {
global: { plugins: [Plugin] }
})
expect((wrapper.vm as any).foo).toBeDefined()
})
})
})

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

0 comments on commit e44cd93

Please sign in to comment.