Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Sep 6, 2023
1 parent 654b3e0 commit 36de1da
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
41 changes: 41 additions & 0 deletions functional_tests/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ test('person properties set in identify() with new distinct_id are sent to decid

resetRequests(token)

// wait for decide callback
// eslint-disable-next-line compat/compat
await new Promise((res) => setTimeout(res, 500))

// Person properties set here should also be sent to the decide endpoint.
posthog.identify('test-id', {
email: '[email protected]',
Expand Down Expand Up @@ -63,6 +67,10 @@ test('person properties set in identify() with the same distinct_id are sent to

resetRequests(token)

// wait for decide callback
// eslint-disable-next-line compat/compat
await new Promise((res) => setTimeout(res, 500))

// First we identify with a new distinct_id but with no properties set
posthog.identify('test-id')

Expand Down Expand Up @@ -98,3 +106,36 @@ test('person properties set in identify() with the same distinct_id are sent to
])
})
})

test('identify() doesnt trigger new request automatically if first request takes too long', async () => {
// TODO: Make this experience nicer, and queue requests, rather than leave
// it upto the user to call reloadFeatureFlags() manually.

const token = v4()
const posthog = await createPosthogInstance(token, { advanced_disable_decide: false })

const anonymousId = posthog.get_distinct_id()

await waitFor(() => {
expect(getRequests(token)['/decide/']).toEqual([
// This is the initial call to the decide endpoint on PostHog init.
{
distinct_id: anonymousId,
groups: {},
token,
},
])
})

resetRequests(token)

// don't wait for decide callback

posthog.identify('test-id', {
email: '[email protected]',
})

await waitFor(() => {
expect(getRequests(token)['/decide/']).toEqual([])
})
})
2 changes: 2 additions & 0 deletions src/__tests__/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ describe('Payload Compression', () => {
},
featureFlags: {
receivedFeatureFlags: jest.fn(),
resetRequestQueue: jest.fn(),
setReloadingPaused: jest.fn(),
},
_hasBootstrappedFeatureFlags: jest.fn(),
get_property: (property_key) =>
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/decide.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('Decide', () => {
},
featureFlags: {
receivedFeatureFlags: jest.fn(),
resetRequestQueue: jest.fn(),
setReloadingPaused: jest.fn(),
},
_hasBootstrappedFeatureFlags: jest.fn(),
getGroups: () => ({ organization: '5' }),
Expand Down
6 changes: 3 additions & 3 deletions src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class Decide {
}

parseDecideResponse(response: DecideResponse): void {
this.instance.featureFlags.resetRequestQueue()
this.instance.featureFlags.setReloadingPaused(false)

if (response?.status === 0) {
console.error('Failed to fetch feature flags from PostHog.')
return
Expand All @@ -61,9 +64,6 @@ export class Decide {
this.instance.featureFlags.receivedFeatureFlags(response)
}

this.instance.featureFlags.resetRequestQueue()
this.instance.featureFlags.setReloadingPaused(false)

this.instance['compression'] = {}
if (response['supportedCompression'] && !this.instance.get_config('disable_compression')) {
const compression: Partial<Record<Compression, boolean>> = {}
Expand Down

0 comments on commit 36de1da

Please sign in to comment.