|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * Guards the `@` menu's keyboard navigation against the async-data race: the suggestion plugin grabs |
| 5 | + * the list's `onKeyDown` handle once, but workspace items arrive later via the store. The handle must |
| 6 | + * read live values so arrow/enter work after the data lands (otherwise keys fall through to the editor). |
| 7 | + * The second test drives the real `ReactRenderer` path the suggestion plugin actually uses. |
| 8 | + */ |
| 9 | +import { act, createRef } from 'react' |
| 10 | +import { Editor } from '@tiptap/core' |
| 11 | +import { EditorContent, ReactRenderer } from '@tiptap/react' |
| 12 | +import { File } from 'lucide-react' |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 14 | +import { createMarkdownEditorExtensions } from '../extensions' |
| 15 | +import { MentionList, type MentionListHandle } from './mention-list' |
| 16 | +import { createMentionStore } from './mention-store' |
| 17 | +import type { MentionItem } from './types' |
| 18 | + |
| 19 | +const items: MentionItem[] = [ |
| 20 | + { kind: 'file', id: 'a', label: 'Alpha', group: 'Files', icon: File }, |
| 21 | + { kind: 'file', id: 'b', label: 'Beta', group: 'Files', icon: File }, |
| 22 | +] |
| 23 | + |
| 24 | +const arrowDown = { event: new KeyboardEvent('keydown', { key: 'ArrowDown' }) } |
| 25 | +const enter = { event: new KeyboardEvent('keydown', { key: 'Enter' }) } |
| 26 | + |
| 27 | +describe('MentionList keyboard nav', () => { |
| 28 | + let container: HTMLElement |
| 29 | + let root: ReturnType<typeof import('react-dom/client').createRoot> |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + // jsdom implements neither — both are exercised by scroll-into-view and ProseMirror. |
| 33 | + Element.prototype.scrollIntoView = vi.fn() |
| 34 | + document.elementFromPoint = vi.fn(() => null) |
| 35 | + const { createRoot } = await import('react-dom/client') |
| 36 | + container = document.createElement('div') |
| 37 | + document.body.appendChild(container) |
| 38 | + root = createRoot(container) |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + act(() => root.unmount()) |
| 43 | + container.remove() |
| 44 | + }) |
| 45 | + |
| 46 | + it('navigates with arrows + inserts on enter once async items have loaded', () => { |
| 47 | + const ref = createRef<MentionListHandle>() |
| 48 | + const command = vi.fn() |
| 49 | + const store = createMentionStore() |
| 50 | + |
| 51 | + // Menu opens before the workspace data resolves — the store is still empty. |
| 52 | + act(() => { |
| 53 | + root.render(<MentionList ref={ref} query='' command={command} store={store} />) |
| 54 | + }) |
| 55 | + expect(ref.current?.onKeyDown(arrowDown)).toBe(false) |
| 56 | + |
| 57 | + // Async data lands; the captured handle must now see the items and intercept the keys. |
| 58 | + act(() => store.set(items)) |
| 59 | + |
| 60 | + let handled: boolean | undefined |
| 61 | + act(() => { |
| 62 | + handled = ref.current?.onKeyDown(arrowDown) |
| 63 | + }) |
| 64 | + expect(handled).toBe(true) |
| 65 | + |
| 66 | + act(() => { |
| 67 | + ref.current?.onKeyDown(enter) |
| 68 | + }) |
| 69 | + expect(command).toHaveBeenCalledWith(items[1]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('exposes a working onKeyDown through ReactRenderer (the suggestion plugin path)', async () => { |
| 73 | + const editor = new Editor({ extensions: createMarkdownEditorExtensions({ placeholder: '' }) }) |
| 74 | + act(() => { |
| 75 | + root.render(<EditorContent editor={editor} />) |
| 76 | + }) |
| 77 | + |
| 78 | + const command = vi.fn() |
| 79 | + const store = createMentionStore() |
| 80 | + const renderer = new ReactRenderer<MentionListHandle>(MentionList, { |
| 81 | + editor, |
| 82 | + props: { query: '', command, store }, |
| 83 | + }) |
| 84 | + // Let the portal mount so ReactRenderer captures the imperative handle. |
| 85 | + await act(async () => {}) |
| 86 | + |
| 87 | + expect(renderer.ref).not.toBeNull() |
| 88 | + expect(renderer.ref?.onKeyDown(arrowDown)).toBe(false) |
| 89 | + |
| 90 | + act(() => store.set(items)) |
| 91 | + expect(renderer.ref?.onKeyDown(arrowDown)).toBe(true) |
| 92 | + |
| 93 | + renderer.destroy() |
| 94 | + editor.destroy() |
| 95 | + }) |
| 96 | +}) |
0 commit comments