Skip to content

Commit eff7c5b

Browse files
committed
refactor(launchdarkly-provider): Check for context changes on initialization
Signed-off-by: mateo <[email protected]>
1 parent 10e560d commit eff7c5b

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.spec.ts

+45-1
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,65 @@ const logger: TestLogger = new TestLogger();
99
const testFlagKey = 'a-key';
1010
describe('LaunchDarklyClientProvider', () => {
1111
let ldClient: LDClient;
12+
let ldProvider: LaunchDarklyClientProvider
1213
let ofClient: Client;
1314

1415
beforeAll(() => {
1516
ldClient = {
1617
variationDetail: jest.fn(),
1718
waitUntilReady: jest.fn().mockResolvedValue({}),
19+
getContext: jest.fn().mockReturnValue({}),
1820
} as any;
19-
OpenFeature.setProvider(new LaunchDarklyClientProvider(ldClient, { logger }));
21+
ldProvider = new LaunchDarklyClientProvider(ldClient, { logger });
22+
OpenFeature.setProvider(ldProvider);
2023
ofClient = OpenFeature.getClient();
2124
})
2225
beforeEach(() => {
2326
logger.reset();
2427
jest.clearAllMocks();
2528
});
2629

30+
describe('initialize', () => {
31+
test('should not update context if it did not change', async () => {
32+
const newContext = {kind: 'user', targetingKey: 'test-key'};
33+
const oldContext = {kind: 'user', key: 'test-key'};
34+
ldClient.getContext = jest.fn().mockReturnValue(oldContext);
35+
ldClient.identify = jest.fn().mockResolvedValue(undefined);
36+
await ldProvider.initialize(newContext);
37+
expect(ldClient.identify).not.toHaveBeenCalled();
38+
});
39+
40+
test('should update context if it changed', async() => {
41+
const newContext = {kind: 'organization', targetingKey: 'test-key'};
42+
const oldContext = {kind: 'user', key: 'test-key'};
43+
ldClient.getContext = jest.fn().mockReturnValue(oldContext);
44+
ldClient.identify = jest.fn().mockResolvedValue(undefined);
45+
await ldProvider.initialize(newContext);
46+
expect(ldClient.identify).toHaveBeenCalledTimes(1);
47+
expect(ldClient.identify).toHaveBeenCalledWith(translateContext(logger, newContext));
48+
});
49+
50+
test('should not update context if anonymous and no key', async() => {
51+
const newContext = {anonymous: true};
52+
const oldContext = {kind: 'user', anonymous: true, key: 'generated-key'};
53+
ldClient.getContext = jest.fn().mockReturnValue(oldContext);
54+
ldClient.identify = jest.fn().mockResolvedValue(undefined);
55+
await ldProvider.initialize(newContext);
56+
expect(ldClient.identify).not.toHaveBeenCalled();
57+
});
58+
59+
test('should update context if anonymous and custom key', async() => {
60+
const newContext = {anonymous: true, key: 'custom-key'};
61+
const oldContext = {kind: 'user', anonymous: true, key: 'generated-key'};
62+
ldClient.getContext = jest.fn().mockReturnValue(oldContext);
63+
ldClient.identify = jest.fn().mockResolvedValue(undefined);
64+
await ldProvider.initialize(newContext);
65+
expect(ldClient.identify).toHaveBeenCalledTimes(1);
66+
expect(ldClient.identify).toHaveBeenCalledWith(translateContext(logger, newContext));
67+
});
68+
69+
});
70+
2771
describe('resolveBooleanEvaluation', () => {
2872
it('calls the client correctly for boolean variations', () => {
2973
// @ts-ignore we don't care about the arguments

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
basicLogger, LDClient
1010
} from 'launchdarkly-js-client-sdk';
11+
import isEqual from 'lodash.isequal';
1112

1213
import {LaunchDarklyProviderOptions} from './launchdarkly-provider-options';
1314
import translateContext from './translate-context';
@@ -43,7 +44,13 @@ export class LaunchDarklyClientProvider implements Provider {
4344
}
4445
}
4546

46-
initialize(context: EvaluationContext): Promise<void> {
47+
async initialize(context: EvaluationContext): Promise<void> {
48+
const newContext = this.translateContext(context);
49+
const oldContext = this.client.getContext();
50+
const ignoreAnonymous = newContext.anonymous && oldContext.anonymous && !newContext.key
51+
if(!ignoreAnonymous && !isEqual(newContext, oldContext)) {
52+
await this.client.identify(newContext);
53+
}
4754
return this.client.waitUntilReady();
4855
}
4956

0 commit comments

Comments
 (0)