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

chat-scroll.test.ts

65 lines1.4 KB

1import { describe, expect, it } from "vitest";
2​
3import {
4 AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
5 isScrollContainerNearBottom,
6} from "./chat-scroll";
7​
8describe("isScrollContainerNearBottom", () => {
9 it("returns true when already at bottom", () => {
10 expect(
11 isScrollContainerNearBottom({
12 scrollTop: 600,
13 clientHeight: 400,
14 scrollHeight: 1_000,
15 }),
16 ).toBe(true);
17 });
18​
19 it("returns true when within the auto-scroll threshold", () => {
20 expect(
21 isScrollContainerNearBottom({
22 scrollTop: 540,
23 clientHeight: 400,
24 scrollHeight: 1_000,
25 }),
26 ).toBe(true);
27 });
28​
29 it("returns false when the user is meaningfully above the bottom", () => {
30 expect(
31 isScrollContainerNearBottom({
32 scrollTop: 520,
33 clientHeight: 400,
34 scrollHeight: 1_000,
35 }),
36 ).toBe(false);
37 });
38​
39 it("clamps negative thresholds to zero", () => {
40 expect(
41 isScrollContainerNearBottom(
42 {
43 scrollTop: 539,
44 clientHeight: 400,
45 scrollHeight: 1_000,
46 },
47 -1,
48 ),
49 ).toBe(false);
50 });
51​
52 it("falls back to the default threshold for non-finite values", () => {
53 expect(
54 isScrollContainerNearBottom(
55 {
56 scrollTop: 540,
57 clientHeight: 400,
58 scrollHeight: 1_000,
59 },
60 Number.NaN,
61 ),
62 ).toBe(true);
63 expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64);
64 });
65});