-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(flags): Re-enable reload only when request finishes (#791)
* fix(flags): Re-enable reload only when request finishes * fix tests
- Loading branch information
1 parent
1a8394c
commit 1166b8c
Showing
7 changed files
with
139 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]', | ||
|
@@ -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') | ||
|
||
|
@@ -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([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { PostHog } from '../posthog-core' | ||
import { PostHogPersistence } from '../posthog-persistence' | ||
|
||
jest.useFakeTimers() | ||
|
||
given('lib', () => Object.assign(new PostHog(), given.overrides)) | ||
|
||
describe('loaded() with flags', () => { | ||
beforeAll(() => { | ||
jest.unmock('../decide') | ||
}) | ||
|
||
given('subject', () => () => given.lib._loaded()) | ||
|
||
given('overrides', () => ({ | ||
get_config: (key) => given.config?.[key], | ||
capture: jest.fn(), | ||
featureFlags: { | ||
setReloadingPaused: jest.fn(), | ||
resetRequestQueue: jest.fn(), | ||
receivedFeatureFlags: jest.fn(), | ||
}, | ||
_start_queue_if_opted_in: jest.fn(), | ||
persistence: new PostHogPersistence(given.config), | ||
_send_request: jest.fn((host, data, header, callback) => callback({ status: 200 })), | ||
})) | ||
given('config', () => ({ loaded: jest.fn(), persistence: 'memory' })) | ||
|
||
describe('toggling flag reloading', () => { | ||
given('config', () => ({ | ||
loaded: (ph) => { | ||
ph.group('org', 'bazinga', { name: 'Shelly' }) | ||
setTimeout(() => { | ||
ph.group('org', 'bazinga2', { name: 'Shelly' }) | ||
}, 100) | ||
}, | ||
persistence: 'memory', | ||
})) | ||
|
||
given('overrides', () => ({ | ||
get_config: (key) => given.config?.[key], | ||
capture: jest.fn(), | ||
_send_request: jest.fn((host, data, header, callback) => setTimeout(() => callback({ status: 200 }), 1000)), | ||
_start_queue_if_opted_in: jest.fn(), | ||
persistence: new PostHogPersistence(given.config), | ||
})) | ||
|
||
beforeEach(() => { | ||
jest.spyOn(given.lib.featureFlags, 'setGroupPropertiesForFlags') | ||
jest.spyOn(given.lib.featureFlags, 'setReloadingPaused') | ||
jest.spyOn(given.lib.featureFlags, 'resetRequestQueue') | ||
jest.spyOn(given.lib.featureFlags, '_reloadFeatureFlagsRequest') | ||
}) | ||
|
||
it('doesnt call flags while initial load is happening', () => { | ||
given.subject() | ||
|
||
jest.runAllTimers() | ||
|
||
expect(given.lib.featureFlags.setGroupPropertiesForFlags).toHaveBeenCalled() // loaded ph.group() calls setGroupPropertiesForFlags | ||
expect(given.lib.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true) | ||
expect(given.lib.featureFlags.resetRequestQueue).toHaveBeenCalledTimes(1) | ||
expect(given.lib.featureFlags.setReloadingPaused).toHaveBeenCalledWith(false) | ||
|
||
// even if the decide request returned late, we should not call _reloadFeatureFlagsRequest | ||
// because it ought to be paused until decide returns | ||
expect(given.overrides._send_request).toHaveBeenCalledTimes(1) | ||
expect(given.lib.featureFlags._reloadFeatureFlagsRequest).toHaveBeenCalledTimes(0) | ||
}) | ||
}) | ||
|
||
it('toggles feature flags on and off', () => { | ||
given.subject() | ||
|
||
expect(given.overrides.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true) | ||
expect(given.overrides.featureFlags.setReloadingPaused).toHaveBeenCalledWith(false) | ||
expect(given.overrides.featureFlags.resetRequestQueue).toHaveBeenCalledTimes(1) | ||
expect(given.overrides.featureFlags.receivedFeatureFlags).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters