Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import NativeReactNativeFeatureFlags from './specs/NativeReactNativeFeatureFlags
const accessedFeatureFlags: Set<string> = new Set();
let overrides: ?ReactNativeFeatureFlagsJsOnlyOverrides;

// This is a list of functions to clear the cached value for each feature flag
// getter. This is only used in development.
const clearCachedValuesFns: Array<() => void> = [];

export type Getter<T> = () => T;

// This defines the types for the overrides object, whose methods also receive
Expand All @@ -33,6 +37,12 @@ function createGetter<T: boolean | number | string>(
): Getter<T> {
let cachedValue: ?T;

if (__DEV__) {
clearCachedValuesFns.push(() => {
cachedValue = undefined;
});
}

return () => {
if (cachedValue == null) {
cachedValue = customValueGetter() ?? defaultValue;
Expand Down Expand Up @@ -115,3 +125,12 @@ function maybeLogUnavailableNativeModuleError(configName: string): void {
);
}
}

export function dangerouslyResetForTesting(): void {
if (__DEV__) {
overrides = null;
accessedFeatureFlags.clear();
reportedConfigNames.clear();
clearCachedValuesFns.forEach(fn => fn());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

import * as ReactNativeFeatureFlags from '../../featureflags/ReactNativeFeatureFlags';
import * as ReactNativeFeatureFlagsBase from '../../featureflags/ReactNativeFeatureFlagsBase';
import setUpDefaultReactNativeEnvironment from 'react-native/src/private/setup/setUpDefaultReactNativeEnvironment';

describe('setUpReactNativeEnvironment', () => {
it('should not read any feature flags', () => {
ReactNativeFeatureFlagsBase.dangerouslyResetForTesting();

setUpDefaultReactNativeEnvironment(false);

expect(() => {
// If any feature flags were read, this call would fail.
ReactNativeFeatureFlags.override({
jsOnlyTestFlag: () => true,
});
}).not.toThrow();
});
});
Loading