From 04e5d554a06c3de77de6883d40e9243d91daf2c3 Mon Sep 17 00:00:00 2001 From: Ben White Date: Thu, 12 Dec 2024 12:59:41 +0100 Subject: [PATCH] fix most tests --- src/__tests__/featureflags.test.ts | 30 +++++----------------------- src/__tests__/posthog-core.loaded.ts | 8 +------- src/posthog-featureflags.ts | 10 ++++++---- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/__tests__/featureflags.test.ts b/src/__tests__/featureflags.test.ts index 6fb0a09dd..30d89c301 100644 --- a/src/__tests__/featureflags.test.ts +++ b/src/__tests__/featureflags.test.ts @@ -299,11 +299,7 @@ describe('featureflags', () => { featureFlags.decide() expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - data: { - disable_flags: true, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toBe(true) }) it('should call decide with flags disabled if set generally', () => { @@ -311,11 +307,7 @@ describe('featureflags', () => { featureFlags.decide() expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - data: { - disable_flags: true, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toBe(true) }) it('should call decide once even if reload called before', () => { @@ -323,11 +315,7 @@ describe('featureflags', () => { featureFlags.decide() expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - data: { - disable_flags: false, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toBe(undefined) jest.runOnlyPendingTimers() expect(instance._send_request).toHaveBeenCalledTimes(1) @@ -339,11 +327,7 @@ describe('featureflags', () => { featureFlags.decide() expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - data: { - disable_flags: false, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toBe(undefined) jest.runOnlyPendingTimers() expect(instance._send_request).toHaveBeenCalledTimes(1) @@ -355,11 +339,7 @@ describe('featureflags', () => { featureFlags.decide() expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - data: { - disable_flags: true, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toBe(true) }) }) diff --git a/src/__tests__/posthog-core.loaded.ts b/src/__tests__/posthog-core.loaded.ts index d866558bc..3ef6c6805 100644 --- a/src/__tests__/posthog-core.loaded.ts +++ b/src/__tests__/posthog-core.loaded.ts @@ -97,13 +97,7 @@ describe('loaded() with flags', () => { expect(instance.featureFlags._callDecideEndpoint).toHaveBeenCalledTimes(1) expect(instance._send_request).toHaveBeenCalledTimes(1) - expect(instance._send_request.mock.calls[0][0]).toMatchObject({ - url: 'https://us.i.posthog.com/decide/?v=3', - data: { - groups: { org: 'bazinga' }, - disable_flags: false, - }, - }) + expect(instance._send_request.mock.calls[0][0].data.disable_flags).toEqual(undefined) jest.runOnlyPendingTimers() // Once for callback jest.runOnlyPendingTimers() // Once for potential debounce diff --git a/src/posthog-featureflags.ts b/src/posthog-featureflags.ts index 9c801fa01..f0f317002 100644 --- a/src/posthog-featureflags.ts +++ b/src/posthog-featureflags.ts @@ -204,7 +204,7 @@ export class PostHogFeatureFlags { * NOTE: This is used both for flags and remote config. Once the RemoteConfig is fully released this will essentially only * be for flags and can eventually be replaced wiht the new flags endpoint */ - _callDecideEndpoint(options?: { data: Record }): void { + _callDecideEndpoint(options?: { disableFlags?: boolean }): void { // Ensure we don't have double queued decide requests this.clearDebouncer() if (this.instance.config.advanced_disable_decide) { @@ -216,15 +216,17 @@ export class PostHogFeatureFlags { return } const token = this.instance.config.token - const data = { + const data: Record = { token: token, distinct_id: this.instance.get_distinct_id(), groups: this.instance.getGroups(), $anon_distinct_id: this.$anon_distinct_id, person_properties: this.instance.get_property(STORED_PERSON_PROPERTIES_KEY), group_properties: this.instance.get_property(STORED_GROUP_PROPERTIES_KEY), - disable_flags: this.instance.config.advanced_disable_feature_flags || undefined, - ...(options?.data || {}), + } + + if (options?.disableFlags || this.instance.config.advanced_disable_feature_flags) { + data.disable_flags = true } this._requestInFlight = true