From 9623bf0ba9ce1a94bdc9093c8613bec8494b73b1 Mon Sep 17 00:00:00 2001 From: wattanx Date: Sat, 9 Nov 2024 22:55:58 +0900 Subject: [PATCH] fix(runtime-utils): support `defineModel` in `mountSuspended` --- .../app-vitest-full/tests/nuxt/mount-suspended.spec.ts | 4 +--- src/runtime-utils/mount.ts | 7 ++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts b/examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts index 11503095d..d9aa769be 100644 --- a/examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts +++ b/examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts @@ -99,15 +99,13 @@ describe('mountSuspended', () => { `) }) - // This test works (you can delete it later) it('can receive emitted events from components using defineModel', () => { const component = mount(WrapperTests) component.find('button#changeModelValue').trigger('click') expect(component.emitted()).toHaveProperty('update:modelValue') }) - // FIXME: fix this failing test - it.todo('can receive emitted events from components mounted within nuxt suspense using defineModel', async () => { + it('can receive emitted events from components mounted within nuxt suspense using defineModel', async () => { const component = await mountSuspended(WrapperTests) component.find('button#changeModelValue').trigger('click') expect(component.emitted()).toHaveProperty('update:modelValue') diff --git a/src/runtime-utils/mount.ts b/src/runtime-utils/mount.ts index d899647b0..919c860f4 100644 --- a/src/runtime-utils/mount.ts +++ b/src/runtime-utils/mount.ts @@ -1,6 +1,6 @@ import { mount } from '@vue/test-utils' import type { ComponentMountingOptions } from '@vue/test-utils' -import { Suspense, h, isReadonly, nextTick, reactive, unref } from 'vue' +import { Suspense, h, isReadonly, nextTick, reactive, unref, getCurrentInstance } from 'vue' import type { DefineComponent, SetupContext } from 'vue' import { defu, createDefu } from 'defu' import type { RouteLocationRaw } from 'vue-router' @@ -116,6 +116,11 @@ export async function mountSuspended( ...component, render: render ? function (this: unknown, _ctx: Record, ...args: unknown[]) { + // When using defineModel, getCurrentInstance().emit is executed internally. it needs to override. + const currentInstance = getCurrentInstance() + if (currentInstance) { + currentInstance.emit = setupContext.emit + } // Set before setupState set to allow asyncData to overwrite data if (data && typeof data === 'function') { // @ts-expect-error error TS2554: Expected 1 arguments, but got 0