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

terminalFocus.browser.test.ts

59 lines1.9 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 consumePendingTerminalFocus,
7 focusTerminal,
8 focusTerminalViewport,
9 registerTerminalFocusTarget,
10} from "./terminalFocus";
11​
12// The drawer's collapsed shape: a 0fr grid row that clips the viewport, which
13// stays focusable, around a surface whose input refuses focus while hidden.
14function mountClippedViewport() {
15 const drawer = document.createElement("div");
16 drawer.style.cssText = "display:grid;grid-template-rows:0fr;overflow:clip";
17 const clip = document.createElement("div");
18 clip.style.cssText = "min-height:0;overflow:clip";
19 const mount = document.createElement("div");
20 mount.tabIndex = -1;
21 const input = document.createElement("textarea");
22 mount.append(input);
23 clip.append(mount);
24 drawer.append(clip);
25 document.body.append(drawer);
26 const state = { visible: false };
27 const surface = {
28 focus() {
29 if (state.visible) input.focus({ preventScroll: true });
30 },
31 };
32 const focus = () =>
33 focusTerminalViewport({ mount, surface, canOperate: true, visible: state.visible });
34 return {
35 input,
36 state,
37 focus,
38 [Symbol.dispose]: () => drawer.remove(),
39 };
41​
42describe("focusTerminalViewport", () => {
43 it("keeps a request made while the drawer is hidden until it shows", () => {
44 const ref = scopeThreadRef(EnvironmentId.make("env"), ThreadId.make("hidden-drawer"));
45 using viewport = mountClippedViewport();
46 const unregister = registerTerminalFocusTarget({
47 threadRef: ref,
48 terminalId: "a",
49 focus: viewport.focus,
50 });
51 using _unregister = { [Symbol.dispose]: unregister };
52​
53 focusTerminal(ref, "a");
54 viewport.state.visible = true;
55 consumePendingTerminalFocus(ref, "a");
56​
57 expect(document.activeElement).toBe(viewport.input);
58 });
59});