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

composerSelectionHold.test.ts

55 lines1.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { selectionHoldsComposerOpen } from "./composerSelectionHold";
4​
5function node(parent: Node | null = null) {
6 const self = {
7 parent,
8 contains(other: Node | null): boolean {
9 let cursor: { parent: Node | null } | null = other as unknown as { parent: Node | null };
10 while (cursor) {
11 if (cursor === (self as unknown)) return true;
12 cursor = cursor.parent as { parent: Node | null } | null;
13 }
14 return false;
15 },
16 };
17 return self as unknown as Node;
19​
20function selection(anchor: Node, collapsed = false) {
21 return {
22 isCollapsed: collapsed,
23 rangeCount: 1,
24 getRangeAt: () => ({ commonAncestorContainer: anchor }) as Range,
25 };
27​
28describe("selectionHoldsComposerOpen", () => {
29 const timeline = node();
30 const message = node(timeline);
31 const elsewhere = node();
32​
33 it("holds for a range inside the timeline", () => {
34 expect(selectionHoldsComposerOpen(selection(message), timeline)).toBe(true);
35 });
36​
37 it("ignores a caret with nothing selected", () => {
38 expect(selectionHoldsComposerOpen(selection(message, true), timeline)).toBe(false);
39 });
40​
41 it("ignores selections outside the timeline", () => {
42 expect(selectionHoldsComposerOpen(selection(elsewhere), timeline)).toBe(false);
43 });
44​
45 it("ignores an empty or missing selection", () => {
46 expect(selectionHoldsComposerOpen(null, timeline)).toBe(false);
47 expect(
48 selectionHoldsComposerOpen(
49 { isCollapsed: false, rangeCount: 0, getRangeAt: () => ({}) as Range },
50 timeline,
51 ),
52 ).toBe(false);
53 expect(selectionHoldsComposerOpen(selection(message), null)).toBe(false);
54 });
55});