-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.spec.ts
88 lines (74 loc) · 2.19 KB
/
index.spec.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
import '.';
describe('Array.prototype.uniqueBy()', () => {
it('should return new Array', () => {
const origin = [];
expect(origin.uniqueBy()).not.toBe(origin);
});
it('should compare items using "!==" while 0 parameter passed in', () => {
expect([1, 2, 3, 3, 2, 1].uniqueBy()).toEqual([1, 2, 3]);
});
const symbol = Symbol.for('test');
const origin = [
{
id: 1,
uid: 10000
},
{
id: 2,
uid: 10000,
[symbol]: 'example'
},
{
id: 3,
uid: 10001,
[symbol]: 'example'
}
];
it('should compare items using "!==" by a String or Symbol key', () => {
expect(origin.uniqueBy('uid')).toEqual([
{
id: 1,
uid: 10000
},
{
id: 3,
uid: 10001,
[symbol]: 'example'
}
]);
expect(origin.uniqueBy(symbol)).toEqual([
{
id: 1,
uid: 10000
},
{
id: 2,
uid: 10000,
[symbol]: 'example'
}
]);
});
it('should compare items using "!==" by Returned values of a Custom callback', () => {
expect(origin.uniqueBy(item => `${item.uid}${item[symbol]}`)).toEqual(
origin
);
});
it('should treat an empty/nullish item as a nullish value', () => {
expect([1, , 2, undefined, null, 1].uniqueBy()).toEqual([1, , 2, null]);
expect(
[{ id: 1 }, , { id: 2 }, undefined, null, { id: 1 }].uniqueBy('id')
).toEqual([{ id: 1 }, , { id: 2 }, null]);
});
it('should treat 0 & -0 as the same', () => {
expect([0, -0].uniqueBy()).toEqual([0]);
expect([{ count: 0 }, { count: -0 }].uniqueBy('count')).toEqual([
{ count: 0 }
]);
});
it('should treat all NaNs as the same', () => {
expect([NaN, NaN].uniqueBy()).toEqual([NaN]);
expect([{ count: NaN }, { count: NaN }].uniqueBy('count')).toEqual([
{ count: NaN }
]);
});
});