forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescript-tests.ts
1206 lines (961 loc) · 25.2 KB
/
typescript-tests.ts
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// <reference path='require.d.ts' />
/// <reference path='tape.d.ts' />
import {
observe, computed, observable, autorun, autorunAsync, extendObservable, action,
IObservableObject, IObservableArray, IArrayChange, IArraySplice, IObservableValue, isObservable, isObservableObject,
extras, Atom, transaction, IObjectChange, spy, useStrict, isAction
} from "../../lib/mobx";
import * as test from 'tape';
import * as mobx from "../../lib/mobx";
var v = observable(3);
observe(v, () => {});
var a = observable([1,2,3]);
var testFunction = function(a:any) {};
class Order {
@observable price:number = 3;
@observable amount:number = 2;
@observable orders:string[] = [];
@observable aFunction = testFunction;
@computed get total() {
return this.amount * this.price * (1 + this.orders.length);
}
// Typescript classes cannot be defined inside functions,
// but if the next line is enabled it should throw...
// @observable hoepie() { return 3; }
}
test('decorators', function(t) {
var o = new Order();
t.equal(isObservableObject(o), true);
t.equal(isObservable(o, 'amount'), true);
t.equal(isObservable(o, 'total'), true);
var events: any[] = [];
var d1 = observe(o, (ev: IObjectChange) => events.push(ev.name, ev.oldValue));
var d2 = observe(o, 'price', (ev) => events.push(ev.newValue, ev.oldValue));
var d3 = observe(o, 'total', (ev) => events.push(ev.newValue, ev.oldValue));
o.price = 4;
d1();
d2();
d3();
o.price = 5;
t.deepEqual(events, [
8, // new total
6, // old total
4, // new price
3, // old price
"price", // event name
3, // event oldValue
]);
t.end();
})
test('observable', function(t) {
var a = observable(3);
var b = computed(() => a.get() * 2);
t.equal(b.get(), 6);
t.end();
})
test('annotations', function(t) {
var order1totals:number[] = [];
var order1 = new Order();
var order2 = new Order();
var disposer = autorun(() => {
order1totals.push(order1.total)
});
order2.price = 4;
order1.amount = 1;
t.equal(order1.price, 3);
t.equal(order1.total, 3);
t.equal(order2.total, 8);
order2.orders.push('bla');
t.equal(order2.total, 16);
order1.orders.splice(0,0,'boe', 'hoi');
t.deepEqual(order1totals, [6,3,9]);
disposer();
order1.orders.pop();
t.equal(order1.total, 6);
t.deepEqual(order1totals, [6,3,9]);
t.equal(order1.aFunction, testFunction);
var x = function() { return 3; };
order1.aFunction = x;
t.equal(order1.aFunction, x);
t.end();
})
test('scope', function(t) {
var x = observable({
y: 3,
// this wo't work here.
get z () {
return 2 * this.y
}
});
t.equal(x.z, 6);
x.y = 4;
t.equal(x.z, 8);
interface IThing {
z: number;
y: number;
}
const Thing = function() {
extendObservable(this, {
y: 3,
// this will work here
z: computed(() => 2 * this.y)
});
}
var x3: IThing = new (<any>Thing)();
t.equal(x3.z, 6);
x3.y = 4;
t.equal(x3.z, 8);
t.end();
})
test('typing', function(t) {
var ar:IObservableArray<number> = observable([1,2]);
ar.observe((d:IArrayChange<number>|IArraySplice<number>) => {
console.log(d.type);
});
var ar2:IObservableArray<number> = observable([1,2]);
ar2.observe((d:IArrayChange<number>|IArraySplice<number>) => {
console.log(d.type);
});
var x:IObservableValue<number> = observable(3);
var d2 = autorunAsync(function() {
});
t.end();
})
const state:any = observable({
authToken: null
});
test('issue8', function(t){
t.throws(() => {
class LoginStoreTest {
loggedIn2: boolean;
constructor() {
extendObservable(this, {
loggedIn2: () => !!state.authToken
});
}
@observable get loggedIn() {
return !!state.authToken;
}
}
const store = new LoginStoreTest();
}, /@computed/);
t.end();
})
class Box {
@observable uninitialized:any;
@observable height = 20;
@observable sizes = [2];
@observable someFunc = function () { return 2; }
@computed get width() {
return this.height * this.sizes.length * this.someFunc() * (this.uninitialized ? 2 : 1);
}
}
test('box', function(t) {
var box = new Box();
var ar:number[] = []
autorun(() => {
ar.push(box.width);
});
t.deepEqual(ar.slice(), [40]);
box.height = 10;
t.deepEqual(ar.slice(), [40, 20]);
box.sizes.push(3, 4);
t.deepEqual(ar.slice(), [40, 20, 60]);
box.someFunc = () => 7;
t.deepEqual(ar.slice(), [40, 20, 60, 210]);
box.uninitialized = true;
t.deepEqual(ar.slice(), [40, 20, 60, 210, 420]);
t.end();
})
test('computed setter should succeed', function(t) {
class Bla {
@observable a = 3;
@computed get propX() {
return this.a * 2;
}
set propX(v) {
this.a = v
}
}
const b = new Bla();
t.equal(b.propX, 6);
b.propX = 4;
t.equal(b.propX, 8);
t.end();
});
test('atom clock example', function(t) {
let ticks = 0;
const time_factor = 500; // speed up / slow down tests
class Clock {
atom: Atom;
intervalHandler: number = null;
currentDateTime: string;
constructor() {
console.log("create");
// creates an atom to interact with the mobx core algorithm
this.atom = new Atom(
// first param a name for this atom, for debugging purposes
"Clock",
// second (optional) parameter: callback for when this atom transitions from unobserved to observed.
() => this.startTicking(),
// third (optional) parameter: callback for when this atom transitions from observed to unobserved
// note that the same atom transition multiple times between these two states
() => this.stopTicking()
);
}
getTime() {
console.log("get time");
// let mobx now this observable data source has been used
this.atom.reportObserved(); // will trigger startTicking and thus tick
return this.currentDateTime;
}
tick() {
console.log("tick");
ticks++;
this.currentDateTime = new Date().toString();
this.atom.reportChanged();
}
startTicking() {
console.log("start ticking");
this.tick();
this.intervalHandler = setInterval(() => this.tick(), 1 * time_factor);
}
stopTicking() {
console.log("stop ticking");
clearInterval(this.intervalHandler);
this.intervalHandler = null;
}
}
const clock = new Clock();
const values: string[] = [];
// ... prints the time each second
const disposer = autorun(() => {
values.push(clock.getTime());
console.log(clock.getTime());
});
// printing stops. If nobody else uses the same `clock` the clock will stop ticking as well.
setTimeout(disposer, 4.5 * time_factor);
setTimeout(() => {
t.equal(ticks, 5);
t.equal(values.length, 5);
t.equal(values.filter(x => x.length > 0).length, 5);
t.end();
}, 10 * time_factor);
});
test('typescript: parameterized computed decorator', (t) => {
class TestClass {
@observable x = 3;
@observable y = 3;
@computed.struct get boxedSum() {
return { sum: Math.round(this.x) + Math.round(this.y) };
}
}
const t1 = new TestClass();
const changes: { sum: number}[] = [];
const d = autorun(() => changes.push(t1.boxedSum));
t1.y = 4; // change
t.equal(changes.length, 2);
t1.y = 4.2; // no change
t.equal(changes.length, 2);
transaction(() => {
t1.y = 3;
t1.x = 4;
}); // no change
t.equal(changes.length, 2);
t1.x = 6; // change
t.equal(changes.length, 3);
d();
t.deepEqual(changes, [{ sum: 6 }, { sum: 7 }, { sum: 9 }]);
t.end();
});
test('issue 165', function(t) {
function report<T>(msg: string, value: T) {
console.log(msg, ':', value);
return value;
}
class Card {
constructor(public game: Game, public id: number) {
}
@computed get isWrong() {
return report('Computing isWrong for card ' + this.id, this.isSelected && this.game.isMatchWrong);
}
@computed get isSelected() {
return report('Computing isSelected for card'+ this.id,
this.game.firstCardSelected === this || this.game.secondCardSelected === this);
}
}
class Game {
@observable firstCardSelected: Card = null;
@observable secondCardSelected: Card = null;
@computed get isMatchWrong() {
return report('Computing isMatchWrong',
this.secondCardSelected !== null && this.firstCardSelected.id !== this.secondCardSelected.id);
}
}
let game = new Game();
let card1 = new Card(game, 1), card2 = new Card(game, 2);
autorun(() => {
console.log('card1.isWrong =', card1.isWrong);
console.log('card2.isWrong =', card2.isWrong);
console.log('------------------------------');
});
console.log('Selecting first card');
game.firstCardSelected = card1;
console.log('Selecting second card');
game.secondCardSelected = card2;
t.equal(card1.isWrong, true);
t.equal(card2.isWrong, true);
t.end();
});
test('issue 191 - shared initializers (ts)', function(t) {
class Test {
@observable obj = { a: 1 };
@observable array = [2];
}
var t1 = new Test();
t1.obj.a = 2;
t1.array.push(3);
var t2 = new Test();
t2.obj.a = 3;
t2.array.push(4);
t.notEqual(t1.obj, t2.obj);
t.notEqual(t1.array, t2.array);
t.equal(t1.obj.a, 2);
t.equal(t2.obj.a, 3);
t.deepEqual(t1.array.slice(), [2,3]);
t.deepEqual(t2.array.slice(), [2,4]);
t.end();
});
function normalizeSpyEvents(events: any[]) {
events.forEach(ev => {
delete ev.fn;
delete ev.time;
});
return events;
}
test("action decorator (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}
@action
add(a: number, b: number): number {
return (a + b) * this.multiplier;
}
}
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(2, 2), 12);
t.equal(store1.add(1, 1), 4);
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "add", spyReportStart: true, object: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "add", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true }
]);
d();
t.end();
});
test("custom action decorator (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}
@action("zoem zoem")
add(a: number, b: number): number {
return (a + b) * this.multiplier;
}
}
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(2, 2), 12);
t.equal(store1.add(1, 1), 4);
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, object: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "zoem zoem", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true }
]);
d();
t.end();
});
test("action decorator on field (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}
@action
add = (a: number, b: number) => {
return (a + b) * this.multiplier;
};
}
const store1 = new Store(2);
const store2 = new Store(7);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(4, 5), 63);
t.equal(store1.add(2, 2), 8);
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 4, 5 ], name: "add", spyReportStart: true, object: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "add", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true }
]);
d();
t.end();
});
test("custom action decorator on field (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}
@action("zoem zoem")
add = (a: number, b: number) => {
return (a + b) * this.multiplier;
};
}
const store1 = new Store(2);
const store2 = new Store(7);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(4, 5), 63);
t.equal(store1.add(2, 2), 8);
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 4, 5 ], name: "zoem zoem", spyReportStart: true, object: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, object: store1, type: "action" },
{ spyReportEnd: true }
]);
d();
t.end();
});
test("267 (typescript) should be possible to declare properties observable outside strict mode", t => {
useStrict(true);
class Store {
@observable timer: number;
}
useStrict(false);
t.end();
});
test("288 atom not detected for object property", t => {
class Store {
@observable foo = '';
}
const store = new Store();
mobx.observe(store, 'foo', () => {
console.log('Change observed');
}, true);
t.end()
})
test("observable performance", t => {
const AMOUNT = 100000;
class A {
@observable a = 1;
@observable b = 2;
@observable c = 3;
@computed get d() {
return this.a + this.b + this.c;
}
}
const objs: any[] = [];
const start = Date.now();
for (var i = 0; i < AMOUNT; i++)
objs.push(new A());
console.log("created in ", Date.now() - start);
for (var j = 0; j < 4; j++) {
for (var i = 0; i < AMOUNT; i++) {
const obj = objs[i]
obj.a += 3;
obj.b *= 4;
obj.c = obj.b - obj.a;
obj.d;
}
}
console.log("changed in ", Date.now() - start);
t.end();
})
test("unbound methods", t => {
class A {
// shared across all instances
@action m1() {
}
// per instance
@action m2 = () => {};
}
const a1 = new A();
const a2 = new A();
t.equal(a1.m1, a2.m1);
t.notEqual(a1.m2, a2.m2);
t.equal(a1.hasOwnProperty("m1"), false);
t.equal(a1.hasOwnProperty("m2"), true);
t.equal(a2.hasOwnProperty("m1"), false);
t.equal(a2.hasOwnProperty("m2"), true);
t.end();
})
test("inheritance", t => {
class A {
@observable a = 2;
}
class B extends A {
@observable b = 3;
@computed get c() {
return this.a + this.b;
}
}
const b1 = new B();
const b2 = new B();
const values: any[] = []
mobx.autorun(() => values.push(b1.c + b2.c));
b1.a = 3;
b1.b = 4;
b2.b = 5;
b2.a = 6;
t.deepEqual(values, [
10,
11,
12,
14,
18
])
t.end();
})
test("inheritance overrides observable", t => {
class A {
@observable a = 2;
}
class B {
@observable a = 5;
@observable b = 3;
@computed get c() {
return this.a + this.b;
}
}
const b1 = new B();
const b2 = new B();
const values: any[] = []
mobx.autorun(() => values.push(b1.c + b2.c));
b1.a = 3;
b1.b = 4;
b2.b = 5;
b2.a = 6;
t.deepEqual(values, [
16,
14,
15,
17,
18
])
t.end();
})
test("reusing initializers", t => {
class A {
@observable a = 3;
@observable b = this.a + 2;
@computed get c() {
return this.a + this.b;
}
@computed get d() {
return this.c + 1;
}
}
const a = new A();
const values: any[] = [];
mobx.autorun(() => values.push(a.d));
a.a = 4;
t.deepEqual(values, [
9,
10
])
t.end();
})
test("enumerability", t => {
class A {
@observable a = 1; // enumerable, on proto
@computed get b () { return this.a } // non-enumerable, on proto
@action m() {} // non-enumerable, on proto
@action m2 = () => {}; // non-enumerable, on self
}
const a = new A();
// not initialized yet
let ownProps = Object.keys(a);
let props: string[] = [];
for (var key in a)
props.push(key);
t.deepEqual(ownProps, [
"a" // yeej!
]);
t.deepEqual(props, [ // also 'a' would be ok
"a"
]);
t.equal("a" in a, true);
t.equal(a.hasOwnProperty("a"), true);
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), true);
t.equal(mobx.isAction(a.m), true);
t.equal(mobx.isAction(a.m2), true);
// after initialization
a.a;
a.b;
a.m;
a.m2;
ownProps = Object.keys(a);
props = [];
for (var key in a)
props.push(key);
t.deepEqual(ownProps, [
"a"
]);
t.deepEqual(props, [
"a"
]);
t.equal("a" in a, true);
t.equal(a.hasOwnProperty("a"), true);
t.equal(a.hasOwnProperty("b"), false);
t.equal(a.hasOwnProperty("m"), false);
t.equal(a.hasOwnProperty("m2"), true);
t.end();
})
test("issue 285 (babel)", t => {
const {observable, toJS} = mobx;
class Todo {
id = 1;
@observable title: string;
@observable finished = false;
@observable childThings = [1,2,3];
constructor(title: string) {
this.title = title;
}
}
var todo = new Todo("Something to do");
t.deepEqual(toJS(todo), {
id: 1,
title: "Something to do",
finished: false,
childThings: [1,2,3]
})
t.end();
})
test("verify object assign (typescript)", t => {
class Todo {
@observable title = "test";
@computed get upperCase() {
return this.title.toUpperCase()
}
}
t.deepEqual((Object as any).assign({}, new Todo()), {
title: "test"
});
t.end();
})
test("379, inheritable actions (typescript)", t => {
class A {
@action method() {
return 42;
}
}
class B extends A {
@action method() {
return super.method() * 2
}
}
class C extends B {
@action method() {
return super.method() + 3
}
}
const b = new B()
t.equal(b.method(), 84)
t.equal(isAction(b.method), true)
const a = new A()
t.equal(a.method(), 42)
t.equal(isAction(a.method), true)
const c = new C()
t.equal(c.method(), 87)
t.equal(isAction(c.method), true)
t.end()
})
test("379, inheritable actions - 2 (typescript)", t => {
class A {
@action("a method") method() {
return 42;
}
}
class B extends A {
@action("b method") method() {
return super.method() * 2
}
}
class C extends B {
@action("c method") method() {
return super.method() + 3
}
}
const b = new B()
t.equal(b.method(), 84)
t.equal(isAction(b.method), true)
const a = new A()
t.equal(a.method(), 42)
t.equal(isAction(a.method), true)
const c = new C()
t.equal(c.method(), 87)
t.equal(isAction(c.method), true)
t.end()
})
test("373 - fix isObservable for unused computed", t => {
class Bla {
@computed get computedVal() { return 3 }
constructor() {
t.equal(isObservable(this, "computedVal"), true)
this.computedVal;
t.equal(isObservable(this, "computedVal"), true)
}
}
new Bla();
t.end();
})
test("505, don't throw when accessing subclass fields in super constructor (typescript)", t => {
const values: any = { }
class A {
@observable a = 1
constructor() {
values.b = (this as any)['b']
values.a = this.a
}
}
class B extends A {
@observable b = 2
}
new B()
t.deepEqual(values, { a: 1, b: undefined}) // undefined, as A constructor runs before B constructor
t.end()
})
test('computed getter / setter for plan objects should succeed (typescript)', function(t) {
const b = observable({
a: 3,
get propX() { return this.a * 2 },
set propX(v) { this.a = v }
})
const values: number[] = []
mobx.autorun(() => values.push(b.propX))
t.equal(b.propX, 6);
b.propX = 4;
t.equal(b.propX, 8);
t.deepEqual(values, [6, 8])
t.end()
})
test("484 - observable objects are IObservableObject", t => {
const needs_observable_object = (o: IObservableObject): any => null;
const o = observable({stuff: "things"});
needs_observable_object(o);
t.pass();
t.end();
});
test("484 - observable objects are still type T", t => {
const o = observable({stuff: "things"});
o.stuff = "new things";
t.pass();
t.end();
});
test("484 - isObservableObject type guard includes type T", t => {
const o = observable({stuff: "things"});
if(isObservableObject(o)) {
o.stuff = "new things";
t.pass();
} else {
t.fail("object should have been observable");
}
t.end();
});
test("484 - isObservableObject type guard includes type IObservableObject", t => {
const requires_observable_object = (o: IObservableObject): void => null;
const o = observable({stuff: "things"});
if(isObservableObject(o)) {
requires_observable_object(o);
t.pass();
} else {
t.fail("object should have been IObservableObject");
}
t.end();
});
test("705 - setter undoing caching (typescript)", t => {
let recomputes = 0;
let autoruns = 0;
class Person {
@observable name: string;
@observable title: string;
// Typescript bug: if fullName is before the getter, the property is defined twice / incorrectly, see #705
// set fullName(val) {
// // Noop
// }
@computed get fullName() {
recomputes++;
return this.title+" "+this.name;
}
// Should also be possible to define the setter _before_ the fullname