Skip to content

Commit 9a65698

Browse files
author
James Fox
committed
update tests
1 parent 14f657a commit 9a65698

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/Feature.spec.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ describe('<OptimizelyFeature>', () => {
110110
expect(component.text()).toBe('true|bar');
111111
});
112112

113+
it('should pass the values for clientReady and didTimeout', async () => {
114+
const component = mount(
115+
<OptimizelyProvider optimizely={optimizelyMock} timeout={200}>
116+
<OptimizelyFeature feature="feature1" timeout={100}>
117+
{(isEnabled, variables, clientReady, didTimeout) =>
118+
`${isEnabled ? 'true' : 'false'}|${variables.foo}|${clientReady}|${didTimeout}`
119+
}
120+
</OptimizelyFeature>
121+
</OptimizelyProvider>
122+
);
123+
124+
// while it's waiting for onReady()
125+
expect(component.text()).toBe('');
126+
resolver.resolve({ success: true });
127+
128+
await optimizelyMock.onReady();
129+
130+
component.update();
131+
132+
expect(optimizelyMock.isFeatureEnabled).toHaveBeenCalledWith('feature1', undefined, undefined);
133+
expect(optimizelyMock.getFeatureVariables).toHaveBeenCalledWith('feature1', undefined, undefined);
134+
expect(component.text()).toBe('true|bar|true|false');
135+
});
136+
113137
it('should respect a locally passed timeout prop', async () => {
114138
const component = mount(
115139
<OptimizelyProvider optimizely={optimizelyMock} timeout={200}>
@@ -134,6 +158,28 @@ describe('<OptimizelyFeature>', () => {
134158
expect(component.text()).toBe('true|bar');
135159
});
136160

161+
it('should pass the override props through', async () => {
162+
const component = mount(
163+
<OptimizelyProvider optimizely={optimizelyMock} timeout={200}>
164+
<OptimizelyFeature feature="feature1" overrideUserId="james123" overrideAttributes={{ betaUser: true }}>
165+
{(isEnabled, variables) => `${isEnabled ? 'true' : 'false'}|${variables.foo}`}
166+
</OptimizelyFeature>
167+
</OptimizelyProvider>
168+
);
169+
170+
// while it's waiting for onReady()
171+
expect(component.text()).toBe('');
172+
resolver.resolve({ success: true });
173+
174+
await optimizelyMock.onReady();
175+
176+
component.update();
177+
178+
expect(optimizelyMock.isFeatureEnabled).toHaveBeenCalledWith('feature1', 'james123', { betaUser: true });
179+
expect(optimizelyMock.getFeatureVariables).toHaveBeenCalledWith('feature1', 'james123', { betaUser: true });
180+
expect(component.text()).toBe('true|bar');
181+
});
182+
137183
describe(`when the "autoUpdate" prop is true`, () => {
138184
it('should update when the OPTIMIZELY_CONFIG_UPDATE handler is called', async () => {
139185
const component = mount(

src/Feature.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ export interface FeatureProps extends WithOptimizelyProps {
2626
autoUpdate?: boolean;
2727
overrideUserId?: string;
2828
overrideAttributes?: UserAttributes;
29-
children: (isEnabled: boolean, variables: VariableValuesObject) => React.ReactNode;
29+
children: (
30+
isEnabled: boolean,
31+
variables: VariableValuesObject,
32+
clientReady: boolean,
33+
didTimeout: boolean
34+
) => React.ReactNode;
3035
}
3136

32-
const FeatureComponent = (props: FeatureProps): any => {
37+
const FeatureComponent: React.FunctionComponent<FeatureProps> = props => {
3338
const { feature, timeout, autoUpdate, children, overrideUserId, overrideAttributes } = props;
3439
const [isEnabled, variables, clientReady, didTimeout] = useFeature(
3540
feature,
@@ -42,7 +47,7 @@ const FeatureComponent = (props: FeatureProps): any => {
4247
return null;
4348
}
4449

45-
return children(isEnabled, variables);
50+
return <>{children(isEnabled, variables, clientReady, didTimeout)}</>;
4651
};
4752

4853
export const OptimizelyFeature = withOptimizely(FeatureComponent);

src/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { useCallback, useContext, useEffect, useState } from 'react';
17-
import * as optimizely from '@optimizely/optimizely-sdk';
17+
import { UserAttributes } from '@optimizely/optimizely-sdk';
1818
import { getLogger } from '@optimizely/js-sdk-logging';
1919

2020
import { setupAutoUpdateListeners } from './autoUpdate';
@@ -38,7 +38,7 @@ type UseFeatureOptions = {
3838

3939
type UseFeatureOverrides = {
4040
overrideUserId?: string;
41-
overrideAttributes?: optimizely.UserAttributes;
41+
overrideAttributes?: UserAttributes;
4242
};
4343

4444
interface UseFeature {

0 commit comments

Comments
 (0)