@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
111 lines • 5.23 kB
JavaScript
"use strict";
/**
* @jest-environment jsdom
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const useAccountsWithFundsListener_1 = require("./useAccountsWithFundsListener");
const index_1 = require("../currencies/index");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const eth = (0, index_1.getCryptoCurrencyById)("ethereum");
const polygon = (0, index_1.getCryptoCurrencyById)("polygon");
const ethMockAccount = {
type: "Account",
id: "js:2:ethereum:0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3:",
used: true,
seedIdentifier: "0441996d9ce858d8fd6304dd790e645500fc6cee7ae0fccfee8c8fa884dfa8ccf1f6f8cc82cc0aa71fc659c895a8a43b69f918b08a22b3a6145a0bbd93c5cb9308",
derivationMode: "",
index: 0,
freshAddress: "0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3",
freshAddressPath: "44'/60'/0'/0/0",
blockHeight: 16626551,
creationDate: new Date("2021-03-23T14:17:07.001Z"),
balance: new bignumber_js_1.default("22913015427119498"),
spendableBalance: new bignumber_js_1.default("22913015427119498"),
operations: [],
operationsCount: 0,
pendingOperations: [],
currency: eth,
lastSyncDate: new Date("2023-02-14T11:01:19.252Z"),
swapHistory: [],
balanceHistoryCache: {
HOUR: { balances: [], latestDate: 1676329200000 },
DAY: { balances: [], latestDate: 1676329200000 },
WEEK: { balances: [], latestDate: 1676329200000 },
},
nfts: [],
subAccounts: [],
};
const polygonMockAccount = {
type: "Account",
id: "js:2:polygon:0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3:",
used: true,
seedIdentifier: "0441996d9ce858d8fd6304dd790e645500fc6cee7ae0fccfee8c8fa884dfa8ccf1f6f8cc82cc0aa71fc659c895a8a43b69f918b08a22b3a6145a0bbd93c5cb9308",
derivationMode: "",
index: 0,
freshAddress: "0x66c4371aE8FFeD2ec1c2EBbbcCfb7E494181E1E3",
freshAddressPath: "44'/60'/0'/0/0",
blockHeight: 16626551,
creationDate: new Date("2021-03-23T14:17:07.001Z"),
balance: new bignumber_js_1.default("22913015427119498"),
spendableBalance: new bignumber_js_1.default("22913015427119498"),
operations: [],
operationsCount: 0,
pendingOperations: [],
currency: polygon,
lastSyncDate: new Date("2023-02-14T11:01:19.252Z"),
swapHistory: [],
syncHash: "[]_6595",
balanceHistoryCache: {
HOUR: { balances: [], latestDate: 1676329200000 },
DAY: { balances: [], latestDate: 1676329200000 },
WEEK: { balances: [], latestDate: 1676329200000 },
},
nfts: [],
subAccounts: [],
};
describe("useAccountsWithFundsListener", () => {
describe("Test the hasAccountsWithFundsChanged helper function", () => {
test("should return true when an account with funds is added", () => {
const newAccounts = [ethMockAccount];
const oldAccounts = [];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeTruthy();
});
test("should return true when an account with funds is deleted", () => {
const newAccounts = [];
const oldAccounts = [ethMockAccount];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeTruthy();
});
test("should return true when an account with funds is deleted and another one is added at the same time", () => {
const newAccounts = [ethMockAccount];
const oldAccounts = [polygonMockAccount];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeTruthy();
});
test("should return true when an account with funds is emptied", () => {
const newAccounts = [ethMockAccount];
const oldAccounts = [{ ...ethMockAccount, balance: new bignumber_js_1.default(0) }];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeTruthy();
});
test("should return true when an empty account receives funds", () => {
const newAccounts = [{ ...ethMockAccount, balance: new bignumber_js_1.default(0) }];
const oldAccounts = [ethMockAccount];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeTruthy();
});
test("should return false when the balance of an account with funds changes but isn't emptied", () => {
const newAccounts = [ethMockAccount];
const oldAccounts = [
{ ...ethMockAccount, balance: new bignumber_js_1.default("10000000000000000") },
];
const res = (0, useAccountsWithFundsListener_1.hasAccountsWithFundsChanged)(newAccounts, oldAccounts);
expect(res).toBeFalsy();
});
});
});
//# sourceMappingURL=useAccountsWithFundsListener.test.js.map