UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

61 lines (60 loc) 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const waitForResult_1 = require("./waitForResult"); (0, vitest_1.describe)('utils', () => { (0, vitest_1.describe)('waitForResult', () => { let mockedFunction; (0, vitest_1.beforeEach)(() => { mockedFunction = vitest_1.vi.fn(); vitest_1.vi.useFakeTimers(); }); (0, vitest_1.afterEach)(() => { vitest_1.vi.useRealTimers(); }); (0, vitest_1.it)('should throw immediately if shouldRetry returns false', async () => { mockedFunction.mockImplementation(() => Promise.reject('some error')); const shouldRetry = vitest_1.vi.fn().mockReturnValue(false); const promise = (0, waitForResult_1.waitForResult)(mockedFunction, 1000, 3, shouldRetry); await (0, vitest_1.expect)(promise).rejects.toThrowError('some error'); (0, vitest_1.expect)(mockedFunction).toHaveBeenCalledTimes(1); (0, vitest_1.expect)(shouldRetry).toHaveBeenCalledWith(0, 'some error'); }); (0, vitest_1.it)('should try until repeat function succeeds', async () => { mockedFunction .mockResolvedValueOnce(undefined) .mockResolvedValueOnce(undefined) .mockResolvedValueOnce('success!'); const promise = (0, waitForResult_1.waitForResult)(mockedFunction, 1000); // Fast-forward through retries for (let i = 0; i < 2; i++) { await vitest_1.vi.advanceTimersByTimeAsync(1000); } const result = await promise; (0, vitest_1.expect)(result).toEqual('success!'); (0, vitest_1.expect)(mockedFunction).toHaveBeenCalledTimes(3); }); (0, vitest_1.it)('should respect the interval between retries', async () => { mockedFunction .mockResolvedValueOnce(undefined) .mockResolvedValueOnce('success!'); const promise = (0, waitForResult_1.waitForResult)(mockedFunction, 2000); await vitest_1.vi.advanceTimersByTimeAsync(2000); const result = await promise; (0, vitest_1.expect)(result).toEqual('success!'); (0, vitest_1.expect)(mockedFunction).toHaveBeenCalledTimes(2); }); (0, vitest_1.it)('should throw an error if repeat function fails and maxRetries is reached', async () => { mockedFunction.mockImplementation(() => Promise.reject('some error')); const maxRetries = 2; const promise = (0, waitForResult_1.waitForResult)(mockedFunction, 1000, maxRetries); const expectPromise = (0, vitest_1.expect)(promise).rejects.toThrowError('some error'); // Fast-forward through retries for (let i = 0; i < maxRetries - 1; i++) { await vitest_1.vi.advanceTimersByTimeAsync(1000); } await expectPromise; (0, vitest_1.expect)(mockedFunction).toHaveBeenCalledTimes(maxRetries); }); }); });