Skip to content

Commit

Permalink
test(common/listApps/v2): custom lock screen size
Browse files Browse the repository at this point in the history
  • Loading branch information
ofreyssinet-ledger committed May 17, 2024
1 parent f8980de commit be28e8d
Showing 1 changed file with 76 additions and 21 deletions.
97 changes: 76 additions & 21 deletions libs/ledger-live-common/src/apps/listApps/v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ import {
ManagerApiRepository,
StubManagerApiRepository,
} from "../../device/factories/HttpManagerApiRepositoryFactory";
import { supportedDeviceModelIds as clsSupportedDeviceModelIds } from "../../device/use-cases/isCustomLockScreenSupported";
import { DeviceModel } from "@ledgerhq/devices";
import customLockScreenFetchSize from "../../hw/customLockScreenFetchSize";
import { getDeviceName } from "../../device/use-cases/getDeviceNameUseCase";
import { currenciesByMarketcap, listCryptoCurrencies } from "../../currencies";

jest.useFakeTimers();
jest.mock("../../hw/customLockScreenFetchSize");
jest.mock("../../device/use-cases/getDeviceNameUseCase");
jest.mock("../../currencies");

const mockedCustomLockScreenFetchSize = jest.mocked(customLockScreenFetchSize);
const mockedGetDeviceName = jest.mocked(getDeviceName);
const mockedListCryptoCurrencies = jest.mocked(listCryptoCurrencies);
const mockedCurrenciesByMarketCap = jest.mocked(currenciesByMarketcap);

describe("listApps v2", () => {
let mockedManagerApiRepository: ManagerApiRepository;
let listAppsCommandSpy: jest.SpyInstance;
let listInstalledAppsSpy: jest.SpyInstance;

beforeEach(() => {
jest
Expand All @@ -21,13 +36,21 @@ describe("listApps v2", () => {
"getLatestFirmwareForDeviceUseCase",
)
.mockReturnValue(Promise.resolve(null));

mockedManagerApiRepository = new StubManagerApiRepository();
mockedGetDeviceName.mockReturnValue(Promise.resolve("Mocked device name"));
mockedCurrenciesByMarketCap.mockReturnValue(Promise.resolve([]));
mockedListCryptoCurrencies.mockReturnValue([]);

listAppsCommandSpy = jest
.spyOn(jest.requireActual("../../hw/listApps"), "default")
.mockReturnValue(Promise.resolve([]));

listInstalledAppsSpy = jest.spyOn(ManagerAPI, "listInstalledApps").mockReturnValue(from([]));
});

afterEach(() => {
jest.clearAllTimers();
jest.clearAllMocks();
jest.restoreAllMocks();
});

it("should return an observable that errors if deviceInfo.isOSU is true", done => {
Expand All @@ -45,7 +68,7 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

Expand All @@ -67,7 +90,7 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

Expand All @@ -89,19 +112,14 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

jest.advanceTimersByTime(1);
});

it("should call hwListApps() if deviceInfo.managerAllowed is true", done => {
const listAppsCommandSpy = jest
.spyOn(jest.requireActual("../../hw/listApps"), "default")
.mockReturnValue(Promise.resolve([]));
const listInstalledAppsSpy = jest.spyOn(ManagerAPI, "listInstalledApps");

const transport = aTransportBuilder();
const deviceInfo = aDeviceInfoBuilder({
isOSU: false,
Expand Down Expand Up @@ -131,11 +149,6 @@ describe("listApps v2", () => {
});

it("should call ManagerAPI.listInstalledApps() if deviceInfo.managerAllowed is false", () => {
const listAppsCommandSpy = jest.spyOn(jest.requireActual("../../hw/listApps"), "default");
const listInstalledAppsSpy = jest
.spyOn(ManagerAPI, "listInstalledApps")
.mockReturnValue(from([]));

const transport = aTransportBuilder();
const deviceInfo = aDeviceInfoBuilder({
isOSU: false,
Expand All @@ -157,7 +170,6 @@ describe("listApps v2", () => {
});

it("should return an observable that errors if getDeviceVersion() throws", done => {
jest.spyOn(ManagerAPI, "listInstalledApps").mockReturnValue(from([]));
jest.spyOn(mockedManagerApiRepository, "getDeviceVersion").mockImplementation(() => {
throw new Error("getDeviceVersion failed");
});
Expand All @@ -181,15 +193,14 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

jest.advanceTimersByTime(1);
});

it("should return an observable that errors if catalogForDevice() throws", done => {
jest.spyOn(ManagerAPI, "listInstalledApps").mockReturnValue(from([]));
jest.spyOn(mockedManagerApiRepository, "catalogForDevice").mockImplementation(() => {
throw new Error("catalogForDevice failed");
});
Expand All @@ -213,15 +224,14 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

jest.advanceTimersByTime(1);
});

it("should return an observable that errors if getLanguagePackagesForDevice() throws", done => {
jest.spyOn(ManagerAPI, "listInstalledApps").mockReturnValue(from([]));
jest
.spyOn(mockedManagerApiRepository, "getLanguagePackagesForDevice")
.mockImplementation(() => {
Expand All @@ -247,10 +257,55 @@ describe("listApps v2", () => {
done();
},
complete: () => {
fail("this observable should not complete");
done("this observable should not complete");
},
});

jest.advanceTimersByTime(1);
});

clsSupportedDeviceModelIds.forEach(deviceModelId => {
it(`should return customImageBlocks different than 0 for a ${deviceModelId} device with a custom lock screen`, done => {
const transport = aTransportBuilder({ deviceModel: { id: deviceModelId } as DeviceModel });
const deviceInfo = aDeviceInfoBuilder({
isOSU: false,
isBootloader: false,
managerAllowed: true,
targetId: 0x33200000,
});

mockedCustomLockScreenFetchSize.mockReturnValue(Promise.resolve(10));

let gotResult = false;

listApps({
transport,
deviceInfo,
managerApiRepository: mockedManagerApiRepository,
forceProvider: 1,
}).subscribe({
next: listAppsEvent => {
if (listAppsEvent.type === "result") {
gotResult = true;
const {
result: { customImageBlocks },
} = listAppsEvent;
try {
expect(customImageBlocks).not.toBe(0);
} catch (e) {
done(e);
}
}
},
complete: () => {
gotResult ? done() : done("this observable should not complete without a result");
},
error: error => {
done(error);
},
});

jest.advanceTimersByTime(1);
});
});
});

0 comments on commit be28e8d

Please sign in to comment.