|
| 1 | +import { groupBy, uniq, sortBy, sumBy } from "./collections" |
| 2 | + |
| 3 | +describe("collections", () => { |
| 4 | + describe("groupBy", () => { |
| 5 | + type TestCase<T> = { |
| 6 | + description: string |
| 7 | + got: T[] |
| 8 | + iteratee: (item: T) => string |
| 9 | + want: Record<string, T[]> |
| 10 | + } |
| 11 | + const tests: TestCase<{ id: number }>[] = [ |
| 12 | + { |
| 13 | + description: "groups items by id", |
| 14 | + got: [{ id: 1 }, { id: 2 }, { id: 1 }], |
| 15 | + iteratee: (item) => item.id.toString(), |
| 16 | + want: { "1": [{ id: 1 }, { id: 1 }], "2": [{ id: 2 }] }, |
| 17 | + }, |
| 18 | + { |
| 19 | + description: "groups items by id with duplicates", |
| 20 | + got: [{ id: 1 }, { id: 2 }, { id: 1 }], |
| 21 | + iteratee: (item) => item.id.toString(), |
| 22 | + want: { "1": [{ id: 1 }, { id: 1 }], "2": [{ id: 2 }] }, |
| 23 | + }, |
| 24 | + { |
| 25 | + description: "handles empty array", |
| 26 | + got: [], |
| 27 | + iteratee: (item) => item.id.toString(), |
| 28 | + want: {}, |
| 29 | + }, |
| 30 | + { |
| 31 | + description: "handles single item", |
| 32 | + got: [{ id: 1 }], |
| 33 | + iteratee: (item) => item.id.toString(), |
| 34 | + want: { "1": [{ id: 1 }] }, |
| 35 | + }, |
| 36 | + { |
| 37 | + description: "handles multiple unique items", |
| 38 | + got: [{ id: 1 }, { id: 2 }, { id: 3 }], |
| 39 | + iteratee: (item) => item.id.toString(), |
| 40 | + want: { "1": [{ id: 1 }], "2": [{ id: 2 }], "3": [{ id: 3 }] }, |
| 41 | + }, |
| 42 | + ] |
| 43 | + |
| 44 | + test.each(tests)( |
| 45 | + "$description", |
| 46 | + ({ got, iteratee, want }: TestCase<{ id: number }>) => { |
| 47 | + expect(groupBy(got, iteratee)).toEqual(want) |
| 48 | + }, |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + describe("uniq", () => { |
| 53 | + type TestCase<T> = { |
| 54 | + description: string |
| 55 | + got: T[] |
| 56 | + want: T[] |
| 57 | + } |
| 58 | + const tests: TestCase<number>[] = [ |
| 59 | + { |
| 60 | + description: "removes duplicates from array", |
| 61 | + got: [1, 2, 2, 3, 4, 4, 5], |
| 62 | + want: [1, 2, 3, 4, 5], |
| 63 | + }, |
| 64 | + { |
| 65 | + description: "removes duplicates from array with duplicates", |
| 66 | + got: [1, 2, 2, 3, 4, 4, 5], |
| 67 | + want: [1, 2, 3, 4, 5], |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "handles empty array", |
| 71 | + got: [], |
| 72 | + want: [], |
| 73 | + }, |
| 74 | + { |
| 75 | + description: "handles array with all same elements", |
| 76 | + got: [1, 1, 1, 1], |
| 77 | + want: [1], |
| 78 | + }, |
| 79 | + { |
| 80 | + description: "handles array with all unique elements", |
| 81 | + got: [1, 2, 3, 4, 5], |
| 82 | + want: [1, 2, 3, 4, 5], |
| 83 | + }, |
| 84 | + ] |
| 85 | + |
| 86 | + test.each(tests)("$description", ({ got, want }: TestCase<number>) => { |
| 87 | + expect(uniq(got)).toEqual(want) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + describe("sortBy", () => { |
| 92 | + type TestCase<T> = { |
| 93 | + description: string |
| 94 | + got: T[] |
| 95 | + getKey: (item: T) => string |
| 96 | + want: T[] |
| 97 | + } |
| 98 | + const tests: TestCase<{ name: string }>[] = [ |
| 99 | + { |
| 100 | + description: "sorts items by name", |
| 101 | + got: [{ name: "b" }, { name: "a" }, { name: "c" }], |
| 102 | + getKey: (item) => item.name, |
| 103 | + want: [{ name: "a" }, { name: "b" }, { name: "c" }], |
| 104 | + }, |
| 105 | + { |
| 106 | + description: "sorts items by numeric string", |
| 107 | + got: [{ name: "30" }, { name: "20" }, { name: "40" }], |
| 108 | + getKey: (item) => item.name, |
| 109 | + want: [{ name: "20" }, { name: "30" }, { name: "40" }], |
| 110 | + }, |
| 111 | + { |
| 112 | + description: "handles empty array", |
| 113 | + got: [], |
| 114 | + getKey: (item) => item.name, |
| 115 | + want: [], |
| 116 | + }, |
| 117 | + { |
| 118 | + description: "sorts items by numeric string with two elements", |
| 119 | + got: [{ name: "2" }, { name: "1" }], |
| 120 | + getKey: (item) => item.name, |
| 121 | + want: [{ name: "1" }, { name: "2" }], |
| 122 | + }, |
| 123 | + { |
| 124 | + description: "sorts items by name with multiple elements", |
| 125 | + got: [{ name: "z" }, { name: "x" }, { name: "y" }], |
| 126 | + getKey: (item) => item.name, |
| 127 | + want: [{ name: "x" }, { name: "y" }, { name: "z" }], |
| 128 | + }, |
| 129 | + ] |
| 130 | + |
| 131 | + test.each(tests)( |
| 132 | + "$description", |
| 133 | + ({ got, getKey, want }: TestCase<{ name: string }>) => { |
| 134 | + expect(sortBy(got, getKey)).toEqual(want) |
| 135 | + }, |
| 136 | + ) |
| 137 | + }) |
| 138 | + |
| 139 | + describe("sumBy", () => { |
| 140 | + type TestCase<T> = { |
| 141 | + description: string |
| 142 | + got: T[] |
| 143 | + iteratee: (item: T) => number |
| 144 | + want: number |
| 145 | + } |
| 146 | + const cases: TestCase<{ value: number }>[] = [ |
| 147 | + { |
| 148 | + description: "sums values in array", |
| 149 | + got: [{ value: 1 }, { value: 2 }, { value: 3 }], |
| 150 | + iteratee: (item) => item.value, |
| 151 | + want: 6, |
| 152 | + }, |
| 153 | + { |
| 154 | + description: "sums values in array with two elements", |
| 155 | + got: [{ value: 10 }, { value: 20 }], |
| 156 | + iteratee: (item) => item.value, |
| 157 | + want: 30, |
| 158 | + }, |
| 159 | + { |
| 160 | + description: "handles empty array", |
| 161 | + got: [], |
| 162 | + iteratee: (item) => item.value, |
| 163 | + want: 0, |
| 164 | + }, |
| 165 | + { |
| 166 | + description: "sums values in array with negative and positive elements", |
| 167 | + got: [{ value: -1 }, { value: 1 }], |
| 168 | + iteratee: (item) => item.value, |
| 169 | + want: 0, |
| 170 | + }, |
| 171 | + { |
| 172 | + description: "sums values in array with same elements", |
| 173 | + got: [{ value: 5 }, { value: 5 }, { value: 5 }], |
| 174 | + iteratee: (item) => item.value, |
| 175 | + want: 15, |
| 176 | + }, |
| 177 | + ] |
| 178 | + |
| 179 | + test.each(cases)( |
| 180 | + "$description", |
| 181 | + ({ got, iteratee, want }: TestCase<{ value: number }>) => { |
| 182 | + expect(sumBy(got, iteratee)).toEqual(want) |
| 183 | + }, |
| 184 | + ) |
| 185 | + }) |
| 186 | +}) |
0 commit comments