-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.spec.ts
66 lines (60 loc) · 1.74 KB
/
reduce.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
import { expectType } from '#/utils'
import { reduce, toAsync } from 'fx-flow'
expectType<number>(reduce((a, b) => a + b, [1, 2, 3]))
expectType<(iterable: Iterable<number>) => number>(reduce((a: number, b: number) => a + b))
expectType<(iterable: AsyncIterable<number>) => Promise<number>>(reduce((a: number, b: number) => a + b))
describe('reduce', () => {
describe('sync', () => {
it('should use first item as initialRes if it is not specified', () => {
const res = reduce(
(prevRes, args) => {
return prevRes + args
},
[1, 2, 3, 4]
)
expect(res).toEqual(10)
})
it('should start with initialRes if it is specified', () => {
const res = reduce(
(prevRes, args) => {
return prevRes + args
},
10,
[1, 2, 3, 4]
)
expect(res).toEqual(20)
})
it('should work when type of return value and arguments are different', () => {
const res = reduce(
(prevRes, args) => {
return prevRes + String(args)
},
'10',
[1, 2, 3, 4]
)
expect(res).toEqual('101234')
})
})
describe('async', () => {
it('should work for async iterable and async callback', () => {
reduce(
async (prevRes, args) => {
return prevRes + args
},
toAsync([Promise.resolve(1), Promise.resolve(2), 3, 4])
).then((res) => {
expect(res).toEqual(10)
})
})
it('should work for async iterable and sync callback', () => {
reduce(
(prevRes, args) => {
return prevRes + args
},
toAsync([Promise.resolve(1), Promise.resolve(2), 3, 4])
).then((res) => {
expect(res).toEqual(10)
})
})
})
})