import { Http } from "../../../commons/utils/http"; import { EventType } from "../../../event/core/event"; import { SessionCartAddEvent } from "./cart-add"; beforeEach(() => { const post = jest.fn(); post.mockClear(); (Http as any).post = post; }); test("session cart add is correctly created and sent to server", async () => { const event = new SessionCartAddEvent(); await event.push(); // Should not call server on empty add event. expect(Http.post).not.toHaveBeenCalled(); event.withProduct("product", 2); expect(event.products).toContainEqual(expect.objectContaining({ product: "product", quantity: 2 })); await event.push(); expect(Http.post).toHaveBeenCalled(); expect(Http.post).toHaveBeenCalledWith( expect.any(String), expect.objectContaining({ type: EventType.SessionCartAdd, products: expect.any(Array) }), ); });