"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 react_1 = require("@testing-library/react");
const rxjs_1 = require("rxjs");
const devices_1 = require("@ledgerhq/devices");
const hw_transport_1 = __importDefault(require("@ledgerhq/hw-transport"));
const deviceAccess_1 = require("../../hw/deviceAccess");
const getVersionUseCase_1 = require("../../device/use-cases/getVersionUseCase");
const useBleDevicePairing_1 = require("./useBleDevicePairing");
jest.mock("../../hw/deviceAccess");
jest.mock("../../device/use-cases/getVersionUseCase");
jest.mock("@ledgerhq/hw-transport");
jest.useFakeTimers();
const mockedGetVersion = jest.mocked(getVersionUseCase_1.getVersion);
const mockedWithDevice = jest.mocked(deviceAccess_1.withDevice);
mockedWithDevice.mockReturnValue(job => (0, rxjs_1.from)(job(new hw_transport_1.default())));
const aFirmwareInfo = {
    isBootloader: false,
    rawVersion: "",
    targetId: 0,
    mcuVersion: "",
    flags: Buffer.from([]),
};
const aDevice = {
    deviceId: "DEVICE_ID_A",
    deviceName: "DEVICE_NAME_A",
    modelId: devices_1.DeviceModelId.stax,
    wired: false,
};
// FIXME: once we have our own definition of BleError class, this won't be needed
class BleError extends Error {
    errorCode;
    constructor(message, errorCode) {
        super(message);
        this.errorCode = errorCode;
    }
}
describe("useBleDevicePairing", () => {
    afterEach(() => {
        mockedGetVersion.mockClear();
        jest.clearAllTimers();
    });
    describe("When the request sent with the BLE transport to the device gets a success response", () => {
        beforeEach(() => {
            mockedGetVersion.mockResolvedValue(aFirmwareInfo);
        });
        it("should inform the hook consumer that the device is paired", async () => {
            const { result } = (0, react_1.renderHook)(() => (0, useBleDevicePairing_1.useBleDevicePairing)({
                deviceId: aDevice.deviceId,
            }));
            await (0, react_1.act)(async () => {
                jest.advanceTimersByTime(1);
            });
            expect(result.current.isPaired).toBe(true);
            expect(result.current.pairingError).toBeNull();
        });
    });
    describe("When the request sent with the BLE transport is rejected with an error", () => {
        beforeEach(() => {
            const bleError = new BleError("An error during pairing", 201);
            mockedGetVersion.mockRejectedValue(bleError);
        });
        it("should inform the hook consumer an error occurred", async () => {
            const { result } = (0, react_1.renderHook)(() => (0, useBleDevicePairing_1.useBleDevicePairing)({
                deviceId: aDevice.deviceId,
            }));
            await (0, react_1.act)(async () => {
                jest.advanceTimersByTime(1);
            });
            expect(result.current.isPaired).toBe(false);
            expect(result.current.pairingError).not.toBeNull();
        });
    });
});
//# sourceMappingURL=useBleDevicePairing.test.js.map