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 d03573c commit e0e4e49
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 51 deletions.
25 changes: 10 additions & 15 deletions apps/desktop/src/components/SendFlow/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,17 @@ export const useSignPageHelpers = (
).catch(() => setEstimationFailed(true));

const onSign = async (tezosToolkit: TezosToolkit) =>
handleAsyncAction(
async () => {
const operation = await executeOperations(
{ ...operations, estimates: form.watch("executeParams") },
tezosToolkit
);
if (mode === "batch") {
clearBatch(operations.sender);
}
await openWith(<SuccessStep hash={operation.opHash} />);
return operation;
},
{
description: "Something went wrong, please try again.",
handleAsyncAction(async () => {
const operation = await executeOperations(
{ ...operations, estimates: form.watch("executeParams") },
tezosToolkit
);
if (mode === "batch") {
clearBatch(operations.sender);
}
);
await openWith(<SuccessStep hash={operation.opHash} />);
return operation;
});

return {
fee: totalFee(form.watch("executeParams")),
Expand Down
8 changes: 5 additions & 3 deletions apps/web/src/components/Menu/ErrorLogsMenu/ErrorLogsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ export const ErrorLogsMenu = () => {
<Text color={color("700")} size="sm">
{errorLog.timestamp}
</Text>
<Text marginTop="12px" color={color("700")} size="sm">
{errorLog.technicalDetails}
</Text>
{errorLog.technicalDetails && (
<Text marginTop="12px" color={color("700")} size="sm">
{errorLog.technicalDetails}
</Text>
)}
</Flex>
</Flex>
</Box>
Expand Down
20 changes: 8 additions & 12 deletions apps/web/src/components/SendFlow/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,14 @@ export const useSignPageHelpers = (
).catch(() => setEstimationFailed(true));

const onSign = async (tezosToolkit: TezosToolkit) =>
handleAsyncAction(
async () => {
const operation = await executeOperations(
{ ...operations, estimates: form.watch("executeParams") },
tezosToolkit
);
await openWith(<SuccessStep hash={operation.opHash} />);
return operation;
},
// Here we should show a generic error message, keeping the original error for errors list
{ description: "Something went wrong, please try again." }
);
handleAsyncAction(async () => {
const operation = await executeOperations(
{ ...operations, estimates: form.watch("executeParams") },
tezosToolkit
);
await openWith(<SuccessStep hash={operation.opHash} />);
return operation;
});

return {
fee: totalFee(form.watch("executeParams")),
Expand Down
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();
});
});
1 change: 0 additions & 1 deletion packages/core/src/ErrorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type ErrorContext = {
};

export const getErrorContext = (error: any): ErrorContext => {
console.log({ error });
let description =
"Something went wrong. Please try again or contact support if the issue persists.";
let technicalDetails;
Expand Down
42 changes: 27 additions & 15 deletions packages/core/src/estimate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,33 @@ 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 e0e4e49

Please sign in to comment.