Skip to content

Commit b149a54

Browse files
authored
Merge pull request #58 from launchdarkly/christie/sc-161955/don-t-send-events-for-all-flags-on-load-by
Don't send events for all flags on load by
2 parents 09166c0 + 85596f8 commit b149a54

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/initLDClient.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const ldClientInitialize = initialize as jest.Mock;
1717
const clientSideID = 'deadbeef';
1818
const defaultUser: LDUser = { key: 'abcdef' };
1919
const options: LDOptions = { bootstrap: 'localStorage' };
20-
const extraOptionsAddedBySdk: LDOptions = { wrapperName: 'react-client-sdk', wrapperVersion: 'mock.version' };
20+
const extraOptionsAddedBySdk: LDOptions = {
21+
wrapperName: 'react-client-sdk',
22+
wrapperVersion: 'mock.version',
23+
sendEventsOnlyForVariation: true,
24+
};
2125
const expectedOptions: LDOptions = { ...options, ...extraOptionsAddedBySdk };
2226
const flags = { 'test-flag': false, 'another-test-flag': true };
2327

@@ -89,4 +93,16 @@ describe('initLDClient', () => {
8993
expect(mockLDClient.variation).toHaveBeenNthCalledWith(2, 'lonelier-flag', false);
9094
expect(flagsClient).toEqual({ flags: { lonelyFlag: true, lonelierFlag: true }, ldClient: mockLDClient });
9195
});
96+
97+
test('may explicity set sendEventsOnlyForVariation to false', async () => {
98+
const anonUser: LDUser = { anonymous: true };
99+
await initLDClient(clientSideID, undefined, undefined, { ...options, sendEventsOnlyForVariation: false });
100+
101+
expect(ldClientInitialize.mock.calls[0]).toEqual([
102+
clientSideID,
103+
anonUser,
104+
{ ...expectedOptions, sendEventsOnlyForVariation: false },
105+
]);
106+
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
107+
});
92108
});

src/initLDClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { AllFlagsLDClient, defaultReactOptions, LDReactOptions } from './types';
33
import { fetchFlags } from './utils';
44
import * as packageInfo from '../package.json';
55

6+
const wrapperOptions: LDOptions = {
7+
wrapperName: 'react-client-sdk',
8+
wrapperVersion: packageInfo.version,
9+
sendEventsOnlyForVariation: true,
10+
};
11+
612
/**
713
* Internal function to initialize the `LDClient`.
814
*
@@ -22,8 +28,7 @@ const initLDClient = async (
2228
options?: LDOptions,
2329
targetFlags?: LDFlagSet,
2430
): Promise<AllFlagsLDClient> => {
25-
const allOptions = { wrapperName: 'react-client-sdk', wrapperVersion: packageInfo.version, ...options };
26-
const ldClient = ldClientInitialize(clientSideID, user, allOptions);
31+
const ldClient = ldClientInitialize(clientSideID, user, { ...wrapperOptions, ...options });
2732

2833
return new Promise<AllFlagsLDClient>((resolve) => {
2934
ldClient.on('ready', () => {

0 commit comments

Comments
 (0)