"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
 */
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const react_1 = require("@testing-library/react");
const index_1 = require("@ledgerhq/coin-evm/network/gasTracker/index");
const react_2 = require("./react");
jest.useFakeTimers();
jest.spyOn(global, "setInterval");
jest.mock("@ledgerhq/coin-evm/network/gasTracker/index");
const mockedGetGasTracker = jest.mocked(index_1.getGasTracker);
const mockedGetGasOptions = jest.fn();
const fakeCurrency = {
    id: "my_new_chain",
    ethereumLikeInfo: {
        chainId: 1,
    },
    units: [{ code: "ETH", name: "ETH", magnitude: 18 }],
};
const coinTransaction = {
    amount: new bignumber_js_1.default(100),
    useAllAmount: false,
    subAccountId: "id",
    recipient: "0x997e135e96114c0E84FFc58754552368E4abf329",
    feesStrategy: "custom",
    family: "evm",
    mode: "send",
    nonce: 0,
    gasLimit: new bignumber_js_1.default(21000),
    chainId: 1,
    maxFeePerGas: new bignumber_js_1.default(100),
    maxPriorityFeePerGas: new bignumber_js_1.default(100),
    type: 2,
};
const expectedGasOptions = {
    slow: {
        maxFeePerGas: new bignumber_js_1.default(9),
        maxPriorityFeePerGas: new bignumber_js_1.default(1),
        gasPrice: null,
        nextBaseFee: new bignumber_js_1.default(4),
    },
    medium: {
        maxFeePerGas: new bignumber_js_1.default(10),
        maxPriorityFeePerGas: new bignumber_js_1.default(2),
        gasPrice: null,
        nextBaseFee: new bignumber_js_1.default(4),
    },
    fast: {
        maxFeePerGas: new bignumber_js_1.default(11),
        maxPriorityFeePerGas: new bignumber_js_1.default(3),
        gasPrice: null,
        nextBaseFee: new bignumber_js_1.default(4),
    },
};
describe("useGasOptions", () => {
    beforeEach(() => {
        mockedGetGasTracker.mockImplementation(() => ({ getGasOptions: mockedGetGasOptions }));
        mockedGetGasOptions.mockReturnValue(Promise.resolve(expectedGasOptions));
    });
    afterEach(() => {
        mockedGetGasTracker.mockReset();
        mockedGetGasOptions.mockReset();
    });
    test("call hook with interval = 0", async () => {
        const { result } = (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: 0,
            });
        });
        expect(result.current).toMatchObject([undefined, null, true]);
        await (0, react_1.waitFor)(() => {
            expect(mockedGetGasTracker).toHaveBeenCalledTimes(1);
            expect(mockedGetGasTracker).toHaveReturnedWith({
                getGasOptions: mockedGetGasOptions,
            });
            expect(mockedGetGasOptions).toHaveBeenCalledTimes(1);
            expect(setInterval).toHaveBeenCalledTimes(0);
            expect(result.current).toMatchObject([expectedGasOptions, null, false]);
        });
    });
    test("call hook with interval < 0", async () => {
        const { result } = (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: -1,
            });
        });
        expect(result.current).toMatchObject([undefined, null, true]);
        await (0, react_1.waitFor)(() => {
            expect(mockedGetGasTracker).toHaveBeenCalledTimes(1);
            expect(mockedGetGasTracker).toHaveReturnedWith({
                getGasOptions: mockedGetGasOptions,
            });
            expect(mockedGetGasOptions).toHaveBeenCalledTimes(1);
            expect(setInterval).toHaveBeenCalledTimes(0);
            expect(result.current).toMatchObject([expectedGasOptions, null, false]);
        });
    });
    test("call hook with interval > 0", async () => {
        const { result } = (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: 100,
            });
        });
        expect(result.current).toMatchObject([undefined, null, true]);
        jest.advanceTimersByTime(150);
        await (0, react_1.waitFor)(() => {
            expect(result.current).toMatchObject([expectedGasOptions, null, false]);
            expect(mockedGetGasTracker).toHaveReturnedWith({
                getGasOptions: mockedGetGasOptions,
            });
        });
    });
    test("should use EIP-1559 when transaction is type 2", async () => {
        (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: 0,
            });
        });
        await (0, react_1.waitFor)(() => expect(mockedGetGasOptions).toHaveBeenCalledWith({
            currency: fakeCurrency,
            options: { useEIP1559: true },
        }));
    });
    test("should not use EIP-1559 when transaction is not type 2", async () => {
        (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: {
                    ...coinTransaction,
                    type: 0,
                    gasPrice: new bignumber_js_1.default(10),
                    maxFeePerGas: undefined,
                    maxPriorityFeePerGas: undefined,
                },
                interval: 0,
            });
        });
        await (0, react_1.waitFor)(() => expect(mockedGetGasOptions).toHaveBeenCalledWith({
            currency: fakeCurrency,
            options: { useEIP1559: false },
        }));
    });
    test("should not return gasOption if can't get gas tracker", async () => {
        mockedGetGasTracker.mockImplementationOnce(() => null);
        const { result } = (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: 0,
            });
        });
        expect(mockedGetGasTracker).toHaveBeenCalledTimes(1);
        expect(result.current).toMatchObject([undefined, null, false]);
    });
    test("should return error if getGasOptions throws", async () => {
        const expectedError = new Error("error");
        mockedGetGasOptions.mockReset();
        mockedGetGasOptions.mockReturnValueOnce(Promise.reject(expectedError));
        const { result } = (0, react_1.renderHook)(() => {
            return (0, react_2.useGasOptions)({
                currency: fakeCurrency,
                transaction: coinTransaction,
                interval: 0,
            });
        });
        expect(result.current).toMatchObject([undefined, null, true]);
        await (0, react_1.waitFor)(() => expect(result.current).toMatchObject([undefined, expectedError, false]));
    });
});
//# sourceMappingURL=react.test.js.map