Skip to content
Open
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
13 changes: 9 additions & 4 deletions packages/react-native/Libraries/Core/setUpGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
* Sets up global variables for React Native.
* You can use this module directly, or just require InitializeCore.
*/
if (global.window === undefined) {
if (globalThis.window === undefined) {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.window = global;
Object.defineProperty(globalThis, 'window', {
value: globalThis,
configurable: false,
enumerable: true,
writable: false,
});
}

if (global.self === undefined) {
if (globalThis.self === undefined) {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.self = global;
globalThis.self = globalThis;
}

// Set up process
Expand Down
7 changes: 6 additions & 1 deletion packages/react-native/Libraries/Core/setUpNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const {polyfillObjectProperty} = require('../Utilities/PolyfillFunctions');
const navigator = global.navigator;
if (navigator === undefined) {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.navigator = {product: 'ReactNative'};
Object.defineProperty(global, 'navigator', {
value: {product: 'ReactNative'},
configurable: true,
enumerable: true,
writable: false,
});
} else {
// see https://github.com/facebook/react-native/issues/10881
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
declare var PerformanceObserverEntryList: unknown;
declare var EventCounts: unknown;

function expectGlobalDescriptorToMatch(
name: string,
expected: {
enumerable: boolean,
configurable: boolean,
writable: boolean,
},
) {
const descriptor = Object.getOwnPropertyDescriptor(globalThis, name);
if (descriptor == null) {
throw new Error(`Expected globalThis.${name} to be defined`);
}
expect({
enumerable: descriptor.enumerable,
configurable: descriptor.configurable,
writable: descriptor.writable,
}).toEqual(expected);
}

describe('setUpDefaultReactNativeEnvironment (globals)', () => {
describe('global object', () => {
it('should be exposed as globalThis, global, window and self', () => {
Expand All @@ -23,6 +42,56 @@ describe('setUpDefaultReactNativeEnvironment (globals)', () => {
});
});

describe('property descriptors', () => {
it('should define window as enumerable but not configurable or writable', () => {
expectGlobalDescriptorToMatch('window', {
enumerable: true,
configurable: false,
writable: false,
});
});

it('should define self as configurable, enumerable and writable', () => {
expectGlobalDescriptorToMatch('self', {
enumerable: true,
configurable: true,
writable: true,
});
});

it('should define navigator as configurable and enumerable but not writable', () => {
expectGlobalDescriptorToMatch('navigator', {
enumerable: true,
configurable: true,
writable: false,
});
});

it('should define Infinity as not configurable, writable or enumerable', () => {
expectGlobalDescriptorToMatch('Infinity', {
enumerable: false,
configurable: false,
writable: false,
});
});

it('should define NaN as not configurable, writable or enumerable', () => {
expectGlobalDescriptorToMatch('NaN', {
enumerable: false,
configurable: false,
writable: false,
});
});

it('should define undefined as not configurable, writable or enumerable', () => {
expectGlobalDescriptorToMatch('undefined', {
enumerable: false,
configurable: false,
writable: false,
});
});
});

describe('environment', () => {
it('should provide process.env.NODE_ENV', () => {
expect(process.env.NODE_ENV).toBe('development');
Expand Down
Loading