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

toast.logic.test.ts

114 lines2.8 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { assert, describe, it } from "vitest";
3import {
4 buildVisibleToastLayout,
5 shouldHideCollapsedToastContent,
6 shouldRenderThreadScopedToast,
7} from "./toast.logic";
8​
9describe("shouldHideCollapsedToastContent", () => {
10 it("keeps a single visible toast readable", () => {
11 assert.equal(shouldHideCollapsedToastContent(0, 1), false);
12 });
13​
14 it("keeps the front-most toast readable in a visible stack", () => {
15 assert.equal(shouldHideCollapsedToastContent(0, 3), false);
16 });
17​
18 it("hides non-front toasts until the stack is expanded", () => {
19 assert.equal(shouldHideCollapsedToastContent(1, 3), true);
20 });
21});
22​
23describe("buildVisibleToastLayout", () => {
24 it("computes indices and offsets from the visible subset", () => {
25 const visibleToasts = [
26 { id: "a", height: 48 },
27 { id: "b", height: 72 },
28 { id: "c", height: 24 },
29 ];
30​
31 const layout = buildVisibleToastLayout(visibleToasts);
32​
33 assert.equal(layout.frontmostHeight, 48);
34 assert.deepEqual(
35 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
36 id: toast.id,
37 visibleIndex,
38 offsetY,
39 })),
40 [
41 { id: "a", visibleIndex: 0, offsetY: 0 },
42 { id: "b", visibleIndex: 1, offsetY: 48 },
43 { id: "c", visibleIndex: 2, offsetY: 120 },
44 ],
45 );
46 });
47​
48 it("treats missing heights as zero", () => {
49 const layout = buildVisibleToastLayout([
50 { id: "a" },
51 { id: "b", height: undefined },
52 { id: "c", height: 30 },
53 ]);
54​
55 assert.equal(layout.frontmostHeight, 0);
56 assert.deepEqual(
57 layout.items.map(({ toast, offsetY }) => ({
58 id: toast.id,
59 offsetY,
60 })),
61 [
62 { id: "a", offsetY: 0 },
63 { id: "b", offsetY: 0 },
64 { id: "c", offsetY: 0 },
65 ],
66 );
67 });
68});
69​
70describe("shouldRenderThreadScopedToast", () => {
71 const activeThreadRef = {
72 environmentId: "environment-a",
73 threadId: "thread-1",
74 } as ScopedThreadRef;
75​
76 it("renders a toast scoped to the active thread ref", () => {
77 assert.equal(
78 shouldRenderThreadScopedToast(
79 {
80 threadRef: activeThreadRef,
81 },
82 activeThreadRef,
83 ),
84 true,
85 );
86 });
87​
88 it("hides a scoped toast when the environment differs", () => {
89 assert.equal(
90 shouldRenderThreadScopedToast(
91 {
92 threadRef: {
93 environmentId: "environment-b",
94 threadId: "thread-1",
95 } as ScopedThreadRef,
96 },
97 activeThreadRef,
98 ),
99 false,
100 );
101 });
102​
103 it("keeps legacy thread-id scoped toasts working", () => {
104 assert.equal(
105 shouldRenderThreadScopedToast(
106 {
107 threadId: "thread-1" as never,
108 },
109 activeThreadRef,
110 ),
111 true,
112 );
113 });
114});