From 2a3077239f7348c1d01490834dee7444c6b019d7 Mon Sep 17 00:00:00 2001 From: Oleg Chendighelean Date: Thu, 5 Dec 2024 14:42:04 +0000 Subject: [PATCH] Update tests --- packages/core/src/ErrorContext.test.ts | 53 +++++++++++++++++++++++--- packages/core/src/estimate.test.ts | 46 ++++++++++++++-------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/packages/core/src/ErrorContext.test.ts b/packages/core/src/ErrorContext.test.ts index 95087996f6..c6ea72ec4c 100644 --- a/packages/core/src/ErrorContext.test.ts +++ b/packages/core/src/ErrorContext.test.ts @@ -1,12 +1,55 @@ import { getErrorContext } from "./ErrorContext"; +import { handleTezError } from "./estimate"; + +jest.mock("./estimate", () => ({ + handleTezError: jest.fn(), +})); describe("getErrorContext", () => { - it("should get error context", () => { - const { description, stacktrace } = getErrorContext({ + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("should handle error object with message and stack", () => { + jest.mocked(handleTezError).mockReturnValue(undefined); + const error = { message: "some error message", stack: "some stacktrace", - }); - expect(description).toEqual("some error message"); - expect(stacktrace).toEqual("some stacktrace"); + }; + + const context = getErrorContext(error); + + expect(context.technicalDetails).toBe("some error message"); + expect(context.stacktrace).toBe("some stacktrace"); + expect(context.description).toBe( + "Something went wrong. Please try again or contact support if the issue persists." + ); + expect(context.timestamp).toBeDefined(); + }); + + it("should handle string errors", () => { + jest.mocked(handleTezError).mockReturnValue(undefined); + const error = "string error message"; + + const context = getErrorContext(error); + + expect(context.technicalDetails).toBe("string error message"); + expect(context.stacktrace).toBe(""); + expect(context.description).toBe( + "Something went wrong. Please try again or contact support if the issue persists." + ); + expect(context.timestamp).toBeDefined(); + }); + + it("should handle Error instances with Tezos-specific errors", () => { + jest.mocked(handleTezError).mockReturnValue("Handled tez error message"); + const error = new Error("test error"); + + const context = getErrorContext(error); + + expect(context.technicalDetails).toBe("test error"); + expect(context.description).toBe("Handled tez error message"); + expect(context.stacktrace).toBeDefined(); + expect(context.timestamp).toBeDefined(); }); }); diff --git a/packages/core/src/estimate.test.ts b/packages/core/src/estimate.test.ts index 141bf148ca..5625361391 100644 --- a/packages/core/src/estimate.test.ts +++ b/packages/core/src/estimate.test.ts @@ -34,21 +34,37 @@ describe("estimate", () => { ); }); - it("catches subtraction_underflow", () => { - const res = handleTezError(new Error("subtraction_underflow")); - expect(res).toEqual("Insufficient balance, please make sure you have enough funds."); - }); - - it("catches non_existing_contract", () => { - const res = handleTezError(new Error("contract.non_existing_contract")); - expect(res).toEqual( - "Contract does not exist, please check if the correct network is selected." - ); - }); - - it("returns the original error if not known", () => { - const err = new Error("unknown error"); - expect(handleTezError(err)).toEqual("unknown error"); + describe("handleTezError", () => { + it("catches subtraction_underflow", () => { + const res = handleTezError(new Error("subtraction_underflow")); + expect(res).toBe("Insufficient balance, please make sure you have enough funds."); + }); + + it("catches non_existing_contract", () => { + const res = handleTezError(new Error("contract.non_existing_contract")); + expect(res).toBe( + "Contract does not exist, please check if the correct network is selected." + ); + }); + + it("catches staking_to_delegate_that_refuses_external_staking", () => { + const res = handleTezError( + new Error("staking_to_delegate_that_refuses_external_staking") + ); + expect(res).toBe( + "The baker you are trying to stake to does not accept external staking." + ); + }); + + it("catches empty_implicit_delegated_contract", () => { + const res = handleTezError(new Error("empty_implicit_delegated_contract")); + expect(res).toBe("Emptying an implicit delegated account is not allowed."); + }); + + it("returns undefined for unknown errors", () => { + const err = new Error("unknown error"); + expect(handleTezError(err)).toBeUndefined(); + }); }); });