Debian Codegraph @

dojox/mvc/tests/doh/computed.js

raw symbols SCIP
1 define([
2 "doh",
3 "dojo/_base/lang",
4 "dojox/mvc/at",
5 "dojox/mvc/computed",
6 "dojox/mvc/getStateful"
7 ], function(doh, lang, at, computed, getStateful){
8 var handles = [];
9 function cleanHandles(){
10 for(var handle = null; (handle = handles.shift());){
11 if(typeof handle.close === "function"){
12 handle.close();
13 }else if(typeof handle.remove === "function"){
14 handle.remove();
15 }else if(typeof handle.destroyRecursive === "function"){
16 handle.destroyRecursive();
17 }else if(typeof handle.destroy === "function"){
18 handle.destroy();
19 }else{
20 throw new Error("Handle cannot be cleaned up.");
21 }
22 }
23 }
24
25 doh.register("dojox.mvc.tests.doh.computed", [
26 function checkParameters(t){
27 var throwCount = 0;
28 try{
29 computed(undefined, "foo");
30 }catch(e){
31 ++throwCount;
32 }
33 try{
34 computed(null, "foo");
35 }catch(e){
36 ++throwCount;
37 }
38 try{
39 computed({}, "*");
40 }catch(e){
41 ++throwCount;
42 }
43 try{
44 computed({}, "foo", function(){}, at({}, "*"));
45 }catch(e){
46 ++throwCount;
47 }
48 t.is(4, throwCount);
49 },
50 {
51 name: "Computed property - Observable",
52 tearDown: cleanHandles,
53 runTest: function(t){
54 var count = 0,
55 stateful = getStateful({
56 first: "John",
57 last: "Doe"
58 });
59
60 handles.push(computed(stateful, "name", function(first, last){
61 return first + " " + last;
62 }, at(stateful, "first"), at(stateful, "last")));
63
64 handles.push(computed(stateful, "nameLength", function(name){
65 return name.length;
66 }, at(stateful, "name")));
67
68 handles.push(stateful.watch("name", function(name, old, current){
69 t.is("Ben Doe", current);
70 t.is("John Doe", old);
71 count++;
72 }));
73
74 handles.push(stateful.watch("nameLength", function(name, old, current){
75 t.is(7, current);
76 t.is(8, old);
77 count++;
78 }));
79
80 t.is("John Doe", stateful.get("name"));
81 t.is(8, stateful.get("nameLength"));
82
83 stateful.set("first", "Ben");
84 t.is(2, count);
85 }
86 },
87 {
88 name: "Computed property - Non-observable",
89 tearDown: cleanHandles,
90 runTest: function(t){
91 var o = {
92 first: "John",
93 last: "Doe"
94 };
95
96 handles.push(computed(o, "name", function(first, last){
97 return first + " " + last;
98 }, at(o, "first"), at(o, "last")));
99
100 handles.push(computed(o, "nameLength", function(name){
101 return name.length;
102 }, at(o, "name")));
103
104 t.is("John Doe", o.name);
105 t.is(8, o.nameLength);
106 }
107 },
108 {
109 name: "Computed array - Observable",
110 tearDown: cleanHandles,
111 runTest: function(t){
112 var count = 0,
113 stateful = getStateful({
114 items: [
115 {name: "Anne Ackerman"},
116 {name: "Ben Beckham"},
117 {name: "Chad Chapman"},
118 {name: "Irene Ira"}
119 ],
120 countShortLessThanFour: true
121 }),
122 callbacks = [
123 function(length, oldLength){
124 t.is(57, length);
125 t.is(45, oldLength);
126 stateful.items[4].set("name", "John Doe");
127 },
128 function(length, oldLength){
129 t.is(53, length);
130 t.is(57, oldLength);
131 stateful.set("countShortLessThanFour", false);
132 },
133 function(length, oldLength){
134 t.is(42, length);
135 t.is(53, oldLength);
136 }
137 ];
138
139 handles.push(computed(stateful, "totalNameLength", function(a, countShortLessThanFour){
140 var total = 0;
141 for(var i = 0; i < a.length; i++){
142 var first = a[i].split(" ")[0];
143 total += (countShortLessThanFour || first.length >= 4 ? a[i].length : 0);
144 }
145 return total;
146 }, lang.mixin(at(stateful.items, "name"), {each: true}), at(stateful, "countShortLessThanFour")));
147
148 handles.push(stateful.watch("totalNameLength", function(name, old, current){
149 callbacks[count++](current, old);
150 }));
151
152 t.is(45, stateful.get("totalNameLength"));
153
154 stateful.items.push(getStateful({name: "John Jacklin"}));
155 t.is(3, count);
156 }
157 },
158 {
159 name: "Computed array - Non-observable",
160 tearDown: cleanHandles,
161 runTest: function(t){
162 var o = {
163 items: [
164 {name: "Anne Ackerman"},
165 {name: "Ben Beckham"},
166 {name: "Chad Chapman"},
167 {name: "Irene Ira"}
168 ],
169 countShortLessThanFour: true
170 };
171
172 handles.push(computed(o, "totalNameLength", function (a, countShortLessThanFour) {
173 var total = 0;
174 for(var i = 0; i < a.length; i++){
175 var first = a[i].split(" ")[0];
176 total += (countShortLessThanFour || first.length >= 4 ? a[i].length : 0);
177 }
178 return total;
179 }, lang.mixin(at(o.items, "name"), {each: true}), at(o, "countShortLessThanFour")));
180
181 t.is(45, o.totalNameLength);
182 }
183 },
184 {
185 name: "Computed property in array",
186 tearDown: cleanHandles,
187 runTest: function(t){
188 var called,
189 statefulArray = getStateful(["foo"]);
190
191 handles.push(computed(statefulArray, 1, function(foo){
192 return "*" + foo + "*";
193 }, at(statefulArray, 0)));
194
195 handles.push(statefulArray.watch(1, function(name, old, current){
196 t.is("*bar*", current);
197 t.is("*foo*", old);
198 called = true;
199 }));
200
201 t.is("*foo*", statefulArray[1]);
202
203 statefulArray.set(0, "bar");
204 t.t(called);
205 }
206 },
207 {
208 name: "Error in computed property callback",
209 tearDown: cleanHandles,
210 runTest: function(t){
211 var count = 0,
212 stateful = getStateful({
213 first: "John",
214 last: "Doe"
215 });
216
217 handles.push(computed(stateful, "name", function(first, last){
218 if (first === "John") {
219 throw undefined;
220 }
221 return first + " " + last;
222 }, at(stateful, "first"), at(stateful, "last")));
223
224 handles.push(computed(stateful, "nameLength", function(name){
225 return name.length;
226 }, at(stateful, "name")));
227
228 handles.push(stateful.watch("name", function(name, old, current){
229 t.is("Ben Doe", current);
230 t.is("undefined", typeof old);
231 count++;
232 }));
233
234 handles.push(stateful.watch("nameLength", function(name, old, current){
235 t.is(7, current);
236 t.is("undefined", typeof old);
237 count++;
238 }));
239
240 t.is("undefined", typeof stateful.get("name"));
241 t.is("undefined", typeof stateful.get("nameLength"));
242
243 stateful.set("first", "Ben");
244 t.is(2, count);
245 }
246 },
247 {
248 name: "Checking for same value",
249 tearDown: cleanHandles,
250 timeout: 1000,
251 runTest: function(t){
252 var dfd = new doh.Deferred(),
253 shouldChange = true,
254 count = 0,
255 stateful = getStateful({
256 foo: NaN
257 });
258 handles.push(computed(stateful, "computed", dfd.getTestErrback(function(foo){
259 if (!shouldChange) {
260 throw new Error("Change is detected even though there is no actual change.");
261 }
262 ++count;
263 return foo;
264 }), at(stateful, "foo")));
265 shouldChange = false;
266 stateful.set("foo", NaN);
267 shouldChange = true;
268 stateful.set("foo", -0);
269 stateful.set("foo", +0);
270 t.is(3, count);
271 dfd.callback(1);
272 }
273 },
274 {
275 name: "Cleaning up computed properties/arrays",
276 tearDown: cleanHandles,
277 runTest: function(){
278 var stateful = getStateful({
279 first: "John",
280 last: "Doe",
281 items: [
282 {name: "Anne Ackerman"},
283 {name: "Ben Beckham"},
284 {name: "Chad Chapman"},
285 {name: "Irene Ira"}
286 ]
287 });
288
289 var computeHandleName = computed(stateful, "name", function (first, last) {
290 return first + " " + last;
291 }, at(stateful, "first"), at(stateful, "last"));
292 handles.push(computeHandleName);
293
294 var computeHandleTotalNameLength = computed(stateful, "totalNameLength", function (names) {
295 var total = 0;
296 for(var i = 0; i < names.length; i++){
297 total += names[i].length;
298 }
299 return total;
300 }, lang.mixin(at(stateful.items, "name"), {each: true}));
301 handles.push(computeHandleTotalNameLength);
302
303 handles.push(stateful.watch("nameLength", function(){
304 throw new Error("Watch callback shouldn't be called for removed computed property.");
305 }));
306
307 handles.push(stateful.watch("totalNameLength", function(){
308 throw new Error("Watch callback shouldn't be called for removed computed array.");
309 }));
310
311 computeHandleName.remove();
312 computeHandleTotalNameLength.remove();
313
314 stateful.set("first", "Ben");
315 stateful.items.push({name: "John Jacklin"});
316 }
317 }
318 ]);
319 });