|
| 1 | +import { expectType } from "ts-expect"; |
1 | 2 | import groupBy from '../groupBy'; |
| 3 | +import { ArrValues, ObjValues } from '../../typings/types'; |
2 | 4 |
|
3 | 5 | describe('utils/object/groupBy', () => { |
4 | 6 | it('group object values by function', () => { |
5 | 7 | const obj = { a: { b: 1, c: 2 }, b: { b: 3 }, c: { b: 1, d: 5 } }; |
6 | | - const fn = jest.fn((x) => x.b); |
7 | 8 |
|
8 | | - expect(groupBy(fn, obj)).toEqual({ |
| 9 | + expect(groupBy((x) => x.b, obj)).toEqual({ |
9 | 10 | 1: [{ b: 1, c: 2 }, { b: 1, d: 5 }], |
10 | 11 | 3: [{ b: 3 }], |
11 | 12 | }); |
12 | | - expect(fn).toBeCalledWith({ b: 1, c: 2 }, 'a', obj); |
13 | | - expect(fn).toBeCalledWith({ b: 3 }, 'b', obj); |
14 | | - expect(fn).toBeCalledWith({ b: 1, d: 5 }, 'c', obj); |
| 13 | + }); |
| 14 | + |
| 15 | + it('group array values by function', () => { |
| 16 | + const arr = [{ b: 1, c: 2 }, { b: 3 }, { b: 1, d: 5 }]; |
| 17 | + |
| 18 | + expect(groupBy((x) => x.b, arr)).toEqual({ |
| 19 | + 1: [{ b: 1, c: 2 }, { b: 1, d: 5 }], |
| 20 | + 3: [{ b: 3 }], |
| 21 | + }) |
| 22 | + }); |
| 23 | + |
| 24 | + it('group object values by function through currying', () => { |
| 25 | + const obj = { a: { b: 1, c: 2 }, b: { b: 3 }, c: { b: 1, d: 5 } }; |
| 26 | + |
| 27 | + expect(groupBy((x: ObjValues<typeof obj>) => x.b)(obj)).toEqual({ |
| 28 | + 1: [{ b: 1, c: 2 }, { b: 1, d: 5 }], |
| 29 | + 3: [{ b: 3 }], |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('group array values by function through currying', () => { |
| 34 | + const arr = [{ b: 1, c: 2 }, { b: 3 }, { b: 1, d: 5 }]; |
| 35 | + |
| 36 | + expect(groupBy((x: ArrValues<typeof arr>) => x.b)(arr)).toEqual({ |
| 37 | + 1: [{ b: 1, c: 2 }, { b: 1, d: 5 }], |
| 38 | + 3: [{ b: 3 }], |
| 39 | + }) |
| 40 | + }); |
| 41 | + |
| 42 | + it('check grouped object type', () => { |
| 43 | + const obj = { a: { b: 1, c: 2 }, b: { b: 3 }, c: { b: 1, d: 5 } }; |
| 44 | + const result = groupBy((x) => x.b, obj); |
| 45 | + const curriedResult = groupBy((x) => x.b)(obj); |
| 46 | + const typedCurriedResult = groupBy((x: ObjValues<typeof obj>) => x.b)(obj); |
| 47 | + |
| 48 | + type ValuesType = { b: number, c: number } | { b: number } | { b: number, d: number }; |
| 49 | + |
| 50 | + type ExpectedType = Record<number, Array<ValuesType>>; |
| 51 | + type CurriedExpectedType = Record<any, Array<ValuesType>>; |
| 52 | + type TypedCurriedExpectedType = Record<number, Array<ValuesType>>; |
| 53 | + |
| 54 | + expectType<ExpectedType>(result); |
| 55 | + expectType<CurriedExpectedType>(curriedResult); |
| 56 | + expectType<TypedCurriedExpectedType>(typedCurriedResult); |
| 57 | + }); |
| 58 | + |
| 59 | + it('check grouped array type', () => { |
| 60 | + const arr = [{ b: 1, c: 2 }, { b: 3 }, { b: 1, d: 5 }]; |
| 61 | + const result = groupBy((x) => x.b, arr); |
| 62 | + const curriedResult = groupBy((x) => x.b)(arr); |
| 63 | + const typedCurriedResult = groupBy((x: ArrValues<typeof arr>) => x.b)(arr); |
| 64 | + |
| 65 | + type ValuesType = { b: number, c: number } | { b: number } | { b: number, d: number }; |
| 66 | + |
| 67 | + type ExpectedType = Record<number, Array<ValuesType>>; |
| 68 | + type CurriedExpectedType = Record<any, Array<ValuesType>>; |
| 69 | + type TypedCurriedExpectedType = Record<number, Array<ValuesType>>; |
| 70 | + |
| 71 | + expectType<ExpectedType>(result); |
| 72 | + expectType<CurriedExpectedType>(curriedResult); |
| 73 | + expectType<TypedCurriedExpectedType>(typedCurriedResult); |
| 74 | + }); |
| 75 | + |
| 76 | + it('check grouped const object type', () => { |
| 77 | + const obj = { a: { b: 1, c: 2 }, b: { b: 3 }, c: { b: 1, d: 5 } } as const; |
| 78 | + const result = groupBy((x) => x.b, obj); |
| 79 | + |
| 80 | + type ValuesType = { b: number, c: number } | { b: number } | { b: number, d: number }; |
| 81 | + |
| 82 | + type ExpectedType = { |
| 83 | + [1]: Array<ValuesType>; |
| 84 | + [3]: Array<ValuesType>; |
| 85 | + }; |
| 86 | + |
| 87 | + expectType<ExpectedType>(result); |
| 88 | + }); |
| 89 | + |
| 90 | + it('check grouped const array type', () => { |
| 91 | + const arr = [{ b: 1, c: 2 }, { b: 3 }, { b: 1, d: 5 }] as const; |
| 92 | + const result = groupBy((x) => x.b, arr); |
| 93 | + |
| 94 | + type ValuesType = { b: number, c: number } | { b: number } | { b: number, d: number }; |
| 95 | + |
| 96 | + type ExpectedType = { |
| 97 | + [1]: ReadonlyArray<ValuesType>; |
| 98 | + [3]: ReadonlyArray<ValuesType>; |
| 99 | + }; |
| 100 | + |
| 101 | + expectType<ExpectedType>(result); |
15 | 102 | }); |
16 | 103 | }); |
0 commit comments