Skip to content

Commit 9ed196d

Browse files
committed
test: update FeatureFlag tests for new API
- Update tests to use new `flagKey` prop instead of `featureKey` - Add comprehensive test coverage for custom predicate function - Add tests for truthy/falsy flag evaluation without match values - Fix string match test to use actual flag value instead of variant - Improve type safety in test predicate functions
1 parent f92f889 commit 9ed196d

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

packages/react/test/declarative.spec.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('Feature Component', () => {
5959
it('should not show the feature component if the flag is not enabled', () => {
6060
render(
6161
<OpenFeatureProvider domain={EVALUATION}>
62-
<FeatureFlag featureKey={BOOL_FLAG_KEY} defaultValue={false}>
62+
<FeatureFlag flagKey={BOOL_FLAG_KEY} defaultValue={false}>
6363
<ChildComponent />
6464
</FeatureFlag>
6565
</OpenFeatureProvider>,
@@ -71,7 +71,7 @@ describe('Feature Component', () => {
7171
it('should fallback when provided', () => {
7272
render(
7373
<OpenFeatureProvider domain={EVALUATION}>
74-
<FeatureFlag featureKey={MISSING_FLAG_KEY} defaultValue={false} fallback={<div>Fallback</div>}>
74+
<FeatureFlag flagKey={MISSING_FLAG_KEY} defaultValue={false} fallback={<div>Fallback</div>}>
7575
<ChildComponent />
7676
</FeatureFlag>
7777
</OpenFeatureProvider>,
@@ -82,10 +82,10 @@ describe('Feature Component', () => {
8282
screen.debug();
8383
});
8484

85-
it('should handle showing multivariate flags with bool match', () => {
85+
it('should handle showing multivariate flags with string match', () => {
8686
render(
8787
<OpenFeatureProvider domain={EVALUATION}>
88-
<FeatureFlag featureKey={STRING_FLAG_KEY} match={'greeting'} defaultValue={'default'}>
88+
<FeatureFlag flagKey={STRING_FLAG_KEY} match={'hi'} defaultValue={'default'}>
8989
<ChildComponent />
9090
</FeatureFlag>
9191
</OpenFeatureProvider>,
@@ -94,16 +94,45 @@ describe('Feature Component', () => {
9494
expect(screen.queryByText(childText)).toBeInTheDocument();
9595
});
9696

97-
it('should show the feature component if the flag is not enabled but negate is true', () => {
97+
it('should support custom predicate function', () => {
98+
const customPredicate = (expected: boolean | undefined, actual: { value: boolean }) => {
99+
// Custom logic: render if flag is NOT the expected value (negation)
100+
return expected !== undefined ? actual.value !== expected : !actual.value;
101+
};
102+
103+
render(
104+
<OpenFeatureProvider domain={EVALUATION}>
105+
<FeatureFlag flagKey={BOOL_FLAG_NEGATE_KEY} match={true} predicate={customPredicate} defaultValue={false}>
106+
<ChildComponent />
107+
</FeatureFlag>
108+
</OpenFeatureProvider>,
109+
);
110+
111+
expect(screen.queryByText(childText)).toBeInTheDocument();
112+
});
113+
114+
it('should render children when no match is provided and flag is truthy', () => {
98115
render(
99116
<OpenFeatureProvider domain={EVALUATION}>
100-
<FeatureFlag featureKey={BOOL_FLAG_KEY} defaultValue={false}>
117+
<FeatureFlag flagKey={BOOL_FLAG_KEY} defaultValue={false}>
101118
<ChildComponent />
102119
</FeatureFlag>
103120
</OpenFeatureProvider>,
104121
);
105122

106123
expect(screen.queryByText(childText)).toBeInTheDocument();
107124
});
125+
126+
it('should not render children when no match is provided and flag is falsy', () => {
127+
render(
128+
<OpenFeatureProvider domain={EVALUATION}>
129+
<FeatureFlag flagKey={BOOL_FLAG_NEGATE_KEY} defaultValue={false}>
130+
<ChildComponent />
131+
</FeatureFlag>
132+
</OpenFeatureProvider>,
133+
);
134+
135+
expect(screen.queryByText(childText)).not.toBeInTheDocument();
136+
});
108137
});
109138
});

0 commit comments

Comments
 (0)