From aed8d8f3bbc038b6824ebac351f0faae391f16ef Mon Sep 17 00:00:00 2001 From: Adrian C <92168447+windushka@users.noreply.github.com> Date: Mon, 26 Jun 2023 11:20:05 +0200 Subject: [PATCH] 302 unit tests retry execute function (#305) * added unit tests for retryExecuteFunction.ts * adding console error mock * add missing args parameter tests * fix lint --------- Co-authored-by: julienbrs <106234742+julienbrs@users.noreply.github.com> --- test/utils/retryExecuteFunction.spec.ts | 72 +++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/utils/retryExecuteFunction.spec.ts diff --git a/test/utils/retryExecuteFunction.spec.ts b/test/utils/retryExecuteFunction.spec.ts new file mode 100644 index 00000000..86279945 --- /dev/null +++ b/test/utils/retryExecuteFunction.spec.ts @@ -0,0 +1,72 @@ +import { JSON_RPC_REQUEST_METHOD } from '../../src/interfaces/JsonRpcMethods'; +import { trySafeExecute } from '../../src/utils/retryExecuteFunction'; +import { wait } from '../../src/utils/time'; + +jest.mock('../../src/utils/time'); + +describe('trySafeExecute function', () => { + beforeAll(() => { + const consoleSpy = jest.spyOn(console, 'error'); + consoleSpy.mockImplementation(() => null); + }); + + beforeEach(() => { + jest.spyOn(global, 'setTimeout'); + (wait as jest.Mock).mockImplementation((delay: number) => { + return new Promise((resolve) => setTimeout(resolve, delay)); + }); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + afterAll(() => { + jest.restoreAllMocks(); + }); + + it('should execute a function successfully on the first attempt', async () => { + const mockFunc = jest.fn().mockResolvedValue('success'); + const result = await trySafeExecute(mockFunc, [ + JSON_RPC_REQUEST_METHOD.GET_STATUS, + {}, + ]); + expect(result).toEqual('success'); + expect(mockFunc).toHaveBeenCalledTimes(1); + }); + + it('should retry the function upon failure and succeed', async () => { + const mockFunc = jest + .fn() + .mockRejectedValueOnce(new Error('failed')) + .mockResolvedValue('success'); + const result = await trySafeExecute(mockFunc, [ + JSON_RPC_REQUEST_METHOD.GET_STATUS, + {}, + ]); + expect(result).toEqual('success'); + expect(mockFunc).toHaveBeenCalledTimes(2); + }); + + it('should retry the function the correct number of times and then throw an error', async () => { + const mockFunc = jest.fn().mockRejectedValue(new Error('failed')); + await expect( + trySafeExecute(mockFunc, [JSON_RPC_REQUEST_METHOD.GET_STATUS, {}], 3), + ).rejects.toThrow('failed'); + expect(mockFunc).toHaveBeenCalledTimes(3); + }); + + it('should throw an error when no function is provided', async () => { + await expect( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + trySafeExecute(null as any, [JSON_RPC_REQUEST_METHOD.GET_STATUS, {}]), + ).rejects.toThrow(`Function execution init conditions are erroneous: null`); + }); + + it('should handle missing args parameter', async () => { + const mockFunc = jest.fn().mockResolvedValue('success'); + const result = await trySafeExecute(mockFunc); + expect(result).toEqual('success'); + expect(mockFunc).toHaveBeenCalledWith(null, {}); + }); +});