-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy patharrayHelper.spec.ts
More file actions
36 lines (30 loc) · 1021 Bytes
/
arrayHelper.spec.ts
File metadata and controls
36 lines (30 loc) · 1021 Bytes
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
import { Arr } from '../../lib/utils/array';
describe('Array Helper', () => {
beforeEach(async () => {});
it('check key exists', () => {
const arr = [1, 2, 3, 4, 5];
expect(Arr.exists(arr, 2)).toBeTruthy();
});
it('check key does not exist', () => {
const arr = [1, 2, 3, 4, 5];
expect(Arr.exists(arr, 6)).toBeFalsy();
});
it('should return last element matching predicate', () => {
const arr = [1, 2, 3, 4, 5];
expect(Arr.last(arr, x => x < 4)).toBe(3);
expect(Arr.last(arr)).toBe(5);
});
it('should return the last object that matches the predicate', () => {
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 30 },
];
const lastUserUnder35 = Arr.last(users, user => user.age < 35);
expect(lastUserUnder35).toEqual({ name: 'David', age: 30 });
});
it('should return undefined for empty array', () => {
expect(Arr.last([])).toBeUndefined();
});
});