@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
206 lines • 7.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const account_1 = require("../../mock/account");
const server_1 = require("./server");
const mockTracking = {
startExchangeRequested: jest.fn(),
startExchangeSuccess: jest.fn(),
startExchangeFail: jest.fn(),
startExchangeNoParams: jest.fn(),
completeExchangeRequested: jest.fn(),
completeExchangeSuccess: jest.fn(),
completeExchangeFail: jest.fn(),
completeExchangeNoParams: jest.fn(),
};
const testAppManifest = {
id: "12",
name: "test",
url: "localhost",
homepageUrl: "localhost",
platforms: ["desktop"],
apiVersion: "2",
manifestVersion: "2",
branch: "debug",
permissions: [],
domains: [],
categories: [],
currencies: ["btc"],
visibility: "complete",
content: {
shortDescription: {
en: "desc",
},
description: {
en: "description",
},
},
};
const mockUiStartExchange = jest.fn();
const mockUiCompleteExchange = jest.fn();
const mockUiSwap = jest.fn();
const mockUiError = jest.fn();
const mockIsReady = jest.fn();
const mockUiHooks = {
"custom.exchange.start": mockUiStartExchange,
"custom.exchange.complete": mockUiCompleteExchange,
"custom.exchange.error": mockUiError,
"custom.isReady": mockIsReady,
"custom.exchange.swap": mockUiSwap,
};
// Mock converter id to send back the id received in params.
jest.mock("../converters", () => {
return {
getAccountIdFromWalletAccountId: (val) => val,
};
});
describe("handlers", () => {
describe("custom.exchange.start", () => {
beforeEach(() => {
mockUiStartExchange.mockClear();
});
it("calls SWAP with correct infos", async () => {
// Given
const accounts = [(0, account_1.genAccount)("accountId1"), (0, account_1.genAccount)("accountId2")];
const handler = (0, server_1.handlers)({
accounts,
tracking: mockTracking,
manifest: testAppManifest,
uiHooks: mockUiHooks,
});
const params = {
exchangeType: "SWAP",
provider: "TestProvider",
fromAccountId: accounts[0].id,
toAccountId: accounts[1].id,
tokenCurrency: undefined,
};
const { request, context, walletHandlers } = prepareSwapRequest(params);
mockUiStartExchange.mockImplementation(params => {
params.onSuccess("NONCE");
});
// When
const result = await handler["custom.exchange.start"](request, context, walletHandlers);
// Then
expect(result).toEqual({ transactionId: "NONCE" });
expect(mockUiStartExchange).toHaveBeenCalledTimes(1);
const receivedParams = mockUiStartExchange.mock.calls[0][0].exchangeParams;
expect(receivedParams.exchangeType).toBe("SWAP");
expect(receivedParams.provider).toBe("TestProvider");
expect(receivedParams.exchange.fromAccount).toBe(accounts[0]);
expect(receivedParams.exchange.fromParentAccount).toBe(accounts[0]);
expect(receivedParams.exchange.toAccount).toBe(accounts[1]);
expect(receivedParams.exchange.toParentAccount).toBe(accounts[1]);
expect(mockUiCompleteExchange).not.toHaveBeenCalled();
});
it("calls SELL with correct infos", async () => {
// Given
const accounts = [(0, account_1.genAccount)("accountId1"), (0, account_1.genAccount)("accountId2")];
const handler = (0, server_1.handlers)({
accounts,
tracking: mockTracking,
manifest: testAppManifest,
uiHooks: mockUiHooks,
});
const params = {
exchangeType: "SELL",
provider: "TestSellProvider",
fromAccountId: accounts[0].id,
};
const { request, context, walletHandlers } = prepareSellRequest(params);
mockUiStartExchange.mockImplementation(params => {
params.onSuccess("NONCE");
});
// When
const result = await handler["custom.exchange.start"](request, context, walletHandlers);
// Then
expect(result).toEqual({ transactionId: "NONCE" });
expect(mockUiStartExchange).toHaveBeenCalledTimes(1);
const receivedParams = mockUiStartExchange.mock.calls[0][0].exchangeParams;
expect(receivedParams.exchangeType).toBe("SELL");
expect(receivedParams.provider).toBe("TestSellProvider");
expect(mockUiCompleteExchange).not.toHaveBeenCalled();
});
it("calls FUND with correct infos", async () => {
// Given
const accounts = [(0, account_1.genAccount)("accountId1"), (0, account_1.genAccount)("accountId2")];
const handler = (0, server_1.handlers)({
accounts,
tracking: mockTracking,
manifest: testAppManifest,
uiHooks: mockUiHooks,
});
const params = {
exchangeType: "FUND",
provider: "TestFundProvider",
fromAccountId: accounts[0].id,
};
const { request, context, walletHandlers } = prepareSellRequest(params);
mockUiStartExchange.mockImplementation(params => {
params.onSuccess("NONCE");
});
// When
const result = await handler["custom.exchange.start"](request, context, walletHandlers);
// Then
expect(result).toEqual({ transactionId: "NONCE" });
expect(mockUiStartExchange).toHaveBeenCalledTimes(1);
const receivedParams = mockUiStartExchange.mock.calls[0][0].exchangeParams;
expect(receivedParams.exchangeType).toBe("FUND");
expect(receivedParams.provider).toBe("TestFundProvider");
expect(mockUiCompleteExchange).not.toHaveBeenCalled();
});
});
});
function prepareSwapRequest(params) {
const request = {
jsonrpc: "2.0",
method: "custom.exchange.start",
params: params,
id: "jsonRpcRequestId",
};
const context = {
currencies$: (0, rxjs_1.of)([]),
accounts$: (0, rxjs_1.of)([]),
config: {
userId: "userId",
tracking: false,
wallet: {
name: "wallet name",
version: "2",
},
appId: "appId",
},
};
return {
request,
context,
walletHandlers: {},
};
}
function prepareSellRequest(params) {
const request = {
jsonrpc: "2.0",
method: "custom.exchange.start",
params: params,
id: "jsonRpcRequestId",
};
const context = {
currencies$: (0, rxjs_1.of)([]),
accounts$: (0, rxjs_1.of)([]),
config: {
userId: "userId",
tracking: false,
wallet: {
name: "wallet name",
version: "2",
},
appId: "appId",
},
};
return {
request,
context,
walletHandlers: {},
};
}
//# sourceMappingURL=server.test.js.map