|
1 | 1 | import { createLogger } from '../../logger';
|
2 |
| -import { runInBackground, sleep } from '../async.utils'; |
| 2 | +import { runInBackground, sleep, waitUntil } from '../async.utils'; |
3 | 3 |
|
4 |
| -jest.mock('../../logger/logger.utils', () => { |
| 4 | +jest.mock('../../logger', () => { |
5 | 5 | const actualModule = jest.requireActual('../../logger');
|
6 | 6 | return {
|
7 | 7 | ...actualModule,
|
@@ -29,7 +29,7 @@ describe('async-utils', () => {
|
29 | 29 |
|
30 | 30 | const end = Date.now();
|
31 | 31 |
|
32 |
| - expect(timeoutSpy).toBeCalledTimes(1); |
| 32 | + expect(timeoutSpy).toHaveBeenCalledTimes(1); |
33 | 33 | expect(end - start).toBeGreaterThanOrEqual(sleepMillis - varianceMillis);
|
34 | 34 | expect(end - start).toBeLessThanOrEqual(sleepMillis + varianceMillis);
|
35 | 35 | });
|
@@ -72,4 +72,45 @@ describe('async-utils', () => {
|
72 | 72 | }, 250);
|
73 | 73 | });
|
74 | 74 | });
|
| 75 | + |
| 76 | + describe('#waitUntil', () => { |
| 77 | + test('when condition is true then it resolves true', async () => { |
| 78 | + const condition = jest.fn().mockReturnValue(true); |
| 79 | + const interval = 100; |
| 80 | + const timeout = 1000; |
| 81 | + |
| 82 | + const result = await waitUntil({ condition, interval, timeout }); |
| 83 | + |
| 84 | + expect(result).toBe(true); |
| 85 | + expect(condition).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + test('when condition is false then it resolves false', async () => { |
| 89 | + const condition = jest.fn().mockReturnValue(false); |
| 90 | + const interval = 100; |
| 91 | + const timeout = 1000; |
| 92 | + |
| 93 | + const result = await waitUntil({ condition, interval, timeout }); |
| 94 | + |
| 95 | + expect(result).toBe(false); |
| 96 | + expect(condition).toHaveBeenCalledTimes(9); |
| 97 | + }); |
| 98 | + |
| 99 | + test('when condition is true after 5 intervals then it resolves true', async () => { |
| 100 | + const condition = jest |
| 101 | + .fn() |
| 102 | + .mockReturnValueOnce(false) |
| 103 | + .mockReturnValueOnce(false) |
| 104 | + .mockReturnValueOnce(false) |
| 105 | + .mockReturnValueOnce(false) |
| 106 | + .mockReturnValueOnce(true); |
| 107 | + const interval = 100; |
| 108 | + const timeout = 1000; |
| 109 | + |
| 110 | + const result = await waitUntil({ condition, interval, timeout }); |
| 111 | + |
| 112 | + expect(result).toBe(true); |
| 113 | + expect(condition).toHaveBeenCalledTimes(5); |
| 114 | + }); |
| 115 | + }); |
75 | 116 | });
|
0 commit comments