From d877bd8593af63d03d1ca0367ff2a43f4f5b4b94 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Mon, 10 Aug 2026 20:15:09 +0800 Subject: [PATCH] =?UTF-8?q?test(editor):=20=E4=BD=BF=E7=94=A8=20vi.waitFor?= =?UTF-8?q?=20=E7=A8=B3=E5=AE=9A=20CompareForm=20=E4=B8=8E=20ViewForm=20?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/unit/components/CompareForm.spec.ts | 16 +++++++----- .../tests/unit/components/ViewForm.spec.ts | 26 ++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/editor/tests/unit/components/CompareForm.spec.ts b/packages/editor/tests/unit/components/CompareForm.spec.ts index 1be271ec..e50f8425 100644 --- a/packages/editor/tests/unit/components/CompareForm.spec.ts +++ b/packages/editor/tests/unit/components/CompareForm.spec.ts @@ -5,7 +5,7 @@ */ import { beforeEach, describe, expect, test, vi } from 'vitest'; import { defineComponent, h, nextTick, ref } from 'vue'; -import { mount } from '@vue/test-utils'; +import { mount, type VueWrapper } from '@vue/test-utils'; import { HookType } from '@tmagic/core'; @@ -63,6 +63,12 @@ beforeEach(() => { capturedFormProps = {}; }); +const waitForFormReady = async (wrapper: VueWrapper) => { + await vi.waitFor(() => { + expect(wrapper.find('.fake-mform').exists()).toBe(true); + }); +}; + describe('CompareForm.vue', () => { test('node 类别按 type 加载 props 配置并展示 MForm', async () => { const wrapper = mount(CompareForm, { @@ -79,8 +85,7 @@ describe('CompareForm.vue', () => { }, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(propsService.getPropsConfig).toHaveBeenCalledWith('text', { node: { id: 'n1', name: 'new' } }); expect(wrapper.find('.fake-mform').exists()).toBe(true); @@ -167,7 +172,7 @@ describe('CompareForm.vue', () => { }); test('showDiff 对 code-select 的空形态视为相等', async () => { - mount(CompareForm, { + const wrapper = mount(CompareForm, { props: { category: 'node', type: 'text', @@ -175,8 +180,7 @@ describe('CompareForm.vue', () => { services, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(capturedShowDiff).toBeTypeOf('function'); expect( capturedShowDiff!({ diff --git a/packages/editor/tests/unit/components/ViewForm.spec.ts b/packages/editor/tests/unit/components/ViewForm.spec.ts index a0643931..1b905233 100644 --- a/packages/editor/tests/unit/components/ViewForm.spec.ts +++ b/packages/editor/tests/unit/components/ViewForm.spec.ts @@ -5,7 +5,7 @@ */ import { beforeEach, describe, expect, test, vi } from 'vitest'; import { defineComponent, h, nextTick } from 'vue'; -import { mount } from '@vue/test-utils'; +import { mount, type VueWrapper } from '@vue/test-utils'; import ViewForm from '@editor/components/ViewForm.vue'; @@ -52,6 +52,12 @@ beforeEach(() => { capturedFormProps = {}; }); +const waitForFormReady = async (wrapper: VueWrapper) => { + await vi.waitFor(() => { + expect(wrapper.find('.fake-mform').exists()).toBe(true); + }); +}; + describe('ViewForm.vue', () => { test('node 类别按 type 加载配置并渲染 MForm', async () => { const wrapper = mount(ViewForm, { @@ -62,15 +68,14 @@ describe('ViewForm.vue', () => { services, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(propsService.getPropsConfig).toHaveBeenCalledWith('text', { node: { id: 'n1', name: 'a' } }); expect(wrapper.find('.fake-mform').exists()).toBe(true); expect(capturedFormProps.initValues).toEqual({ id: 'n1', name: 'a' }); }); test('默认 disabled 为 true', async () => { - mount(ViewForm, { + const wrapper = mount(ViewForm, { props: { category: 'node', type: 'text', @@ -78,13 +83,12 @@ describe('ViewForm.vue', () => { services, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(capturedFormProps.disabled).toBe(true); }); test('可通过 disabled=false 覆盖为可编辑', async () => { - mount(ViewForm, { + const wrapper = mount(ViewForm, { props: { category: 'node', type: 'text', @@ -93,13 +97,12 @@ describe('ViewForm.vue', () => { services, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(capturedFormProps.disabled).toBe(false); }); test('size 透传给 MForm', async () => { - mount(ViewForm, { + const wrapper = mount(ViewForm, { props: { category: 'node', type: 'text', @@ -108,8 +111,7 @@ describe('ViewForm.vue', () => { services, }, }); - await nextTick(); - await nextTick(); + await waitForFormReady(wrapper); expect(capturedFormProps.size).toBe('small'); });