|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { createRoot } from "solid-js" |
| 3 | +import { createStore } from "solid-js/store" |
| 4 | +import { createComposerEditor } from "../src/composer/editor/interaction" |
| 5 | +import type { ComposerPersistedState } from "../src/composer/types" |
| 6 | + |
| 7 | +test.each([ |
| 8 | + { key: "Escape", isComposing: true, keyCode: 27, list: false, stops: 0, selections: 0, prevented: false }, |
| 9 | + { key: "Escape", isComposing: false, keyCode: 229, list: false, stops: 0, selections: 0, prevented: false }, |
| 10 | + { key: "Escape", isComposing: false, keyCode: 27, list: false, stops: 1, selections: 0, prevented: true }, |
| 11 | + { key: "Tab", isComposing: true, keyCode: 9, list: true, stops: 0, selections: 0, prevented: false }, |
| 12 | + { key: "Tab", isComposing: false, keyCode: 229, list: true, stops: 0, selections: 0, prevented: false }, |
| 13 | + { key: "Enter", isComposing: false, keyCode: 229, list: true, stops: 0, selections: 0, prevented: false }, |
| 14 | + { key: "Tab", isComposing: false, keyCode: 9, list: true, stops: 0, selections: 1, prevented: true }, |
| 15 | +])("IME key ownership: $key, composing=$isComposing, keyCode=$keyCode", (row) => { |
| 16 | + const value = createRoot((dispose) => { |
| 17 | + const store = createStore<ComposerPersistedState>({ prompt: [], context: { items: [] } }) |
| 18 | + |
| 19 | + const [calls, setCalls] = createStore({ stops: 0, selections: 0 }) |
| 20 | + |
| 21 | + const editor = createComposerEditor({ |
| 22 | + store, |
| 23 | + commands: () => [{ id: "review", kind: "command", label: "/review" }], |
| 24 | + context: () => [], |
| 25 | + searchContextFiles: () => [], |
| 26 | + onSuggestionSelect: () => () => setCalls("selections", (count) => count + 1), |
| 27 | + view: { |
| 28 | + submit: { |
| 29 | + working: () => true, |
| 30 | + stopping: () => false, |
| 31 | + onSubmit() {}, |
| 32 | + onStop: () => setCalls("stops", (count) => count + 1), |
| 33 | + }, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + return { editor, calls, dispose } |
| 38 | + }) |
| 39 | + |
| 40 | + try { |
| 41 | + if (row.list) { |
| 42 | + value.editor.openCommands() |
| 43 | + value.editor.dispatch({ type: "popover.results", ids: ["review"] }) |
| 44 | + expect(value.editor.state.popover.type).toBe("command-inline") |
| 45 | + expect(value.editor.suggestions().map((item) => item.id)).toEqual(["review"]) |
| 46 | + } |
| 47 | + |
| 48 | + const event = new KeyboardEvent("keydown", { |
| 49 | + key: row.key, |
| 50 | + isComposing: row.isComposing, |
| 51 | + cancelable: true, |
| 52 | + }) |
| 53 | + |
| 54 | + // happy-dom does not initialize the legacy keyCode field from KeyboardEventInit. |
| 55 | + Object.defineProperty(event, "keyCode", { value: row.keyCode }) |
| 56 | + expect(value.editor.onKeyDown(event)).toBe(true) |
| 57 | + expect(value.calls.stops).toBe(row.stops) |
| 58 | + expect(value.calls.selections).toBe(row.selections) |
| 59 | + expect(event.defaultPrevented).toBe(row.prevented) |
| 60 | + |
| 61 | + if (row.list && !row.selections) expect(value.editor.state.popover.type).toBe("command-inline") |
| 62 | + } finally { |
| 63 | + value.dispose() |
| 64 | + } |
| 65 | +}) |
0 commit comments