Skip to content

Commit 2948e61

Browse files
committed
test(utils): add unit tests for chunkItems
Add 8 test cases covering fixed-size array splitting, empty input, size <= 0, size=1, size > length, exact division, readonly input preservation, and fractional size behavior.
1 parent 6cb82ea commit 2948e61

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

src/utils/chunk-items.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)