-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordering.test.ts
61 lines (52 loc) · 1.67 KB
/
ordering.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 {byNumber, byString} from './comparators';
import {ordering} from './ordering';
describe('Ordering', () => {
describe('reversed', () => {
it('caches the original comparator in case of double reversal', () => {
const numberOrdering = ordering(byNumber);
const doubleReversed = numberOrdering.reversed().reversed();
expect(doubleReversed.compare).toBe(numberOrdering.compare);
});
});
describe('join', () => {
it('returns this if given nothing to join', () => {
const numberOrdering = ordering(byNumber);
const result = numberOrdering.join();
expect(result).toBe(numberOrdering);
});
});
});
describe('ordering', () => {
it('forwards to the latest underlying comparator', () => {
const oldUnderlying = jest.fn(byNumber);
const newUnderlying = jest.fn(byNumber);
const orderingByNumber = ordering(oldUnderlying);
orderingByNumber.compare = newUnderlying;
[2, 1].sort(orderingByNumber);
expect(oldUnderlying).not.toHaveBeenCalled();
expect(newUnderlying).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
2,
1,
],
],
"results": Array [
Object {
"type": "return",
"value": 1,
},
],
}
`);
});
it('updates its own name when the underlying comparator changes', () => {
const oldUnderlying = byNumber;
const newUnderlying = byString;
const o = ordering<any>(oldUnderlying);
expect(o.name).toMatchInlineSnapshot(`"ordering(byNumber)"`);
o.compare = newUnderlying;
expect(o.name).toMatchInlineSnapshot(`"ordering(byString)"`);
});
});