70160
Ce
cel-tui / files
cel-tui / packages / core / src / scroll.test.ts
2.5 KB
1import { describe, expect, test } from "bun:test"; 2import { layout } from "./layout.js"; 3import { HStack, VStack } from "./primitives/stacks.js"; 4import { Text } from "./primitives/text.js"; 5import { TextInput } from "./primitives/text-input.js"; 6import { getMaxScrollOffset, getScrollStep } from "./scroll.js"; 7 8function item<T>(items: readonly T[], index: number): T { 9 const value = items[index]; 10 expect(value).toBeDefined(); 11 if (value === undefined) { 12 throw new Error(`Missing item at index ${index}`); 13 } 14 return value; 15} 16 17describe("scroll", () => { 18 test("TextInput max scroll offset uses visual wrapping and padding", () => { 19 const root = layout( 20 VStack({ width: 8, height: 4 }, [ 21 TextInput({ 22 width: 8, 23 height: 4, 24 padding: { x: 1, y: 1 }, 25 value: "foo bar baz", 26 onChange: () => {}, 27 }), 28 ]), 29 8, 30 4, 31 ); 32 33 expect(getMaxScrollOffset(item(root.children, 0))).toBe(1); 34 }); 35 36 test("scrollable VStack max scroll offset matches content overflow", () => { 37 const root = layout( 38 VStack({ width: 10, height: 3, overflow: "scroll" }, [ 39 Text("line0"), 40 Text("line1"), 41 Text("line2"), 42 Text("line3"), 43 ]), 44 10, 45 3, 46 ); 47 48 expect(getMaxScrollOffset(root)).toBe(1); 49 }); 50 51 test("adaptive scroll step uses the scroll target viewport and clamps to 3..8", () => { 52 const small = layout( 53 VStack({ width: 10, height: 2, overflow: "scroll" }, [Text("x")]), 54 10, 55 2, 56 ); 57 const medium = layout( 58 VStack({ width: 10, height: 12, overflow: "scroll" }, [Text("x")]), 59 10, 60 12, 61 ); 62 const large = layout( 63 HStack({ width: 30, height: 3, overflow: "scroll" }, [Text("x")]), 64 30, 65 3, 66 ); 67 68 expect(getScrollStep(small)).toBe(3); 69 expect(getScrollStep(medium)).toBe(4); 70 expect(getScrollStep(large)).toBe(8); 71 }); 72 73 test("scrollStep prop overrides the adaptive default", () => { 74 const scrollable = layout( 75 VStack({ width: 10, height: 12, overflow: "scroll", scrollStep: 6 }, [ 76 Text("x"), 77 ]), 78 10, 79 12, 80 ); 81 const textInputRoot = layout( 82 VStack({ width: 8, height: 4 }, [ 83 TextInput({ 84 width: 8, 85 height: 4, 86 scrollStep: 5, 87 value: "line1\nline2\nline3\nline4\nline5\nline6", 88 onChange: () => {}, 89 }), 90 ]), 91 8, 92 4, 93 ); 94 95 expect(getScrollStep(scrollable)).toBe(6); 96 expect(getScrollStep(item(textInputRoot.children, 0))).toBe(5); 97 }); 98});