"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const types_1 = require("./types");
const devices_1 = require("@ledgerhq/devices");
const restoreAppDataUseCase_1 = require("./restoreAppDataUseCase");
describe("restoreAppDataUseCase", () => {
    const storageProviderMock = {
        getItem: jest.fn().mockReturnValue(Promise.resolve("data")),
        setItem: jest.fn(),
        removeItem: jest.fn(),
    };
    const appName = "MyApp";
    const deviceModelId = devices_1.DeviceModelId.stax;
    let restoreAppDataFnMock;
    it("should transfer the events when backup data found", done => {
        restoreAppDataFnMock = jest.fn().mockImplementation(() => {
            return new rxjs_1.Observable(subscriber => {
                subscriber.next({
                    type: types_1.RestoreAppDataEventType.AppDataInitialized,
                });
                subscriber.next({
                    type: types_1.RestoreAppDataEventType.Progress,
                    data: 0.25,
                });
                subscriber.next({
                    type: types_1.RestoreAppDataEventType.AppDataRestored,
                });
                subscriber.complete();
            });
        });
        const expectedEvents = [
            {
                type: types_1.RestoreAppDataEventType.AppDataInitialized,
            },
            {
                type: types_1.RestoreAppDataEventType.Progress,
                data: 0.25,
            },
            {
                type: types_1.RestoreAppDataEventType.AppDataRestored,
            },
        ];
        const events = [];
        (0, restoreAppDataUseCase_1.restoreAppDataUseCase)(appName, deviceModelId, storageProviderMock, restoreAppDataFnMock).subscribe({
            next: event => {
                events.push(event);
            },
            complete: () => {
                try {
                    expect(events).toHaveLength(expectedEvents.length);
                    expect(events).toEqual(expectedEvents);
                    done();
                }
                catch (e) {
                    done(e);
                }
            },
            error: (e) => {
                done(e);
            },
        });
    });
    it("should throw an error when backup data not found", async () => {
        const restoreAppDataFnMock = jest.fn(() => (0, rxjs_1.of)({}));
        jest.spyOn(storageProviderMock, "getItem").mockResolvedValue(null);
        const restoreAppDataUseCaseObservable = (0, restoreAppDataUseCase_1.restoreAppDataUseCase)(appName, deviceModelId, storageProviderMock, restoreAppDataFnMock);
        await (0, rxjs_1.firstValueFrom)(restoreAppDataUseCaseObservable).catch(e => {
            expect(e).toBeInstanceOf(types_1.RestoreAppDataError);
            expect(e.message).toBe("No backed up data found");
        });
    });
});
//# sourceMappingURL=restoreAppDataUseCase.test.js.map