@tranthor/sdk-node
Version:
Tranthor's node sdk for customer engagement.
108 lines • 4.24 kB
JavaScript
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 fetch from "cross-fetch";
import { TranthorSdk } from "./index";
jest.mock("cross-fetch", () => ({
__esModule: true,
default: jest.fn(),
}));
const mockedFetch = fetch;
describe("TranthorSdk", () => {
const initParams = {
writeKey: "test-write-key",
};
beforeEach(() => {
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 TranthorSdk.init(initParams);
const identifyParams = {
userId: "123",
traits: {
email: "test@example.com",
},
};
TranthorSdk.identify(identifyParams);
yield TranthorSdk.flush();
expect(fetch).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 TranthorSdk.initNew(initParams);
const trackParams = {
userId: "123",
event: "Made Purchase",
properties: {
itemId: "abc",
},
};
sdkInstance.track(trackParams);
yield sdkInstance.flush();
expect(fetch).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 TranthorSdk.init(initParams);
const pageParams = {
userId: "123",
name: "Home",
properties: {
path: "/",
},
};
TranthorSdk.page(pageParams);
yield TranthorSdk.flush();
expect(fetch).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 TranthorSdk.initNew(initParams);
const screenParams = {
userId: "123",
name: "Recipe Screen",
properties: {
recipeType: "Soup",
},
};
sdkInstance.screen(screenParams);
yield sdkInstance.flush();
expect(fetch).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