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

fab-scroll.test.ts

40 lines1.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { updateFabScroll, type FabScrollState } from "./fab-scroll";
4​
5describe("Material FAB scroll direction", () => {
6 const initial: FabScrollState = { anchor: 0, expanded: true };
7​
8 it("shrinks scrolling down and expands scrolling up without returning to the top", () => {
9 const collapsed = updateFabScroll(initial, 80, 500);
10 expect(collapsed.expanded).toBe(false);
11 const lower = updateFabScroll(collapsed, 180, 500);
12 expect(lower.expanded).toBe(false);
13 expect(updateFabScroll(lower, 160, 500).expanded).toBe(true);
14 });
15​
16 it("accumulates travel but ignores jitter around a direction change", () => {
17 let state = updateFabScroll(initial, 10, 500);
18 expect(state.expanded).toBe(true);
19 state = updateFabScroll(state, 14, 500);
20 expect(state.expanded).toBe(false);
21 state = updateFabScroll(state, 100, 500);
22 state = updateFabScroll(state, 95, 500);
23 state = updateFabScroll(state, 98, 500);
24 expect(state.expanded).toBe(false);
25 expect(updateFabScroll(state, 88, 500).expanded).toBe(true);
26 });
27​
28 it("does not expand from bottom bounce or collapse from top bounce", () => {
29 const bottom = updateFabScroll(initial, 500, 500);
30 const bounce = updateFabScroll(bottom, 560, 500);
31 expect(updateFabScroll(bounce, 500, 500).expanded).toBe(false);
32 expect(updateFabScroll(initial, -50, 500)).toEqual(initial);
33 });
34​
35 it("expands near the top and when filtering leaves a non-scrollable list", () => {
36 const collapsed = updateFabScroll(initial, 80, 500);
37 expect(updateFabScroll(collapsed, 5, 500).expanded).toBe(true);
38 expect(updateFabScroll(collapsed, 80, -20)).toEqual(initial);
39 });
40});