UNPKG

@ledgerhq/live-common

Version:
115 lines 5.04 kB
"use strict"; 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 cryptoassets_1 = require("@ledgerhq/cryptoassets"); const react_1 = require("@testing-library/react"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const index_1 = require("../../../account/index"); const mock_1 = __importDefault(require("../../../families/evm/bridge/mock")); const account_1 = require("@ledgerhq/coin-framework/mocks/account"); const account_2 = require("../../../mock/account"); const useUpdateMaxAmount_1 = require("./useUpdateMaxAmount"); // Needs to be mocked since userSupportedCurrencies is initially empty. jest.mock("../../../account/support"); const mockedCheckAccount = jest.mocked(index_1.checkAccountSupported); // Mock to use a custom estimate value and test the result. jest.mock("../../../families/evm/bridge/mock"); const mockedEstimateMaxSpendable = jest.mocked(mock_1.default.accountBridge.estimateMaxSpendable); const ETH = (0, cryptoassets_1.getCryptoCurrencyById)("ethereum"); const usdtToken = (0, cryptoassets_1.findTokenById)("ethereum/erc20/usd_tether__erc20_"); if (!usdtToken) throw new Error("USDT token not found"); const USDT = usdtToken; const parentAccount = (0, account_2.genAccount)("parent-account", { currency: ETH, }); const account = (0, account_1.genTokenAccount)(1, parentAccount, USDT); describe("updateAmountUsingMax", () => { const setFromAmount = jest.fn(); const defaultProps = { setFromAmount, account, parentAccount, bridge: mock_1.default.accountBridge, feesStrategy: "slow", }; beforeAll(() => { mockedCheckAccount.mockImplementation(() => null); }); afterAll(() => { mockedCheckAccount.mockReset(); }); beforeEach(() => { setFromAmount.mockClear(); }); it("should toggle the amount", async () => { const amount = new bignumber_js_1.default(0.5); mockedEstimateMaxSpendable.mockResolvedValue(amount); const { result } = (0, react_1.renderHook)(useUpdateMaxAmount_1.useUpdateMaxAmount, { initialProps: defaultProps, }); expect(result.current.isMaxEnabled).toBe(false); expect(setFromAmount).toBeCalledTimes(0); await (0, react_1.act)(async () => result.current.toggleMax()); expect(result.current.isMaxEnabled).toBe(true); // Lest resort solution, since waitFor and other helpers will not work here. expect(setFromAmount).toBeCalledTimes(1); expect(setFromAmount.mock.calls[0][0]).toBe(amount); setFromAmount.mockClear(); await (0, react_1.act)(async () => result.current.toggleMax()); expect(result.current.isMaxEnabled).toBe(false); expect(setFromAmount).toBeCalledTimes(1); expect(setFromAmount.mock.calls[0][0]).toBe(useUpdateMaxAmount_1.ZERO); }); it("should update the max amount whenever the dependencies change", async () => { const { result, rerender } = (0, react_1.renderHook)(useUpdateMaxAmount_1.useUpdateMaxAmount, { initialProps: defaultProps, }); const feesGenerator = (function* feesGenerator() { const feesArray = ["medium", "fast", "custom", "slow"]; let index = 0; while (true) { yield feesArray[index]; index = (index + 1) % feesArray.length; } })(); // setFromAmount, account, parentAccount, feesStrategy const propsVariants = [ { ...defaultProps, account: { ...account }, }, { ...defaultProps, parentAccount: { ...parentAccount }, }, { ...defaultProps, feesStrategy: feesGenerator.next().value, }, ]; // Updating dependencies when the toggle is off should not do anything. propsVariants.forEach(rerender); expect(setFromAmount).toBeCalledTimes(0); mockedEstimateMaxSpendable.mockResolvedValue(new bignumber_js_1.default(0)); await (0, react_1.act)(async () => result.current.toggleMax()); // Checking that updating dependencies update the max amount when the toggle is on. let idx = 1; for await (const props of propsVariants) { const amount = new bignumber_js_1.default(idx); setFromAmount.mockReset(); mockedEstimateMaxSpendable.mockResolvedValue(amount); await (0, react_1.act)(async () => rerender(props)); expect(setFromAmount).toBeCalledTimes(1); expect(setFromAmount.mock.calls[0][0]).toBe(amount); idx += 1; } }); }); //# sourceMappingURL=useUpdateMaxAmount.test.js.map