test(editor): wait for async selectionchange in cross-block selection test

selectionchange is dispatched on a macrotask, so awaiting nextTick (microtasks) didn't wait for the editor to sync the native selection into the model — the assertion saw the initial selection. Poll with vi.waitFor instead. (Surfaced now that per-package CI runs editor's browser tests, which the root vitest projects list omitted.)
This commit is contained in:
2026-06-08 17:20:40 +07:00
parent 73e701a2c5
commit f335c7db61
@@ -1,5 +1,5 @@
import { render } from 'vitest-browser-vue'; import { render } from 'vitest-browser-vue';
import { describe, expect, it } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import { createDoc, createNode, textSelection } from '../../model'; import { createDoc, createNode, textSelection } from '../../model';
import { createDefaultRegistry } from '../../preset'; import { createDefaultRegistry } from '../../preset';
@@ -49,17 +49,20 @@ describe('EditorRoot (single contenteditable)', () => {
const bText = hosts[1]!.firstChild!; // text node "world" const bText = hosts[1]!.firstChild!; // text node "world"
selectNative({ node: aText, offset: 1 }, { node: bText, offset: 3 }); selectNative({ node: aText, offset: 1 }, { node: bText, offset: 3 });
await nextTick();
await nextTick();
const sel = editor.state.selection; // `selectionchange` is dispatched on a macrotask, so awaiting microtasks
expect(sel.kind).toBe('text'); // (nextTick) isn't enough — poll until the editor has synced the model.
if (sel.kind === 'text') { const sel = await vi.waitFor(() => {
expect(sel.anchor.blockId).toBe('a'); const s = editor.state.selection;
expect(sel.anchor.offset).toBe(1); if (s.kind !== 'text' || s.anchor.offset !== 1)
expect(sel.focus.blockId).toBe('b'); throw new Error('selection not synced yet');
expect(sel.focus.offset).toBe(3); return s;
} });
expect(sel.anchor.blockId).toBe('a');
expect(sel.anchor.offset).toBe(1);
expect(sel.focus.blockId).toBe('b');
expect(sel.focus.offset).toBe(3);
}); });
it('writes a cross-block model selection back to a native range spanning blocks', async () => { it('writes a cross-block model selection back to a native range spanning blocks', async () => {