-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoDictionary.tests.ts
72 lines (53 loc) · 1.82 KB
/
toDictionary.tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { toDictionary } from '../src/dictionaries'
describe('Convert array to js object', () => {
const testData = [
{ key: 'alfa', values: [1, 2, 3], someNumber: 10, someString: 'aaa' },
{ key: 'beta', values: [4], someNumber: 20, someString: 'bbb' },
{ key: 'gamma', values: [5, 6], someNumber: 30, someString: 'ccc' }
]
test('default params', () => {
const res = testData.reduce(toDictionary(), {})
expect(res).toEqual({
alfa: [1, 2, 3],
beta: [4],
gamma: [5, 6]
})
})
test('custom keySelector', () => {
const res = testData.reduce(toDictionary(x => x.someNumber), {})
expect(res).toEqual({
10: [1, 2, 3],
20: [4],
30: [5, 6]
})
})
test('custom valueSelector', () => {
const res = testData.reduce(toDictionary(undefined, x => x.someNumber), {})
expect(res).toEqual({
alfa: 10,
beta: 20,
gamma: 30
})
})
test('custom keySelector and valueSelector', () => {
const res = testData.reduce(toDictionary(x => x.someString, x => x.key), {})
expect(res).toEqual({
aaa: 'alfa',
bbb: 'beta',
ccc: 'gamma'
})
})
test('keySelector that returns neither string or number is not allowed', () => {
expect(() => {
testData.reduce(toDictionary(x => x.values), {})
}).toThrow()
})
test('complex valueSelector', () => {
const res = testData.reduce(toDictionary(undefined, x => ({ num: x.someNumber, text: x.someString })), {})
expect(res).toEqual({
alfa: { num: 10, text: 'aaa' },
beta: { num: 20, text: 'bbb' },
gamma: { num: 30, text: 'ccc' }
})
})
})