Skip to content

Commit 557f2df

Browse files
author
Emmanuel Zamora
committed
[SDKS-6942][DW] Update tests and constants
1 parent 3306115 commit 557f2df

6 files changed

Lines changed: 46 additions & 46 deletions

File tree

src/SplitTreatments.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function argsAreEqual(newArgs: any[], lastArgs: any[]): boolean {
1313
shallowEqual(newArgs[4], lastArgs[4]); // client attributes
1414
}
1515

16-
function evaluateSplits(client: SplitIO.IBrowserClient, lastUpdate: number, names: SplitIO.SplitNames, attributes?: SplitIO.Attributes, _clientAttributes?: SplitIO.Attributes) {
16+
function evaluateFeatureFlags(client: SplitIO.IBrowserClient, lastUpdate: number, names: SplitIO.SplitNames, attributes?: SplitIO.Attributes, _clientAttributes?: SplitIO.Attributes) {
1717
return client.getTreatmentsWithConfig(names, attributes);
1818
}
1919

@@ -29,7 +29,7 @@ class SplitTreatments extends React.Component<ISplitTreatmentsProps> {
2929

3030
// Attaching a memoized `client.getTreatmentsWithConfig` function to the component instance, to avoid duplicated impressions because
3131
// the function result is the same given the same `client` instance, `lastUpdate` timestamp, and list of split `names` and `attributes`.
32-
private evaluateSplits = memoizeOne(evaluateSplits, argsAreEqual);
32+
private evaluateFeatureFlags = memoizeOne(evaluateFeatureFlags, argsAreEqual);
3333

3434
render() {
3535
const { names, children, attributes } = this.props;
@@ -43,7 +43,7 @@ class SplitTreatments extends React.Component<ISplitTreatmentsProps> {
4343
if (client && isOperational) {
4444
// Cloning `client.getAttributes` result for memoization, because it returns the same reference unless `client.clearAttributes` is called.
4545
// Caveat: same issue happens with `names` and `attributes` props if the user follows the bad practice of mutating the object instead of providing a new one.
46-
treatments = this.evaluateSplits(client, lastUpdate, names, attributes, { ...client.getAttributes() });
46+
treatments = this.evaluateFeatureFlags(client, lastUpdate, names, attributes, { ...client.getAttributes() });
4747
} else {
4848
treatments = getControlTreatmentsWithConfig(names);
4949
if (!client) { this.logWarning = true; }

src/__tests__/SplitTreatments.test.tsx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ describe('SplitTreatments', () => {
3131
afterEach(() => { logSpy.mockClear() });
3232

3333
it('passes as treatments prop the value returned by the function "getControlTreatmentsWithConfig" if the SDK is not ready.', (done) => {
34-
const splitNames = ['split1', 'split2'];
34+
const featureFlagNames = ['split1', 'split2'];
3535
render(
3636
<SplitFactory config={sdkBrowser} >{
3737
({ factory }) => {
3838
return (
3939
<SplitClient splitKey='user1' >
40-
<SplitTreatments names={splitNames} >
40+
<SplitTreatments names={featureFlagNames} >
4141
{({ treatments }: ISplitTreatmentsChildProps) => {
4242
const clientMock: any = factory?.client('user1');
4343
expect(clientMock.getTreatmentsWithConfig.mock.calls.length).toBe(0);
44-
expect(treatments).toEqual(getControlTreatmentsWithConfig(splitNames));
44+
expect(treatments).toEqual(getControlTreatmentsWithConfig(featureFlagNames));
4545
done();
4646
return null;
4747
}}
@@ -53,7 +53,7 @@ describe('SplitTreatments', () => {
5353
});
5454

5555
it('passes as treatments prop the value returned by the method "client.getTreatmentsWithConfig" if the SDK is ready.', (done) => {
56-
const splitNames = ['split1', 'split2'];
56+
const featureFlagNames = ['split1', 'split2'];
5757
const outerFactory = SplitSdk(sdkBrowser);
5858
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
5959

@@ -63,12 +63,12 @@ describe('SplitTreatments', () => {
6363
expect(getStatus(outerFactory.client()).isReady).toBe(isReady);
6464
expect(isReady).toBe(true);
6565
return (
66-
<SplitTreatments names={splitNames} >
66+
<SplitTreatments names={featureFlagNames} >
6767
{({ treatments, isReady: isReady2, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate }: ISplitTreatmentsChildProps) => {
6868
const clientMock: any = factory?.client();
6969
expect(clientMock.getTreatmentsWithConfig.mock.calls.length).toBe(1);
7070
expect(treatments).toBe(clientMock.getTreatmentsWithConfig.mock.results[0].value);
71-
expect(splitNames).toBe(clientMock.getTreatmentsWithConfig.mock.calls[0][0]);
71+
expect(featureFlagNames).toBe(clientMock.getTreatmentsWithConfig.mock.calls[0][0]);
7272
expect([isReady2, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate]).toStrictEqual([true, false, false, false, false, 0]);
7373
done();
7474
return null;
@@ -80,17 +80,17 @@ describe('SplitTreatments', () => {
8080
});
8181

8282
it('logs error and passes control treatments ("getControlTreatmentsWithConfig") if rendered outside an SplitProvider component.', () => {
83-
const splitNames = ['split1', 'split2'];
83+
const featureFlagNames = ['split1', 'split2'];
8484
let passedTreatments;
8585
render(
86-
<SplitTreatments names={splitNames} >
86+
<SplitTreatments names={featureFlagNames} >
8787
{({ treatments }: ISplitTreatmentsChildProps) => {
8888
passedTreatments = treatments;
8989
return null;
9090
}}
9191
</SplitTreatments>);
9292
expect(logSpy).toBeCalledWith(WARN_ST_NO_CLIENT);
93-
expect(getControlTreatmentsWithConfig).toBeCalledWith(splitNames);
93+
expect(getControlTreatmentsWithConfig).toBeCalledWith(featureFlagNames);
9494
expect(getControlTreatmentsWithConfig).toHaveReturnedWith(passedTreatments);
9595
});
9696

@@ -99,22 +99,22 @@ describe('SplitTreatments', () => {
9999
* is not ready doesn't emit errors, and logs meaningful messages instead.
100100
*/
101101
it('Input validation: invalid "names" and "attributes" props in SplitTreatments.', (done) => {
102-
const splitNames = ['split1', 'split2'];
102+
const featureFlagNames = ['split1', 'split2'];
103103

104104
render(
105105
<SplitFactory config={sdkBrowser} >{
106106
() => {
107107
return (
108108
<>
109109
{/* @ts-expect-error Test error handling */}
110-
<SplitTreatments split_names={splitNames} >
110+
<SplitTreatments split_names={featureFlagNames} >
111111
{({ treatments }: ISplitTreatmentsChildProps) => {
112112
expect(treatments).toEqual({});
113113
return null;
114114
}}
115115
</SplitTreatments>
116116
{/* @ts-expect-error Test error handling */}
117-
<SplitTreatments names={splitNames[0]} >
117+
<SplitTreatments names={featureFlagNames[0]} >
118118
{({ treatments }: ISplitTreatmentsChildProps) => {
119119
expect(treatments).toEqual({});
120120
return null;
@@ -186,28 +186,28 @@ describe('SplitTreatments optimization', () => {
186186
wrapper.unmount(); // unmount to remove event listener from factory
187187
})
188188

189-
it('rerenders but does not re-evaluate splits if client, lastUpdate, names and attributes are the same object.', () => {
189+
it('rerenders but does not re-evaluate feature flags if client, lastUpdate, names and attributes are the same object.', () => {
190190
wrapper.rerender(<Component names={names} attributes={attributes} splitKey={splitKey} />);
191191

192192
expect(renderTimes).toBe(2);
193193
expect(outerFactory.client().getTreatmentsWithConfig).toBeCalledTimes(1);
194194
});
195195

196-
it('rerenders but does not re-evaluate splits if client, lastUpdate, names and attributes are equals (shallow comparison).', () => {
196+
it('rerenders but does not re-evaluate feature flags if client, lastUpdate, names and attributes are equals (shallow comparison).', () => {
197197
wrapper.rerender(<Component names={[...names]} attributes={{ ...attributes }} splitKey={splitKey} />);
198198

199199
expect(renderTimes).toBe(2);
200200
expect(outerFactory.client().getTreatmentsWithConfig).toBeCalledTimes(1);
201201
});
202202

203-
it('rerenders and re-evaluates splits if names are not equals (shallow array comparison).', () => {
203+
it('rerenders and re-evaluates feature flags if names are not equals (shallow array comparison).', () => {
204204
wrapper.rerender(<Component names={[...names, 'split3']} attributes={{ ...attributes }} splitKey={splitKey} />);
205205

206206
expect(renderTimes).toBe(2);
207207
expect(outerFactory.client().getTreatmentsWithConfig).toBeCalledTimes(2);
208208
});
209209

210-
it('rerenders and re-evaluates splits if attributes are not equals (shallow object comparison).', () => {
210+
it('rerenders and re-evaluates feature flags if attributes are not equals (shallow object comparison).', () => {
211211
const attributesRef = { ...attributes, att2: 'att2' };
212212
wrapper.rerender(<Component names={[...names]} attributes={attributesRef} splitKey={splitKey} />);
213213

@@ -221,7 +221,7 @@ describe('SplitTreatments optimization', () => {
221221
expect(outerFactory.client().getTreatmentsWithConfig).toBeCalledTimes(2);
222222
});
223223

224-
it('rerenders and re-evaluates splits if lastUpdate timestamp changes (e.g., SDK_UPDATE event).', (done) => {
224+
it('rerenders and re-evaluates feature flags if lastUpdate timestamp changes (e.g., SDK_UPDATE event).', (done) => {
225225
expect(renderTimes).toBe(1);
226226

227227
// State update and split evaluation
@@ -243,7 +243,7 @@ describe('SplitTreatments optimization', () => {
243243
})
244244
});
245245

246-
it('rerenders and re-evaluates splits if client changes.', () => {
246+
it('rerenders and re-evaluates feature flags if client changes.', () => {
247247
wrapper.rerender(<Component names={names} attributes={attributes} splitKey={'otherKey'} />);
248248
act(() => (outerFactory as any).client('otherKey').__emitter__.emit(Event.SDK_READY));
249249

@@ -253,7 +253,7 @@ describe('SplitTreatments optimization', () => {
253253
expect(outerFactory.client('otherKey').getTreatmentsWithConfig).toBeCalledTimes(1);
254254
});
255255

256-
it('rerenders and re-evaluate splits when Split context changes (in both SplitFactory and SplitClient components).', async () => {
256+
it('rerenders and re-evaluate splfeature flagsits when Split context changes (in both SplitFactory and SplitClient components).', async () => {
257257
// changes in SplitContext implies that either the factory, the client (user key), or its status changed, what might imply a change in treatments
258258
const outerFactory = SplitSdk(sdkBrowser);
259259
const names = ['split1', 'split2'];
@@ -327,7 +327,7 @@ describe('SplitTreatments optimization', () => {
327327
expect(outerFactory.client('user2').getTreatmentsWithConfig).toBeCalledTimes(4); // idem
328328
});
329329

330-
it('rerenders and re-evaluates splits if client attributes changes.', (done) => {
330+
it('rerenders and re-evaluates feature flags if client attributes changes.', (done) => {
331331
const originalFactory = outerFactory;
332332
outerFactory = newSplitFactoryLocalhostInstance();
333333

src/__tests__/constants.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ describe('getControlTreatmentsWithConfig', () => {
88
});
99

1010
it('should return an empty object if an empty array is provided', () => {
11-
const splitNames = ['split1', 'split2'];
12-
const treatments: SplitIO.TreatmentsWithConfig = getControlTreatmentsWithConfig(splitNames);
13-
splitNames.forEach((splitName) => {
14-
expect(treatments[splitName]).toBe(CONTROL_WITH_CONFIG);
11+
const featureFlagNames = ['split1', 'split2'];
12+
const treatments: SplitIO.TreatmentsWithConfig = getControlTreatmentsWithConfig(featureFlagNames);
13+
featureFlagNames.forEach((featureFlagName) => {
14+
expect(treatments[featureFlagName]).toBe(CONTROL_WITH_CONFIG);
1515
});
16-
expect(Object.keys(treatments).length).toBe(splitNames.length);
16+
expect(Object.keys(treatments).length).toBe(featureFlagNames.length);
1717
});
1818

1919
});

src/__tests__/useTreatments.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import useTreatments from '../useTreatments';
2525

2626
describe('useTreatments', () => {
2727

28-
const splitNames = ['split1'];
28+
const featureFlagNames = ['split1'];
2929
const attributes = { att1: 'att1' };
3030

3131
test('returns the treatments evaluated by the client at Split context updated by SplitFactory, or control if the client is not operational.', () => {
@@ -36,7 +36,7 @@ describe('useTreatments', () => {
3636
render(
3737
<SplitFactory factory={outerFactory} >{
3838
React.createElement(() => {
39-
treatments = useTreatments(splitNames, attributes);
39+
treatments = useTreatments(featureFlagNames, attributes);
4040
return null;
4141
})}</SplitFactory>,
4242
);
@@ -45,10 +45,10 @@ describe('useTreatments', () => {
4545
expect(client.getTreatmentsWithConfig).not.toBeCalled();
4646
expect(treatments!).toEqual({ split1: CONTROL_WITH_CONFIG });
4747

48-
// once operational (SDK_READY), it evaluates splits
48+
// once operational (SDK_READY), it evaluates feature flags
4949
act(() => client.__emitter__.emit(Event.SDK_READY));
5050

51-
expect(client.getTreatmentsWithConfig).toBeCalledWith(splitNames, attributes);
51+
expect(client.getTreatmentsWithConfig).toBeCalledWith(featureFlagNames, attributes);
5252
expect(client.getTreatmentsWithConfig).toHaveReturnedWith(treatments);
5353
});
5454

@@ -61,7 +61,7 @@ describe('useTreatments', () => {
6161
<SplitFactory factory={outerFactory} >
6262
<SplitClient splitKey='user2' >{
6363
React.createElement(() => {
64-
treatments = useTreatments(splitNames, attributes);
64+
treatments = useTreatments(featureFlagNames, attributes);
6565
return null;
6666
})}
6767
</SplitClient>
@@ -72,10 +72,10 @@ describe('useTreatments', () => {
7272
expect(client.getTreatmentsWithConfig).not.toBeCalled();
7373
expect(treatments!).toEqual({ split1: CONTROL_WITH_CONFIG });
7474

75-
// once operational (SDK_READY_FROM_CACHE), it evaluates splits
75+
// once operational (SDK_READY_FROM_CACHE), it evaluates feature flags
7676
act(() => client.__emitter__.emit(Event.SDK_READY_FROM_CACHE));
7777

78-
expect(client.getTreatmentsWithConfig).toBeCalledWith(splitNames, attributes);
78+
expect(client.getTreatmentsWithConfig).toBeCalledWith(featureFlagNames, attributes);
7979
expect(client.getTreatmentsWithConfig).toHaveReturnedWith(treatments);
8080
});
8181

@@ -90,7 +90,7 @@ describe('useTreatments', () => {
9090
render(
9191
<SplitFactory factory={outerFactory} >{
9292
React.createElement(() => {
93-
treatments = useTreatments(splitNames, attributes, 'user2');
93+
treatments = useTreatments(featureFlagNames, attributes, 'user2');
9494
return null;
9595
})}
9696
</SplitFactory>,
@@ -108,16 +108,16 @@ describe('useTreatments', () => {
108108
render(
109109
React.createElement(
110110
() => {
111-
treatments = useTreatments(splitNames, attributes);
111+
treatments = useTreatments(featureFlagNames, attributes);
112112
return null;
113113
}),
114114
);
115-
expect(getControlTreatmentsWithConfig).toBeCalledWith(splitNames);
115+
expect(getControlTreatmentsWithConfig).toBeCalledWith(featureFlagNames);
116116
expect(getControlTreatmentsWithConfig).toHaveReturnedWith(treatments);
117117
});
118118

119119
/**
120-
* Input validation. Passing invalid split names or attributes while the Sdk
120+
* Input validation. Passing invalid feature flag names or attributes while the Sdk
121121
* is not ready doesn't emit errors, and logs meaningful messages instead.
122122
*/
123123
test('Input validation: invalid "names" and "attributes" params in useTreatments.', (done) => {

src/__tests__/withSplitTreatments.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ describe('withSplitTreatments', () => {
2020
it(`passes Split props and outer props to the child.
2121
In this test, the value of "props.treatments" is obteined by the function "getControlTreatmentsWithConfig",
2222
and not "client.getTreatmentsWithConfig" since the client is not ready.`, (done) => {
23-
const splitNames = ['split1', 'split2'];
23+
const featureFlagNames = ['split1', 'split2'];
2424
const Component = withSplitFactory(sdkBrowser)<{ outerProp1: string, outerProp2: number }>(
2525
({ outerProp1, outerProp2, factory }) => {
2626
const SubComponent = withSplitClient('user1')<{ outerProp1: string, outerProp2: number }>(
27-
withSplitTreatments(splitNames)(
27+
withSplitTreatments(featureFlagNames)(
2828
(props: ISplitTreatmentsChildProps & { outerProp1: string, outerProp2: number }) => {
2929
const clientMock = factory!.client('user1');
3030
expect(props.outerProp1).toBe('outerProp1');
3131
expect(props.outerProp2).toBe(2);
3232
expect((clientMock.getTreatmentsWithConfig as jest.Mock).mock.calls.length).toBe(0);
33-
expect(props.treatments).toEqual(getControlTreatmentsWithConfig(splitNames));
33+
expect(props.treatments).toEqual(getControlTreatmentsWithConfig(featureFlagNames));
3434
expect(props.isReady).toBe(false);
3535
expect(props.isReadyFromCache).toBe(false);
3636
expect(props.hasTimedout).toBe(false);

src/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export const CONTROL_WITH_CONFIG: SplitIO.TreatmentWithConfig = {
1616
};
1717

1818
export const getControlTreatmentsWithConfig = (splitNames: unknown): SplitIO.TreatmentsWithConfig => {
19-
// validate split Names
20-
const validatedSplitNames = validateFeatureFlags(splitNames);
19+
// validate featureFlags Names
20+
const validatedFeatureFlagNames = validateFeatureFlags(splitNames);
2121

2222
// return empty object if the returned value is false
23-
if (!validatedSplitNames) return {};
23+
if (!validatedFeatureFlagNames) return {};
2424

2525
// return control treatments for each validated split name
26-
return validatedSplitNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
26+
return validatedFeatureFlagNames.reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
2727
pValue[cValue] = CONTROL_WITH_CONFIG;
2828
return pValue;
2929
}, {});

0 commit comments

Comments
 (0)