Skip to content

Commit

Permalink
Fixes all round
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 12, 2024
1 parent e6df151 commit c49d42f
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 298 deletions.
221 changes: 0 additions & 221 deletions src/__tests__/decide.test.ts

This file was deleted.

94 changes: 93 additions & 1 deletion src/__tests__/featureflags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('featureflags', () => {

beforeEach(() => {
instance = {
config,
config: { ...config },
get_distinct_id: () => 'blah id',
getGroups: () => {},
persistence: new PostHogPersistence(config),
Expand All @@ -38,6 +38,7 @@ describe('featureflags', () => {
json: {},
})
),
_onRemoteConfig: jest.fn(),
reloadFeatureFlags: () => featureFlags.reloadFeatureFlags(),
}

Expand Down Expand Up @@ -271,6 +272,97 @@ describe('featureflags', () => {
})
})

describe('decide()', () => {
it('should not call decide if advanced_disable_decide is true', () => {
instance.config.advanced_disable_decide = true
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(0)
})

it('should call decide', () => {
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: undefined,
},
})

jest.runOnlyPendingTimers()
expect(instance._send_request).toHaveBeenCalledTimes(1)
})

it('should call decide with flags disabled if set', () => {
instance.config.advanced_disable_feature_flags_on_first_load = true
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: true,
},
})
})

it('should call decide with flags disabled if set generally', () => {
instance.config.advanced_disable_feature_flags = true
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: true,
},
})
})

it('should call decide once even if reload called before', () => {
featureFlags.reloadFeatureFlags()
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: false,
},
})

jest.runOnlyPendingTimers()
expect(instance._send_request).toHaveBeenCalledTimes(1)
})

it('should not disable flags if reload was called on decide', () => {
instance.config.advanced_disable_feature_flags_on_first_load = true
featureFlags.reloadFeatureFlags()
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: false,
},
})

jest.runOnlyPendingTimers()
expect(instance._send_request).toHaveBeenCalledTimes(1)
})

it('should always disable flags if set', () => {
instance.config.advanced_disable_feature_flags = true
featureFlags.reloadFeatureFlags()
featureFlags.decide()

expect(instance._send_request).toHaveBeenCalledTimes(1)
expect(instance._send_request.mock.calls[0][0]).toMatchObject({
data: {
disable_flags: true,
},
})
})
})

describe('onFeatureFlags', () => {
beforeEach(() => {
instance._send_request = jest.fn().mockImplementation(({ callback }) =>
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/helpers/posthog-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createPosthogInstance = async (
// written, we first create an instance, then call init on it which then
// creates another instance.
const posthog = new PostHog()

// eslint-disable-next-line compat/compat
return await new Promise<PostHog>((resolve) =>
posthog.init(
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/personProcessing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jest.mock('../utils/globals', () => {
document: {
...orig.document,
createElement: (...args: any[]) => orig.document.createElement(...args),
body: {},
get referrer() {
return mockReferrerGetter()
},
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/posthog-core.identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { uuidv7 } from '../uuidv7'
import { createPosthogInstance, defaultPostHog } from './helpers/posthog-instance'
import { PostHog } from '../posthog-core'

jest.mock('../decide')

describe('identify()', () => {
let instance: PostHog
let beforeSendMock: jest.Mock
Expand All @@ -19,6 +17,7 @@ describe('identify()', () => {
},
uuidv7()
)

instance = Object.assign(posthog, {
register: jest.fn(),
featureFlags: {
Expand Down
Loading

0 comments on commit c49d42f

Please sign in to comment.