Skip to content

Commit

Permalink
Marc/num 3528 add e2e tests for engage sdk (#3)
Browse files Browse the repository at this point in the history
* Return success or not from sendEvent()

* Send payload to the new /sdk/track endpoint

* Clean up unit tests

* Add integration test
  • Loading branch information
KeKs0r authored Oct 28, 2024
1 parent c5d9285 commit 4185d09
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/__tests__/engage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import assert from "node:assert";
import { createEngage } from "../create-engage";
import type { SentEvent } from "../base-event";

const originalFetch = global.fetch;
const mockFetch = mock();

describe("Engage", () => {
beforeAll(() => {
global.fetch = mockFetch;
});
afterEach(() => {
console.log("Reset");
mockFetch.mockReset();
document.location.href = "http://numia.xyz";
window.localStorage.clear();
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it, expect } from "bun:test";
import { createEngage } from "../create-engage";
import { fetch } from "bun";

/**
* HappyDOM fetch connects to https on port 80
*/
globalThis.fetch = fetch;

describe("E2E Tracking things", () => {
const apiKey = "4TzmfANyA1bRcMrWACToGBqMMiJ0jDXNEzfp6fxUbW1";

it("Can track page()", async () => {
const engage = createEngage({
app: "test",
apiKey,
});

const r = await engage.page();
expect(r).toHaveProperty("success", true);
});

it("Can track track()", async () => {
const engage = createEngage({
app: "test",
apiKey,
});

const r = await engage.track("customEvent", {
foo: "bar",
});
expect(r).toHaveProperty("success", true);
});

it("Identify", async () => {
const engage = createEngage({
app: "test",
apiKey,
});

const r = await engage.identify("user123", {
name: "John Doe",
email: "john@example.com",
});
expect(r).toHaveProperty("success", true);
});

it("Alias", async () => {
const engage = createEngage({
app: "test",
apiKey,
});

const r = await engage.alias("newUser456");
expect(r).toHaveProperty("success", true);
});
});
8 changes: 7 additions & 1 deletion src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ export class Analytics {
this.middlewares = options.middlewares;
}

private async sendEvent(event: SentEvent): Promise<void> {
private async sendEvent(event: SentEvent): Promise<{ success: boolean }> {
try {
const finalEvent = await runMiddlewares(
this.middlewares,
{ storage: this.storage },
event
);
await this.transport.sendEvent(finalEvent);
return {
success: true,
};
} catch (error) {
console.error("Failed to send analytics event:", error);
return {
success: false,
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/create-engage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function createEngage(options: EngageOptions) {

const storage = createLocalStorage({ prefix: "num" });
const transport = createFetchTransport({
url: `${baseUrl}/track`,
url: `${baseUrl}/sdk/track`,
apiKey,
});
const engage = new Analytics({
Expand Down

0 comments on commit 4185d09

Please sign in to comment.