fix(sidebar): focus object search after opening a database
This commit is contained in:
6 files changed
+117
-10
No files matched your search
@@ -0,0 +1,22 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { OBJECT_BROWSER_SEARCH_FOCUS_EVENT, objectBrowserSearchFocusTabId, requestObjectBrowserSearchFocus } from "@/lib/tabs/objectBrowserSearchFocus";
|
||||
|
||||
describe("object browser search focus requests", () => {
|
||||
it("dispatches the target tab id", () => {
|
||||
const listener = vi.fn();
|
||||
window.addEventListener(OBJECT_BROWSER_SEARCH_FOCUS_EVENT, listener);
|
||||
|
||||
requestObjectBrowserSearchFocus("objects-1");
|
||||
|
||||
expect(listener).toHaveBeenCalledOnce();
|
||||
expect(objectBrowserSearchFocusTabId(listener.mock.calls[0][0])).toBe("objects-1");
|
||||
window.removeEventListener(OBJECT_BROWSER_SEARCH_FOCUS_EVENT, listener);
|
||||
});
|
||||
|
||||
it("ignores unrelated or malformed events", () => {
|
||||
expect(objectBrowserSearchFocusTabId(new Event(OBJECT_BROWSER_SEARCH_FOCUS_EVENT))).toBeUndefined();
|
||||
expect(objectBrowserSearchFocusTabId(new CustomEvent(OBJECT_BROWSER_SEARCH_FOCUS_EVENT, { detail: {} }))).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
export const OBJECT_BROWSER_SEARCH_FOCUS_EVENT = "dbx:focus-object-browser-search";
|
||||
|
||||
type ObjectBrowserSearchFocusDetail = {
|
||||
tabId: string;
|
||||
};
|
||||
|
||||
export function requestObjectBrowserSearchFocus(tabId: string) {
|
||||
if (typeof window === "undefined") return;
|
||||
window.dispatchEvent(new CustomEvent<ObjectBrowserSearchFocusDetail>(OBJECT_BROWSER_SEARCH_FOCUS_EVENT, { detail: { tabId } }));
|
||||
}
|
||||
|
||||
export function objectBrowserSearchFocusTabId(event: Event): string | undefined {
|
||||
if (!(event instanceof CustomEvent)) return undefined;
|
||||
const tabId = (event.detail as Partial<ObjectBrowserSearchFocusDetail> | null)?.tabId;
|
||||
return typeof tabId === "string" && tabId.length > 0 ? tabId : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user