forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepFreezeAndThrowOnMutationInDev-test.js
More file actions
129 lines (116 loc) · 4.56 KB
/
deepFreezeAndThrowOnMutationInDev-test.js
File metadata and controls
129 lines (116 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
var 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();
});
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;
var 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;
var 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;
var 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;
var 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;
var 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;
var 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;
var 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;
var 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;
var o = {};
o.circular = o;
deepFreezeAndThrowOnMutationInDev(o);
});
});