|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + COMMAND_PRIORITY_EDITOR, |
| 4 | + createEditor, |
| 5 | + KEY_ENTER_COMMAND, |
| 6 | +} from "lexical"; |
| 7 | +import { |
| 8 | + registerPromptLexicalPreventedEnterCommand, |
| 9 | +} from "@/components/ai-elements/prompt-lexical-editor.commands"; |
| 10 | + |
| 11 | +describe("prompt Lexical Enter handling", () => { |
| 12 | + test("consumes Enter after the prompt composer prevents it", () => { |
| 13 | + const editor = createEditor({ namespace: "prompt-enter-prevented" }); |
| 14 | + let plainTextHandlerCalls = 0; |
| 15 | + const unregisterGuard = |
| 16 | + registerPromptLexicalPreventedEnterCommand(editor); |
| 17 | + const unregisterPlainTextHandler = editor.registerCommand( |
| 18 | + KEY_ENTER_COMMAND, |
| 19 | + () => { |
| 20 | + plainTextHandlerCalls += 1; |
| 21 | + return true; |
| 22 | + }, |
| 23 | + COMMAND_PRIORITY_EDITOR, |
| 24 | + ); |
| 25 | + |
| 26 | + const handled = editor.dispatchCommand(KEY_ENTER_COMMAND, { |
| 27 | + defaultPrevented: true, |
| 28 | + } as KeyboardEvent); |
| 29 | + |
| 30 | + expect(handled).toBe(true); |
| 31 | + expect(plainTextHandlerCalls).toBe(0); |
| 32 | + unregisterPlainTextHandler(); |
| 33 | + unregisterGuard(); |
| 34 | + }); |
| 35 | + |
| 36 | + test("leaves multiline Enter behavior to Lexical when it is not prevented", () => { |
| 37 | + const editor = createEditor({ namespace: "prompt-enter-multiline" }); |
| 38 | + let plainTextHandlerCalls = 0; |
| 39 | + const unregisterGuard = |
| 40 | + registerPromptLexicalPreventedEnterCommand(editor); |
| 41 | + const unregisterPlainTextHandler = editor.registerCommand( |
| 42 | + KEY_ENTER_COMMAND, |
| 43 | + () => { |
| 44 | + plainTextHandlerCalls += 1; |
| 45 | + return true; |
| 46 | + }, |
| 47 | + COMMAND_PRIORITY_EDITOR, |
| 48 | + ); |
| 49 | + |
| 50 | + const handled = editor.dispatchCommand(KEY_ENTER_COMMAND, { |
| 51 | + defaultPrevented: false, |
| 52 | + } as KeyboardEvent); |
| 53 | + |
| 54 | + expect(handled).toBe(true); |
| 55 | + expect(plainTextHandlerCalls).toBe(1); |
| 56 | + unregisterPlainTextHandler(); |
| 57 | + unregisterGuard(); |
| 58 | + }); |
| 59 | +}); |
0 commit comments