From d9062b378c557149bc32ff04c07ce3101e64e551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Hamburger=20Gr=C3=B8ngaard?= Date: Tue, 11 Nov 2025 12:12:48 +0100 Subject: [PATCH] test: make test more reliable --- packages/editor/tests/event.mutation.test.tsx | 75 +++++++++++-------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/packages/editor/tests/event.mutation.test.tsx b/packages/editor/tests/event.mutation.test.tsx index 84f05e68b..5683a6bb6 100644 --- a/packages/editor/tests/event.mutation.test.tsx +++ b/packages/editor/tests/event.mutation.test.tsx @@ -1,3 +1,5 @@ +import {compileSchema, defineSchema} from '@portabletext/schema' +import {getTersePt} from '@portabletext/test' import {describe, expect, test, vi} from 'vitest' import {userEvent} from 'vitest/browser' import type {EditorEmittedEvent, MutationEvent} from '../src' @@ -8,30 +10,35 @@ describe('event.mutation', () => { test('Scenario: Deferring mutation events when read-only', async () => { const onEvent = vi.fn<(event: EditorEmittedEvent) => void>() + let resolveFooMutation: () => void + const fooMutationPromise = new Promise((resolve) => { + resolveFooMutation = resolve + }) + const {editor, locator} = await createTestEditor({ - children: , + children: ( + { + onEvent(event) + if ( + event.type === 'mutation' && + getTersePt({ + schema: compileSchema(defineSchema({})), + value: event.value ?? [], + }).at(0) === 'foo' + ) { + resolveFooMutation() + } + }} + /> + ), }) await userEvent.type(locator, 'foo') - await new Promise((resolve) => setTimeout(resolve, 250)) - - expect(onEvent).toHaveBeenCalledWith( - expect.objectContaining({ - type: 'mutation', - value: [ - { - _type: 'block', - _key: 'k0', - children: [{_type: 'span', _key: 'k1', text: 'foo', marks: []}], - markDefs: [], - style: 'normal', - }, - ], - }), - ) + await fooMutationPromise - await userEvent.type(locator, 'bar') + editor.send({type: 'insert.text', text: 'bar'}) editor.send({type: 'update readOnly', readOnly: true}) @@ -56,20 +63,24 @@ describe('event.mutation', () => { await new Promise((resolve) => setTimeout(resolve, 250)) - expect(onEvent).toHaveBeenCalledWith( - expect.objectContaining({ - type: 'mutation', - value: [ - { - _type: 'block', - _key: 'k0', - children: [{_type: 'span', _key: 'k1', text: 'foobar', marks: []}], - markDefs: [], - style: 'normal', - }, - ], - }), - ) + await vi.waitFor(() => { + expect(onEvent).toHaveBeenCalledWith( + expect.objectContaining({ + type: 'mutation', + value: [ + { + _type: 'block', + _key: 'k0', + children: [ + {_type: 'span', _key: 'k1', text: 'foobar', marks: []}, + ], + markDefs: [], + style: 'normal', + }, + ], + }), + ) + }) }) test('Scenario: Batching typing mutations', async () => {