Skip to content

Commit 40a278f

Browse files
committed
fix(runtime-utils): pass all globalProperties in mount + render helpers
1 parent c452751 commit 40a278f

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>
3+
<div id="s1">
4+
{{ $pluginProvidedValues.value }}
5+
</div>
6+
<div id="s2">
7+
{{ $pluginProvidedValues.func('value') }}
8+
</div>
9+
<div id="s3">
10+
{{ $pluginProvidedValues.object.value }}
11+
</div>
12+
</div>
13+
</template>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default defineNuxtPlugin(() => {
2+
return {
3+
provide: {
4+
pluginProvidedValues: {
5+
value: 'pluginProvided.value',
6+
func: (value: string) => `pluginProvided.func(${value})`,
7+
object: { value: 'pluginProvided.object.value' },
8+
},
9+
},
10+
}
11+
})

examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import ComponentWithReservedProp from '~/components/ComponentWithReservedProp.vu
2626
import ComponentWithReservedState from '~/components/ComponentWithReservedState.vue'
2727
import ComponentWithImports from '~/components/ComponentWithImports.vue'
2828
import ComponentWithCssVar from '~/components/ComponentWithCssVar.vue'
29+
import ComponentWithPluginProvidedValue from '~/components/ComponentWithPluginProvidedValue.vue'
2930
import GenericStateComponent from '~/components/GenericStateComponent.vue'
3031

3132
import { BoundAttrs } from '#components'
@@ -284,6 +285,13 @@ describe('mountSuspended', () => {
284285
expect(component.find('#s3').classes()).toHaveLength(0)
285286
})
286287

288+
it('can mount components with use plugin provided value in template', async () => {
289+
const component = await mountSuspended(ComponentWithPluginProvidedValue)
290+
expect(component.find('#s1').text()).toBe('pluginProvided.value')
291+
expect(component.find('#s2').text()).toBe('pluginProvided.func(value)')
292+
expect(component.find('#s3').text()).toBe('pluginProvided.object.value')
293+
})
294+
287295
describe('Options API', () => {
288296
beforeEach(() => {
289297
vi.spyOn(console, 'error').mockImplementation((message) => {

examples/app-vitest-full/tests/nuxt/render-suspended.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ExportDefaultComponent from '~/components/ExportDefaultComponent.vue'
1313
import ExportDefineComponent from '~/components/ExportDefineComponent.vue'
1414
import ComponentWithAttrs from '~/components/ComponentWithAttrs.vue'
1515
import ComponentWithCssVar from '~/components/ComponentWithCssVar.vue'
16+
import ComponentWithPluginProvidedValue from '~/components/ComponentWithPluginProvidedValue.vue'
1617
import ExportDefaultWithRenderComponent from '~/components/ExportDefaultWithRenderComponent.vue'
1718
import ExportDefaultReturnsRenderComponent from '~/components/ExportDefaultReturnsRenderComponent.vue'
1819
import OptionsApiPage from '~/pages/other/options-api.vue'
@@ -179,6 +180,13 @@ describe('renderSuspended', () => {
179180
expect(container.querySelector('#s3')?.classList).toHaveLength(0)
180181
})
181182

183+
it('can render components with use plugin provided value in template', async () => {
184+
const { container } = await renderSuspended(ComponentWithPluginProvidedValue)
185+
expect(container.querySelector('#s1')?.textContent).toBe('pluginProvided.value')
186+
expect(container.querySelector('#s2')?.textContent).toBe('pluginProvided.func(value)')
187+
expect(container.querySelector('#s3')?.textContent).toBe('pluginProvided.object.value')
188+
})
189+
182190
describe('Options API', () => {
183191
beforeEach(() => {
184192
vi.spyOn(console, 'error').mockImplementation((message) => {

src/runtime-utils/mount.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,13 @@ export async function mountSuspended<T>(
261261
attrs,
262262
global: {
263263
config: {
264-
globalProperties: vueApp.config.globalProperties,
264+
// make all properties/keys enumerable.
265+
globalProperties: Object.fromEntries(
266+
[
267+
...Object.getOwnPropertyNames(vueApp.config.globalProperties),
268+
...Object.getOwnPropertySymbols(vueApp.config.globalProperties),
269+
].map(key => [key, vueApp.config.globalProperties[key]]),
270+
) as typeof vueApp.config.globalProperties,
265271
},
266272
directives: vueApp._context.directives,
267273
provide: vueApp._context.provides,

src/runtime-utils/render.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ export async function renderSuspended<T>(component: T, options?: RenderOptions<T
251251
attrs,
252252
global: {
253253
config: {
254-
globalProperties: vueApp.config.globalProperties,
254+
// make all properties/keys enumerable.
255+
globalProperties: Object.fromEntries(
256+
[
257+
...Object.getOwnPropertyNames(vueApp.config.globalProperties),
258+
...Object.getOwnPropertySymbols(vueApp.config.globalProperties),
259+
].map(key => [key, vueApp.config.globalProperties[key]]),
260+
) as typeof vueApp.config.globalProperties,
255261
},
256262
directives: vueApp._context.directives,
257263
provide: vueApp._context.provides,

0 commit comments

Comments
 (0)