From 7505d60888946b5bbaa36020e83016bcb806cffa Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 9 Aug 2024 11:31:05 +0100 Subject: [PATCH] Extract out processor and spy variables to reduce repetition. --- .../posthog/tests/PosthogProcessor.spec.ts | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/packages/backend-core/src/events/processors/posthog/tests/PosthogProcessor.spec.ts b/packages/backend-core/src/events/processors/posthog/tests/PosthogProcessor.spec.ts index 1d9e341ffe..1486753e4f 100644 --- a/packages/backend-core/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +++ b/packages/backend-core/src/events/processors/posthog/tests/PosthogProcessor.spec.ts @@ -16,6 +16,9 @@ const newIdentity = () => { } describe("PosthogProcessor", () => { + let processor: PosthogProcessor + let spy: jest.SpyInstance + beforeAll(() => { testEnv.singleTenant() }) @@ -25,25 +28,21 @@ describe("PosthogProcessor", () => { await cache.bustCache( `${CacheKey.EVENTS_RATE_LIMIT}:${Event.SERVED_BUILDER}` ) + + processor = new PosthogProcessor("test") + spy = jest.spyOn(processor.posthog, "capture") }) describe("processEvent", () => { it("processes event", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") - const identity = newIdentity() const properties = {} await processor.processEvent(Event.APP_CREATED, identity, properties) - expect(spy).toHaveBeenCalledTimes(1) }) it("honours exclusions", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") - const identity = newIdentity() const properties = {} @@ -52,9 +51,6 @@ describe("PosthogProcessor", () => { }) it("removes audited information", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") - const identity = newIdentity() const properties = { email: "test", @@ -65,6 +61,7 @@ describe("PosthogProcessor", () => { await processor.processEvent(Event.USER_CREATED, identity, properties) expect(spy).toHaveBeenCalled() + // @ts-ignore const call = processor.posthog.capture.mock.calls[0][0] expect(call.properties.audited).toBeUndefined() @@ -73,9 +70,6 @@ describe("PosthogProcessor", () => { describe("rate limiting", () => { it("sends daily event once in same day", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") - const identity = newIdentity() const properties = {} @@ -89,8 +83,6 @@ describe("PosthogProcessor", () => { }) it("sends daily event once per unique day", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") const identity = newIdentity() const properties = {} @@ -110,9 +102,6 @@ describe("PosthogProcessor", () => { }) it("sends event again after cache expires", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") - const identity = newIdentity() const properties = {} @@ -130,8 +119,6 @@ describe("PosthogProcessor", () => { }) it("sends per app events once per day per app", async () => { - const processor = new PosthogProcessor("test") - const spy = jest.spyOn(processor.posthog, "capture") const identity = newIdentity() const properties = {}