forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrict-mode.js
More file actions
242 lines (196 loc) · 6.17 KB
/
Copy pathstrict-mode.js
File metadata and controls
242 lines (196 loc) · 6.17 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var mobx = require("../../src/mobx.ts")
var utils = require("../utils/test-utils")
var strictError = /Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: /
test("strict mode should not allow changes outside action", () => {
var a = mobx.observable.box(2)
mobx.configure({ enforceActions: true })
// allowed, a is not observed
a.set(3)
var d = mobx.autorun(() => a.get())
// not-allowed, a is observed
expect(() => a.set(3)).toThrowError(strictError)
d()
mobx.configure({ enforceActions: false })
a.set(4)
expect(a.get()).toBe(4)
})
test("actions can modify observed state in strict mode", () => {
var a = mobx.observable.box(2)
var d = mobx.autorun(() => a.get())
mobx.configure({ enforceActions: true })
mobx.action(() => {
a.set(3)
var b = mobx.observable.box(4)
})()
mobx.configure({ enforceActions: false })
d()
})
test("actions can modify non-observed state in strict mode", () => {
var a = mobx.observable.box(2)
mobx.configure({ enforceActions: true })
mobx.action(() => {
a.set(3)
var b = mobx.observable.box(4)
})()
mobx.configure({ enforceActions: false })
})
test("reactions cannot modify state in strict mode", () => {
var a = mobx.observable.box(3)
var b = mobx.observable.box(4)
mobx.configure({ enforceActions: true })
mobx._resetGlobalState() // should preserve strict mode
var bd = mobx.autorun(() => {
b.get() // make sure it is observed
})
var d = mobx.autorun(() => {
expect(() => {
a.get()
b.set(3)
}).toThrowError(strictError)
})
d = mobx.autorun(() => {
if (a.get() > 5) b.set(7)
})
mobx.action(() => a.set(4))() // ok
expect(() => a.set(5)).toThrowError(strictError)
mobx.configure({ enforceActions: false })
d()
bd()
})
test("action inside reaction in strict mode can modify state", () => {
var a = mobx.observable.box(1)
var b = mobx.observable.box(2)
var bd = mobx.autorun(() => {
b.get() // make sure it is observed
})
mobx.configure({ enforceActions: true })
var act = mobx.action(() => b.set(b.get() + 1))
var d = mobx.autorun(() => {
if (a.get() % 2 === 0) act()
if (a.get() == 16) {
expect(() => b.set(55)).toThrowError(strictError)
}
})
var setA = mobx.action(val => a.set(val))
expect(b.get()).toBe(2)
setA(4)
expect(b.get()).toBe(3)
setA(5)
expect(b.get()).toBe(3)
setA(16)
expect(b.get()).toBe(4)
mobx.configure({ enforceActions: false })
bd()
d()
})
test("cannot create or modify objects in strict mode without action", () => {
var obj = mobx.observable({ a: 2 })
var ar = mobx.observable([1])
var map = mobx.observable.map({ a: 2 })
mobx.configure({ enforceActions: true })
// introducing new observables is ok!
// mobx.observable({ a: 2, b: function() { return this.a }});
// mobx.observable({ b: function() { return this.a } });
// mobx.observable.map({ a: 2});
// mobx.observable([1, 2, 3]);
// mobx.extendObservable(obj, { b: 4});
// t.throws(() => obj.a = 3, strictError);
// t.throws(() => ar[0] = 2, strictError);
// t.throws(() => ar.push(3), strictError);
// t.throws(() => map.set("a", 3), strictError);
// t.throws(() => map.set("b", 4), strictError);
// t.throws(() => map.delete("a"), strictError);
mobx.configure({ enforceActions: false })
// can modify again
obj.a = 42
})
test("can create objects in strict mode with action", () => {
var obj = mobx.observable({ a: 2 })
var ar = mobx.observable([1])
var map = mobx.observable.map({ a: 2 })
mobx.configure({ enforceActions: true })
mobx.action(() => {
mobx.observable({
a: 2,
b: function() {
return this.a
}
})
mobx.observable.map({ a: 2 })
mobx.observable([1, 2, 3])
obj.a = 3
mobx.extendObservable(obj, { b: 4 })
ar[0] = 2
ar.push(3)
map.set("a", 3)
map.set("b", 4)
map.delete("a")
})()
mobx.configure({ enforceActions: false })
})
test("strict mode checks", function() {
var x = mobx.observable.box(3)
var d = mobx.autorun(() => x.get())
mobx._allowStateChanges(false, function() {
x.get()
})
mobx._allowStateChanges(true, function() {
x.set(7)
})
expect(function() {
mobx._allowStateChanges(false, function() {
x.set(4)
})
}).toThrowError(/Side effects like changing state are not allowed at this point/)
mobx._resetGlobalState()
d()
})
test("enforceActions 'strict' does not allow changing unobserved observables", () => {
try {
mobx.configure({ enforceActions: "always" })
const x = mobx.observable({
a: 1,
b: 2
})
const d = mobx.autorun(() => {
x.a
})
expect(() => {
x.a = 2
}).toThrow(/Since strict-mode is enabled/)
expect(() => {
x.b = 2
}).toThrow(/Since strict-mode is enabled/)
d()
} finally {
mobx.configure({ enforceActions: false })
}
})
test("enforceActions 'strict' should not throw exception while observable array initialization", () => {
try {
mobx.configure({ enforceActions: "strict" })
expect(() => {
const x = mobx.observable({
a: [1, 2]
})
}).not.toThrow(/Since strict-mode is enabled/)
} finally {
mobx.configure({ enforceActions: false })
}
})
test("warn on unsafe reads", function() {
try {
mobx.configure({ computedRequiresReaction: true })
const x = mobx.observable({
y: 3,
get yy() {
return this.y * 2
}
})
utils.consoleWarn(() => {
x.yy
}, /being read outside a reactive context/)
} finally {
mobx.configure({ computedRequiresReaction: false })
}
})