@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
120 lines • 5.64 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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const currencies_1 = require("../../../currencies");
const account_1 = require("../../../mock/account");
const usePickDefaultAccount_1 = require("./usePickDefaultAccount");
const wrapper_1 = require("./wrapper");
(0, currencies_1.setSupportedCurrencies)(["ethereum"]);
function* accountGenerator(currency) {
let id = 0;
while (true) {
id += 1;
// operations are not taken into account by the usePickDefaultAccount hooks
yield (0, account_1.genAccount)(`mocked-account-${id}`, { currency, operationsSize: 0 });
}
}
const getAccountCreator = (currencyId) => {
const generator = accountGenerator((0, currencies_1.getCryptoCurrencyById)(currencyId));
return () => generator.next().value;
};
describe("usePickDefaultAccount", () => {
const getEthAccount = getAccountCreator("ethereum");
const getBtcAccount = getAccountCreator("bitcoin");
const setFromAccount = jest.fn();
beforeEach(() => {
setFromAccount.mockClear();
});
test("do nothing when fromAccount is not null/undefined", () => {
const accounts = [getEthAccount(), getBtcAccount()];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, getEthAccount(), setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(0);
});
test("do nothing when all passed accounts are disabled", () => {
const accounts = [
{ ...getEthAccount(), disabled: true },
{ ...getBtcAccount(), disabled: true },
{ ...getEthAccount(), disabled: true },
{ ...getEthAccount(), disabled: true },
];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, undefined, setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(0);
});
test("returns an ethereum account when it's available", () => {
const ethAccount = getEthAccount();
ethAccount.balance = new bignumber_js_1.default(1);
const accounts = [ethAccount, getBtcAccount()];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, undefined, setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(1);
expect(setFromAccount).toHaveBeenCalledWith(ethAccount);
});
test("returns the first marketcap account when all of the enabled accounts have the same balance", () => {
const ethAccount = getEthAccount();
const btcAccount = getBtcAccount();
btcAccount.balance = new bignumber_js_1.default(1);
const accounts = [
ethAccount,
btcAccount,
getEthAccount(),
{ ...getEthAccount(), disabled: true },
];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, undefined, setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(1);
expect(setFromAccount).toHaveBeenCalledWith(btcAccount);
});
test("returns the ethereum enabled account with the highest balance", () => {
const ethAccount = getEthAccount();
const ethAccount2 = getEthAccount();
const ethAccount3 = getEthAccount();
const ethAccount4 = getEthAccount();
const ethAccount5 = getEthAccount();
ethAccount.balance = new bignumber_js_1.default(0.2);
ethAccount2.balance = new bignumber_js_1.default(0);
ethAccount3.balance = new bignumber_js_1.default(0.001);
ethAccount4.balance = new bignumber_js_1.default(0.0001);
ethAccount5.balance = new bignumber_js_1.default(0.0006);
const accounts = [
{ ...getEthAccount(), disabled: true },
ethAccount,
getBtcAccount(),
ethAccount2,
ethAccount3,
ethAccount4,
ethAccount5,
];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, undefined, setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(1);
expect(setFromAccount).toHaveBeenCalledWith(ethAccount);
});
test("returns the highest bitcoin account when no ethereum accounts are passed/enabled and all btc accounts have the same balance", () => {
const btcAccount = getBtcAccount();
const btcAccount2 = getBtcAccount();
btcAccount.balance = new bignumber_js_1.default(0.2);
btcAccount2.balance = new bignumber_js_1.default(0.001);
const accounts = [{ ...getEthAccount(), disabled: true }, btcAccount, btcAccount2];
(0, react_1.renderHook)(() => (0, usePickDefaultAccount_1.usePickDefaultAccount)(accounts, undefined, setFromAccount), {
wrapper: wrapper_1.Wrapper,
});
expect(setFromAccount).toHaveBeenCalledTimes(1);
expect(setFromAccount).toHaveBeenCalledWith(btcAccount);
});
});
//# sourceMappingURL=usePickDefaultAccount.test.js.map