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

composerEventScope.test.ts

73 lines2.8 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("keeps resting image previews focused without expanding their subtree", () => {
33 vi.stubGlobal("Element", FakeElement);
34​
35 const target = new FakeElement('[data-chat-composer-resting-images="true"]');
36 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
37 });
38​
39 it("includes composer-owned floating layers in the resting control scope", () => {
40 vi.stubGlobal("Element", FakeElement);
41​
42 const target = new FakeElement('[data-chat-composer-floating-layer="true"]');
43 expect(isInsideComposerFloatingLayer(target as unknown as EventTarget)).toBe(true);
44 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(true);
45 });
46​
47 it("leaves unrelated floating layers outside the composer scope", () => {
48 vi.stubGlobal("Element", FakeElement);
49​
50 const target = new FakeElement('[data-slot="popover-popup"]');
51 expect(isInsideComposerFloatingLayer(target as unknown as EventTarget)).toBe(false);
52 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(false);
53 });
54​
55 it("leaves ordinary composer targets outside the portaled control scope", () => {
56 vi.stubGlobal("Element", FakeElement);
57​
58 const target = new FakeElement(null);
59 expect(isInsideRestingComposerControlScope(target as unknown as EventTarget)).toBe(false);
60 expect(isInsideRestingComposerControlScope(null)).toBe(false);
61 });
62​
63 it("recognizes banner and drawer controls docked above the surface", () => {
64 vi.stubGlobal("Element", FakeElement);
65​
66 const target = new FakeElement('[data-chat-composer-collapsed-controls="true"]');
67 expect(isInsideCollapsedComposerControls(target as unknown as EventTarget)).toBe(true);
68 expect(isInsideCollapsedComposerControls(new FakeElement(null) as unknown as EventTarget)).toBe(
69 false,
70 );
71 expect(isInsideCollapsedComposerControls(null)).toBe(false);
72 });
73});