Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalFocus.test.ts

56 lines1.7 KB

1import { afterEach, describe, expect, it } from "vitest";
2​
3import { isTerminalFocused } from "./terminalFocus";
4​
5class MockHTMLElement {
6 isConnected = false;
7 className = "";
8​
9 readonly classList = {
10 contains: (value: string) => this.className.split(/\s+/).includes(value),
11 };
12​
13 closest(selector: string): MockHTMLElement | null {
14 return selector === ".thread-terminal-drawer .xterm" && this.isConnected ? this : null;
15 }
17​
18const originalDocument = globalThis.document;
19const originalHTMLElement = globalThis.HTMLElement;
20​
21afterEach(() => {
22 if (originalDocument === undefined) {
23 delete (globalThis as { document?: Document }).document;
24 } else {
25 globalThis.document = originalDocument;
26 }
27​
28 if (originalHTMLElement === undefined) {
29 delete (globalThis as { HTMLElement?: typeof HTMLElement }).HTMLElement;
30 } else {
31 globalThis.HTMLElement = originalHTMLElement;
32 }
33});
34​
35describe("isTerminalFocused", () => {
36 it("returns false for detached xterm helper textareas", () => {
37 const detached = new MockHTMLElement();
38 detached.className = "xterm-helper-textarea";
39​
40 globalThis.HTMLElement = MockHTMLElement as unknown as typeof HTMLElement;
41 globalThis.document = { activeElement: detached } as Document;
42​
43 expect(isTerminalFocused()).toBe(false);
44 });
45​
46 it("returns true for connected xterm helper textareas", () => {
47 const attached = new MockHTMLElement();
48 attached.className = "xterm-helper-textarea";
49 attached.isConnected = true;
50​
51 globalThis.HTMLElement = MockHTMLElement as unknown as typeof HTMLElement;
52 globalThis.document = { activeElement: attached } as Document;
53​
54 expect(isTerminalFocused()).toBe(true);
55 });
56});