-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathutils.test.ts
62 lines (51 loc) · 1.82 KB
/
utils.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { retryUntilConditionMet } from '../utils.js';
jest.mock('@redocly/openapi-core', () => ({
pause: jest.requireActual('@redocly/openapi-core').pause,
}));
describe('retryUntilConditionMet()', () => {
it('should retry until condition meet and return result', async () => {
const operation = jest
.fn()
.mockResolvedValueOnce({ status: 'pending' })
.mockResolvedValueOnce({ status: 'pending' })
.mockResolvedValueOnce({ status: 'done' });
const data = await retryUntilConditionMet({
operation,
condition: (result: any) => result?.status === 'done',
retryIntervalMs: 100,
retryTimeoutMs: 1000,
});
expect(data).toEqual({ status: 'done' });
});
it('should throw error if condition not meet for desired timeout', async () => {
const operation = jest.fn().mockResolvedValue({ status: 'pending' });
await expect(
retryUntilConditionMet({
operation,
condition: (result: any) => result?.status === 'done',
retryIntervalMs: 100,
retryTimeoutMs: 1000,
})
).rejects.toThrow('Timeout exceeded');
});
it('should call "onConditionNotMet" and "onRetry" callbacks', async () => {
const operation = jest
.fn()
.mockResolvedValueOnce({ status: 'pending' })
.mockResolvedValueOnce({ status: 'pending' })
.mockResolvedValueOnce({ status: 'done' });
const onConditionNotMet = jest.fn();
const onRetry = jest.fn();
const data = await retryUntilConditionMet({
operation,
condition: (result: any) => result?.status === 'done',
retryIntervalMs: 100,
retryTimeoutMs: 1000,
onConditionNotMet,
onRetry,
});
expect(data).toEqual({ status: 'done' });
expect(onConditionNotMet).toHaveBeenCalledTimes(2);
expect(onRetry).toHaveBeenCalledTimes(2);
});
});