Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Dec 5, 2024
1 parent e853ad8 commit 2a30772
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
53 changes: 48 additions & 5 deletions packages/core/src/ErrorContext.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
46 changes: 31 additions & 15 deletions packages/core/src/estimate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down

0 comments on commit 2a30772

Please sign in to comment.