Skip to content

Commit 01746e3

Browse files
Merge branch 'hooks_export' into hooks_useSplitManager
2 parents 50299e7 + 209a412 commit 01746e3

9 files changed

Lines changed: 50 additions & 22 deletions

src/__tests__/withSplitTreatments.test.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,42 @@ import { sdkBrowser } from './testUtils/sdkConfigs';
1212
import { withSplitFactory } from '../withSplitFactory';
1313
import { withSplitClient } from '../withSplitClient';
1414
import { withSplitTreatments } from '../withSplitTreatments';
15-
import { ISplitTreatmentsChildProps } from '../types';
1615
import { getControlTreatmentsWithConfig } from '../constants';
1716

1817
describe('withSplitTreatments', () => {
1918

2019
it(`passes Split props and outer props to the child.
21-
In this test, the value of "props.treatments" is obteined by the function "getControlTreatmentsWithConfig",
22-
and not "client.getTreatmentsWithConfig" since the client is not ready.`, (done) => {
20+
In this test, the value of "props.treatments" is obtained by the function "getControlTreatmentsWithConfig",
21+
and not "client.getTreatmentsWithConfig" since the client is not ready.`, () => {
2322
const featureFlagNames = ['split1', 'split2'];
23+
2424
const Component = withSplitFactory(sdkBrowser)<{ outerProp1: string, outerProp2: number }>(
2525
({ outerProp1, outerProp2, factory }) => {
2626
const SubComponent = withSplitClient('user1')<{ outerProp1: string, outerProp2: number }>(
2727
withSplitTreatments(featureFlagNames)(
28-
(props: ISplitTreatmentsChildProps & { outerProp1: string, outerProp2: number }) => {
28+
(props) => {
2929
const clientMock = factory!.client('user1');
30-
expect(props.outerProp1).toBe('outerProp1');
31-
expect(props.outerProp2).toBe(2);
3230
expect((clientMock.getTreatmentsWithConfig as jest.Mock).mock.calls.length).toBe(0);
33-
expect(props.treatments).toEqual(getControlTreatmentsWithConfig(featureFlagNames));
34-
expect(props.isReady).toBe(false);
35-
expect(props.isReadyFromCache).toBe(false);
36-
expect(props.hasTimedout).toBe(false);
37-
expect(props.isTimedout).toBe(false);
38-
expect(props.isDestroyed).toBe(false);
39-
expect(props.lastUpdate).toBe(0);
40-
done();
31+
32+
expect(props).toStrictEqual({
33+
factory: factory, client: clientMock,
34+
outerProp1: 'outerProp1', outerProp2: 2,
35+
treatments: getControlTreatmentsWithConfig(featureFlagNames),
36+
isReady: false,
37+
isReadyFromCache: false,
38+
hasTimedout: false,
39+
isTimedout: false,
40+
isDestroyed: false,
41+
lastUpdate: 0
42+
});
43+
4144
return null;
4245
}
4346
)
4447
);
4548
return <SubComponent outerProp1={outerProp1} outerProp2={outerProp2} />;
4649
});
50+
4751
render(<Component outerProp1='outerProp1' outerProp2={2} />);
4852
});
4953

src/useClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useSplitClient } from './useSplitClient';
88
* @return A Split Client instance, or null if used outside the scope of SplitFactory
99
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
1010
*
11-
* @deprecated useSplitClient is the new hook to use.
11+
* @deprecated Replace with the new `useSplitClient` hook.
1212
*/
1313
export function useClient(splitKey?: SplitIO.SplitKey, trafficType?: string, attributes?: SplitIO.Attributes): SplitIO.IBrowserClient | null {
1414
return useSplitClient({ splitKey, trafficType, attributes }).client;

src/useSplitClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export const DEFAULT_UPDATE_OPTIONS = {
1515
* It uses the 'useContext' hook to access the context, which is updated by SplitFactory and SplitClient components in the hierarchy of components.
1616
*
1717
* @return A Split Context object
18+
*
19+
* @example
20+
* ```js
21+
* const { factory, client, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitClient({ splitKey: 'user_id' });
22+
* ```
23+
*
1824
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
1925
*/
2026
export function useSplitClient(options?: IUseSplitClientOptions): ISplitContextValues {

src/useSplitTreatments.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import { ISplitTreatmentsChildProps, IUseSplitTreatmentsOptions } from './types'
55
import { useSplitClient } from './useSplitClient';
66

77
/**
8-
* 'useSplitTreatments' is a hook that returns an SplitContext object extended with a `treatments` property containing an object of feature flag evaluations (i.e., treatments).
8+
* 'useSplitTreatments' is a hook that returns an SplitContext object extended with a `treatments` property object that contains feature flag evaluations.
99
* It uses the 'useSplitClient' hook to access the client from the Split context, and invokes the 'getTreatmentsWithConfig' method.
1010
*
11-
* @return A Split Context object extended with a TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if split names do not exist.
11+
* @return A Split Context object extended with a TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if feature flag names do not exist.
12+
*
13+
* @example
14+
* ```js
15+
* const { treatments: { feature_1, feature_2 }, isReady, isReadyFromCache, hasTimedout, lastUpdate, ... } = useSplitTreatments({ names: ['feature_1', 'feature_2']});
16+
* ```
17+
*
1218
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
1319
*/
1420
export function useSplitTreatments(options: IUseSplitTreatmentsOptions): ISplitTreatmentsChildProps {

src/useTreatments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useSplitTreatments } from './useSplitTreatments';
88
* @return A TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if feature flag names do not exist.
99
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
1010
*
11-
* @deprecated useSplitTreatments is the new hook to use.
11+
* @deprecated Replace with the new `useSplitTreatments` hook.
1212
*/
1313
export function useTreatments(featureFlagNames: string[], attributes?: SplitIO.Attributes, splitKey?: SplitIO.SplitKey): SplitIO.TreatmentsWithConfig {
1414
return useSplitTreatments({ names: featureFlagNames, attributes, splitKey }).treatments;

types/useClient.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @return A Split Client instance, or null if used outside the scope of SplitFactory
77
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
88
*
9-
* @deprecated useSplitClient is the new hook to use.
9+
* @deprecated Replace with the new `useSplitClient` hook.
1010
*/
1111
export declare function useClient(splitKey?: SplitIO.SplitKey, trafficType?: string, attributes?: SplitIO.Attributes): SplitIO.IBrowserClient | null;

types/useSplitClient.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export declare const DEFAULT_UPDATE_OPTIONS: {
1010
* It uses the 'useContext' hook to access the context, which is updated by SplitFactory and SplitClient components in the hierarchy of components.
1111
*
1212
* @return A Split Context object
13+
*
14+
* @example
15+
* ```js
16+
* const { factory, client, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitClient({ splitKey: 'user_id' });
17+
* ```
18+
*
1319
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
1420
*/
1521
export declare function useSplitClient(options?: IUseSplitClientOptions): ISplitContextValues;

types/useSplitTreatments.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { ISplitTreatmentsChildProps, IUseSplitTreatmentsOptions } from './types';
22
/**
3-
* 'useSplitTreatments' is a hook that returns an SplitContext object extended with a `treatments` property containing an object of feature flag evaluations (i.e., treatments).
3+
* 'useSplitTreatments' is a hook that returns an SplitContext object extended with a `treatments` property object that contains feature flag evaluations.
44
* It uses the 'useSplitClient' hook to access the client from the Split context, and invokes the 'getTreatmentsWithConfig' method.
55
*
6-
* @return A Split Context object extended with a TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if split names do not exist.
6+
* @return A Split Context object extended with a TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if feature flag names do not exist.
7+
*
8+
* @example
9+
* ```js
10+
* const { treatments: { feature_1, feature_2 }, isReady, isReadyFromCache, hasTimedout, lastUpdate, ... } = useSplitTreatments({ names: ['feature_1', 'feature_2']});
11+
* ```
12+
*
713
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
814
*/
915
export declare function useSplitTreatments(options: IUseSplitTreatmentsOptions): ISplitTreatmentsChildProps;

types/useTreatments.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @return A TreatmentsWithConfig instance, that might contain control treatments if the client is not available or ready, or if feature flag names do not exist.
77
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#get-treatments-with-configurations}
88
*
9-
* @deprecated useSplitTreatments is the new hook to use.
9+
* @deprecated Replace with the new `useSplitTreatments` hook.
1010
*/
1111
export declare function useTreatments(featureFlagNames: string[], attributes?: SplitIO.Attributes, splitKey?: SplitIO.SplitKey): SplitIO.TreatmentsWithConfig;

0 commit comments

Comments
 (0)