|
| 1 | +import { chunk } from '../Chunk.js' |
| 2 | + |
| 3 | +describe('chunk', () => { |
| 4 | + test('chunk([1,2,3,4,5], 2) returns [[1,2],[3,4],[5]]', () => { |
| 5 | + expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]) |
| 6 | + }) |
| 7 | + |
| 8 | + test('chunk([1,2,3], 3) returns [[1,2,3]]', () => { |
| 9 | + expect(chunk([1, 2, 3], 3)).toEqual([[1, 2, 3]]) |
| 10 | + }) |
| 11 | + |
| 12 | + test('chunk([1,2,3,4], 2) returns [[1,2],[3,4]]', () => { |
| 13 | + expect(chunk([1, 2, 3, 4], 2)).toEqual([ |
| 14 | + [1, 2], |
| 15 | + [3, 4] |
| 16 | + ]) |
| 17 | + }) |
| 18 | + |
| 19 | + test('chunk with size 1 returns array of single elements', () => { |
| 20 | + expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]]) |
| 21 | + }) |
| 22 | + |
| 23 | + test('chunk empty array returns empty array', () => { |
| 24 | + expect(chunk([], 2)).toEqual([]) |
| 25 | + }) |
| 26 | + |
| 27 | + test('chunk with size larger than array returns one chunk', () => { |
| 28 | + expect(chunk([1, 2, 3], 10)).toEqual([[1, 2, 3]]) |
| 29 | + }) |
| 30 | + |
| 31 | + test('chunk does not mutate original array', () => { |
| 32 | + const arr = [1, 2, 3, 4] |
| 33 | + chunk(arr, 2) |
| 34 | + expect(arr).toEqual([1, 2, 3, 4]) |
| 35 | + }) |
| 36 | + |
| 37 | + test('chunk works with strings', () => { |
| 38 | + expect(chunk(['a', 'b', 'c', 'd'], 2)).toEqual([ |
| 39 | + ['a', 'b'], |
| 40 | + ['c', 'd'] |
| 41 | + ]) |
| 42 | + }) |
| 43 | + |
| 44 | + test('throws TypeError for non-array input', () => { |
| 45 | + expect(() => chunk('123', 2)).toThrow(TypeError) |
| 46 | + }) |
| 47 | + |
| 48 | + test('throws TypeError for size 0', () => { |
| 49 | + expect(() => chunk([1, 2, 3], 0)).toThrow(TypeError) |
| 50 | + }) |
| 51 | + |
| 52 | + test('throws TypeError for negative size', () => { |
| 53 | + expect(() => chunk([1, 2, 3], -1)).toThrow(TypeError) |
| 54 | + }) |
| 55 | + |
| 56 | + test('throws TypeError for float size', () => { |
| 57 | + expect(() => chunk([1, 2, 3], 1.5)).toThrow(TypeError) |
| 58 | + }) |
| 59 | +}) |
0 commit comments