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

composerEventScope.test.ts

80 lines3.1 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 isInsideCollapsedComposerControls,
5 isInsideComposerFloatingLayer,
6 isInsideRestingComposerControlScope,
7} from "./composerEventScope";
8​
9class FakeElement {
10 constructor(private readonly matchingSelector: string | null) {}
11​
12 closest(selector: string): FakeElement | null {
13 return this.matchingSelector !== null &&
14 selector.split(",").some((candidate) => candidate === this.matchingSelector)
15 ? this
16 : null;
17 }
19​
20afterEach(() => {
21 vi.unstubAllGlobals();
22});
23​
24describe("composer event scopes", () => {
25 it("recognizes events from the portaled resting controls", () => {
26 vi.stubGlobal("Element", FakeElement);
27​
28 const target = new FakeElement('[data-chat-composer-resting-controls="true"]');
29 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
30 });
31​
32 it("recognizes events from the composer context strip controls", () => {
33 vi.stubGlobal("Element", FakeElement);
34​
35 const target = new FakeElement("[data-composer-context-control]");
36 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
37 });
38​
39 it("keeps resting image previews focused without expanding their subtree", () => {
40 vi.stubGlobal("Element", FakeElement);
41​
42 const target = new FakeElement('[data-chat-composer-resting-images="true"]');
43 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
44 });
45​
46 it("includes composer-owned floating layers in the resting control scope", () => {
47 vi.stubGlobal("Element", FakeElement);
48​
49 const target = new FakeElement('[data-chat-composer-floating-layer="true"]');
50 expect(isInsideComposerFloatingLayer(target as unknown as EventTarget)).toBe(true);
51 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
52 });
53​
54 it("leaves unrelated floating layers outside the composer scope", () => {
55 vi.stubGlobal("Element", FakeElement);
56​
57 const target = new FakeElement('[data-slot="popover-popup"]');
58 expect(isInsideComposerFloatingLayer(target as unknown as EventTarget)).toBe(false);
59 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(false);
60 });
61​
62 it("leaves ordinary composer targets outside the portaled control scope", () => {
63 vi.stubGlobal("Element", FakeElement);
64​
65 const target = new FakeElement(null);
66 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(false);
67 expect(isInsideRestingComposerControlScope(null)).toBe(false);
68 });
69​
70 it("recognizes banner and drawer controls docked above the surface", () => {
71 vi.stubGlobal("Element", FakeElement);
72​
73 const target = new FakeElement('[data-chat-composer-collapsed-controls="true"]');
74 expect(isInsideCollapsedComposerControls(target as unknown as EventTarget)).toBe(true);
75 expect(isInsideCollapsedComposerControls(new FakeElement(null) as unknown as EventTarget)).toBe(
76 false,
77 );
78 expect(isInsideCollapsedComposerControls(null)).toBe(false);
79 });
80});