6615
Yc
yoctoselect / files
yoctoselect / src / viewport.test.ts
2.1 KB
1import { describe, expect, test } from "bun:test"; 2import { adjustScroll, clampHighlight, moveDown, moveUp } from "./viewport.ts"; 3 4describe("moveDown", () => { 5 test("advances by one", () => { 6 expect(moveDown(0, 5)).toBe(1); 7 }); 8 9 test("wraps from last to first", () => { 10 expect(moveDown(4, 5)).toBe(0); 11 }); 12 13 test("returns 0 when total is 0", () => { 14 expect(moveDown(0, 0)).toBe(0); 15 }); 16}); 17 18describe("moveUp", () => { 19 test("goes back by one", () => { 20 expect(moveUp(2, 5)).toBe(1); 21 }); 22 23 test("wraps from first to last", () => { 24 expect(moveUp(0, 5)).toBe(4); 25 }); 26 27 test("returns 0 when total is 0", () => { 28 expect(moveUp(0, 0)).toBe(0); 29 }); 30}); 31 32describe("clampHighlight", () => { 33 test("returns same index when in range", () => { 34 expect(clampHighlight(3, 10)).toBe(3); 35 }); 36 37 test("clamps to last item when past end", () => { 38 expect(clampHighlight(15, 10)).toBe(9); 39 }); 40 41 test("returns 0 when total is 0", () => { 42 expect(clampHighlight(5, 0)).toBe(0); 43 }); 44}); 45 46describe("adjustScroll", () => { 47 test("no scroll needed when highlight is within viewport", () => { 48 expect(adjustScroll(3, 0, 5)).toBe(0); 49 }); 50 51 test("scrolls down when highlight passes bottom of viewport", () => { 52 // highlight=5, scrollOffset=0, maxVisible=5 → need scrollOffset=1 53 expect(adjustScroll(5, 0, 5)).toBe(1); 54 }); 55 56 test("scrolls up when highlight is above viewport", () => { 57 // highlight=2, scrollOffset=5, maxVisible=5 → need scrollOffset=2 58 expect(adjustScroll(2, 5, 5)).toBe(2); 59 }); 60 61 test("keeps scrollOffset when highlight is at top edge of viewport", () => { 62 expect(adjustScroll(3, 3, 5)).toBe(3); 63 }); 64 65 test("keeps scrollOffset when highlight is at bottom edge of viewport", () => { 66 // highlight=7, scrollOffset=3, maxVisible=5 → visible 3..7, highlight=7 is last visible 67 expect(adjustScroll(7, 3, 5)).toBe(3); 68 }); 69 70 test("wrapping to 0 resets scroll to 0", () => { 71 expect(adjustScroll(0, 10, 5)).toBe(0); 72 }); 73 74 test("wrapping to last item scrolls to show it", () => { 75 // 25 items, maxVisible=5, highlight wraps to 24 76 expect(adjustScroll(24, 0, 5)).toBe(20); 77 }); 78});