UNPKG

@ledgerhq/live-common

Version:
102 lines 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const backupAppDataUseCase_1 = require("./backupAppDataUseCase"); const rxjs_1 = require("rxjs"); const types_1 = require("./types"); const devices_1 = require("@ledgerhq/devices"); describe("backupAppDataUseCase", () => { const storageProviderMock = { getItem: jest.fn(), setItem: jest.fn(), removeItem: jest.fn(), }; const appName = "MyApp"; const deviceModelId = devices_1.DeviceModelId.stax; it("should transfer the AppDataInfoFetched event when no backup found", async () => { const backupAppDataFnMock = jest.fn(() => (0, rxjs_1.of)({ type: types_1.BackupAppDataEventType.AppDataInfoFetched, data: {}, })); const expectedEvent = { type: types_1.BackupAppDataEventType.AppDataInfoFetched, data: {}, }; const backupAppDataUseCaseObservable = (0, backupAppDataUseCase_1.backupAppDataUseCase)(appName, deviceModelId, storageProviderMock, backupAppDataFnMock); const firstValue = await (0, rxjs_1.firstValueFrom)(backupAppDataUseCaseObservable); expect(firstValue).toEqual(expectedEvent); }); it("should return AppDataAlreadyBackedUp event when backup found in storage", async () => { jest.spyOn(storageProviderMock, "getItem").mockResolvedValue({ appDataInfo: { hash: "hashfortest" }, }); const backupAppDataFnMock = jest.fn(() => (0, rxjs_1.of)({ type: types_1.BackupAppDataEventType.AppDataInfoFetched, data: { hash: "hashfortest" }, })); const expectedEvent = { type: types_1.BackupAppDataEventType.AppDataAlreadyBackedUp, }; const backupAppDataUseCaseObservable = (0, backupAppDataUseCase_1.backupAppDataUseCase)(appName, deviceModelId, storageProviderMock, backupAppDataFnMock); const firstValue = await (0, rxjs_1.firstValueFrom)(backupAppDataUseCaseObservable); expect(firstValue).toEqual(expectedEvent); }); it("should store the app data and return AppDataBackedUp event with empty string as data", async () => { const backupAppDataFnMock = jest.fn().mockReturnValue((0, rxjs_1.concat)((0, rxjs_1.of)({ type: types_1.BackupAppDataEventType.AppDataInfoFetched, data: { size: 100 }, }), (0, rxjs_1.of)({ type: types_1.BackupAppDataEventType.AppDataBackedUp, data: "mockeddatafortest", }).pipe((0, rxjs_1.delay)(3)))); const expectedEvent = { type: types_1.BackupAppDataEventType.AppDataBackedUp, data: "", }; const backupAppDataUseCaseObservable = (0, backupAppDataUseCase_1.backupAppDataUseCase)(appName, deviceModelId, storageProviderMock, backupAppDataFnMock); // Initial event should be AppDataInfoFetched const firstValue = await (0, rxjs_1.firstValueFrom)(backupAppDataUseCaseObservable); expect(firstValue).toEqual({ type: types_1.BackupAppDataEventType.AppDataInfoFetched, data: { size: 100 }, }); // test store the app data const lastValue = await (0, rxjs_1.lastValueFrom)(backupAppDataUseCaseObservable); expect(storageProviderMock.setItem).toHaveBeenCalledWith("stax-MyApp", { appDataInfo: { size: 100 }, appData: "mockeddatafortest", }); expect(lastValue).toEqual(expectedEvent); }); it("should throw an error when an invalid event type is received", async () => { const backupAppDataFnMock = jest.fn(() => (0, rxjs_1.of)({ type: "InvalidEventType", })); const backupAppDataUseCaseObservable = (0, backupAppDataUseCase_1.backupAppDataUseCase)(appName, deviceModelId, storageProviderMock, backupAppDataFnMock); await (0, rxjs_1.firstValueFrom)(backupAppDataUseCaseObservable).catch(e => { expect(e).toBeInstanceOf(types_1.BackupAppDataError); expect(e.message).toBe("Invalid event type"); }); }); it("should transfer the events when NoAppDataToBackup / Progress event received ", async () => { const expectedEvents = [ { type: types_1.BackupAppDataEventType.Progress, data: 0.5, }, { type: types_1.BackupAppDataEventType.Progress, data: 0.75, }, { type: types_1.BackupAppDataEventType.NoAppDataToBackup, }, ]; for (const event of expectedEvents) { const backupAppDataFnMock = jest.fn(() => (0, rxjs_1.of)(event)); const backupAppDataUseCaseObservable = (0, backupAppDataUseCase_1.backupAppDataUseCase)(appName, deviceModelId, storageProviderMock, backupAppDataFnMock); const firstValue = await (0, rxjs_1.firstValueFrom)(backupAppDataUseCaseObservable); expect(firstValue).toEqual(event); } }); }); //# sourceMappingURL=backupAppDataUseCase.test.js.map