|
| 1 | +/** |
| 2 | + * @fileoverview Unit tests for array utility functions. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | +import { |
| 7 | + arrayChunk, |
| 8 | + arrayUnique, |
| 9 | + isArray, |
| 10 | + joinAnd, |
| 11 | + joinOr, |
| 12 | +} from '../../../src/lib/arrays' |
| 13 | + |
| 14 | +describe('arrays', () => { |
| 15 | + describe('arrayChunk', () => { |
| 16 | + it('should split array into chunks of specified size', () => { |
| 17 | + const arr = [1, 2, 3, 4, 5, 6] |
| 18 | + const result = arrayChunk(arr, 2) |
| 19 | + expect(result).toEqual([[1, 2], [3, 4], [5, 6]]) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should handle uneven chunks', () => { |
| 23 | + const arr = [1, 2, 3, 4, 5] |
| 24 | + const result = arrayChunk(arr, 2) |
| 25 | + expect(result).toEqual([[1, 2], [3, 4], [5]]) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should default to chunk size of 2', () => { |
| 29 | + const arr = [1, 2, 3, 4] |
| 30 | + const result = arrayChunk(arr) |
| 31 | + expect(result).toEqual([[1, 2], [3, 4]]) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should handle single element arrays', () => { |
| 35 | + const arr = [1] |
| 36 | + const result = arrayChunk(arr, 3) |
| 37 | + expect(result).toEqual([[1]]) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should handle empty arrays', () => { |
| 41 | + const arr: number[] = [] |
| 42 | + const result = arrayChunk(arr, 2) |
| 43 | + expect(result).toEqual([]) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should throw error for chunk size <= 0', () => { |
| 47 | + const arr = [1, 2, 3] |
| 48 | + expect(() => arrayChunk(arr, 0)).toThrow('Chunk size must be greater than 0') |
| 49 | + expect(() => arrayChunk(arr, -1)).toThrow('Chunk size must be greater than 0') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should handle chunk size larger than array', () => { |
| 53 | + const arr = [1, 2, 3] |
| 54 | + const result = arrayChunk(arr, 10) |
| 55 | + expect(result).toEqual([[1, 2, 3]]) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should work with readonly arrays', () => { |
| 59 | + const arr: readonly number[] = [1, 2, 3, 4] |
| 60 | + const result = arrayChunk(arr, 2) |
| 61 | + expect(result).toEqual([[1, 2], [3, 4]]) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('arrayUnique', () => { |
| 66 | + it('should remove duplicate primitive values', () => { |
| 67 | + const arr = [1, 2, 2, 3, 3, 3, 4] |
| 68 | + const result = arrayUnique(arr) |
| 69 | + expect(result).toEqual([1, 2, 3, 4]) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should remove duplicate strings', () => { |
| 73 | + const arr = ['a', 'b', 'b', 'c', 'a'] |
| 74 | + const result = arrayUnique(arr) |
| 75 | + expect(result).toEqual(['a', 'b', 'c']) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should handle empty arrays', () => { |
| 79 | + const arr: number[] = [] |
| 80 | + const result = arrayUnique(arr) |
| 81 | + expect(result).toEqual([]) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should handle arrays with no duplicates', () => { |
| 85 | + const arr = [1, 2, 3, 4] |
| 86 | + const result = arrayUnique(arr) |
| 87 | + expect(result).toEqual([1, 2, 3, 4]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should work with readonly arrays', () => { |
| 91 | + const arr: readonly string[] = ['x', 'y', 'x', 'z'] |
| 92 | + const result = arrayUnique(arr) |
| 93 | + expect(result).toEqual(['x', 'y', 'z']) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should handle mixed types', () => { |
| 97 | + const arr = [1, '1', 2, '2', 1, '1'] |
| 98 | + const result = arrayUnique(arr) |
| 99 | + expect(result).toEqual([1, '1', 2, '2']) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('isArray', () => { |
| 104 | + it('should return true for arrays', () => { |
| 105 | + expect(isArray([])).toBe(true) |
| 106 | + expect(isArray([1, 2, 3])).toBe(true) |
| 107 | + expect(isArray(new Array(5))).toBe(true) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should return false for non-arrays', () => { |
| 111 | + expect(isArray(null)).toBe(false) |
| 112 | + expect(isArray(undefined)).toBe(false) |
| 113 | + expect(isArray({})).toBe(false) |
| 114 | + expect(isArray('array')).toBe(false) |
| 115 | + expect(isArray(123)).toBe(false) |
| 116 | + expect(isArray({ length: 0 })).toBe(false) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should return true for array-like typed arrays', () => { |
| 120 | + expect(isArray(new Uint8Array(0))).toBe(false) |
| 121 | + expect(isArray(new Int32Array(0))).toBe(false) |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + describe('joinAnd', () => { |
| 126 | + it('should join two items with "and"', () => { |
| 127 | + const result = joinAnd(['apple', 'banana']) |
| 128 | + expect(result).toBe('apple and banana') |
| 129 | + }) |
| 130 | + |
| 131 | + it('should join three items with commas and "and"', () => { |
| 132 | + const result = joinAnd(['apple', 'banana', 'cherry']) |
| 133 | + expect(result).toBe('apple, banana, and cherry') |
| 134 | + }) |
| 135 | + |
| 136 | + it('should handle single item', () => { |
| 137 | + const result = joinAnd(['apple']) |
| 138 | + expect(result).toBe('apple') |
| 139 | + }) |
| 140 | + |
| 141 | + it('should handle empty array', () => { |
| 142 | + const result = joinAnd([]) |
| 143 | + expect(result).toBe('') |
| 144 | + }) |
| 145 | + |
| 146 | + it('should work with readonly arrays', () => { |
| 147 | + const arr: readonly string[] = ['red', 'green', 'blue'] |
| 148 | + const result = joinAnd(arr) |
| 149 | + expect(result).toBe('red, green, and blue') |
| 150 | + }) |
| 151 | + |
| 152 | + it('should handle many items', () => { |
| 153 | + const result = joinAnd(['one', 'two', 'three', 'four', 'five']) |
| 154 | + expect(result).toBe('one, two, three, four, and five') |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + describe('joinOr', () => { |
| 159 | + it('should join two items with "or"', () => { |
| 160 | + const result = joinOr(['apple', 'banana']) |
| 161 | + expect(result).toBe('apple or banana') |
| 162 | + }) |
| 163 | + |
| 164 | + it('should join three items with commas and "or"', () => { |
| 165 | + const result = joinOr(['apple', 'banana', 'cherry']) |
| 166 | + expect(result).toBe('apple, banana, or cherry') |
| 167 | + }) |
| 168 | + |
| 169 | + it('should handle single item', () => { |
| 170 | + const result = joinOr(['apple']) |
| 171 | + expect(result).toBe('apple') |
| 172 | + }) |
| 173 | + |
| 174 | + it('should handle empty array', () => { |
| 175 | + const result = joinOr([]) |
| 176 | + expect(result).toBe('') |
| 177 | + }) |
| 178 | + |
| 179 | + it('should work with readonly arrays', () => { |
| 180 | + const arr: readonly string[] = ['red', 'green', 'blue'] |
| 181 | + const result = joinOr(arr) |
| 182 | + expect(result).toBe('red, green, or blue') |
| 183 | + }) |
| 184 | + |
| 185 | + it('should handle many items', () => { |
| 186 | + const result = joinOr(['one', 'two', 'three', 'four', 'five']) |
| 187 | + expect(result).toBe('one, two, three, four, or five') |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
0 commit comments