UNPKG

@ledgerhq/live-common

Version:
74 lines 3.38 kB
"use strict"; 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 account_1 = require("@ledgerhq/coin-framework/mocks/account"); const account_2 = require("../../../mock/account"); const useReverseAccounts_1 = require("./useReverseAccounts"); const BTC = (0, cryptoassets_1.getCryptoCurrencyById)("bitcoin"); 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 fromParentAccount = (0, account_2.genAccount)("mocked-account-2", { currency: ETH, }); const fromAccount = (0, account_1.genTokenAccount)(1, fromParentAccount, USDT); const toAccount = (0, account_2.genAccount)("mocked-account-1", { currency: BTC }); const allAccounts = [fromAccount, fromParentAccount, toAccount]; describe("useReverseAccounts", () => { const setFromAccount = jest.fn(); const setToAccount = jest.fn(); const defaultProps = { accounts: allAccounts, fromAccount, fromParentAccount, toAccount, fromCurrency: USDT, setFromAccount, setToAccount, }; it("should tell if the accounts are swappable", () => { const { result, rerender } = (0, react_1.renderHook)(props => (0, useReverseAccounts_1.useReverseAccounts)(props), { initialProps: defaultProps, }); expect(result.current.isSwapReversable).toBe(true); rerender({ ...defaultProps, toAccount: undefined }); expect(result.current.isSwapReversable).toBe(false); rerender({ ...defaultProps, fromCurrency: undefined }); expect(result.current.isSwapReversable).toBe(false); rerender({ ...defaultProps, // detached account - not in the main accounts list toAccount: (0, account_2.genAccount)("mocked-account-3", { currency: BTC }), }); expect(result.current.isSwapReversable).toBe(false); }); it("should reverse accounts when 'reverseSwap' is called", () => { const { result, rerender } = (0, react_1.renderHook)(useReverseAccounts_1.useReverseAccounts, { initialProps: { setToAccount, setFromAccount, }, }); // if isSwapReversable is false, then reverseSwap should be a no-op expect(result.current.isSwapReversable).toBe(false); (0, react_1.act)(() => result.current.reverseSwap()); expect(setToAccount).toHaveBeenCalledTimes(0); expect(setFromAccount).toHaveBeenCalledTimes(0); // Reverse from/to accounts rerender(defaultProps); expect(result.current.isSwapReversable).toBe(true); (0, react_1.act)(() => result.current.reverseSwap()); expect(setToAccount).toHaveBeenCalledTimes(1); expect(setToAccount.mock.calls[0]).toMatchObject([USDT, fromAccount, fromParentAccount]); expect(setFromAccount).toHaveBeenCalledTimes(1); expect(setFromAccount.mock.calls[0]).toMatchObject([toAccount]); }); }); //# sourceMappingURL=useReverseAccounts.test.js.map