-
Notifications
You must be signed in to change notification settings - Fork 1
/
observable-decorators-test.js
125 lines (97 loc) · 2.44 KB
/
observable-decorators-test.js
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
import QUnit from 'steal-qunit';
import observable, { observableFromConfig } from './observable-decorators';
import 'rxjs/add/operator/combineLatest'
import 'rxjs/add/operator/scan'
import Kefir from 'kefir';
import kefirConfig from './config/kefir';
const assertStreamValues = (obj, key, values, subscribeFn = 'subscribe') => {
values.forEach(() => {
QUnit.stop();
});
let count = 0;
const stream = obj[key];
stream[subscribeFn]((val) => {
QUnit.deepEqual(val, values[count], `${key}[${count}] should be ${values[count]}`);
count++;
QUnit.start();
});
}
QUnit.module('observable-decorators - observable');
QUnit.test('can create settable streams of primitive values', () => {
class Person {
@observable
first = 'Kevin';
}
const person = new Person();
assertStreamValues(person, 'first', [ 'Kevin', 'Tracy' ]);
person.first = 'Tracy';
});
QUnit.test('can create streams derived from other streams', () => {
class Person {
constructor(override) {
Object.assign(this, override);
}
@observable
first = 'Kevin';
@observable
last = 'Phillips';
@observable
get fullName() {
return this.first.combineLatest(this.last, (first, last) => {
return first + ' ' + last;
});
}
}
const person = new Person({
first: 'Kevin'
});
person.last = 'McCallister';
assertStreamValues(person, 'fullName', [
'Kevin Phillips',
'Kevin McCallister'
]);
});
QUnit.test('should cache streams', () => {
class Person {
@observable
first = 'Kevin';
@observable
last = 'Phillips';
@observable
get fullName() {
return this.first.combineLatest(this.last, (first, last) => {
return first + ' ' + last;
});
}
}
const person = new Person();
person.first.foo = 'bar';
QUnit.equal(person.first.foo, 'bar');
person.fullName.foo = 'bar';
QUnit.equal(person.fullName.foo, 'bar');
});
QUnit.module('observable-decorators - observableFromConfig');
QUnit.test('works with Kefir', () => {
const kefirObservable = observableFromConfig(kefirConfig);
class Person {
constructor(override) {
Object.assign(this, override);
}
@kefirObservable
first = 'Kevin';
@kefirObservable
last = 'Phillips';
@kefirObservable
get fullName() {
return Kefir.combine([ this.first, this.last ], (first, last) => {
return first + ' ' + last;
});
}
}
const person = new Person();
assertStreamValues(person, 'fullName', [
'Kevin Phillips',
'Kevin McCallister'
], 'onValue');
person.last = 'McCallister';
});