|
| 1 | +import React from 'react'; |
| 2 | +import '@testing-library/jest-dom'; // see: https://testing-library.com/docs/react-testing-library/setup |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import { FeatureFlag } from '../src/declarative/FeatureFlag'; // Assuming Feature.tsx is in the same directory or adjust path |
| 5 | +import { InMemoryProvider, OpenFeature, OpenFeatureProvider } from '../src'; |
| 6 | + |
| 7 | +describe('Feature Component', () => { |
| 8 | + const EVALUATION = 'evaluation'; |
| 9 | + const MISSING_FLAG_KEY = 'missing-flag'; |
| 10 | + const BOOL_FLAG_KEY = 'boolean-flag'; |
| 11 | + const BOOL_FLAG_NEGATE_KEY = 'boolean-flag-negate'; |
| 12 | + const BOOL_FLAG_VARIANT = 'on'; |
| 13 | + const BOOL_FLAG_VALUE = true; |
| 14 | + const STRING_FLAG_KEY = 'string-flag'; |
| 15 | + const STRING_FLAG_VARIANT = 'greeting'; |
| 16 | + const STRING_FLAG_VALUE = 'hi'; |
| 17 | + |
| 18 | + const FLAG_CONFIG: ConstructorParameters<typeof InMemoryProvider>[0] = { |
| 19 | + [BOOL_FLAG_KEY]: { |
| 20 | + disabled: false, |
| 21 | + variants: { |
| 22 | + [BOOL_FLAG_VARIANT]: BOOL_FLAG_VALUE, |
| 23 | + off: false, |
| 24 | + }, |
| 25 | + defaultVariant: BOOL_FLAG_VARIANT, |
| 26 | + }, |
| 27 | + [BOOL_FLAG_NEGATE_KEY]: { |
| 28 | + disabled: false, |
| 29 | + variants: { |
| 30 | + [BOOL_FLAG_VARIANT]: BOOL_FLAG_VALUE, |
| 31 | + off: false, |
| 32 | + }, |
| 33 | + defaultVariant: 'off', |
| 34 | + }, |
| 35 | + [STRING_FLAG_KEY]: { |
| 36 | + disabled: false, |
| 37 | + variants: { |
| 38 | + [STRING_FLAG_VARIANT]: STRING_FLAG_VALUE, |
| 39 | + parting: 'bye', |
| 40 | + }, |
| 41 | + defaultVariant: STRING_FLAG_VARIANT, |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const makeProvider = () => { |
| 46 | + return new InMemoryProvider(FLAG_CONFIG); |
| 47 | + }; |
| 48 | + |
| 49 | + OpenFeature.setProvider(EVALUATION, makeProvider()); |
| 50 | + |
| 51 | + const childText = 'Feature is active'; |
| 52 | + const ChildComponent = () => <div>{childText}</div>; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + jest.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('<FeatureFlag />', () => { |
| 59 | + it('should not show the feature component if the flag is not enabled', () => { |
| 60 | + render( |
| 61 | + <OpenFeatureProvider domain={EVALUATION}> |
| 62 | + <FeatureFlag featureKey={BOOL_FLAG_KEY} defaultValue={false}> |
| 63 | + <ChildComponent /> |
| 64 | + </FeatureFlag> |
| 65 | + </OpenFeatureProvider>, |
| 66 | + ); |
| 67 | + |
| 68 | + expect(screen.queryByText(childText)).toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fallback when provided', () => { |
| 72 | + render( |
| 73 | + <OpenFeatureProvider domain={EVALUATION}> |
| 74 | + <FeatureFlag featureKey={MISSING_FLAG_KEY} defaultValue={false} fallback={<div>Fallback</div>}> |
| 75 | + <ChildComponent /> |
| 76 | + </FeatureFlag> |
| 77 | + </OpenFeatureProvider>, |
| 78 | + ); |
| 79 | + |
| 80 | + expect(screen.queryByText('Fallback')).toBeInTheDocument(); |
| 81 | + |
| 82 | + screen.debug(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle showing multivariate flags with bool match', () => { |
| 86 | + render( |
| 87 | + <OpenFeatureProvider domain={EVALUATION}> |
| 88 | + <FeatureFlag featureKey={STRING_FLAG_KEY} match={'greeting'} defaultValue={'default'}> |
| 89 | + <ChildComponent /> |
| 90 | + </FeatureFlag> |
| 91 | + </OpenFeatureProvider>, |
| 92 | + ); |
| 93 | + |
| 94 | + expect(screen.queryByText(childText)).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should show the feature component if the flag is not enabled but negate is true', () => { |
| 98 | + render( |
| 99 | + <OpenFeatureProvider domain={EVALUATION}> |
| 100 | + <FeatureFlag featureKey={BOOL_FLAG_KEY} defaultValue={false}> |
| 101 | + <ChildComponent /> |
| 102 | + </FeatureFlag> |
| 103 | + </OpenFeatureProvider>, |
| 104 | + ); |
| 105 | + |
| 106 | + expect(screen.queryByText(childText)).toBeInTheDocument(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments