UNPKG

@ledgerhq/coin-stellar

Version:
130 lines 5.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const msw_1 = require("msw"); const config_1 = __importDefault(require("../config")); const currencies_1 = require("@ledgerhq/cryptoassets/lib/currencies"); const synchronization_1 = require("./synchronization"); const msw_handles_fixture_1 = require("./msw-handles.fixture"); const generators_fixture_1 = require("./generators.fixture"); describe("getAccountShape", () => { beforeAll(() => { config_1.default.setCoinConfig(() => ({ status: { type: "active" }, explorer: { url: msw_handles_fixture_1.HORIZON_TEST_BASE_URL, fetchLimit: 100, }, })); msw_handles_fixture_1.mockServer.listen({ onUnhandledRequest: "error" }); }); beforeEach(() => { let requestCount = 0; // track the number of requests msw_handles_fixture_1.mockServer.resetHandlers(); msw_handles_fixture_1.mockServer.use(...msw_handles_fixture_1.handles, msw_1.http.get(`${msw_handles_fixture_1.HORIZON_TEST_BASE_URL}/accounts/:addr/operations`, ({ params, request }) => { const addr = params.addr; const url = new URL(request.url); const order = url.searchParams.get("order"); let operationsCount = 100; // init sync, reach the threshold for fetching operations if (addr == "GC65W5WLL7FN6WASRY5RCQAFF2CM75RBY2A6G7GF6UP244PVPUFBLIB6") { if (order !== "desc") { return msw_1.HttpResponse.json({}, { status: 400 }); } } // init sync, not reach the threshold for fetching operations if (addr == "GBRBXWKLSAE2JMVCJSPC2NJEOS33MLMQ6DPSBEK76DEB5A6Q3DR43BJC") { if (order !== "desc") { return msw_1.HttpResponse.json({}, { status: 400 }); } if (requestCount >= 3) { operationsCount = 0; } } // sync, fetch new operations after init sync if (addr == "GBYLYVN3JLX4CTJ7HRAME5MNT5BJ32JLYBI7M7R5TI35I3HR6DLU46CC") { switch (requestCount) { case 0: operationsCount = 80; break; case 2: { operationsCount = 10; if (order !== "asc") { return msw_1.HttpResponse.json({}, { status: 400 }); } } break; default: operationsCount = 0; } } requestCount++; const resp = { _links: { self: { href: `${msw_handles_fixture_1.HORIZON_TEST_BASE_URL}/accounts/${addr}/operations?order=${order}`, }, next: { href: `${msw_handles_fixture_1.HORIZON_TEST_BASE_URL}/accounts/${addr}/operations?order=${order}`, }, prev: { href: `${msw_handles_fixture_1.HORIZON_TEST_BASE_URL}/accounts/${addr}/operations?order=${order}`, }, }, _embedded: { records: (0, generators_fixture_1.generateOperationsList)(addr, operationsCount), }, }; return msw_1.HttpResponse.json(resp); })); }); afterAll(() => { msw_handles_fixture_1.mockServer.close(); }); it("init sync, reach the threshold for fetching operations", async () => { const info = { address: "GC65W5WLL7FN6WASRY5RCQAFF2CM75RBY2A6G7GF6UP244PVPUFBLIB6", currency: (0, currencies_1.getCryptoCurrencyById)("stellar"), derivationMode: "", index: 0, }; const syncConfig = {}; const account = await (0, synchronization_1.getAccountShape)(info, syncConfig); expect(account.operations?.length).toEqual(1000); }); it("init sync, not reach the threshold for fetching operations", async () => { const info = { address: "GBRBXWKLSAE2JMVCJSPC2NJEOS33MLMQ6DPSBEK76DEB5A6Q3DR43BJC", currency: (0, currencies_1.getCryptoCurrencyById)("stellar"), derivationMode: "", index: 0, }; const syncConfig = {}; const account = await (0, synchronization_1.getAccountShape)(info, syncConfig); expect(account.operations?.length).toEqual(300); }); it("sync, fetch new operations after init sync", async () => { const initInfo = { address: "GBYLYVN3JLX4CTJ7HRAME5MNT5BJ32JLYBI7M7R5TI35I3HR6DLU46CC", currency: (0, currencies_1.getCryptoCurrencyById)("stellar"), derivationMode: "", index: 0, }; const syncConfig = {}; const initAccount = await (0, synchronization_1.getAccountShape)(initInfo, syncConfig); expect(initAccount.operations?.length).toEqual(80); const info = { address: "GBYLYVN3JLX4CTJ7HRAME5MNT5BJ32JLYBI7M7R5TI35I3HR6DLU46CC", currency: (0, currencies_1.getCryptoCurrencyById)("stellar"), derivationMode: "", index: 0, initialAccount: initAccount, }; const account = await (0, synchronization_1.getAccountShape)(info, syncConfig); expect(account.operations?.length).toEqual(90); }); }); //# sourceMappingURL=synchronization.unit.test.js.map