UNPKG

@tranthor/sdk-node

Version:

Tranthor's node sdk for customer engagement.

113 lines 4.64 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cross_fetch_1 = __importDefault(require("cross-fetch")); const index_1 = require("./index"); jest.mock("cross-fetch", () => ({ __esModule: true, default: jest.fn(), })); const mockedFetch = cross_fetch_1.default; describe("TranthorSdk", () => { const initParams = { writeKey: "test-write-key", }; beforeEach(() => { index_1.TranthorSdk["instance"] = null; const { Response } = jest.requireActual("cross-fetch"); mockedFetch.mockImplementation(() => Promise.resolve(new Response("", { status: 200, }))); }); it("should initialize and identify", () => __awaiter(void 0, void 0, void 0, function* () { yield index_1.TranthorSdk.init(initParams); const identifyParams = { userId: "123", traits: { email: "test@example.com", }, }; index_1.TranthorSdk.identify(identifyParams); yield index_1.TranthorSdk.flush(); expect(cross_fetch_1.default).toHaveBeenCalledWith("https://app.tranthor.com/api/public/apps/batch", expect.objectContaining({ method: "POST", headers: { authorization: "test-write-key", "Content-Type": "application/json", }, body: expect.any(String), })); })); it("should initialize new instance and identify", () => __awaiter(void 0, void 0, void 0, function* () { const sdkInstance = yield index_1.TranthorSdk.initNew(initParams); const trackParams = { userId: "123", event: "Made Purchase", properties: { itemId: "abc", }, }; sdkInstance.track(trackParams); yield sdkInstance.flush(); expect(cross_fetch_1.default).toHaveBeenCalledWith("https://app.tranthor.com/api/public/apps/batch", expect.objectContaining({ method: "POST", headers: { authorization: "test-write-key", "Content-Type": "application/json", }, body: expect.any(String), })); })); it("should initialize and page", () => __awaiter(void 0, void 0, void 0, function* () { yield index_1.TranthorSdk.init(initParams); const pageParams = { userId: "123", name: "Home", properties: { path: "/", }, }; index_1.TranthorSdk.page(pageParams); yield index_1.TranthorSdk.flush(); expect(cross_fetch_1.default).toHaveBeenCalledWith("https://app.tranthor.com/api/public/apps/batch", expect.objectContaining({ method: "POST", headers: { authorization: "test-write-key", "Content-Type": "application/json", }, body: expect.any(String), })); })); it("should initialize new instance and page", () => __awaiter(void 0, void 0, void 0, function* () { const sdkInstance = yield index_1.TranthorSdk.initNew(initParams); const screenParams = { userId: "123", name: "Recipe Screen", properties: { recipeType: "Soup", }, }; sdkInstance.screen(screenParams); yield sdkInstance.flush(); expect(cross_fetch_1.default).toHaveBeenCalledWith("https://app.tranthor.com/api/public/apps/batch", expect.objectContaining({ method: "POST", headers: { authorization: "test-write-key", "Content-Type": "application/json", }, body: expect.any(String), })); })); }); //# sourceMappingURL=index.test.js.map