Skip to content

Commit d577ceb

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

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mount } from '@vue/test-utils'
22
import type { ComponentMountingOptions } from '@vue/test-utils'
33
import { Suspense, h, isReadonly, nextTick, reactive, unref, getCurrentInstance, effectScope, isRef } from 'vue'
4-
import type { ComponentInternalInstance, DefineComponent, SetupContext } from 'vue'
4+
import type { App, ComponentInternalInstance, DefineComponent, SetupContext } from 'vue'
55
import { defu } from 'defu'
66
import type { RouteLocationRaw } from 'vue-router'
77

@@ -63,7 +63,7 @@ export async function mountSuspended<T>(
6363
cleanupFunction()
6464
}
6565

66-
const vueApp = tryUseNuxtApp()?.vueApp
66+
const vueApp: App<Element> = tryUseNuxtApp()?.vueApp
6767
// @ts-expect-error untyped global __unctx__
6868
|| globalThis.__unctx__.get('nuxt-app').tryUse().vueApp
6969
const { render, setup, data, computed, methods, ...componentRest } = component as DefineComponent<Record<string, unknown>, Record<string, unknown>>
@@ -261,7 +261,14 @@ export async function mountSuspended<T>(
261261
attrs,
262262
global: {
263263
config: {
264-
globalProperties: vueApp.config.globalProperties,
264+
globalProperties: {
265+
...vueApp.config.globalProperties,
266+
// make all properties/keys enumerable.
267+
...Object.fromEntries(
268+
Object.getOwnPropertyNames(vueApp.config.globalProperties)
269+
.map(key => [key, vueApp.config.globalProperties[key]]),
270+
),
271+
},
265272
},
266273
directives: vueApp._context.directives,
267274
provide: vueApp._context.provides,

src/runtime-utils/render.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Suspense, effectScope, h, nextTick, isReadonly, reactive, unref, defineComponent, getCurrentInstance } from 'vue'
2-
import type { ComponentInternalInstance, DefineComponent, SetupContext } from 'vue'
2+
import type { App, ComponentInternalInstance, DefineComponent, SetupContext } from 'vue'
33
import type { RenderOptions as TestingLibraryRenderOptions } from '@testing-library/vue'
44
import { defu } from 'defu'
55
import type { RouteLocationRaw } from 'vue-router'
@@ -59,7 +59,7 @@ export async function renderSuspended<T>(component: T, options?: RenderOptions<T
5959

6060
const { render: renderFromTestingLibrary } = await import('@testing-library/vue')
6161

62-
const vueApp = tryUseNuxtApp()?.vueApp
62+
const vueApp: App<Element> = tryUseNuxtApp()?.vueApp
6363
// @ts-expect-error untyped global __unctx__
6464
|| globalThis.__unctx__.get('nuxt-app').tryUse().vueApp
6565
const { render, setup, data, computed, methods, ...componentRest } = component as DefineComponent<Record<string, unknown>, Record<string, unknown>>
@@ -251,7 +251,14 @@ export async function renderSuspended<T>(component: T, options?: RenderOptions<T
251251
attrs,
252252
global: {
253253
config: {
254-
globalProperties: vueApp.config.globalProperties,
254+
globalProperties: {
255+
...vueApp.config.globalProperties,
256+
// make all properties/keys enumerable.
257+
...Object.fromEntries(
258+
Object.getOwnPropertyNames(vueApp.config.globalProperties)
259+
.map(key => [key, vueApp.config.globalProperties[key]]),
260+
),
261+
},
255262
},
256263
directives: vueApp._context.directives,
257264
provide: vueApp._context.provides,

0 commit comments

Comments
 (0)