|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { calculateVisibleStart } from "../ui/DropdownMenu"; |
| 4 | + |
| 5 | +test("calculateVisibleStart centers active item when possible", () => { |
| 6 | + // 10 items, max 5 visible, active index 4 (middle) |
| 7 | + // Should show items 2-6 (start at 2) |
| 8 | + const start = calculateVisibleStart(4, 10, 5); |
| 9 | + assert.equal(start, 2); |
| 10 | +}); |
| 11 | + |
| 12 | +test("calculateVisibleStart handles active item at the beginning", () => { |
| 13 | + // 10 items, max 5 visible, active index 0 |
| 14 | + // Should show items 0-4 (start at 0) |
| 15 | + const start = calculateVisibleStart(0, 10, 5); |
| 16 | + assert.equal(start, 0); |
| 17 | +}); |
| 18 | + |
| 19 | +test("calculateVisibleStart handles active item at the end", () => { |
| 20 | + // 10 items, max 5 visible, active index 9 (last) |
| 21 | + // Should show items 5-9 (start at 5) |
| 22 | + const start = calculateVisibleStart(9, 10, 5); |
| 23 | + assert.equal(start, 5); |
| 24 | +}); |
| 25 | + |
| 26 | +test("calculateVisibleStart handles fewer items than maxVisible", () => { |
| 27 | + // 3 items, max 5 visible, active index 1 |
| 28 | + // Should show all items (start at 0) |
| 29 | + const start = calculateVisibleStart(1, 3, 5); |
| 30 | + assert.equal(start, 0); |
| 31 | +}); |
| 32 | + |
| 33 | +test("calculateVisibleStart handles single item", () => { |
| 34 | + // 1 item, max 5 visible, active index 0 |
| 35 | + // Should start at 0 |
| 36 | + const start = calculateVisibleStart(0, 1, 5); |
| 37 | + assert.equal(start, 0); |
| 38 | +}); |
| 39 | + |
| 40 | +test("calculateVisibleStart handles empty list", () => { |
| 41 | + // 0 items, max 5 visible, active index 0 |
| 42 | + // Should start at 0 |
| 43 | + const start = calculateVisibleStart(0, 0, 5); |
| 44 | + assert.equal(start, 0); |
| 45 | +}); |
| 46 | + |
| 47 | +test("calculateVisibleStart handles activeIndex near start with odd maxVisible", () => { |
| 48 | + // 10 items, max 7 visible (odd), active index 2 |
| 49 | + // floor((7-1)/2) = 3, so 2-3 = -1, clamped to 0 |
| 50 | + const start = calculateVisibleStart(2, 10, 7); |
| 51 | + assert.equal(start, 0); |
| 52 | +}); |
| 53 | + |
| 54 | +test("calculateVisibleStart handles activeIndex near start with even maxVisible", () => { |
| 55 | + // 10 items, max 6 visible (even), active index 2 |
| 56 | + // floor((6-1)/2) = 2, so 2-2 = 0 |
| 57 | + const start = calculateVisibleStart(2, 10, 6); |
| 58 | + assert.equal(start, 0); |
| 59 | +}); |
| 60 | + |
| 61 | +test("calculateVisibleStart keeps active item centered in middle range", () => { |
| 62 | + // 20 items, max 5 visible, active index 10 |
| 63 | + // floor((5-1)/2) = 2, so 10-2 = 8 |
| 64 | + const start = calculateVisibleStart(10, 20, 5); |
| 65 | + assert.equal(start, 8); |
| 66 | +}); |
| 67 | + |
| 68 | +test("calculateVisibleStart handles activeIndex at exact boundary", () => { |
| 69 | + // 10 items, max 5 visible, active index 2 (boundary where centering starts) |
| 70 | + // floor((5-1)/2) = 2, so 2-2 = 0 |
| 71 | + const start = calculateVisibleStart(2, 10, 5); |
| 72 | + assert.equal(start, 0); |
| 73 | +}); |
| 74 | + |
| 75 | +test("calculateVisibleStart handles activeIndex just after boundary", () => { |
| 76 | + // 10 items, max 5 visible, active index 3 |
| 77 | + // floor((5-1)/2) = 2, so 3-2 = 1 |
| 78 | + const start = calculateVisibleStart(3, 10, 5); |
| 79 | + assert.equal(start, 1); |
| 80 | +}); |
| 81 | + |
| 82 | +test("calculateVisibleStart handles large maxVisible", () => { |
| 83 | + // 10 items, max 100 visible, active index 5 |
| 84 | + // Should show all items (start at 0) |
| 85 | + const start = calculateVisibleStart(5, 10, 100); |
| 86 | + assert.equal(start, 0); |
| 87 | +}); |
| 88 | + |
| 89 | +test("calculateVisibleStart handles activeIndex equal to totalItems", () => { |
| 90 | + // 10 items, max 5 visible, active index 10 (out of bounds) |
| 91 | + // floor((5-1)/2) = 2, so 10-2 = 8, clamped to 5 (10-5) |
| 92 | + const start = calculateVisibleStart(10, 10, 5); |
| 93 | + assert.equal(start, 5); |
| 94 | +}); |
| 95 | + |
| 96 | +test("calculateVisibleStart with maxVisible of 1", () => { |
| 97 | + // 5 items, max 1 visible, active index 2 |
| 98 | + // floor((1-1)/2) = 0, so 2-0 = 2, clamped to 4 (5-1) |
| 99 | + const start = calculateVisibleStart(2, 5, 1); |
| 100 | + assert.equal(start, 2); |
| 101 | +}); |
| 102 | + |
| 103 | +test("calculateVisibleStart with maxVisible of 1 at end", () => { |
| 104 | + // 5 items, max 1 visible, active index 4 (last) |
| 105 | + // floor((1-1)/2) = 0, so 4-0 = 4, clamped to 4 (5-1) |
| 106 | + const start = calculateVisibleStart(4, 5, 1); |
| 107 | + assert.equal(start, 4); |
| 108 | +}); |
| 109 | + |
| 110 | +test("calculateVisibleStart scrolling behavior - moving down", () => { |
| 111 | + // Simulate scrolling through a list |
| 112 | + // 10 items, max 5 visible |
| 113 | + |
| 114 | + // Start at index 0 |
| 115 | + assert.equal(calculateVisibleStart(0, 10, 5), 0); |
| 116 | + |
| 117 | + // Move to index 2 (still centered) |
| 118 | + assert.equal(calculateVisibleStart(2, 10, 5), 0); |
| 119 | + |
| 120 | + // Move to index 5 (window should scroll) |
| 121 | + assert.equal(calculateVisibleStart(5, 10, 5), 3); |
| 122 | + |
| 123 | + // Move to index 8 (near end) |
| 124 | + assert.equal(calculateVisibleStart(8, 10, 5), 5); |
| 125 | + |
| 126 | + // Move to index 9 (at end) |
| 127 | + assert.equal(calculateVisibleStart(9, 10, 5), 5); |
| 128 | +}); |
| 129 | + |
| 130 | +test("calculateVisibleStart scrolling behavior - moving up", () => { |
| 131 | + // Simulate scrolling up through a list |
| 132 | + // 10 items, max 5 visible |
| 133 | + |
| 134 | + // Start at index 9 (end) |
| 135 | + assert.equal(calculateVisibleStart(9, 10, 5), 5); |
| 136 | + |
| 137 | + // Move to index 6 |
| 138 | + assert.equal(calculateVisibleStart(6, 10, 5), 4); |
| 139 | + |
| 140 | + // Move to index 4 (window should scroll up) |
| 141 | + assert.equal(calculateVisibleStart(4, 10, 5), 2); |
| 142 | + |
| 143 | + // Move to index 1 (near start) |
| 144 | + assert.equal(calculateVisibleStart(1, 10, 5), 0); |
| 145 | + |
| 146 | + // Move to index 0 (at start) |
| 147 | + assert.equal(calculateVisibleStart(0, 10, 5), 0); |
| 148 | +}); |
0 commit comments