"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 _1 = require(".");
const useBridgeTransaction_1 = __importDefault(require("../../../bridge/useBridgeTransaction"));
const account_1 = require("@ledgerhq/coin-framework/mocks/account");
const account_2 = require("../../../mock/account");
const useFromState_1 = require("./useFromState");
const BTC = (0, cryptoassets_1.getCryptoCurrencyById)("bitcoin");
const ETH = (0, cryptoassets_1.getCryptoCurrencyById)("ethereum");
const USDT = (0, cryptoassets_1.getTokenById)("ethereum/erc20/usd_tether__erc20_");
jest.useFakeTimers();
const mockedAccounts = [
    (0, account_2.genAccount)("mocked-account-1"),
    (0, account_2.genAccount)("mocked-account-2", {
        currency: ETH,
    }),
];
const mockedTokenAccount = (0, account_1.genTokenAccount)(1, mockedAccounts[1], USDT);
const allAccounts = [...mockedAccounts, mockedTokenAccount];
describe("useFromState", () => {
    test("call hook without arguments", () => {
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return (0, useFromState_1.useFromState)({
                bridgeTransaction,
            });
        });
        expect(result.current).toMatchObject({
            fromState: _1.selectorStateDefaultValues,
        });
    });
    test("call hook with default currency", () => {
        const defaultCurrency = BTC;
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return (0, useFromState_1.useFromState)({
                defaultCurrency,
                bridgeTransaction,
            });
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                currency: defaultCurrency,
            },
        });
    });
    test("call hook with default accounts", () => {
        const defaultAccount = mockedTokenAccount;
        const defaultParentAccount = mockedAccounts[1];
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return (0, useFromState_1.useFromState)({
                defaultAccount,
                defaultParentAccount,
                bridgeTransaction,
            });
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                account: defaultAccount,
                parentAccount: defaultParentAccount,
            },
        });
    });
    test("call hook and set the account and all the related properties", () => {
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return {
                ...(0, useFromState_1.useFromState)({
                    accounts: allAccounts,
                    bridgeTransaction,
                }),
                bridgeTransaction,
            };
        });
        expect(result.current).toMatchObject({
            fromState: _1.selectorStateDefaultValues,
        });
        (0, react_1.act)(() => {
            result.current.setFromAccount(mockedTokenAccount);
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                account: mockedTokenAccount,
                parentAccount: mockedAccounts[1],
                currency: USDT,
            },
        });
        expect(result.current.bridgeTransaction).toMatchObject({
            account: mockedTokenAccount,
            parentAccount: mockedAccounts[1],
        });
    });
    test("call hook and set the amount after 400ms", () => {
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return (0, useFromState_1.useFromState)({
                accounts: allAccounts,
                bridgeTransaction,
            });
        });
        expect(result.current).toMatchObject({
            fromState: _1.selectorStateDefaultValues,
        });
        (0, react_1.act)(() => {
            result.current.setFromAmount(new bignumber_js_1.default(10));
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: undefined,
            },
        });
        (0, react_1.act)(() => {
            jest.advanceTimersByTime(400);
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: new bignumber_js_1.default(10),
            },
        });
    });
    test("call hook and set the the most recent amount input after 400ms", () => {
        const { result } = (0, react_1.renderHook)(() => {
            const bridgeTransaction = (0, useBridgeTransaction_1.default)();
            return (0, useFromState_1.useFromState)({
                accounts: allAccounts,
                bridgeTransaction,
            });
        });
        expect(result.current).toMatchObject({
            fromState: _1.selectorStateDefaultValues,
        });
        (0, react_1.act)(() => {
            result.current.setFromAmount(new bignumber_js_1.default(10));
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: undefined,
            },
        });
        (0, react_1.act)(() => {
            jest.advanceTimersByTime(300);
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: undefined,
            },
        });
        (0, react_1.act)(() => {
            result.current.setFromAmount(new bignumber_js_1.default(20));
            jest.advanceTimersByTime(100);
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: undefined,
            },
        });
        (0, react_1.act)(() => {
            jest.advanceTimersByTime(300);
        });
        expect(result.current).toMatchObject({
            fromState: {
                ..._1.selectorStateDefaultValues,
                amount: new bignumber_js_1.default(20),
            },
        });
    });
});
//# sourceMappingURL=useFromState.test.js.map