Skip to content

Commit a744b65

Browse files
authored
fix(runtime-utils): pass non-enumerable globalProperties in mount + render helpers (#1476)
1 parent dc46354 commit a744b65

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,14 @@ export async function mountSuspended<T>(
273273
attrs,
274274
global: {
275275
config: {
276-
globalProperties: vueApp.config.globalProperties,
276+
globalProperties: {
277+
...vueApp.config.globalProperties,
278+
// make all properties/keys enumerable.
279+
...Object.fromEntries(
280+
Object.getOwnPropertyNames(vueApp.config.globalProperties)
281+
.map(key => [key, vueApp.config.globalProperties[key]]),
282+
),
283+
},
277284
},
278285
directives: vueApp._context.directives,
279286
provide: vueApp._context.provides,

src/runtime-utils/render.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ export async function renderSuspended<T>(component: T, options?: RenderOptions<T
263263
attrs,
264264
global: {
265265
config: {
266-
globalProperties: vueApp.config.globalProperties,
266+
globalProperties: {
267+
...vueApp.config.globalProperties,
268+
// make all properties/keys enumerable.
269+
...Object.fromEntries(
270+
Object.getOwnPropertyNames(vueApp.config.globalProperties)
271+
.map(key => [key, vueApp.config.globalProperties[key]]),
272+
),
273+
},
267274
},
268275
directives: vueApp._context.directives,
269276
provide: vueApp._context.provides,

0 commit comments

Comments
 (0)