forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.test.ts
46 lines (42 loc) · 1.1 KB
/
find.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
import { createLazyInvocationCounter } from '../test/lazy_invocation_counter';
import { find } from './find';
import { pipe } from './pipe';
const array = [
{ a: 1, b: 1 },
{ a: 1, b: 2 },
{ a: 2, b: 1 },
{ a: 1, b: 3 },
] as const;
const expected = { a: 1, b: 2 };
describe('data first', () => {
test('find', () => {
expect(find(array, x => x.b === 2)).toEqual(expected);
});
test('find.indexed', () => {
expect(find.indexed(array, (x, idx) => x.b === 2 && idx === 1)).toEqual(
expected
);
});
});
describe('data last', () => {
test('find', () => {
const counter = createLazyInvocationCounter();
const actual = pipe(
array,
counter.fn(),
find(x => x.b === 2)
);
expect(counter.count).toHaveBeenCalledTimes(2);
expect(actual).toEqual(expected);
});
test('find.indexed', () => {
const counter = createLazyInvocationCounter();
const actual = pipe(
array,
counter.fn(),
find.indexed((x, idx) => x.b === 2 && idx === 1)
);
expect(counter.count).toHaveBeenCalledTimes(2);
expect(actual).toEqual(expected);
});
});