PageRenderTime 95ms CodeModel.GetById 41ms app.highlight 35ms RepoModel.GetById 15ms app.codeStats 0ms

/tests/observable/do.js

https://github.com/davgit/RxJS
JavaScript | 378 lines | 355 code | 23 blank | 0 comment | 0 complexity | e8f9ffed3bbe60a279a0f4e83726f26b MD5 | raw file
  1QUnit.module('Do');
  2
  3var Observable = Rx.Observable,
  4    TestScheduler = Rx.TestScheduler,
  5    onNext = Rx.ReactiveTest.onNext,
  6    onError = Rx.ReactiveTest.onError,
  7    onCompleted = Rx.ReactiveTest.onCompleted,
  8    subscribe = Rx.ReactiveTest.subscribe;
  9
 10test('Do_ShouldSeeAllValues', function () {
 11    var i, scheduler, sum, xs;
 12    scheduler = new TestScheduler();
 13    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
 14    i = 0;
 15    sum = 2 + 3 + 4 + 5;
 16    scheduler.startWithCreate(function () {
 17        return xs.doAction(function (x) {
 18            i++;
 19            return sum -= x;
 20        });
 21    });
 22    equal(4, i);
 23    equal(0, sum);
 24});
 25
 26test('Do_PlainAction', function () {
 27    var i, scheduler, xs;
 28    scheduler = new TestScheduler();
 29    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
 30    i = 0;
 31    scheduler.startWithCreate(function () {
 32        return xs.doAction(function (x) {
 33            return i++;
 34        });
 35    });
 36    equal(4, i);
 37});
 38
 39test('Do_NextCompleted', function () {
 40    var completed, i, scheduler, sum, xs;
 41    scheduler = new TestScheduler();
 42    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
 43    i = 0;
 44    sum = 2 + 3 + 4 + 5;
 45    completed = false;
 46    scheduler.startWithCreate(function () {
 47        return xs.doAction(function (x) {
 48            i++;
 49            sum -= x;
 50        }, undefined, function () {
 51            completed = true;
 52        });
 53    });
 54    equal(4, i);
 55    equal(0, sum);
 56    ok(completed);
 57});
 58
 59test('Do_NextCompleted_Never', function () {
 60    var completed, i, scheduler;
 61    scheduler = new TestScheduler();
 62    i = 0;
 63    completed = false;
 64    scheduler.startWithCreate(function () {
 65        return Rx.Observable.never().doAction(function (x) {
 66            i++;
 67        }, undefined, function () {
 68            completed = true;
 69        });
 70    });
 71    equal(0, i);
 72    ok(!completed);
 73});
 74
 75test('Do_NextError', function () {
 76    var ex, i, sawError, scheduler, sum, xs;
 77    ex = 'ex';
 78    scheduler = new TestScheduler();
 79    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onError(250, ex));
 80    i = 0;
 81    sum = 2 + 3 + 4 + 5;
 82    sawError = false;
 83    scheduler.startWithCreate(function () {
 84        return xs.doAction(function (x) {
 85            i++;
 86            sum -= x;
 87        }, function (e) {
 88            sawError = e === ex;
 89        });
 90    });
 91    equal(4, i);
 92    equal(0, sum);
 93    ok(sawError);
 94});
 95
 96test('Do_NextErrorNot', function () {
 97    var i, sawError, scheduler, sum, xs;
 98    scheduler = new TestScheduler();
 99    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
100    i = 0;
101    sum = 2 + 3 + 4 + 5;
102    sawError = false;
103    scheduler.startWithCreate(function () {
104        return xs.doAction(function (x) {
105            i++;
106            sum -= x;
107        }, function (e) {
108            sawError = true;
109        });
110    });
111    equal(4, i);
112    equal(0, sum);
113    ok(!sawError);
114});
115
116test('Do_NextErrorCompleted', function () {
117    var hasCompleted, i, sawError, scheduler, sum, xs;
118    scheduler = new TestScheduler();
119    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
120    i = 0;
121    sum = 2 + 3 + 4 + 5;
122    sawError = false;
123    hasCompleted = false;
124    scheduler.startWithCreate(function () {
125        return xs.doAction(function (x) {
126            i++;
127            sum -= x;
128        }, function (e) {
129            sawError = true;
130        }, function () {
131            hasCompleted = true;
132        });
133    });
134    equal(4, i);
135    equal(0, sum);
136    ok(!sawError);
137    ok(hasCompleted);
138});
139
140test('Do_NextErrorCompletedError', function () {
141    var ex, hasCompleted, i, sawError, scheduler, sum, xs;
142    ex = 'ex';
143    scheduler = new TestScheduler();
144    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onError(250, ex));
145    i = 0;
146    sum = 2 + 3 + 4 + 5;
147    sawError = false;
148    hasCompleted = false;
149    scheduler.startWithCreate(function () {
150        return xs.doAction(function (x) {
151            i++;
152            sum -= x;
153        }, function (e) {
154            sawError = ex === e;
155        }, function () {
156            hasCompleted = true;
157        });
158    });
159    equal(4, i);
160    equal(0, sum);
161    ok(sawError);
162    ok(!hasCompleted);
163});
164
165test('Do_NextErrorCompletedNever', function () {
166    var hasCompleted, i, sawError, scheduler;
167    scheduler = new TestScheduler();
168    i = 0;
169    sawError = false;
170    hasCompleted = false;
171    scheduler.startWithCreate(function () {
172        return Rx.Observable.never().doAction(function (x) {
173            i++;
174        }, function (e) {
175            sawError = true;
176        }, function () {
177            hasCompleted = true;
178        });
179    });
180    equal(0, i);
181    ok(!sawError);
182    ok(!hasCompleted);
183});
184
185test('Do_Observer_SomeDataWithError', function () {
186    var ex, hasCompleted, i, sawError, scheduler, sum, xs;
187    ex = 'ex';
188    scheduler = new TestScheduler();
189    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onError(250, ex));
190    i = 0;
191    sum = 2 + 3 + 4 + 5;
192    sawError = false;
193    hasCompleted = false;
194    scheduler.startWithCreate(function () {
195        return xs.doAction(Rx.Observer.create(function (x) {
196            i++;
197            sum -= x;
198        }, function (e) {
199            sawError = e === ex;
200        }, function () {
201            hasCompleted = true;
202        }));
203    });
204    equal(4, i);
205    equal(0, sum);
206    ok(sawError);
207    ok(!hasCompleted);
208});
209
210test('Do_Observer_SomeDataWithError', function () {
211    var hasCompleted, i, sawError, scheduler, sum, xs;
212    scheduler = new TestScheduler();
213    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onNext(220, 3), onNext(230, 4), onNext(240, 5), onCompleted(250));
214    i = 0;
215    sum = 2 + 3 + 4 + 5;
216    sawError = false;
217    hasCompleted = false;
218    scheduler.startWithCreate(function () {
219        return xs.doAction(Rx.Observer.create(function (x) {
220            i++;
221            sum -= x;
222        }, function (e) {
223            sawError = true;
224        }, function () {
225            hasCompleted = true;
226        }));
227    });
228    equal(4, i);
229    equal(0, sum);
230    ok(!sawError);
231    ok(hasCompleted);
232});
233
234test('Do1422_Next_NextThrows', function () {
235    var ex, results, scheduler, xs;
236    ex = 'ex';
237    scheduler = new TestScheduler();
238    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
239    results = scheduler.startWithCreate(function () {
240        return xs.doAction(function () {
241            throw ex;
242        });
243    });
244    results.messages.assertEqual(onError(210, ex));
245});
246
247test('Do1422_NextCompleted_NextThrows', function () {
248    var ex, results, scheduler, xs, _undefined;
249    ex = 'ex';
250    scheduler = new TestScheduler();
251    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
252    results = scheduler.startWithCreate(function () {
253        return xs.doAction(function () {
254            throw ex;
255        }, _undefined, function () { });
256    });
257    results.messages.assertEqual(onError(210, ex));
258});
259
260test('Do1422_NextCompleted_CompletedThrows', function () {
261    var ex, results, scheduler, xs, _undefined;
262    ex = 'ex';
263    scheduler = new TestScheduler();
264    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
265    results = scheduler.startWithCreate(function () {
266        return xs.doAction(function () { }, _undefined, function () {
267            throw ex;
268        });
269    });
270    results.messages.assertEqual(onNext(210, 2), onError(250, ex));
271});
272
273test('Do1422_NextError_NextThrows', function () {
274    var ex, results, scheduler, xs;
275    ex = 'ex';
276    scheduler = new TestScheduler();
277    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
278    results = scheduler.startWithCreate(function () {
279        return xs.doAction(function () {
280            throw ex;
281        }, function () { });
282    });
283    results.messages.assertEqual(onError(210, ex));
284});
285
286test('Do1422_NextError_NextThrows', function () {
287    var ex1, ex2, results, scheduler, xs;
288    ex1 = 'ex1';
289    ex2 = 'ex2';
290    scheduler = new TestScheduler();
291    xs = scheduler.createHotObservable(onNext(150, 1), onError(210, ex1));
292    results = scheduler.startWithCreate(function () {
293        return xs.doAction(function () { }, function () {
294            throw ex2;
295        });
296    });
297    results.messages.assertEqual(onError(210, ex2));
298});
299
300test('Do1422_NextErrorCompleted_NextThrows', function () {
301    var ex, results, scheduler, xs;
302    ex = 'ex';
303    scheduler = new TestScheduler();
304    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
305    results = scheduler.startWithCreate(function () {
306        return xs.doAction(function () {
307            throw ex;
308        }, function () { }, function () { });
309    });
310    results.messages.assertEqual(onError(210, ex));
311});
312
313test('Do1422_NextErrorCompleted_ErrorThrows', function () {
314    var ex1, ex2, results, scheduler, xs;
315    ex1 = 'ex1';
316    ex2 = 'ex2';
317    scheduler = new TestScheduler();
318    xs = scheduler.createHotObservable(onNext(150, 1), onError(210, ex1));
319    results = scheduler.startWithCreate(function () {
320        return xs.doAction(function () { }, function () {
321            throw ex2;
322        }, function () { });
323    });
324    results.messages.assertEqual(onError(210, ex2));
325});
326
327test('Do1422_NextErrorCompleted_CompletedThrows', function () {
328    var ex, results, scheduler, xs;
329    ex = 'ex';
330    scheduler = new TestScheduler();
331    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
332    results = scheduler.startWithCreate(function () {
333        return xs.doAction(function () { }, function () { }, function () {
334            throw ex;
335        });
336    });
337    results.messages.assertEqual(onNext(210, 2), onError(250, ex));
338});
339
340test('Do1422_Observer_NextThrows', function () {
341    var ex, results, scheduler, xs;
342    ex = 'ex';
343    scheduler = new TestScheduler();
344    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
345    results = scheduler.startWithCreate(function () {
346        return xs.doAction(Rx.Observer.create(function () {
347            throw ex;
348        }, function () { }, function () { }));
349    });
350    results.messages.assertEqual(onError(210, ex));
351});
352
353test('Do1422_Observer_ErrorThrows', function () {
354    var ex1, ex2, results, scheduler, xs;
355    ex1 = 'ex1';
356    ex2 = 'ex2';
357    scheduler = new TestScheduler();
358    xs = scheduler.createHotObservable(onNext(150, 1), onError(210, ex1));
359    results = scheduler.startWithCreate(function () {
360        return xs.doAction(Rx.Observer.create(function () { }, function () {
361            throw ex2;
362        }, function () { }));
363    });
364    results.messages.assertEqual(onError(210, ex2));
365});
366
367test('Do1422_Observer_CompletedThrows', function () {
368    var ex, results, scheduler, xs;
369    ex = 'ex';
370    scheduler = new TestScheduler();
371    xs = scheduler.createHotObservable(onNext(150, 1), onNext(210, 2), onCompleted(250));
372    results = scheduler.startWithCreate(function () {
373        return xs.doAction(Rx.Observer.create(function () { }, function () { }, function () {
374            throw ex;
375        }));
376    });
377    results.messages.assertEqual(onNext(210, 2), onError(250, ex));
378});