|
| 1 | +// Chunk items tests cover fixed-size array splitting edge cases. |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { chunkItems } from "./chunk-items.js"; |
| 4 | + |
| 5 | +describe("chunkItems", () => { |
| 6 | + it("splits items into fixed-size chunks", () => { |
| 7 | + expect(chunkItems([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); |
| 8 | + expect(chunkItems(["a", "b", "c", "d"], 3)).toEqual([["a", "b", "c"], ["d"]]); |
| 9 | + }); |
| 10 | + |
| 11 | + it("returns a single chunk when size is 0 or negative", () => { |
| 12 | + expect(chunkItems([1, 2, 3], 0)).toEqual([[1, 2, 3]]); |
| 13 | + expect(chunkItems([1, 2, 3], -1)).toEqual([[1, 2, 3]]); |
| 14 | + }); |
| 15 | + |
| 16 | + it("returns empty array for empty input", () => { |
| 17 | + expect(chunkItems([], 5)).toEqual([]); |
| 18 | + expect(chunkItems([], 0)).toEqual([[]]); |
| 19 | + expect(chunkItems([], -1)).toEqual([[]]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("wraps each item when size is 1", () => { |
| 23 | + expect(chunkItems([1, 2, 3], 1)).toEqual([[1], [2], [3]]); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns a single chunk when size exceeds array length", () => { |
| 27 | + expect(chunkItems([1, 2, 3], 10)).toEqual([[1, 2, 3]]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("exactly divides when size matches array length", () => { |
| 31 | + expect(chunkItems([1, 2, 3], 3)).toEqual([[1, 2, 3]]); |
| 32 | + }); |
| 33 | + |
| 34 | + it("preserves readonly input without mutation", () => { |
| 35 | + const input: readonly number[] = [10, 20, 30, 40]; |
| 36 | + const result = chunkItems(input, 2); |
| 37 | + expect(result).toEqual([ |
| 38 | + [10, 20], |
| 39 | + [30, 40], |
| 40 | + ]); |
| 41 | + expect(input).toEqual([10, 20, 30, 40]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("handles fractional size using slice native truncation behavior", () => { |
| 45 | + // slice() truncates non-integer args, so 2.5 → chunk boundaries at 0, 2.5, 5.0 |
| 46 | + expect(chunkItems([1, 2, 3, 4, 5], 2.5)).toEqual([ |
| 47 | + [1, 2], |
| 48 | + [3, 4, 5], |
| 49 | + ]); |
| 50 | + }); |
| 51 | +}); |
0 commit comments