Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { assert, describe, it } from "vite-plus/test";
3import {
4 buildVisibleToastLayout,
5 hasVisibleToastAction,
6 shouldHideCollapsedToastContent,
7 shouldRenderThreadScopedToast,
8} from "./toast.logic";
9
10describe("hasVisibleToastAction", () => {
11 it("treats a labeled action as visible", () => {
12 assert.equal(hasVisibleToastAction({ children: "Update" }), true);
13 });
14
15 it("hides an explicit empty action used to clear a previous CTA", () => {
16 assert.equal(hasVisibleToastAction({ children: null }), false);
17 assert.equal(hasVisibleToastAction({ children: "" }), false);
18 assert.equal(hasVisibleToastAction(undefined), false);
19 });
20});
21
22describe("shouldHideCollapsedToastContent", () => {
23 it("keeps a single visible toast readable", () => {
24 assert.equal(shouldHideCollapsedToastContent(0, 1), false);
25 });
26
27 it("keeps the front-most toast readable in a visible stack", () => {
28 assert.equal(shouldHideCollapsedToastContent(0, 3), false);
29 });
30
31 it("hides non-front toasts until the stack is expanded", () => {
32 assert.equal(shouldHideCollapsedToastContent(1, 3), true);
33 });
34});
35
36describe("buildVisibleToastLayout", () => {
37 it("computes indices and offsets from the visible subset", () => {
38 const visibleToasts = [
39 { id: "a", height: 48 },
40 { id: "b", height: 72 },
41 { id: "c", height: 24 },
42 ];
43
44 const layout = buildVisibleToastLayout(visibleToasts);
45
46 assert.equal(layout.frontmostHeight, 48);
47 assert.deepEqual(
48 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
49 id: toast.id,
50 visibleIndex,
51 offsetY,
52 })),
53 [
54 { id: "a", visibleIndex: 0, offsetY: 0 },
55 { id: "b", visibleIndex: 1, offsetY: 48 },
56 { id: "c", visibleIndex: 2, offsetY: 120 },
57 ],
58 );
59 });
60
61 it("reflows live toasts forward when the front toast is dismissed", () => {
62 const visibleToasts = [
63 { id: "a", height: 48, transitionStatus: "ending" as const },
64 { id: "b", height: 72 },
65 { id: "c", height: 24 },
66 ];
67
68 const layout = buildVisibleToastLayout(visibleToasts);
69
70 assert.equal(layout.frontmostHeight, 72);
71 assert.deepEqual(
72 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
73 id: toast.id,
74 visibleIndex,
75 offsetY,
76 })),
77 [
78 { id: "a", visibleIndex: 0, offsetY: 0 },
79 { id: "b", visibleIndex: 0, offsetY: 0 },
80 { id: "c", visibleIndex: 1, offsetY: 72 },
81 ],
82 );
83 });
84
85 it("keeps a non-front ending toast at its current slot so it exits straight", () => {
86 const visibleToasts = [
87 { id: "a", height: 48 },
88 { id: "b", height: 72, transitionStatus: "ending" as const },
89 { id: "c", height: 24 },
90 ];
91
92 const layout = buildVisibleToastLayout(visibleToasts);
93
94 assert.equal(layout.frontmostHeight, 48);
95 assert.deepEqual(
96 layout.items.map(({ toast, visibleIndex, offsetY }) => ({
97 id: toast.id,
98 visibleIndex,
99 offsetY,
100 })),
101 [
102 { id: "a", visibleIndex: 0, offsetY: 0 },
103 { id: "b", visibleIndex: 1, offsetY: 48 },
104 { id: "c", visibleIndex: 1, offsetY: 48 },
105 ],
106 );
107 });
108
109 it("treats missing heights as zero", () => {
110 const layout = buildVisibleToastLayout([
111 { id: "a" },
112 { id: "b", height: undefined },
113 { id: "c", height: 30 },
114 ]);
115
116 assert.equal(layout.frontmostHeight, 0);
117 assert.deepEqual(
118 layout.items.map(({ toast, offsetY }) => ({
119 id: toast.id,
120 offsetY,
121 })),
122 [
123 { id: "a", offsetY: 0 },
124 { id: "b", offsetY: 0 },
125 { id: "c", offsetY: 0 },
126 ],
127 );
128 });
129});
130
131describe("shouldRenderThreadScopedToast", () => {
132 const activeThreadRef = {
133 environmentId: "environment-a",
134 threadId: "thread-1",
135 } as ScopedThreadRef;
136
137 it("renders a toast scoped to the active thread ref", () => {
138 assert.equal(
139 shouldRenderThreadScopedToast(
140 {
141 threadRef: activeThreadRef,
142 },
143 activeThreadRef,
144 ),
145 true,
146 );
147 });
148
149 it("hides a scoped toast when the environment differs", () => {
150 assert.equal(
151 shouldRenderThreadScopedToast(
152 {
153 threadRef: {
154 environmentId: "environment-b",
155 threadId: "thread-1",
156 } as ScopedThreadRef,
157 },
158 activeThreadRef,
159 ),
160 false,
161 );
162 });
163
164 it("keeps legacy thread-id scoped toasts working", () => {
165 assert.equal(
166 shouldRenderThreadScopedToast(
167 {
168 threadId: "thread-1" as never,
169 },
170 activeThreadRef,
171 ),
172 true,
173 );
174 });
175});