From 59ebd0f1225ae7a496c1b0d56635610fcfe76548 Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Mon, 9 Mar 2026 23:59:21 +0000 Subject: [PATCH] test: add test for attribute_autocomplete --- .../services/attribute_autocomplete.spec.ts | 47 +++++++++++++++ .../src/services/attribute_autocomplete.ts | 59 ++++++++++--------- 2 files changed, 78 insertions(+), 28 deletions(-) create mode 100644 apps/client/src/services/attribute_autocomplete.spec.ts diff --git a/apps/client/src/services/attribute_autocomplete.spec.ts b/apps/client/src/services/attribute_autocomplete.spec.ts new file mode 100644 index 0000000000..eb35790723 --- /dev/null +++ b/apps/client/src/services/attribute_autocomplete.spec.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from "vitest"; + +import { shouldAutocompleteHandleEnterKey } from "./attribute_autocomplete.js"; + +describe("attribute autocomplete enter handling", () => { + it("delegates plain Enter when the panel is open and an item is active", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "Enter", ctrlKey: false, metaKey: false }, + { isPanelOpen: true, hasActiveItem: true } + )).toBe(true); + }); + + it("does not delegate plain Enter when there is no active suggestion", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "Enter", ctrlKey: false, metaKey: false }, + { isPanelOpen: true, hasActiveItem: false } + )).toBe(false); + }); + + it("does not delegate plain Enter when the panel is closed", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "Enter", ctrlKey: false, metaKey: false }, + { isPanelOpen: false, hasActiveItem: false } + )).toBe(false); + }); + + it("does not delegate Ctrl+Enter even when an item is active", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "Enter", ctrlKey: true, metaKey: false }, + { isPanelOpen: true, hasActiveItem: true } + )).toBe(false); + }); + + it("does not delegate Cmd+Enter even when an item is active", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "Enter", ctrlKey: false, metaKey: true }, + { isPanelOpen: true, hasActiveItem: true } + )).toBe(false); + }); + + it("ignores non-Enter keys", () => { + expect(shouldAutocompleteHandleEnterKey( + { key: "ArrowDown", ctrlKey: false, metaKey: false }, + { isPanelOpen: false, hasActiveItem: false } + )).toBe(true); + }); +}); diff --git a/apps/client/src/services/attribute_autocomplete.ts b/apps/client/src/services/attribute_autocomplete.ts index f3e7d3a36a..0da8ad0607 100644 --- a/apps/client/src/services/attribute_autocomplete.ts +++ b/apps/client/src/services/attribute_autocomplete.ts @@ -13,6 +13,21 @@ interface NameItem extends BaseItem { name: string; } +export function shouldAutocompleteHandleEnterKey( + event: Pick, + { isPanelOpen, hasActiveItem }: { isPanelOpen: boolean; hasActiveItem: boolean } +) { + if (event.key !== "Enter") { + return true; + } + + if (event.ctrlKey || event.metaKey) { + return false; + } + + return isPanelOpen && hasActiveItem; +} + interface InitAttributeNameOptions { /** The element where the user types */ $el: JQuery; @@ -200,22 +215,16 @@ function initAttributeNameAutocomplete({ $el, attributeType, open, onValueChange }, 50); }, onKeyDown(e, handlers) { - if (e.key === "Enter") { - if (e.ctrlKey || e.metaKey) { - // Let the outer widget handle save shortcuts such as Ctrl+Enter. - return; - } - - if (isPanelOpen && hasActiveItem) { - // Prevent the enter key from propagating to parent dialogs - // (which might interpret it as "submit" or "save and close") - e.stopPropagation(); - } else { - // No active suggestion means the user is keeping their typed value. - // Let outer shortcuts continue to bubble. - return; - } + if (!shouldAutocompleteHandleEnterKey(e, { isPanelOpen, hasActiveItem })) { + return; } + + if (e.key === "Enter") { + // Prevent the enter key from propagating to parent dialogs + // (which might interpret it as "submit" or "save and close") + e.stopPropagation(); + } + handlers.onKeyDown(e as any); } }); @@ -397,20 +406,14 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }: }, 50); }, onKeyDown(e, handlers) { - if (e.key === "Enter") { - if (e.ctrlKey || e.metaKey) { - // Let the outer widget handle save shortcuts such as Ctrl+Enter. - return; - } - - if (isPanelOpen && hasActiveItem) { - e.stopPropagation(); - } else { - // No active suggestion means the user is keeping their typed value. - // Let outer shortcuts such as Ctrl+Enter continue to bubble. - return; - } + if (!shouldAutocompleteHandleEnterKey(e, { isPanelOpen, hasActiveItem })) { + return; } + + if (e.key === "Enter") { + e.stopPropagation(); + } + handlers.onKeyDown(e as any); } });