forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextras.js
More file actions
489 lines (410 loc) · 13.9 KB
/
Copy pathextras.js
File metadata and controls
489 lines (410 loc) · 13.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
const mobx = require("../../src/mobx.ts")
const m = mobx
const { $mobx } = mobx
test("treeD", function() {
m._resetGlobalState()
mobx._getGlobalState().mobxGuid = 0
var a = m.observable.box(3)
var aName = "ObservableValue@1"
var dtree = m.getDependencyTree
expect(dtree(a)).toEqual({
name: aName
})
var b = m.computed(() => a.get() * a.get())
var bName = "ComputedValue@3"
expect(dtree(b)).toEqual({
name: bName
// no dependencies yet, since it isn't observed yet
})
var c = m.autorun(() => b.get())
var cName = "Autorun@4"
expect(dtree(c[$mobx])).toEqual({
name: cName,
dependencies: [
{
name: bName,
dependencies: [
{
name: aName
}
]
}
]
})
expect(aName !== bName).toBeTruthy()
expect(bName !== cName).toBeTruthy()
expect(m.getObserverTree(a)).toEqual({
name: aName,
observers: [
{
name: bName,
observers: [
{
name: cName
}
]
}
]
})
var x = mobx.observable.map({ temperature: 0 })
var d = mobx.autorun(function() {
Array.from(x.keys())
if (x.has("temperature")) x.get("temperature")
x.has("absent")
})
expect(m.getDependencyTree(d[$mobx])).toEqual({
name: "Autorun@7",
dependencies: [
{
name: "ObservableMap@6.keys()"
},
{
name: "ObservableMap@6.temperature?"
},
{
name: "ObservableMap@6.temperature"
},
{
name: "ObservableMap@6.absent?"
}
]
})
})
test("names", function() {
m._resetGlobalState()
mobx._getGlobalState().mobxGuid = 0
var struct = {
x: "ObservableValue@1",
y: {
z: 7
},
ar: [
4,
{
w: 5
}
]
}
var rstruct = m.observable(struct)
m.extendObservable(rstruct.y, { a: { b: 2 } })
rstruct.ar.push({ b: 2 })
rstruct.ar.push([])
expect(rstruct[$mobx].values.get("x").name).toBe("ObservableObject@1.x")
expect(rstruct[$mobx].values.get("y").name).toBe("ObservableObject@1.y")
expect(rstruct.y[$mobx].values.get("z").name).toBe("ObservableObject@1.y.z")
expect(rstruct[$mobx].values.get("ar").name).toBe("ObservableObject@1.ar")
expect(rstruct.ar[$mobx].atom.name).toBe("ObservableObject@1.ar")
expect(rstruct.ar[1][$mobx].values.get("w").name).toBe("ObservableObject@1.ar[..].w")
expect(rstruct.y.a[$mobx].values.get("b").name).toBe("ObservableObject@1.y.a.b")
expect(rstruct.ar[2][$mobx].values.get("b").name).toBe("ObservableObject@1.ar[..].b")
var d = m.autorun(function() {})
expect(d[$mobx].name).toBeTruthy()
expect(m.autorun(function namedFunction() {})[$mobx].name).toBe("namedFunction")
expect(m.computed(function() {})).toBeTruthy()
expect(m.computed(function namedFunction() {}).name).toBe("namedFunction")
function Task() {
m.extendObservable(this, {
title: "test"
})
}
var task = new Task()
expect(task[$mobx].name).toBe("Task@8")
expect(task[$mobx].values.get("title").name).toBe("Task@8.title")
})
function stripTrackerOutput(output) {
return output.map(function(i) {
if (Array.isArray(i)) return stripTrackerOutput(i)
delete i.object
delete i.time
delete i.fn
return i
})
}
test("spy 1", function() {
m._resetGlobalState()
var lines = []
var a = m.observable.box(3)
var b = m.computed(function() {
return a.get() * 2
})
var c = m.autorun(function() {
b.get()
})
var stop = m.spy(function(line) {
lines.push(line)
})
a.set(4)
stop()
a.set(5)
expect(stripTrackerOutput(lines)).toMatchSnapshot()
})
test("get atom", function() {
mobx._resetGlobalState()
mobx._getGlobalState().mobxGuid = 0 // hmm dangerous reset?
function Clazz() {
mobx.extendObservable(this, {
a: 17
})
}
var a = mobx.observable.box(3)
var b = mobx.observable({ a: 3 })
var c = mobx.observable.map({ a: 3 })
var d = mobx.observable([1, 2])
var e = mobx.computed(() => 3)
var f = mobx.autorun(() => c.has("b"))
var g = new Clazz()
function atom(thing, prop) {
return mobx.getAtom(thing, prop).constructor.name
}
var ovClassName = mobx.observable.box(3).constructor.name
var atomClassName = mobx.createAtom("test").constructor.name
var reactionClassName = mobx.Reaction.name
expect(atom(a)).toBe(ovClassName)
expect(atom(b, "a")).toBe(ovClassName)
expect(() => atom(b)).toThrowError(/please specify a property/)
expect(() => atom(b, "b")).toThrowError(
/no observable property 'b' found on the observable object 'ObservableObject@2'/
)
expect(atom(c)).toBe(atomClassName) // returns ke, "bla".constructor, === "Atomys
expect(atom(c, "a")).toBe(ovClassName) // returns ent, "bla".constructor, === "Atomry
expect(atom(c, "b")).toBe(ovClassName) // returns has entry (see autoru, "bla", "Atomn)
expect(() => atom(c, "c")).toThrowError(
/the entry 'c' does not exist in the observable map 'ObservableMap@3'/
)
expect(atom(d)).toBe(atomClassName)
expect(() => atom(d, 0)).toThrowError(/It is not possible to get index atoms from arrays/)
expect(atom(e)).toBe(mobx.computed(() => {}).constructor.name)
expect(atom(f)).toBe(mobx.Reaction.name)
expect(() => atom(g)).toThrowError(/please specify a property/)
expect(atom(g, "a")).toBe(ovClassName)
f()
})
test("get debug name", function() {
mobx._resetGlobalState()
mobx._getGlobalState().mobxGuid = 0 // hmm dangerous reset?
function Clazz() {
mobx.extendObservable(this, {
a: 17
})
}
var a = mobx.observable.box(3)
var b = mobx.observable({ a: 3 })
var c = mobx.observable.map({ a: 3 })
var d = mobx.observable([1, 2])
var e = mobx.computed(() => 3)
var f = mobx.autorun(() => c.has("b"))
var g = new Clazz()
var h = mobx.observable({ b: function() {}, c() {} })
function name(thing, prop) {
return mobx.getDebugName(thing, prop)
}
expect(name(a)).toBe("ObservableValue@1")
expect(name(b, "a")).toBe("ObservableObject@2.a")
expect(() => name(b, "b")).toThrowError(
/no observable property 'b' found on the observable object 'ObservableObject@2'/
)
expect(name(c)).toBe("ObservableMap@3") // returns ke, "bla"ys
expect(name(c, "a")).toBe("ObservableMap@3.a") // returns ent, "bla"ry
expect(name(c, "b")).toBe("ObservableMap@3.b?") // returns has entry (see autoru, "bla"n)
expect(() => name(c, "c")).toThrowError(
/the entry 'c' does not exist in the observable map 'ObservableMap@3'/
)
expect(name(d)).toBe("ObservableArray@4")
expect(() => name(d, 0)).toThrowError(/It is not possible to get index atoms from arrays/)
expect(name(e)).toBe("ComputedValue@6")
expect(name(f)).toBe("Autorun@7")
expect(name(g)).toBe("Clazz@9")
expect(name(g, "a")).toBe("Clazz@9.a")
expect(name(h, "b")).toBe("ObservableObject@10.b")
expect(name(h, "c")).toBe("ObservableObject@10.c")
f()
})
test("get administration", function() {
mobx._resetGlobalState()
mobx._getGlobalState().mobxGuid = 0 // hmm dangerous reset?
function Clazz() {
mobx.extendObservable(this, {
a: 17
})
}
var a = mobx.observable.box(3)
var b = mobx.observable({ a: 3 })
var c = mobx.observable.map({ a: 3 })
var d = mobx.observable([1, 2])
var e = mobx.computed(() => 3)
var f = mobx.autorun(() => c.has("b"))
var g = new Clazz()
const h = {}
mobx.extendObservable(h, { a: 3 })
function adm(thing, prop) {
return mobx._getAdministration(thing, prop).constructor.name
}
var ovClassName = mobx.observable.box(3).constructor.name
var mapClassName = mobx.observable.map().constructor.name
expect(adm(a)).toBe(ovClassName)
expect(adm(b, "a")).toBe(ovClassName)
expect(adm(b)).toBe(b[$mobx].constructor.name)
expect(() => adm(b, "b")).toThrowError(
/no observable property 'b' found on the observable object 'ObservableObject@2'/
)
expect(adm(h, "a")).toBe(ovClassName)
expect(adm(h)).toBe(h[$mobx].constructor.name)
expect(() => adm(h, "b")).toThrowError(
/no observable property 'b' found on the observable object 'ObservableObject@10'/
)
expect(adm(c)).toBe(mapClassName)
expect(adm(c, "a")).toBe(ovClassName)
expect(adm(c, "b")).toBe(ovClassName)
expect(() => adm(c, "c")).toThrowError(
/the entry 'c' does not exist in the observable map 'ObservableMap@3'/
)
expect(adm(d)).toBe(d[$mobx].constructor.name)
expect(() => adm(d, 0)).toThrowError(/It is not possible to get index atoms from arrays/)
expect(adm(e)).toBe(mobx.computed(() => {}).constructor.name)
expect(adm(f)).toBe(mobx.Reaction.name)
expect(adm(g)).toBe(h[$mobx].constructor.name)
expect(adm(g, "a")).toBe(ovClassName)
})
test("onBecome(Un)Observed simple", () => {
const x = mobx.observable.box(3)
const events = []
const d1 = mobx.onBecomeObserved(x, () => {
events.push("x observed")
})
const d2 = mobx.onBecomeUnobserved(x, () => {
events.push("x unobserved")
})
expect(events.length).toBe(0) // nothing happened yet
x.get()
expect(events.length).toBe(0) // nothing happened yet
x.set(4)
expect(events.length).toBe(0) // nothing happened yet
const d5 = mobx.reaction(() => x.get(), () => {})
expect(events.length).toBe(1)
expect(events).toEqual(["x observed"])
d5()
expect(events.length).toBe(2)
expect(events).toEqual(["x observed", "x unobserved"])
})
test("onBecome(Un)Observed - less simple", () => {
const x = mobx.observable({
a: 3,
get b() {
return this.a * 2
}
})
const events = []
const d1 = mobx.onBecomeObserved(x, "a", () => {
events.push("a observed")
})
const d2 = mobx.onBecomeUnobserved(x, "a", () => {
events.push("a unobserved")
})
const d3 = mobx.onBecomeObserved(x, "b", () => {
events.push("b observed")
})
const d4 = mobx.onBecomeUnobserved(x, "b", () => {
events.push("b unobserved")
})
x.b
x.a = 4
expect(events.length).toBe(0) // nothing happened yet
const d5 = mobx.reaction(() => x.b, () => {})
expect(events.length).toBe(2)
expect(events).toEqual(["b observed", "a observed"])
const d6 = mobx.reaction(() => x.b, () => {})
expect(events.length).toBe(2)
d5()
expect(events.length).toBe(2)
d6()
expect(events.length).toBe(4)
expect(events).toEqual(["b observed", "a observed", "b unobserved", "a unobserved"])
d1()
d2()
d3()
d4()
events.splice(0)
const d7 = mobx.reaction(() => x.b, () => {})
d7()
expect(events.length).toBe(0)
})
test("onBecomeObserved correctly disposes second listener #1537", () => {
const x = mobx.observable.box(3)
const events = []
const d1 = mobx.onBecomeObserved(x, "a", () => {
events.push("a observed")
})
const d2 = mobx.onBecomeObserved(x, "b", () => {
events.push("b observed")
})
d1()
mobx.reaction(() => x.get(), () => {})
expect(events.length).toBe(1)
expect(events).toEqual(["b observed"])
})
test("onBecomeObserved correctly disposes second listener #1537", () => {
const x = mobx.observable.box(3)
const events = []
const d1 = mobx.onBecomeObserved(x, "a", () => {
events.push("a observed")
})
const d2 = mobx.onBecomeObserved(x, "b", () => {
events.push("b observed")
})
d1()
const d3 = mobx.reaction(() => x.get(), () => {})
d3()
expect(events.length).toBe(1)
expect(events).toEqual(["b observed"])
d2()
const d4 = mobx.reaction(() => x.get(), () => {})
expect(events).toEqual(["b observed"])
})
test("onBecomeUnobserved correctly disposes second listener #1537", () => {
const x = mobx.observable.box(3)
const events = []
const d1 = mobx.onBecomeUnobserved(x, "a", () => {
events.push("a unobserved")
})
const d2 = mobx.onBecomeUnobserved(x, "b", () => {
events.push("b unobserved")
})
d1()
const d3 = mobx.reaction(() => x.get(), () => {})
d3()
expect(events.length).toBe(1)
expect(events).toEqual(["b unobserved"])
d2()
const d4 = mobx.reaction(() => x.get(), () => {})
expect(events).toEqual(["b unobserved"])
})
test("deepEquals should yield correct results for complex objects #1118 - 1", () => {
const d2016jan1 = new Date("2016-01-01")
const d2016jan1_2 = new Date("2016-01-01")
const d2017jan1 = new Date("2017-01-01")
expect(d2016jan1).toEqual(d2016jan1_2)
expect(d2016jan1).not.toEqual(d2017jan1)
expect(mobx.comparer.structural(d2016jan1, d2016jan1)).toBe(true)
expect(mobx.comparer.structural(d2016jan1, d2017jan1)).toBe(false)
expect(mobx.comparer.structural(d2016jan1, d2016jan1_2)).toBe(true)
})
test("deepEquals should yield correct results for complex objects #1118 - 2", () => {
class A {
x = 3
y = 4
constructor(x) {
this.x = x
}
}
const a1 = new A(2)
const a2 = new A(2)
const a3 = new A(3)
const a4 = new A(2)
a4.z = 2
expect(a1).toEqual(a2)
expect(a1).not.toEqual(a3)
expect(mobx.comparer.structural(a1, a1)).toBe(true)
expect(mobx.comparer.structural(a1, a3)).toBe(false)
expect(mobx.comparer.structural(a1, a2)).toBe(true)
expect(mobx.comparer.structural(a1, a4)).toBe(false)
})