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

change vm to always provide global property #2386

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a more appropriate name, please let me know 🙏

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