|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 12 | + |
| 13 | +import * as ReactNativeFeatureFlags from '../ReactNativeFeatureFlags'; |
| 14 | +import * as ReactNativeFeatureFlagsBase from '../ReactNativeFeatureFlagsBase'; |
| 15 | + |
| 16 | +/** |
| 17 | + * These tests cover the JS-only feature flag behavior that does not require |
| 18 | + * jest.mock() / jest.doMock() / jest.fn() and is therefore convertible to |
| 19 | + * Fantom integration tests. |
| 20 | + * |
| 21 | + * Tests that are NOT convertible (require jest module mocking): |
| 22 | + * - "should provide default values for common flags and NOT log an error if |
| 23 | + * the native module is available but the method is NOT defined" |
| 24 | + * - "should access and cache common flags from the native module if it is available" |
| 25 | + * - "when the native module is NOT available" (all sub-cases) |
| 26 | + */ |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + ReactNativeFeatureFlagsBase.dangerouslyResetForTesting(); |
| 30 | +}); |
| 31 | + |
| 32 | +test('should provide default values for JS-only flags', () => { |
| 33 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(false); |
| 34 | +}); |
| 35 | + |
| 36 | +test('should access and cache overridden JS-only flags', () => { |
| 37 | + let callCount = 0; |
| 38 | + ReactNativeFeatureFlags.override({ |
| 39 | + jsOnlyTestFlag: () => { |
| 40 | + callCount++; |
| 41 | + return true; |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + expect(callCount).toBe(0); |
| 46 | + |
| 47 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(true); |
| 48 | + expect(callCount).toBe(1); |
| 49 | + |
| 50 | + // Second call should return cached value without calling the override again. |
| 51 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(true); |
| 52 | + expect(callCount).toBe(1); |
| 53 | +}); |
| 54 | + |
| 55 | +test('should throw an error if any of the JS flags has been accessed before overriding', () => { |
| 56 | + ReactNativeFeatureFlags.jsOnlyTestFlag(); |
| 57 | + |
| 58 | + expect(() => |
| 59 | + ReactNativeFeatureFlags.override({ |
| 60 | + jsOnlyTestFlag: () => true, |
| 61 | + }), |
| 62 | + ).toThrow( |
| 63 | + 'Feature flags were accessed before being overridden: jsOnlyTestFlag', |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test('should NOT throw an error if any of the common flags has been accessed before overriding', () => { |
| 68 | + ReactNativeFeatureFlags.commonTestFlag(); |
| 69 | + |
| 70 | + expect(() => { |
| 71 | + ReactNativeFeatureFlags.override({ |
| 72 | + jsOnlyTestFlag: () => true, |
| 73 | + }); |
| 74 | + }).not.toThrow(); |
| 75 | + |
| 76 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(true); |
| 77 | +}); |
| 78 | + |
| 79 | +test('should throw an error when trying to set overrides twice', () => { |
| 80 | + ReactNativeFeatureFlags.override({ |
| 81 | + jsOnlyTestFlag: () => true, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(() => |
| 85 | + ReactNativeFeatureFlags.override({ |
| 86 | + jsOnlyTestFlag: () => false, |
| 87 | + }), |
| 88 | + ).toThrow('Feature flags cannot be overridden more than once'); |
| 89 | +}); |
| 90 | + |
| 91 | +test('should evaluate to default value if the override returns null', () => { |
| 92 | + ReactNativeFeatureFlags.override({ |
| 93 | + // $FlowExpectedError[incompatible-call] |
| 94 | + jsOnlyTestFlag: () => null, |
| 95 | + }); |
| 96 | + |
| 97 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(false); |
| 98 | +}); |
| 99 | + |
| 100 | +test('should evaluate to default value if the override returns undefined', () => { |
| 101 | + ReactNativeFeatureFlags.override({ |
| 102 | + // $FlowExpectedError[incompatible-call] |
| 103 | + jsOnlyTestFlag: () => undefined, |
| 104 | + }); |
| 105 | + |
| 106 | + expect(ReactNativeFeatureFlags.jsOnlyTestFlag()).toBe(false); |
| 107 | +}); |
0 commit comments