Skip to content

Commit d250a02

Browse files
committed
Add unit tests
1 parent dc894fb commit d250a02

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/reduce.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,50 @@ describe('asyncReduce()', () => {
5858

5959
await expect(() => asyncReduce(['a', 'b', 'c'], mapper)).rejects.toThrow('Some error');
6060
});
61+
62+
it('returns type string given function that returns Promise<string> and no initial value', async () => {
63+
// @ts-expect-no-error
64+
const result: string = await asyncReduce(['a', 'b', 'c'], async (temp, cur) => cur);
65+
66+
expect(result).toBe('c');
67+
68+
// Sanity check
69+
const resultSync: string = ['a', 'b', 'c'].reduce((temp, cur) => cur);
70+
71+
expect(resultSync).toBe('c');
72+
});
73+
74+
it('returns type string[] given function that returns Promise<string> and initial value of type string[]', async () => {
75+
// @ts-expect-no-error
76+
const result: string[] = await asyncReduce(
77+
['a', 'b', 'c'],
78+
async (temp, cur) => [...temp, cur],
79+
[] as string[],
80+
);
81+
82+
expect(result).toEqual(['a', 'b', 'c']);
83+
84+
// Sanity check
85+
const resultSync: string[] = ['a', 'b', 'c'].reduce(
86+
(temp, cur) => [...temp, cur],
87+
[] as string[],
88+
);
89+
90+
expect(resultSync).toEqual(['a', 'b', 'c']);
91+
});
92+
93+
it('returns type number[] given function that returns Promise<number> and initial value of type number', async () => {
94+
const result: number[] = await asyncReduce(
95+
['a', 'b', 'c'],
96+
async (temp, cur, idx) => [...temp, idx],
97+
[0],
98+
);
99+
100+
expect(result).toEqual([0, 0, 1, 2]);
101+
102+
// Sanity check
103+
const resultSync: number[] = ['a', 'b', 'c'].reduce((temp, cur, idx) => [...temp, idx], [0]);
104+
105+
expect(resultSync).toEqual([0, 0, 1, 2]);
106+
});
61107
});

0 commit comments

Comments
 (0)