UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

175 lines 9.35 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { server } from "../../../mocks/servers"; import { exampleOperation, mockBuffer, expectedTransactionInfo, mockConfirmationProof, mockStringFaultyBrid, mockThirtyTwoBytesBuffer, } from "../../common/mocks"; import { errorHandler } from "../../../mocks/handlers"; import { blockInfoData } from "../../../resources/testData"; import { ResponseStatus } from "../../../src/blockchainClient/enums"; import { UnexpectedStatusError } from "../../../src/blockchainClient/errors"; import * as utils from "../../../src/blockchainClient/utils"; import * as httpUtils from "../../../src/blockchainClient/httpUtil"; import { http, HttpResponse } from "msw"; import { getTestsClient } from "../../common/setups"; let client; describe("Blockchain client", () => { beforeAll(() => __awaiter(void 0, void 0, void 0, function* () { server.listen(); client = yield getTestsClient(); })); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { jest.clearAllMocks(); jest.spyOn(utils, "getAnchoringClientAndSystemChainRid").mockResolvedValue({ anchoringClient: client, systemAnchoringChainBridString: client.config.blockchainRid, }); })); afterEach(() => __awaiter(void 0, void 0, void 0, function* () { jest.restoreAllMocks(); server.resetHandlers(); })); it("throws an error when blockchainRid is invalid", () => __awaiter(void 0, void 0, void 0, function* () { const clientFaultyBrid = yield getTestsClient({ blockchainRid: mockStringFaultyBrid }); server.use(errorHandler.getTransactionInfo); yield expect(clientFaultyBrid.getTransactionInfo(mockBuffer)).rejects.toThrow(new UnexpectedStatusError(404, `{"error":"Can't find blockchain with blockchainRID: ${mockStringFaultyBrid}"}`)); })); it("fetches a transaction correctly once a transaction is sent and Dapp Confirmed", () => __awaiter(void 0, void 0, void 0, function* () { const response = yield client.sendTransaction(exampleOperation); expect(response).toEqual({ status: ResponseStatus.Confirmed, statusCode: 200, transactionRid: mockBuffer, }); })); it("fetches the latest transaction when the before time is now and limit is 1", () => __awaiter(void 0, void 0, void 0, function* () { yield client.sendTransaction(exampleOperation); const transactionsInfo = yield client.getTransactionsInfo(1, new Date()); expect(transactionsInfo).toEqual([expectedTransactionInfo]); })); describe("getBlockInfo", () => { it("gets a block and strings are formatted as buffers", () => __awaiter(void 0, void 0, void 0, function* () { const block = yield client.getBlockInfo(1); expect(block).toEqual(blockInfoData); })); }); describe("getLatestBlock", () => { it("get latest block", () => __awaiter(void 0, void 0, void 0, function* () { const info = yield client.getLatestBlock(); expect(info).toEqual(blockInfoData); })); }); describe("getTransactionInfo", () => { it("returns transaction info for client blockchain", () => __awaiter(void 0, void 0, void 0, function* () { const transactionReceipt = yield client.sendTransaction(exampleOperation); const transactionInfo = yield client.getTransactionInfo(transactionReceipt.transactionRid); expect(transactionInfo).toEqual(expectedTransactionInfo); })); }); describe("getTransactionCount", () => { it("fetches total number of transactions for a dapp", () => __awaiter(void 0, void 0, void 0, function* () { const transactionsCount = yield client.getTransactionCount(); expect(transactionsCount).toEqual(0); })); }); describe("getTransactionStatus", () => { it("returns Waiting status", () => __awaiter(void 0, void 0, void 0, function* () { const transactionStatus = { statusCode: 200, status: ResponseStatus.Waiting, }; jest.spyOn(client, "getTransactionStatus").mockResolvedValue(transactionStatus); const result = yield client.getTransactionStatus(mockBuffer); expect(result).toEqual(transactionStatus); })); it("getTransactionStatus returns Confirmed status", () => __awaiter(void 0, void 0, void 0, function* () { const transactionStatus = { status: ResponseStatus.Confirmed, statusCode: 200, transactionRid: mockBuffer, }; jest.spyOn(client, "getTransactionStatus").mockResolvedValue(transactionStatus); const result = yield client.getTransactionStatus(mockBuffer); expect(result).toEqual(transactionStatus); })); }); describe("getConfirmationProof", () => { it("after transaction was send", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(client, "getConfirmationProof").mockResolvedValue(mockConfirmationProof); jest.spyOn(client, "sendTransaction").mockResolvedValue({ status: ResponseStatus.Confirmed, statusCode: 200, transactionRid: mockBuffer, }); const confirmationProof = yield client.getConfirmationProof(mockBuffer); expect(confirmationProof).toEqual(mockConfirmationProof); })); }); describe("getTransaction", () => { it("returns a promise", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(client, "sendTransaction").mockResolvedValue({ status: ResponseStatus.Confirmed, statusCode: 200, transactionRid: mockBuffer, }); const promise = client.getTransaction(mockBuffer); expect(typeof promise.then).toBe("function"); })); it("gets transaction with a specific txRID", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield client.getTransaction(mockBuffer); expect(result).toBeInstanceOf(Buffer); expect(result).toEqual(mockThirtyTwoBytesBuffer); })); it("gets signed transaction with a specific txRID", () => __awaiter(void 0, void 0, void 0, function* () { jest.spyOn(client, "signTransaction").mockResolvedValue(mockBuffer); jest.spyOn(client, "sendTransaction").mockResolvedValue({ status: ResponseStatus.Confirmed, statusCode: 200, transactionRid: mockBuffer, }); const result = yield client.getTransaction(mockBuffer); expect(result).toBeInstanceOf(Buffer); expect(result).toEqual(mockThirtyTwoBytesBuffer); })); it("retries default number of times", () => __awaiter(void 0, void 0, void 0, function* () { const handleRequestSpy = jest.spyOn(httpUtils, "handleRequest"); try { yield client.getTransaction(mockBuffer); } catch (error) { expect(error).not.toBeNull(); expect(handleRequestSpy).toHaveBeenCalledTimes(9); } handleRequestSpy.mockRestore(); })); it("uses default attemptInterval of 500", () => __awaiter(void 0, void 0, void 0, function* () { const invalidNodeUrlPoolClient = yield getTestsClient({ nodeUrlPool: ["http://localhost1:7740", "http://localhost2:7740", "http://localhost3:7740"], failOverConfig: { attemptsPerEndpoint: 1, }, }); server.use(http.get(`http://localhost1:7740/tx/:blockchainRid/:transactionRid`, () => { return new HttpResponse("", { status: 500 }); }), http.get(`http://localhost2:7740/tx/:blockchainRid/:transactionRid`, () => { return new HttpResponse("", { status: 500 }); }), http.get(`http://localhost3:7740/tx/:blockchainRid/:transactionRid`, () => { return new HttpResponse("", { status: 500 }); })); const handleRequestSpy = jest.spyOn(httpUtils, "handleRequest"); try { yield invalidNodeUrlPoolClient.getTransaction(mockBuffer); } catch (error) { expect(handleRequestSpy).toHaveBeenCalledTimes(3); } handleRequestSpy.mockRestore(); })); }); }); //# sourceMappingURL=client.test.js.map