import { AnalyticsService, TransactionReport, } from "../../src/Service/AnalyticsService"; describe("AnalyticsService", () => { describe("TransactionReport", () => { it("should create a TransactionReport", () => { // arrange const transactionReportProperties = [ "CurrentFlow", "CurrentStatus", "CurrentTxFlowState", "DurationMs", "Event", "LibraryLanguage", "LibraryVersion", "PosRefId", "PosVendorId", "PosVersion", "SerialNumber", "TxEndTime", "TxResult", "TxStartTime", "TxType", ]; // act const transactionReport = new TransactionReport(); // assert expect(Object.keys(transactionReport).sort()).toEqual( transactionReportProperties ); }); }); it("should correctly convert the internal properties to a message", () => { // arrange const transactionReport = new TransactionReport(); transactionReport.CurrentFlow = null; transactionReport.CurrentStatus = "CurrentStatus"; transactionReport.CurrentTxFlowState = "CurrentTxFlowState"; transactionReport.DurationMs = null; transactionReport.Event = "Event"; transactionReport.LibraryLanguage = "LibraryLanguage"; transactionReport.LibraryVersion = "LibraryVersion"; transactionReport.PosRefId = "PosRefId"; transactionReport.PosVendorId = "PosVendorId"; transactionReport.PosVersion = "PosVersion"; transactionReport.SerialNumber = "SerialNumber"; transactionReport.TxEndTime = null; transactionReport.TxResult = "TxResult"; transactionReport.TxStartTime = null; transactionReport.TxType = "TxType"; // act const message = transactionReport.ToMessage(); // assert expect(transactionReport).toEqual( jasmine.objectContaining({ CurrentFlow: null, CurrentStatus: "CurrentStatus", CurrentTxFlowState: "CurrentTxFlowState", DurationMs: null, Event: "Event", LibraryLanguage: "LibraryLanguage", LibraryVersion: "LibraryVersion", PosRefId: "PosRefId", PosVendorId: "PosVendorId", PosVersion: "PosVersion", SerialNumber: "SerialNumber", TxEndTime: null, TxResult: "TxResult", TxStartTime: null, TxType: "TxType", }) ); }); describe("AnalyticsService", () => { let fetchHelper; beforeEach(() => { const fetchPromise = new Promise((resolve, reject) => { fetchHelper = { resolve, reject }; // eslint-disable-next-line @typescript-eslint/no-explicit-any }) as any; spyOn(window, "fetch").and.returnValue(fetchPromise); }); it("should successfully send a report to sandbox", async () => { // arrange const transactionReport = new TransactionReport(); const apiKey = "spi-sample-pos"; const tenantCode = "wbc"; const isTestMode = true; fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act await AnalyticsService.ReportTransaction( transactionReport, apiKey, tenantCode, isTestMode ); // assert expect(window.fetch).toHaveBeenCalledWith( jasmine.stringMatching(/\/spi-analytics-api-sb\./), jasmine.anything() ); }); it("should successfully send a report to production", async () => { // arrange const transactionReport = new TransactionReport(); const apiKey = "spi-sample-pos"; const tenantCode = "wbc"; const isTestMode = false; fetchHelper.resolve({ ok: true }); // Mock the response from ReportTx // act await AnalyticsService.ReportTransaction( transactionReport, apiKey, tenantCode, isTestMode ); // assert expect(window.fetch).toHaveBeenCalledWith( jasmine.stringMatching(/\/spi-analytics-api\./), jasmine.anything() ); }); it("should throw an error when the request returned an status code other than 2xx", async () => { // arrange const transactionReport = new TransactionReport(); const apiKey = "spi-sample-pos"; const tenantCode = "wbc"; const isTestMode = false; const errorResponse = { error: { code: 418, message: "Still a teapot, not a terminal", }, }; fetchHelper.resolve({ ok: false, json: () => Promise.resolve(errorResponse), }); // Mock the response from ReportTx // act let errorReturned = null as Error | null; try { await AnalyticsService.ReportTransaction( transactionReport, apiKey, tenantCode, isTestMode ); } catch (error) { errorReturned = error; } // assert expect(errorReturned).toBeInstanceOf(Error); expect(errorReturned?.message).toMatch(/teapot/); }); }); });