import { Http } from "../../commons/utils/http"; import { EventType } from "../../event/core/event"; import { PageCartEvent } from "../../event/types/page/cart"; import { SessionUserEvent } from "../../event/types/session/user"; import { BatchAlreadyClosedError, BatchEventBuilder } from "./batch-event-builder"; beforeEach(() => { const post = jest.fn(); post.mockClear(); (Http as any).post = post; }); test("creating batch event with necessary paramters", async () => { const builder = BatchEventBuilder.start().withHomePageViewEvent().withUserLoginEvent("test@biggy.com.br"); expect((builder as any).events).toContainEqual(expect.objectContaining({ type: EventType.PageHome })); expect((builder as any).events).toContainEqual( expect.objectContaining({ type: EventType.SessionUser, email: "test@biggy.com.br", marketing: true }), ); await builder.push(); expect(Http.post).toHaveBeenCalled(); expect(Http.post).toHaveBeenCalledWith(expect.any(String), builder); }); test("creating a batch event with generic track events", async () => { const builder = BatchEventBuilder.start() .withEvent(new PageCartEvent().withProducts([{ product: "product1", quantity: 5 }])) .withEvent(new SessionUserEvent("test@biggy.com.br")); expect((builder as any).events).toContainEqual(expect.objectContaining({ type: EventType.PageCart })); expect((builder as any).events).toContainEqual( expect.objectContaining({ type: EventType.SessionUser, email: "test@biggy.com.br", marketing: true }), ); await builder.push(); expect(Http.post).toHaveBeenCalled(); expect(Http.post).toHaveBeenCalledWith(expect.any(String), builder); }); test("closed batch should not be able to be sent", async () => { const builder = BatchEventBuilder.start().withPageConfirmationEvent("orderId", [ { product: "product1", price: 59.99, quantity: 1 }, ]); expect((builder as any).events).toContainEqual(expect.objectContaining({ type: EventType.PageConfirmation })); await builder.push(); expect(Http.post).toHaveBeenCalled(); expect(Http.post).toHaveBeenCalledWith(expect.any(String), builder); const failedPush = await builder.push().catch((e) => { expect(e).toEqual(new BatchAlreadyClosedError()); return false; }); expect(Http.post).toHaveBeenCalledTimes(1); expect(failedPush).toBe(false); });