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