forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_test.js
267 lines (207 loc) · 5.83 KB
/
compute_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
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
(function() {
module('can/observe/compute')
test("Basic Compute",function(){
var o = new can.Observe({first: "Justin", last: "Meyer"});
var prop = can.compute(function(){
return o.attr("first") + " " +o.attr("last")
})
equals(prop(), "Justin Meyer");
var handler = function(ev, newVal, oldVal){
equals(newVal, "Brian Meyer")
equals(oldVal, "Justin Meyer")
}
prop.bind("change", handler);
o.attr("first","Brian");
prop.unbind("change", handler)
o.attr("first","Brian");
});
test("compute on prototype", function(){
var Person = can.Observe({
fullName: function(){
return this.attr("first") + " " +this.attr("last")
}
})
var me = new Person({
first : "Justin",
last : "Meyer"
});
var fullName = can.compute( me.fullName, me );
equals(fullName(), "Justin Meyer");
var called = 0;
fullName.bind("change", function( ev, newVal, oldVal ) {
called++;
equal(called, 1, "called only once");
equal(newVal, "Justin Shah");
equal(oldVal, "Justin Meyer")
});
me.attr('last',"Shah")
// to make this work, we'd have to look for a computed function and bind to it's change ...
// maybe bind can just work this way?
})
test("setter compute", function(){
var project = new can.Observe({
progress: 0.5
});
// a setter compute that converts 50 to .5 and vice versa
var computed = can.compute(function(val){
if(val) {
project.attr('progress', val / 100)
} else {
return parseInt( project.attr('progress') * 100 );
}
});
equals(computed(), 50, "the value is right");
computed(25);
equals(project.attr('progress'), 0.25);
equals(computed(),25 );
computed.bind("change", function(ev, newVal, oldVal){
equals(newVal, 75);
equals(oldVal, 25)
})
computed(75);
})
test("compute a compute", function() {
var project = new can.Observe({
progress: 0.5
});
var percent = can.compute(function(val){
if(val) {
project.attr('progress', val / 100);
} else {
return parseInt( project.attr('progress') * 100, 10);
}
});
equals(percent(),50,'percent starts right');
percent.bind('change',function() {
// noop
});
var fraction = can.compute(function(val) {
if(val) {
percent(parseInt(val.split('/')[0],10));
} else {
return percent() + '/100';
}
});
fraction.bind('change',function() {
// noop
});
equals(fraction(),'50/100','fraction starts right');
percent(25);
equals(percent(),25);
equals(project.attr('progress'),0.25,'progress updated');
equals(fraction(),'25/100','fraction updated');
fraction('15/100');
equals(fraction(),'15/100');
equals(project.attr('progress'),0.15,'progress updated');
equals(percent(),15,'% updated');
});
test("compute with a simple compute", function() {
expect(4);
var a = can.compute(5);
var b = can.compute(function() {
return a() * 2;
});
equal(b(),10,'b starts correct');
a(3);
equal(b(),6,'b updates');
b.bind('change',function() {
equal(b(),24,'b fires change');
});
a(12);
equal(b(),24,'b updates when bound');
});
test("empty compute", function(){
var c = can.compute();
c.bind("change", function(ev, newVal, oldVal){
ok(oldVal === undefined, "was undefined")
ok(newVal === 0, "now zero")
})
c(0);
});
test("only one update on a batchTransaction",function(){
var person = new can.Observe({first: "Justin", last: "Meyer"});
var func = function(){
return person.attr('first')+" "+person.attr('last')+Math.random()
};
var callbacks = 0;
can.compute.binder(func, window, function(newVal, oldVal){
callbacks++;
});
person.attr({
first: "Brian",
last: "Moschel"
});
equal(callbacks,1,"only one callback")
})
test("only one update on a start and end transaction",function(){
var person = new can.Observe({first: "Justin", last: "Meyer"}),
age = can.compute(5);
var func = function(newVal,oldVal){
return person.attr('first')+" "+person.attr('last')+age()+Math.random();
};
var callbacks = 0;
can.compute.binder(func, window, function(newVal, oldVal){
callbacks++;
});
can.Observe.startBatch();
person.attr('first',"Brian");
stop();
setTimeout(function(){
person.attr('last',"Moschel");
age(12)
can.Observe.stopBatch();
equal(callbacks,1,"only one callback")
start();
})
})
test("Compute emits change events when an embbedded observe has properties added or removed", 4, function() {
var obs = new can.Observe(),
compute1 = can.compute(function(){
var txt = obs.attr('foo');
obs.each(function(val){
txt += val.toString();
});
return txt;
});
compute1.bind('change', function(ev, newVal, oldVal) {
ok(true, 'change handler fired: ' + newVal);
})
// we're binding on adding / removing and foo
obs.attr('foo', 1);
obs.attr('bar', 2);
obs.attr('foo', 3);
obs.removeAttr('bar');
obs.removeAttr('bar');
});
test("compute only updates once when a list's contents are replaced",function(){
var list = new can.Observe.List([{name: "Justin"}]),
computedCount = 0;
var compute = can.compute(function(){
computedCount++;
list.each(function(item){
item.attr('name')
})
})
equals(0,computedCount, "computes are not called until their value is read")
compute.bind("change", function(ev, newVal, oldVal){
})
equals(1,computedCount, "binding computes to store the value");
list.replace([{name: "hank"}]);
equals(2,computedCount, "only one compute")
});
test("Generate computes from Observes with can.Observe.prototype.compute (#203)", 5, function() {
var obs = new can.Observe({
test : 'testvalue'
});
var compute = obs.compute('test');
ok(compute.isComputed, '`test` is computed');
equal(compute(), 'testvalue', 'Value is as expected');
obs.attr('test', 'observeValue');
equal(compute(), 'observeValue', 'Value is as expected');
obs.bind('change', function(ev, name, how, newVal) {
equal(newVal, 'computeValue', 'Got new value from compute');
});
compute('computeValue');
equal(compute(), 'computeValue', 'Got updated value');
});
})();