@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
106 lines • 4.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @jest-environment jsdom
*/
require("../../../__tests__/test-helpers/dom-polyfill");
const react_1 = require("@testing-library/react");
const errors_1 = require("@ledgerhq/errors");
const useSwapTransaction_1 = require("./useSwapTransaction");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
jest.mock("@ledgerhq/coin-framework/account/index", () => ({
...jest.requireActual("@ledgerhq/coin-framework/account/index"),
getFeesUnit: () => ({
name: "wei",
code: "ETH",
magnitude: 18,
showAllDigits: false,
prefixCode: false,
}),
getAccountCurrency: () => ({
ticker: "ETH",
name: "Ethereum",
}),
}));
function generateTransactionWithError({ account, parentAccount, errors, estimatedFees = (0, bignumber_js_1.default)(0), }) {
return {
account,
parentAccount,
status: {
errors,
estimatedFees,
},
};
}
/**
* @DEV: The useSwapTransaction hook is a composition hook. It doesn't own specific
* logic, but it's composed of other hooks. That's why only the useFromAmountStatusMessage
* logic is tested here, it's the only logic owned by the useSwapTransaction hook.
* All other lines are covered by other hooks tests.
*/
describe("useSwapTransaction", () => {
test("useFromAmountStatusMessage - returns nothing when no errors are caught", () => {
const mockTransaction = generateTransactionWithError({ errors: {} });
const { result } = (0, react_1.renderHook)(() => (0, useSwapTransaction_1.useFromAmountStatusMessage)(mockTransaction, ["gasPrice", "amount"]));
expect(result.current).toBeUndefined();
});
test("useFromAmountStatusMessage - returns the first error caught", () => {
const gasPriceError = new Error("Gas price is too high");
const amountError = new Error("Amount is too low");
const notEnoughGasError = new errors_1.NotEnoughGas();
const tests = [
{
input: generateTransactionWithError({
errors: { gasPrice: gasPriceError, amount: undefined },
}),
output: gasPriceError,
},
{
input: generateTransactionWithError({
errors: { gasPrice: undefined, amount: amountError },
}),
output: amountError,
},
{
input: generateTransactionWithError({
errors: { gasPrice: gasPriceError, amount: amountError },
}),
output: gasPriceError,
},
{
input: generateTransactionWithError({
errors: { gasPrice: notEnoughGasError },
}),
output: notEnoughGasError,
},
{
input: generateTransactionWithError({
errors: { gasPrice: notEnoughGasError },
}),
output: notEnoughGasError,
},
];
tests.forEach(({ input, output }) => {
const { result } = (0, react_1.renderHook)(() => (0, useSwapTransaction_1.useFromAmountStatusMessage)(input, ["gasPrice", "amount"]));
expect(result.current).toEqual(output);
});
});
test("useFromAmountStatusMessage - return NotEnoughGasSwap error if fields exist in tx", () => {
const mockAccount = {};
const input = generateTransactionWithError({
errors: { gasPrice: new errors_1.NotEnoughGas() },
account: mockAccount,
});
const { result } = (0, react_1.renderHook)(() => (0, useSwapTransaction_1.useFromAmountStatusMessage)(input, ["gasPrice"]));
expect(result.current).toBeInstanceOf(errors_1.NotEnoughGasSwap);
});
test("useFromAmountStatusMessage - do not return AmountRequired error", () => {
const amountError = new errors_1.AmountRequired("This error will be filtered");
const { result } = (0, react_1.renderHook)(() => (0, useSwapTransaction_1.useFromAmountStatusMessage)(generateTransactionWithError({ errors: { gasPrice: undefined, amount: amountError } }), ["gasPrice", "amount"]));
expect(result.current).toBeUndefined();
});
});
//# sourceMappingURL=useSwapTransaction.test.js.map