react-native/Libraries/Utilities/__tests__/deepFreezeAndThrowOnMutatio...

144 lines
4.6 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationInDev');
describe('deepFreezeAndThrowOnMutationInDev', function() {
it('should be a noop on non object values', () => {
__DEV__ = true;
expect(() => deepFreezeAndThrowOnMutationInDev('')).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(null)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(false)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(5)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev()).not.toThrow();
__DEV__ = false;
expect(() => deepFreezeAndThrowOnMutationInDev('')).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(null)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(false)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev(5)).not.toThrow();
expect(() => deepFreezeAndThrowOnMutationInDev()).not.toThrow();
});
Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… (#19598) Summary: This PR fixes a bug in `deepFreezeAndThrowOnMutationInDev` which did not take into account that objects passed to it may have been created with `Object.create(null)` and thus may not have a prototype. Such objects don't have the methods `hasOwnProperty`, `__defineGetter__`, or `__defineSetter__` on the instance. I ran into an unrecoverable error in React Native when passing this type of object across the bridge because `deepFreezeAndThrowOnMutationInDev` attempts to call `object.hasOwnProperty(key)`, `object.__defineGetter__` and `object__defineSetter__` on objects passed to it. But my object instance does not have these prototype methods. Changes: * Defined `Object.prototype.hasOwnProperty` as a `const` (pattern used elsewhere in React Native) * Modified calls to `object.hasOwnProperty(key)` to use `hasOwnProperty.call(object, key)` (Per ESLint rule [here](https://eslint.org/docs/rules/no-prototype-builtins)) * Modified calls to deprecated methods `object.__defineGetter__` and `object.__defineSetter__` to instead use `Object.defineProperty` to define get and set methods on the object. (Per guidance on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__)) * Added a new test to `deepFreezeAndThrowOnMutationInDev-test` to verify the fix. I tried to create a reproducible example to post to Snack by passing prototype-less objects to a `Text` component, in various ways, but they appear to be converted to plain objects before crossing the bridge and therefore they do not throw an error. However, I was able to create a new test to reproduce the issue. I added the following test to `deepFreezeAndThrowOnMutationInDev-test`: ```JavaScript it('should not throw on object without prototype', () => { __DEV__ = true; var o = Object.create(null); o.key = 'Value'; expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow(); }); ``` The changes in this PR include this new test. ESLint test produced no change in Error count (3) or Warnings (671) N/A Other areas with _possibly_ the same issue: https://github.com/facebook/react-native/blob/c6b96c0df789717d53ec520ad28ba0ae00db6ec2/Libraries/vendor/core/mergeInto.js#L50 https://github.com/facebook/react-native/blob/8dc3ba0444c94d9bbb66295b5af885bff9b9cd34/Libraries/ReactNative/requireNativeComponent.js#L134 [GENERAL] [BUGFIX] [Libraries/Utilities/deepFreezeAndThrowOnMutationInDev] -Fix for compatibility with objects without a prototype. Closes https://github.com/facebook/react-native/pull/19598 Differential Revision: D8310845 Pulled By: TheSavior fbshipit-source-id: 020c414a1062a637e97f9ee99bf8e5ba2d1fcf4f
2018-06-07 09:26:17 +00:00
it('should not throw on object without prototype', () => {
__DEV__ = true;
var o = Object.create(null);
o.key = 'Value';
expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow();
});
it('should throw on mutation in dev with strict', () => {
'use strict';
__DEV__ = true;
const o = {key: 'oldValue'};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key = 'newValue';
}).toThrowError(
'You attempted to set the key `key` with the value `"newValue"` ' +
'on an object that is meant to be immutable and has been frozen.',
);
expect(o.key).toBe('oldValue');
});
it('should throw on mutation in dev without strict', () => {
__DEV__ = true;
const o = {key: 'oldValue'};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key = 'newValue';
}).toThrowError(
'You attempted to set the key `key` with the value `"newValue"` ' +
'on an object that is meant to be immutable and has been frozen.',
);
expect(o.key).toBe('oldValue');
});
it('should throw on nested mutation in dev with strict', () => {
'use strict';
__DEV__ = true;
const o = {key1: {key2: {key3: 'oldValue'}}};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key1.key2.key3 = 'newValue';
}).toThrowError(
'You attempted to set the key `key3` with the value `"newValue"` ' +
'on an object that is meant to be immutable and has been frozen.',
);
expect(o.key1.key2.key3).toBe('oldValue');
});
it('should throw on nested mutation in dev without strict', () => {
__DEV__ = true;
const o = {key1: {key2: {key3: 'oldValue'}}};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key1.key2.key3 = 'newValue';
}).toThrowError(
'You attempted to set the key `key3` with the value `"newValue"` ' +
'on an object that is meant to be immutable and has been frozen.',
);
expect(o.key1.key2.key3).toBe('oldValue');
});
it('should throw on insertion in dev with strict', () => {
'use strict';
__DEV__ = true;
const o = {oldKey: 'value'};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.newKey = 'value';
}).toThrowError(
/(Cannot|Can't) add property newKey, object is not extensible/,
);
expect(o.newKey).toBe(undefined);
});
it('should not throw on insertion in dev without strict', () => {
__DEV__ = true;
const o = {oldKey: 'value'};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.newKey = 'value';
}).not.toThrow();
expect(o.newKey).toBe(undefined);
});
it('should mutate and not throw on mutation in prod', () => {
'use strict';
__DEV__ = false;
const o = {key: 'oldValue'};
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key = 'newValue';
}).not.toThrow();
expect(o.key).toBe('newValue');
});
// This is a limitation of the technique unfortunately
it('should not deep freeze already frozen objects', () => {
'use strict';
__DEV__ = true;
const o = {key1: {key2: 'oldValue'}};
Object.freeze(o);
deepFreezeAndThrowOnMutationInDev(o);
expect(() => {
o.key1.key2 = 'newValue';
}).not.toThrow();
expect(o.key1.key2).toBe('newValue');
});
it("shouldn't recurse infinitely", () => {
__DEV__ = true;
const o = {};
o.circular = o;
deepFreezeAndThrowOnMutationInDev(o);
});
});