-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathsort.test.ts
61 lines (53 loc) · 2.67 KB
/
sort.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
import { sortBy } from '@shell/utils/sort';
describe('fx: sort', () => {
describe('sortBy', () => {
const testSortBy = <T = object[]>(ary: T[], key: string[], expected: T[], desc?: boolean) => {
const result = sortBy(ary, key, desc);
expect(result).toStrictEqual(expected);
};
it.each([
[[{ a: 1 }, { a: 9 }], ['a'], [{ a: 1 }, { a: 9 }]],
[[{ a: 2 }, { a: 1 }], ['a'], [{ a: 1 }, { a: 2 }]],
])('should sort by single property', (ary, key, expected) => {
testSortBy(ary, key, expected);
});
it.each([
[[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 9, b: 9 }]],
[[{ a: 2, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 2, b: 2 }]],
[[{ a: 2, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 9 }, { a: 2, b: 1 }]],
[[{ a: 1, b: 2 }, { a: 9, b: 1 }], ['a', 'b'], [{ a: 1, b: 2 }, { a: 9, b: 1 }]],
[[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 9, b: 9 }]],
])('should sort by two properties (primary property always first)', (ary, key, expected) => {
testSortBy(ary, key, expected);
});
it.each([
[[{ a: 1, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 1, b: 9 }]],
[[{ a: 1, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 1 }, { a: 1, b: 2 }]],
])('should sort by two properties (primary property the same)', (ary, key, expected) => {
testSortBy(ary, key, expected);
});
describe('descending', () => {
it.each([
[[{ a: 1 }, { a: 9 }], ['a'], [{ a: 9 }, { a: 1 }]],
[[{ a: 2 }, { a: 1 }], ['a'], [{ a: 2 }, { a: 1 }]],
])('should sort by single property', (ary, key, expected) => {
testSortBy(ary, key, expected, true);
});
it.each([
[[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 9, b: 9 }, { a: 1, b: 1 }]],
[[{ a: 2, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 2, b: 2 }, { a: 1, b: 1 }]],
[[{ a: 2, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 2, b: 1 }, { a: 1, b: 9 }]],
[[{ a: 1, b: 2 }, { a: 9, b: 1 }], ['a', 'b'], [{ a: 9, b: 1 }, { a: 1, b: 2 }]],
[[{ a: 1, b: 1 }, { a: 9, b: 9 }], ['a', 'b'], [{ a: 9, b: 9 }, { a: 1, b: 1 }]],
])('should sort by two properties', (ary, key, expected) => {
testSortBy(ary, key, expected, true);
});
it.each([
[[{ a: 1, b: 1 }, { a: 1, b: 9 }], ['a', 'b'], [{ a: 1, b: 9 }, { a: 1, b: 1 }]],
[[{ a: 1, b: 2 }, { a: 1, b: 1 }], ['a', 'b'], [{ a: 1, b: 2 }, { a: 1, b: 1 }]],
])('should sort by two properties (primary property the same)', (ary, key, expected) => {
testSortBy(ary, key, expected, true);
});
});
});
});