-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ts
94 lines (83 loc) · 2.66 KB
/
test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import multiColumnSort from './index'
import unsortedRaw from './data/unsorted.json'
import firstNameASCSortedRaw from './data/firstNameASCSorted.json'
import firstNameDESCSortedRaw from './data/firstNameDESCSorted.json'
import firstNameASCBalanceDESCSortedRaw from './data/firstNameASCBalanceDESCSorted.json'
import firstNameDESCBalanceASCSortedRaw from './data/firstNameDESCBalanceASCSorted.json'
interface Data {
firstName: string
lastName: string
balance: string
}
const unsorted: Data[] = unsortedRaw
const firstNameASCSorted: Data[] = firstNameASCSortedRaw
const firstNameDESCSorted: Data[] = firstNameDESCSortedRaw
const firstNameASCBalanceDESCSorted: Data[] = firstNameASCBalanceDESCSortedRaw
const firstNameDESCBalanceASCSorted: Data[] = firstNameDESCBalanceASCSortedRaw
describe('multiColumnSort', () => {
const mapValues = (arr: Data[]) => arr.map(({ firstName }) => ({ firstName }))
it('sorts by one column ASC', () => {
expect(
mapValues(multiColumnSort(unsorted, [['firstName', 'ASC']]))
).toEqual(mapValues(firstNameASCSorted))
})
it('sorts by one column DESC', () => {
expect(
mapValues(multiColumnSort(unsorted, [['firstName', 'DESC']]))
).toEqual(mapValues(firstNameDESCSorted))
})
test('object signature', () => {
expect(mapValues(multiColumnSort(unsorted, { firstName: 'ASC' }))).toEqual(
mapValues(firstNameASCSorted)
)
})
describe('with getColumnValue', () => {
const mapValues = (arr: Data[]) =>
arr.map(({ firstName, balance }) => ({ firstName, balance }))
const getColumnValue = (column: keyof Data, value: Data[keyof Data]) => {
switch (column) {
case 'balance':
return parseFloat(value.replace(/\,|\$/g, ''))
default:
return value
}
}
it('sorts by multiple columns', () => {
expect(
mapValues(
multiColumnSort(
unsorted,
[
['firstName', 'DESC'],
['balance', 'ASC']
],
getColumnValue
)
)
).toEqual(mapValues(firstNameDESCBalanceASCSorted))
expect(
mapValues(
multiColumnSort(
unsorted,
[
['firstName', 'ASC'],
['balance', 'DESC']
],
getColumnValue
)
)
).toEqual(mapValues(firstNameASCBalanceDESCSorted))
})
test('object signature', () => {
expect(
mapValues(
multiColumnSort(
unsorted,
{ firstName: 'DESC', balance: 'ASC' },
getColumnValue
)
)
).toEqual(mapValues(firstNameDESCBalanceASCSorted))
})
})
})