How to use expect.promise method in unexpected

Best JavaScript code snippet using unexpected

Promise.js

Source:Promise.js Github

copy

Full Screen

1/* global jasmine, describe, it, beforeEach, afterEach, expect, waitsFor, runs, xit, xdescribe */2/*3 Adapted from:4 Copyright (c) 2013 [DeftJS Framework Contributors](http://deftjs.org)5 Open source under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).6 */7describe('Ext.promise.Promise', function() {8 var Deferred = Ext.Deferred,9 ExtPromise = Ext.promise.Promise,10 targetScope = {},11 deferred, promise, extLog;12 if (Object.freeze) {13 Object.freeze(targetScope);14 }15 beforeEach(function() {16 // Prevent raised errors from polluting the console17 extLog = Ext.log;18 Ext.log = Ext.emptyFn;19 20 deferred = promise = null;21 });22 23 afterEach(function() {24 Ext.log = extLog;25 26 deferred = promise = extLog = null;27 });28 function eventuallyResolvesTo(promise, value, equals) {29 var done = false,30 result;31 promise.then(function(v) {32 result = v;33 done = true;34 });35 waitsFor(function() {36 return done;37 });38 runs(function() {39 if (equals) {40 expect(result).toEqual(value);41 } else {42 expect(result).toBe(value);43 }44 });45 }46 function eventuallyRejectedWith(promise, error, message) {47 var done = false,48 result, reason;49 promise.then(function(v) {50 result = v;51 done = true;52 }, function(v) {53 reason = v;54 done = true;55 });56 waitsFor(function() {57 return done;58 });59 runs(function() {60 expect(result).toBe(undefined);61 if (typeof error === 'string') {62 expect(reason).toBe(error);63 } else {64 expect(reason instanceof error).toBe(true);65 if (message) {66 expect(reason.message).toBe(message);67 }68 }69 });70 }71 function formatValue(value) {72 var formattedValues;73 74 if (value instanceof ExtPromise) {75 return 'Promise';76 }77 78 if (value instanceof Deferred) {79 return 'Deferred';80 }81 82 if (value instanceof Ext.ClassManager.get('Ext.Base')) {83 return Ext.ClassManager.getName(value);84 }85 86 if (Ext.isArray(value)) {87 formattedValues = Ext.Array.map(value, formatValue);88 return "[" + (formattedValues.join(', ')) + "]";89 }90 91 if (Ext.isObject(value)) {92 return 'Object';93 }94 95 if (Ext.isString(value)) {96 return '"' + value + '"';97 }98 99 return '' + value;100 }101 describe('resolved()', function() {102 var values = [void 0, null, false, 0, 1, 'expected value', [1, 2, 3], {}, new Error('error message')];103 describe('returns a Promise that will resolve with the specified value', function() {104 Ext.each(values, function(value) {105 it(formatValue(value), function() {106 promise = Deferred.resolved(value);107 expect(promise instanceof ExtPromise).toBe(true);108 eventuallyResolvesTo(promise, value);109 });110 });111 });112 describe('returns a Promise that will resolve with the resolved value for the specified Promise when it resolves', function() {113 Ext.each(values, function(value) {114 it(formatValue(value), function() {115 var deferred = new Deferred();116 deferred.resolve(value);117 promise = Deferred.resolved(deferred.promise);118 expect(promise).not.toBe(deferred.promise);119 expect(promise instanceof ExtPromise).toBe(true);120 eventuallyResolvesTo(promise, value);121 });122 });123 });124 describe('returns a Promise that will reject with the error associated with the specified Promise when it rejects', function() {125 it('Error: error message', function() {126 deferred = new Deferred();127 deferred.reject(new Error('error message'));128 promise = Deferred.resolved(deferred.promise);129 expect(promise).not.toBe(deferred.promise);130 expect(promise instanceof ExtPromise).toBe(true);131 eventuallyRejectedWith(promise, Error, 'error message');132 });133 });134 describe('returns a new Promise that will adapt the specified untrusted (aka third-party) then-able', function() {135 var MockThirdPartyPromise = function() {};136 MockThirdPartyPromise.prototype.then = function(successCallback, failureCallback) {137 this.successCallback = successCallback;138 this.failureCallback = failureCallback;139 switch (this.state) {140 case 'resolved':141 this.successCallback(this.value);142 break;143 case 'rejected':144 this.failureCallback(this.value);145 }146 };147 MockThirdPartyPromise.prototype.resolve = function(value) {148 this.value = value;149 this.state = 'resolved';150 if (this.successCallback != null) {151 this.successCallback(this.value);152 }153 };154 MockThirdPartyPromise.prototype.reject = function(value) {155 this.value = value;156 this.state = 'rejected';157 if (this.failureCallback != null) {158 this.failureCallback(this.value);159 }160 };161 it('resolves when resolved', function() {162 var mockThirdPartyPromise = new MockThirdPartyPromise();163 mockThirdPartyPromise.resolve('expected value');164 promise = Deferred.resolved(mockThirdPartyPromise);165 expect(promise).not.toBe(mockThirdPartyPromise);166 expect(promise instanceof ExtPromise).toBe(true);167 eventuallyResolvesTo(promise, 'expected value');168 });169 it('rejects when rejected', function() {170 var mockThirdPartyPromise = new MockThirdPartyPromise();171 mockThirdPartyPromise.reject('error message');172 promise = Deferred.resolved(mockThirdPartyPromise);173 expect(promise).not.toBe(mockThirdPartyPromise);174 expect(promise instanceof ExtPromise).toBe(true);175 eventuallyRejectedWith(promise, 'error message');176 });177 });178 });179 describe('Promise.is()', function() {180 describe('returns true for a Promise or then()-able', function() {181 it('Promise', function() {182 promise = new Deferred().promise;183 expect(ExtPromise.is(promise)).toBe(true);184 });185 186 it('returns true for any then()-able', function() {187 promise = {188 then: function() {}189 };190 expect(ExtPromise.is(promise)).toBe(true);191 });192 });193 describe('returns false for non-promises', function() {194 var values = [void 0, null, false, 0, 1, 'value', [1, 2, 3], {}, new Error('error message')];195 Ext.each(values, function(value) {196 it(formatValue(value), function() {197 expect(ExtPromise.is(value)).toBe(false);198 });199 });200 });201 });202 describe('all()', function() {203 describe('returns a new Promise that will resolve with the resolved values for the specified Array of Promises(s) or values.', function() {204 it('Empty Array', function() {205 var value = [];206 promise = ExtPromise.all(value);207 expect(promise instanceof ExtPromise).toBe(true);208 eventuallyResolvesTo(promise, value, true);209 });210 it('Array with one value', function() {211 var value = ['expected value'];212 promise = ExtPromise.all(value);213 expect(promise instanceof ExtPromise).toBe(true);214 eventuallyResolvesTo(promise, value, true);215 });216 it('Array of values', function() {217 var value = [1, 2, 3];218 promise = ExtPromise.all(value);219 expect(promise instanceof ExtPromise).toBe(true);220 eventuallyResolvesTo(promise, value, true);221 });222 it('Sparse Array', function() {223 var value = [, 2, , 4, 5];224 promise = ExtPromise.all(value);225 expect(promise instanceof ExtPromise).toBe(true);226 eventuallyResolvesTo(promise, value, true);227 });228 it('Array with one resolved Promise', function() {229 promise = ExtPromise.all([230 Deferred.resolved('expected value')231 ]);232 expect(promise instanceof ExtPromise).toBe(true);233 eventuallyResolvesTo(promise, ['expected value'], true);234 });235 it('Array of resolved Promises', function() {236 promise = ExtPromise.all([237 Deferred.resolved(1),238 Deferred.resolved(2),239 Deferred.resolved(3)240 ]);241 expect(promise instanceof ExtPromise).toBe(true);242 eventuallyResolvesTo(promise, [1, 2, 3], true);243 });244 });245 describe('returns a new Promise that will resolve with the resolved values for the specified resolved Promise of an Array of Promises(s) or values.', function() {246 it('Promise of an empty Array', function() {247 promise = ExtPromise.all(Deferred.resolved([]));248 expect(promise instanceof ExtPromise).toBe(true);249 eventuallyResolvesTo(promise, [], true);250 });251 it('Promise of an Array with one value', function() {252 promise = ExtPromise.all(Deferred.resolved(['expected value']));253 expect(promise instanceof ExtPromise).toBe(true);254 eventuallyResolvesTo(promise, ['expected value'], true);255 });256 it('Promise of an Array of values', function() {257 promise = ExtPromise.all(Deferred.resolved([1, 2, 3]));258 expect(promise instanceof ExtPromise).toBe(true);259 eventuallyResolvesTo(promise, [1, 2, 3], true);260 });261 it('Promise of a sparse Array', function() {262 promise = ExtPromise.all(Deferred.resolved([, 2, , 4, 5]));263 expect(promise instanceof ExtPromise).toBe(true);264 eventuallyResolvesTo(promise, [, 2, , 4, 5], true);265 });266 it('Promise of an Array with one resolved Promise', function() {267 promise = ExtPromise.all(Deferred.resolved([Deferred.resolved('expected value')]));268 expect(promise instanceof ExtPromise).toBe(true);269 eventuallyResolvesTo(promise, ['expected value'], true);270 });271 it('Promise of an Array of resolved Promises', function() {272 promise = ExtPromise.all(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]));273 expect(promise instanceof ExtPromise).toBe(true);274 eventuallyResolvesTo(promise, [1, 2, 3], true);275 });276 });277 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified Array of Promise(s) or value(s) that rejects', function() {278 it('Array with one rejected Promise', function() {279 promise = ExtPromise.all([Deferred.rejected(new Error('error message'))]);280 expect(promise instanceof ExtPromise).toBe(true);281 eventuallyRejectedWith(promise, Error, 'error message');282 });283 it('Array of resolved Promises and a rejected Promise', function() {284 promise = ExtPromise.all([Deferred.resolved(1), Deferred.rejected(new Error('error message')), Deferred.resolved(3)]);285 expect(promise instanceof ExtPromise).toBe(true);286 eventuallyRejectedWith(promise, Error, 'error message');287 });288 it('Array of values, pending and resolved Promises and a rejected Promise', function() {289 promise = ExtPromise.all([1, 2,290 Deferred.rejected(new Error('error message')),291 Deferred.resolved(4),292 new Deferred().promise293 ]);294 295 expect(promise instanceof ExtPromise).toBe(true);296 eventuallyRejectedWith(promise, Error, 'error message');297 });298 });299 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified resolved Promise of an Array of Promise(s) or value(s) that rejects', function() {300 it('Promise of an Array with one rejected Promise', function() {301 promise = ExtPromise.all(Deferred.resolved([Deferred.rejected(new Error('error message'))]));302 expect(promise instanceof ExtPromise).toBe(true);303 eventuallyRejectedWith(promise, Error, 'error message');304 });305 it('Promise of an Array of resolved Promises and a rejected Promise', function() {306 promise = ExtPromise.all(Deferred.resolved([307 Deferred.resolved(1),308 Deferred.rejected(new Error('error message')),309 Deferred.resolved(3)310 ]));311 expect(promise instanceof ExtPromise).toBe(true);312 eventuallyRejectedWith(promise, Error, 'error message');313 });314 it('Promise of an Array of values, pending and resolved Promises and a rejected Promise', function() {315 promise = ExtPromise.all(Deferred.resolved([316 1, 2,317 Deferred.rejected(new Error('error message')),318 Deferred.resolved(4),319 new Deferred().promise320 ]));321 expect(promise instanceof ExtPromise).toBe(true);322 eventuallyRejectedWith(promise, Error, 'error message');323 });324 });325 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an Array of Promise(s) or value(s)', function() {326 it('Error: error message', function() {327 promise = ExtPromise.all(Deferred.rejected(new Error('error message')));328 expect(promise instanceof ExtPromise).toBe(true);329 eventuallyRejectedWith(promise, Error, 'error message');330 });331 });332 describe('throws an Error if anything other than Array or Promise of an Array is specified', function() {333 it('no parameters', function() {334 expect(function() {335 return ExtPromise.all();336 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');337 });338 it('a single non-Array parameter', function() {339 expect(function() {340 return ExtPromise.all(1);341 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');342 });343 it('multiple non-Array parameters', function() {344 expect(function() {345 return ExtPromise.all(1, 2, 3);346 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');347 });348 });349 });350 describe('any()', function() {351 function eventuallyResolvesToOneOf(promise, values) {352 var done = false,353 result;354 promise.then(function(v) {355 result = v;356 done = true;357 });358 waitsFor(function() {359 return done;360 });361 runs(function() {362 expect(Ext.Array.indexOf(values, result)).not.toBe(-1);363 });364 }365 describe('returns a new Promise that will resolve once any one of the specified Array of Promises(s) or values have resolved.', function() {366 it('Array with one value', function() {367 promise = Deferred.any(['expected value']);368 expect(promise instanceof ExtPromise).toBe(true);369 eventuallyResolvesTo(promise, 'expected value', true);370 });371 it('Array of values', function() {372 promise = Deferred.any([1, 2, 3]);373 expect(promise instanceof ExtPromise).toBe(true);374 eventuallyResolvesToOneOf(promise, [1, 2, 3]);375 });376 it('Sparse Array', function() {377 promise = Deferred.any([, 2, , 4, 5]);378 expect(promise instanceof ExtPromise).toBe(true);379 eventuallyResolvesToOneOf(promise, [2, 4, 5]);380 });381 it('Array with one resolved Promise', function() {382 promise = Deferred.any([Deferred.resolved('expected value')]);383 expect(promise instanceof ExtPromise).toBe(true);384 eventuallyResolvesTo(promise, 'expected value', true);385 });386 it('Array of resolved Promises', function() {387 promise = Deferred.any([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]);388 expect(promise instanceof ExtPromise).toBe(true);389 eventuallyResolvesToOneOf(promise, [1, 2, 3]);390 });391 it('Array of rejected Promises and one resolved Promise', function() {392 promise = Deferred.any([Deferred.rejected('error message'), Deferred.resolved('expected value'), Deferred.rejected('error message')]);393 expect(promise instanceof ExtPromise).toBe(true);394 eventuallyResolvesTo(promise, 'expected value', true);395 });396 it('Array of pending and rejected Promises and one resolved Promise', function() {397 promise = Deferred.any([new Deferred().promise, Deferred.resolved('expected value'), Deferred.rejected('error message')]);398 expect(promise instanceof ExtPromise).toBe(true);399 eventuallyResolvesTo(promise, 'expected value', true);400 });401 it('Array of pending and rejected Promises and multiple resolved Promises', function() {402 promise = Deferred.any([new Deferred().promise, Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)]);403 expect(promise instanceof ExtPromise).toBe(true);404 eventuallyResolvesToOneOf(promise, [1, 2]);405 });406 });407 describe('returns a new Promise that will resolve once any one of the specified resolved Promise of an Array of Promises(s) or values have resolved.', function() {408 it('Promise of an Array with one value', function() {409 promise = Deferred.any(Deferred.resolved(['expected value']));410 expect(promise instanceof ExtPromise).toBe(true);411 eventuallyResolvesTo(promise, 'expected value', true);412 });413 it('Promise of an Array of values', function() {414 promise = Deferred.any(Deferred.resolved([1, 2, 3]));415 expect(promise instanceof ExtPromise).toBe(true);416 eventuallyResolvesToOneOf(promise, [1, 2, 3]);417 });418 it('Promise of a sparse Array', function() {419 promise = Deferred.any(Deferred.resolved([, 2, , 4, 5]));420 expect(promise instanceof ExtPromise).toBe(true);421 eventuallyResolvesToOneOf(promise, [2, 4, 5]);422 });423 it('Promise of an Array with one resolved Promise', function() {424 promise = Deferred.any(Deferred.resolved([Deferred.resolved('expected value')]));425 expect(promise instanceof ExtPromise).toBe(true);426 eventuallyResolvesTo(promise, 'expected value', true);427 });428 it('Promise of an Array of resolved Promise', function() {429 promise = Deferred.any(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]));430 expect(promise instanceof ExtPromise).toBe(true);431 eventuallyResolvesToOneOf(promise, [1, 2, 3]);432 });433 it('Promise of an Array of rejected Promises and one resolved Promise', function() {434 promise = Deferred.any(Deferred.resolved([Deferred.rejected('error message'), Deferred.resolved('expected value'), Deferred.rejected('error message')]));435 expect(promise instanceof ExtPromise).toBe(true);436 eventuallyResolvesTo(promise, 'expected value', true);437 });438 it('Promise of an Array of pending and rejected Promises and one resolved Promise', function() {439 promise = Deferred.any(Deferred.resolved([new Deferred().promise, Deferred.resolved('expected value'), Deferred.rejected('error message')]));440 expect(promise instanceof ExtPromise).toBe(true);441 eventuallyResolvesTo(promise, 'expected value', true);442 });443 it('Promise of an Array of pending and rejected Promises and multiple resolved Promises', function() {444 promise = Deferred.any(Deferred.resolved([new Deferred().promise, Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)]));445 expect(promise instanceof ExtPromise).toBe(true);446 eventuallyResolvesToOneOf(promise, [1, 2]);447 });448 });449 describe('returns a new Promise that will reject if none of the specified Array of Promises(s) or values resolves.', function() {450 it('Empty Array', function() {451 promise = Deferred.any([]);452 expect(promise instanceof ExtPromise).toBe(true);453 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');454 });455 it('Array with one rejected Promise', function() {456 promise = Deferred.any([Deferred.rejected('error message')]);457 expect(promise instanceof ExtPromise).toBe(true);458 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');459 });460 it('Array of rejected Promises', function() {461 promise = Deferred.any([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')]);462 expect(promise instanceof ExtPromise).toBe(true);463 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');464 });465 });466 describe('returns a new Promise that will reject if none of the specified resolved Promise of an Array of Promises(s) or values resolves.', function() {467 it('Promise of an empty Array', function() {468 promise = Deferred.any(Deferred.resolved([]));469 expect(promise instanceof ExtPromise).toBe(true);470 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');471 });472 it('Promise of an Array with one rejected Promise', function() {473 promise = Deferred.any(Deferred.resolved([Deferred.rejected('error message')]));474 expect(promise instanceof ExtPromise).toBe(true);475 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');476 });477 it('Promise of an Array of rejected Promises', function() {478 promise = Deferred.any(Deferred.resolved([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')]));479 expect(promise instanceof ExtPromise).toBe(true);480 eventuallyRejectedWith(promise, Error, 'No Promises were resolved.');481 });482 });483 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an Array of Promise(s) or value(s)', function() {484 it('Error: error message', function() {485 promise = Deferred.any(Deferred.rejected(new Error('error message')));486 expect(promise instanceof ExtPromise).toBe(true);487 eventuallyRejectedWith(promise, Error, 'error message');488 });489 });490 describe('throws an Error if anything other than Array or Promise of an Array is specified', function() {491 it('no parameters', function() {492 expect(function() {493 return Deferred.any();494 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');495 });496 it('a single non-Array parameter', function() {497 expect(function() {498 return Deferred.any(1);499 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');500 });501 it('multiple non-Array parameters', function() {502 expect(function() {503 return Deferred.any(1, 2, 3);504 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');505 });506 });507 });508 describe('some()', function() {509 function eventuallyResolvesToSomeOf (promise, length, values) {510 var done = false,511 result;512 promise.then(function(v) {513 result = v;514 done = true;515 });516 waitsFor(function() {517 return done;518 });519 runs(function() {520 expect(result.length).toBe(length);521 var map = {};522 for (var i = 0; i < result.length; ++i) {523 var index = Ext.Array.indexOf(values, result[i]);524 expect(index).not.toBe(-1);525 expect(map[index]).not.toBe(true);526 map[index] = true;527 }528 });529 }530 describe('returns a new Promise that will resolve once the specified number of the specified Array of Promises(s) or values have resolved.', function() {531 it('Array with one value', function() {532 promise = Deferred.some(['expected value'], 1);533 expect(promise instanceof ExtPromise).toBe(true);534 eventuallyResolvesTo(promise, ['expected value'], true);535 });536 it('Array of values', function() {537 promise = Deferred.some([1, 2, 3], 2);538 expect(promise instanceof ExtPromise).toBe(true);539 eventuallyResolvesToSomeOf(promise, 2, [1, 2, 3]);540 });541 it('Sparse Array', function() {542 promise = Deferred.some([, 2, , 4, 5], 2);543 expect(promise instanceof ExtPromise).toBe(true);544 eventuallyResolvesToSomeOf(promise, 2, [2, 4, 5]);545 });546 it('Array with one resolved Promise', function() {547 promise = Deferred.some([Deferred.resolved('expected value')], 1);548 expect(promise instanceof ExtPromise).toBe(true);549 eventuallyResolvesTo(promise, ['expected value'], true);550 });551 it('Array of resolved Promises', function() {552 promise = Deferred.some([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], 2);553 expect(promise instanceof ExtPromise).toBe(true);554 eventuallyResolvesToSomeOf(promise, 2, [1, 2, 3]);555 });556 it('Array of rejected Promises and one resolved Promise', function() {557 promise = Deferred.some([Deferred.rejected('error message'), Deferred.resolved('expected value'), Deferred.rejected('error message')], 1);558 expect(promise instanceof ExtPromise).toBe(true);559 eventuallyResolvesTo(promise, ['expected value'], true);560 });561 it('Array of pending and rejected Promises and one resolved Promise', function() {562 promise = Deferred.some([new Deferred().promise, Deferred.resolved('expected value'), Deferred.rejected('error message')], 1);563 expect(promise instanceof ExtPromise).toBe(true);564 eventuallyResolvesTo(promise, ['expected value'], true);565 });566 it('Array of rejected Promises and multiple resolved Promises', function() {567 promise = Deferred.some([Deferred.rejected('error message'), Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)], 2);568 expect(promise instanceof ExtPromise).toBe(true);569 eventuallyResolvesToSomeOf(promise, 2, [1, 2]);570 });571 it('Array of pending and rejected Promises and multiple resolved Promises', function() {572 promise = Deferred.some([new Deferred().promise, Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)], 2);573 expect(promise instanceof ExtPromise).toBe(true);574 eventuallyResolvesToSomeOf(promise, 2, [1, 2]);575 });576 });577 describe('returns a new Promise that will resolve once the specified number of the specified resolved Promise of an Array of Promises(s) or values have resolved.', function() {578 it('Promise of an Array with one value', function() {579 promise = Deferred.some(Deferred.resolved(['expected value']), 1);580 expect(promise instanceof ExtPromise).toBe(true);581 eventuallyResolvesTo(promise, ['expected value'], true);582 });583 it('Promise of an Array of values', function() {584 promise = Deferred.some(Deferred.resolved([1, 2, 3]), 2);585 expect(promise instanceof ExtPromise).toBe(true);586 eventuallyResolvesToSomeOf(promise, 2, [1, 2, 3]);587 });588 it('Promise of a sparse Array', function() {589 promise = Deferred.some(Deferred.resolved([, 2, , 4, 5]), 2);590 expect(promise instanceof ExtPromise).toBe(true);591 eventuallyResolvesToSomeOf(promise, 2, [2, 4, 5]);592 });593 it('Promise of an Array with one resolved Promise', function() {594 promise = Deferred.some(Deferred.resolved([Deferred.resolved('expected value')]), 1);595 expect(promise instanceof ExtPromise).toBe(true);596 eventuallyResolvesTo(promise, ['expected value'], true);597 });598 it('Promise of an Array of resolved Promises', function() {599 promise = Deferred.some(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), 2);600 expect(promise instanceof ExtPromise).toBe(true);601 eventuallyResolvesToSomeOf(promise, 2, [1, 2, 3]);602 });603 it('Promise of an Array of rejected Promises and one resolved Promise', function() {604 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message'), Deferred.resolved('expected value'), Deferred.rejected('error message')]), 1);605 expect(promise instanceof ExtPromise).toBe(true);606 eventuallyResolvesTo(promise, ['expected value'], true);607 });608 it('Promise of an Array of pending and rejected Promises and one resolved Promise', function() {609 promise = Deferred.some(Deferred.resolved([new Deferred().promise, Deferred.resolved('expected value'), Deferred.rejected('error message')]), 1);610 expect(promise instanceof ExtPromise).toBe(true);611 eventuallyResolvesTo(promise, ['expected value'], true);612 });613 it('Promise of an Array of rejected Promises and multiple resolved Promises', function() {614 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message'), Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)]), 2);615 expect(promise instanceof ExtPromise).toBe(true);616 eventuallyResolvesToSomeOf(promise, 2, [1, 2]);617 });618 it('Promise of an Array of pending and rejected Promises and multiple resolved Promises', function() {619 promise = Deferred.some(Deferred.resolved([new Deferred().promise, Deferred.resolved(1), Deferred.rejected('error message'), Deferred.resolved(2)]), 2);620 expect(promise instanceof ExtPromise).toBe(true);621 eventuallyResolvesToSomeOf(promise, 2, [1, 2]);622 });623 });624 describe('returns a new Promise that will reject if too few of the specified Array of Promises(s) or values resolves.', function() {625 it('Empty Array with one resolved value requested', function() {626 promise = Deferred.some([], 1);627 expect(promise instanceof ExtPromise).toBe(true);628 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');629 });630 it('Empty Array with multiple resolved values requested', function() {631 promise = Deferred.some([], 2);632 expect(promise instanceof ExtPromise).toBe(true);633 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');634 });635 it('Array with one rejected Promise with one resolved value requested', function() {636 promise = Deferred.some([Deferred.rejected('error message')], 1);637 expect(promise instanceof ExtPromise).toBe(true);638 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');639 });640 it('Array with one rejected Promise with multiple resolved values requested', function() {641 promise = Deferred.some([Deferred.rejected('error message')], 2);642 expect(promise instanceof ExtPromise).toBe(true);643 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');644 });645 it('Array of rejected Promises with one resolved value requested', function() {646 promise = Deferred.some([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')], 1);647 expect(promise instanceof ExtPromise).toBe(true);648 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');649 });650 it('Array of rejected Promises with multiple resolved values requested', function() {651 promise = Deferred.some([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')], 2);652 expect(promise instanceof ExtPromise).toBe(true);653 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');654 });655 });656 describe('returns a new Promise that will reject if too few of the specified resolved Promise of an Array of Promises(s) or values resolves.', function() {657 it('Promise of an empty Array with one resolved value requested', function() {658 promise = Deferred.some(Deferred.resolved([]), 1);659 expect(promise instanceof ExtPromise).toBe(true);660 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');661 });662 it('Promise of an empty Array with multiple resolved values requested', function() {663 promise = Deferred.some(Deferred.resolved([]), 2);664 expect(promise instanceof ExtPromise).toBe(true);665 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');666 });667 it('Promise of an Array with one rejected Promise with one resolved value requested', function() {668 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message')]), 1);669 expect(promise instanceof ExtPromise).toBe(true);670 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');671 });672 it('Promise of an Array with one rejected Promise with multiple resolved values requested', function() {673 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message')]), 2);674 expect(promise instanceof ExtPromise).toBe(true);675 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');676 });677 it('Promise of an Array of rejected Promises with one resolved value requested', function() {678 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')]), 1);679 expect(promise instanceof ExtPromise).toBe(true);680 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');681 });682 it('Promise of an Array of rejected Promises with multiple resolved values requested', function() {683 promise = Deferred.some(Deferred.resolved([Deferred.rejected('error message'), Deferred.rejected('error message'), Deferred.rejected('error message')]), 2);684 expect(promise instanceof ExtPromise).toBe(true);685 eventuallyRejectedWith(promise, Error, 'Too few Promises were resolved.');686 });687 });688 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an Array of Promise(s) or value(s)', function() {689 it('Error: error message', function() {690 promise = Deferred.some(Deferred.rejected(new Error('error message')), 2);691 expect(promise instanceof ExtPromise).toBe(true);692 eventuallyRejectedWith(promise, Error, 'error message');693 });694 });695 describe('throws an Error if anything other than Array or Promise of an Array is specified', function() {696 it('no parameters', function() {697 expect(function() {698 return Deferred.some();699 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');700 });701 702 it('a single non-Array parameter', function() {703 expect(function() {704 return Deferred.some(1);705 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');706 });707 708 it('multiple non-Array parameters', function() {709 expect(function() {710 return Deferred.some(1, 2, 3);711 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');712 });713 714 it('a single Array parameter', function() {715 expect(function() {716 return Deferred.some([1, 2, 3]);717 }).toThrow('Invalid parameter: expected a positive integer.');718 });719 720 it('a single Array parameter and a non-numeric value', function() {721 expect(function() {722 return Deferred.some([1, 2, 3], 'value');723 }).toThrow('Invalid parameter: expected a positive integer.');724 });725 });726 }); // some727 describe('delay()', function() {728 // We have to be careful testing timing due to load during test runs on the729 // build system. We basically ensure that delays are at least the specified730 // amount but also allow for sloppy IE timers (+/- 16ms).731 describe('returns a new Promise that will resolve after the specified delay', function() {732 it('0 ms delay', function() {733 promise = Deferred.delay(0);734 expect(promise instanceof ExtPromise).toBe(true);735 eventuallyResolvesTo(promise, void 0, true);736 });737 it('value with 100 ms delay', function() {738 promise = Deferred.delay(100);739 var start = Ext.now();740 expect(promise instanceof ExtPromise).toBe(true);741 promise = promise.then(function(value) {742 expect(Ext.now() - start).toBeGE(84);743 return value;744 });745 eventuallyResolvesTo(promise, void 0, true);746 });747 });748 describe('returns a new Promise that will resolve with the specified Promise or value after the specified delay', function() {749 it('value with 0 ms delay', function() {750 promise = Deferred.delay('expected value', 0);751 expect(promise instanceof ExtPromise).toBe(true);752 eventuallyResolvesTo(promise, 'expected value', true);753 });754 it('resolved Promise with 0 delay', function() {755 promise = Deferred.delay(Deferred.resolved('expected value'), 0);756 expect(promise instanceof ExtPromise).toBe(true);757 eventuallyResolvesTo(promise, 'expected value', true);758 });759 it('value with 100 ms delay', function() {760 promise = Deferred.delay('expected value', 100);761 var start = Ext.now();762 expect(promise instanceof ExtPromise).toBe(true);763 promise = promise.then(function(value) {764 expect(Ext.now() - start).toBeGE(84);765 return value;766 });767 eventuallyResolvesTo(promise, 'expected value', true);768 });769 it('resolved Promise with 100 ms delay', function() {770 promise = Deferred.delay(Deferred.resolved('expected value'), 100);771 var start = Ext.now();772 expect(promise instanceof ExtPromise).toBe(true);773 promise = promise.then(function(value) {774 expect(Ext.now() - start).toBeGE(84);775 return value;776 });777 eventuallyResolvesTo(promise, 'expected value', true);778 });779 });780 describe('returns a new Promise that will reject with the error associated with the specified rejected Promise after the specified delay', function() {781 it('rejected Promise with 100 ms delay', function() {782 promise = Deferred.delay(Deferred.rejected(new Error('error message')), 100);783 var start = Ext.now();784 expect(promise instanceof ExtPromise).toBe(true);785 promise = promise.then(function(value) {786 return value;787 }, function(error) {788 expect(Ext.now() - start).toBeGE(84);789 throw error;790 });791 eventuallyRejectedWith(promise, Error, 'error message');792 });793 });794 }); // delay795 796 // TODO Timeout tests are fragile in Firefox797 (Ext.isGecko ? xdescribe : describe)('timeout()', function() {798 describe('returns a new Promise that will resolve with the specified Promise or value if it resolves before the specified timeout', function() {799 it('value with 100 ms timeout', function() {800 promise = Deferred.timeout('expected value', 100);801 expect(promise instanceof ExtPromise).toBe(true);802 eventuallyResolvesTo(promise, 'expected value', true);803 });804 it('Promise that resolves in 50 ms with a 100 ms timeout', function() {805 promise = Deferred.timeout(Deferred.delay('expected value', 50), 100);806 expect(promise instanceof ExtPromise).toBe(true);807 eventuallyResolvesTo(promise, 'expected value', true);808 });809 });810 describe('returns a new Promise that will reject with the error associated with the specified rejected Promise if it rejects before the specified timeout', function() {811 it('Promise that rejects in 50 ms with a 100 ms timeout', function() {812 promise = Deferred.timeout(Deferred.delay(Deferred.rejected(new Error('error message')), 50), 100);813 expect(promise instanceof ExtPromise).toBe(true);814 eventuallyRejectedWith(promise, Error, 'error message');815 });816 });817 describe('returns a new Promise that will reject after the specified timeout if the specified Promise or value has not yet resolved or rejected', function() {818 it('Promise that resolves in 100 ms with a 50 ms timeout', function() {819 promise = Deferred.timeout(Deferred.delay('expected value', 100), 50);820 expect(promise instanceof ExtPromise).toBe(true);821 eventuallyRejectedWith(promise, Error, 'Promise timed out.');822 });823 it('Promise that rejects in 50 ms with a 100 ms timeout', function() {824 promise = Deferred.timeout(Deferred.delay(Deferred.rejected(new Error('error message')), 100), 50);825 expect(promise instanceof ExtPromise).toBe(true);826 eventuallyRejectedWith(promise, Error, 'Promise timed out.');827 });828 });829 }); // timeout830 describe('memoize()', function() {831 function fib (n) {832 return (n < 2) ? n : fib(n - 1) + fib(n - 2);833 }834 function fibonacci (n) {835 ++fibonacci.calls;836 fibonacci.scope = this;837 return fib(n);838 }839 beforeEach(function() {840 fibonacci.calls = 0;841 delete fibonacci.scope;842 });843 describe('returns a new function that wraps the specified function, caching results for previously processed inputs, and returns a Promise that will resolve with the result value', function() {844 it('value', function() {845 var memoFn = Deferred.memoize(fibonacci);846 promise = ExtPromise.all([memoFn(12), memoFn(12)]).then(function(value) {847 expect(fibonacci.calls).toBe(1);848 return value;849 }, function(error) {850 throw error;851 });852 eventuallyResolvesTo(promise, [fib(12), fib(12)], true);853 });854 it('resolved Promise', function() {855 var memoFn = Deferred.memoize(fibonacci);856 promise = ExtPromise.all([memoFn(Deferred.resolved(12)), memoFn(Deferred.resolved(12))]).then(function(value) {857 expect(fibonacci.calls).toBe(1);858 return value;859 }, function(error) {860 throw error;861 });862 eventuallyResolvesTo(promise, [fib(12), fib(12)], true);863 });864 });865 describe('executes the wrapped function in the optionally specified scope', function() {866 it('optional scope omitted', function() {867 var memoFn = Deferred.memoize(fibonacci);868 promise = memoFn(12).then(function(value) {869 expect(fibonacci.calls).toBe(1);870 expect(fibonacci.scope == window).toBe(true); // IE needs == not ===871 return value;872 }, function(error) {873 throw error;874 });875 eventuallyResolvesTo(promise, fib(12), true);876 });877 it('scope specified', function() {878 var memoFn = Deferred.memoize(fibonacci, targetScope);879 promise = memoFn(12).then(function(value) {880 expect(fibonacci.calls).toBe(1);881 expect(fibonacci.scope).toBe(targetScope);882 return value;883 }, function(error) {884 throw error;885 });886 eventuallyResolvesTo(promise, fib(12), true);887 });888 });889 describe('returns a new function that wraps the specified function and returns a Promise that will reject with the associated error when the wrapper function is called with a rejected Promise', function() {890 it('rejected Promise', function() {891 var memoFn = Deferred.memoize(fibonacci);892 promise = memoFn(Deferred.rejected(new Error('error message')));893 eventuallyRejectedWith(promise, Error, 'error message');894 });895 });896 }); // memoize897 describe('map()', function() {898 function doubleFunction(value, index, array) {899 expect(arguments.length).toBe(3);900 expect(array instanceof Array).toBe(true);901 expect(index).toBeGE(0);902 expect(index).toBeLT(array.length);903 return value * 2;904 }905 function doublePromiseFunction(value, index, array) {906 expect(arguments.length).toBe(3);907 expect(array instanceof Array).toBe(true);908 expect(index).toBeGE(0);909 expect(index).toBeLT(array.length);910 return Deferred.resolved(value * 2);911 }912 913 function rejectFunction(value, index, array) {914 expect(arguments.length).toBe(3);915 expect(array instanceof Array).toBe(true);916 expect(index).toBeGE(0);917 expect(index).toBeLT(array.length);918 return Deferred.rejected(new Error('error message'));919 }920 describe('returns a new Promise that will resolve with an Array of the mapped values for the specified Array of Promise(s) or value(s)', function() {921 it('Empty Array', function() {922 promise = Deferred.map([], doubleFunction);923 expect(promise instanceof ExtPromise).toBe(true);924 eventuallyResolvesTo(promise, [], true);925 });926 it('Array with one value', function() {927 promise = Deferred.map([1], doubleFunction);928 expect(promise instanceof ExtPromise).toBe(true);929 eventuallyResolvesTo(promise, [2], true);930 });931 it('Array of values', function() {932 promise = Deferred.map([1, 2, 3], doubleFunction);933 expect(promise instanceof ExtPromise).toBe(true);934 eventuallyResolvesTo(promise, [2, 4, 6], true);935 });936 it('Sparse Array', function() {937 promise = Deferred.map([, 2, , 4, 5], doubleFunction);938 expect(promise instanceof ExtPromise).toBe(true);939 eventuallyResolvesTo(promise, [, 4, , 8, 10], true);940 });941 it('Array with one resolved Promise', function() {942 promise = Deferred.map([Deferred.resolved(1)], doubleFunction);943 expect(promise instanceof ExtPromise).toBe(true);944 eventuallyResolvesTo(promise, [2], true);945 });946 it('Array of resolved Promises', function() {947 promise = Deferred.map([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], doubleFunction);948 expect(promise instanceof ExtPromise).toBe(true);949 eventuallyResolvesTo(promise, [2, 4, 6], true);950 });951 it('Array of values and resolved Promises', function() {952 promise = Deferred.map([1, Deferred.resolved(2), Deferred.resolved(3), 4], doubleFunction);953 expect(promise instanceof ExtPromise).toBe(true);954 eventuallyResolvesTo(promise, [2, 4, 6, 8], true);955 });956 });957 describe('returns a new Promise that will resolve with an Array of the mapped values for the specified resolved Promise of an Array of Promise(s) or value(s)', function() {958 it('Promise of an empty Array', function() {959 promise = Deferred.map(Deferred.resolved([]), doubleFunction);960 expect(promise instanceof ExtPromise).toBe(true);961 eventuallyResolvesTo(promise, [], true);962 });963 it('Promise of an Array with one value', function() {964 promise = Deferred.map(Deferred.resolved([1]), doubleFunction);965 expect(promise instanceof ExtPromise).toBe(true);966 eventuallyResolvesTo(promise, [2], true);967 });968 it('Promise of an Array of values', function() {969 promise = Deferred.map(Deferred.resolved([1, 2, 3]), doubleFunction);970 expect(promise instanceof ExtPromise).toBe(true);971 eventuallyResolvesTo(promise, [2, 4, 6], true);972 });973 it('Promise of a sparse Array', function() {974 promise = Deferred.map(Deferred.resolved([, 2, , 4, 5]), doubleFunction);975 expect(promise instanceof ExtPromise).toBe(true);976 eventuallyResolvesTo(promise, [, 4, , 8, 10], true);977 });978 it('Promise of an Array with one resolved Promise', function() {979 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1)]), doubleFunction);980 expect(promise instanceof ExtPromise).toBe(true);981 eventuallyResolvesTo(promise, [2], true);982 });983 it('Promise of an Array of resolved Promises', function() {984 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), doubleFunction);985 expect(promise instanceof ExtPromise).toBe(true);986 eventuallyResolvesTo(promise, [2, 4, 6], true);987 });988 it('Promise of an Array of values and resolved Promises', function() {989 promise = Deferred.map(Deferred.resolved([1, Deferred.resolved(2), Deferred.resolved(3), 4]), doubleFunction);990 expect(promise instanceof ExtPromise).toBe(true);991 eventuallyResolvesTo(promise, [2, 4, 6, 8], true);992 });993 });994 describe('returns a new Promise that will resolve with an Array of the resolved mapped Promises values for the specified Array of Promise(s) or value(s)', function() {995 it('Empty Array', function() {996 promise = Deferred.map([], doublePromiseFunction);997 expect(promise instanceof ExtPromise).toBe(true);998 eventuallyResolvesTo(promise, [], true);999 });1000 it('Array with one value', function() {1001 promise = Deferred.map([1], doublePromiseFunction);1002 expect(promise instanceof ExtPromise).toBe(true);1003 eventuallyResolvesTo(promise, [2], true);1004 });1005 it('Array of values', function() {1006 promise = Deferred.map([1, 2, 3], doublePromiseFunction);1007 expect(promise instanceof ExtPromise).toBe(true);1008 eventuallyResolvesTo(promise, [2, 4, 6], true);1009 });1010 it('Sparse Array', function() {1011 promise = Deferred.map([, 2, , 4, 5], doublePromiseFunction);1012 expect(promise instanceof ExtPromise).toBe(true);1013 eventuallyResolvesTo(promise, [, 4, , 8, 10], true);1014 });1015 it('Array with one resolved Promise', function() {1016 promise = Deferred.map([Deferred.resolved(1)], doublePromiseFunction);1017 expect(promise instanceof ExtPromise).toBe(true);1018 eventuallyResolvesTo(promise, [2], true);1019 });1020 it('Array of resolved Promises', function() {1021 promise = Deferred.map([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], doublePromiseFunction);1022 expect(promise instanceof ExtPromise).toBe(true);1023 eventuallyResolvesTo(promise, [2, 4, 6], true);1024 });1025 it('Array of values and resolved Promises', function() {1026 promise = Deferred.map([1, Deferred.resolved(2), Deferred.resolved(3), 4], doublePromiseFunction);1027 expect(promise instanceof ExtPromise).toBe(true);1028 eventuallyResolvesTo(promise, [2, 4, 6, 8], true);1029 });1030 });1031 describe('returns a new Promise that will resolve with an Array of the resolved mapped Promises values for the specified resolved Promise of an Array of Promise(s) or value(s)', function() {1032 it('Promise of an empty Array', function() {1033 promise = Deferred.map(Deferred.resolved([]), doublePromiseFunction);1034 expect(promise instanceof ExtPromise).toBe(true);1035 eventuallyResolvesTo(promise, [], true);1036 });1037 it('Promise of an Array with one value', function() {1038 promise = Deferred.map(Deferred.resolved([1]), doublePromiseFunction);1039 expect(promise instanceof ExtPromise).toBe(true);1040 eventuallyResolvesTo(promise, [2], true);1041 });1042 it('Promise of an Array of values', function() {1043 promise = Deferred.map(Deferred.resolved([1, 2, 3]), doublePromiseFunction);1044 expect(promise instanceof ExtPromise).toBe(true);1045 eventuallyResolvesTo(promise, [2, 4, 6], true);1046 });1047 it('Promise of a sparse Array', function() {1048 promise = Deferred.map(Deferred.resolved([, 2, , 4, 5]), doublePromiseFunction);1049 expect(promise instanceof ExtPromise).toBe(true);1050 eventuallyResolvesTo(promise, [, 4, , 8, 10], true);1051 });1052 it('Promise of an Array with one resolved Promise', function() {1053 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1)]), doublePromiseFunction);1054 expect(promise instanceof ExtPromise).toBe(true);1055 eventuallyResolvesTo(promise, [2], true);1056 });1057 it('Promise of an Array of resolved Promises', function() {1058 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), doublePromiseFunction);1059 expect(promise instanceof ExtPromise).toBe(true);1060 eventuallyResolvesTo(promise, [2, 4, 6], true);1061 });1062 it('Promise of an Array of values and resolved Promises', function() {1063 promise = Deferred.map(Deferred.resolved([1, Deferred.resolved(2), Deferred.resolved(3), 4]), doublePromiseFunction);1064 expect(promise instanceof ExtPromise).toBe(true);1065 eventuallyResolvesTo(promise, [2, 4, 6, 8], true);1066 });1067 });1068 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified Array of Promise(s) or value(s) that rejects', function() {1069 it('Array with one rejected Promise', function() {1070 promise = Deferred.map([Deferred.rejected(new Error('error message'))], doubleFunction);1071 expect(promise instanceof ExtPromise).toBe(true);1072 eventuallyRejectedWith(promise, Error, 'error message');1073 });1074 it('Array of values and a rejected Promise', function() {1075 promise = Deferred.map([1, Deferred.rejected(new Error('error message')), 3], doubleFunction);1076 expect(promise instanceof ExtPromise).toBe(true);1077 eventuallyRejectedWith(promise, Error, 'error message');1078 });1079 it('Array of resolved Promises and a rejected Promise', function() {1080 promise = Deferred.map([Deferred.resolved(1), Deferred.rejected(new Error('error message')), Deferred.resolved(3)], doubleFunction);1081 expect(promise instanceof ExtPromise).toBe(true);1082 eventuallyRejectedWith(promise, Error, 'error message');1083 });1084 it('Array of values, pending and resolved Promises and a rejected Promise', function() {1085 promise = Deferred.map([1, 2, Deferred.rejected(new Error('error message')), Deferred.resolved(4), new Deferred().promise], doubleFunction);1086 expect(promise instanceof ExtPromise).toBe(true);1087 eventuallyRejectedWith(promise, Error, 'error message');1088 });1089 });1090 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified resolved Promise of an Array of Promise(s) or value(s) that rejects', function() {1091 it('Promise of an Array with one rejected Promise', function() {1092 promise = Deferred.map(Deferred.resolved([Deferred.rejected(new Error('error message'))]), doubleFunction);1093 expect(promise instanceof ExtPromise).toBe(true);1094 eventuallyRejectedWith(promise, Error, 'error message');1095 });1096 it('Promise of an Array of values and a rejected Promise', function() {1097 promise = Deferred.map(Deferred.resolved([1, Deferred.rejected(new Error('error message')), 3]), doubleFunction);1098 expect(promise instanceof ExtPromise).toBe(true);1099 eventuallyRejectedWith(promise, Error, 'error message');1100 });1101 it('Promise of an Array of resolved Promises and a rejected Promise', function() {1102 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1), Deferred.rejected(new Error('error message')), Deferred.resolved(3)]), doubleFunction);1103 expect(promise instanceof ExtPromise).toBe(true);1104 eventuallyRejectedWith(promise, Error, 'error message');1105 });1106 it('Promise of an Array of values, pending and resolved Promises and a rejected Promise', function() {1107 promise = Deferred.map(Deferred.resolved([1, 2, Deferred.rejected(new Error('error message')), Deferred.resolved(4), new Deferred().promise]), doubleFunction);1108 expect(promise instanceof ExtPromise).toBe(true);1109 eventuallyRejectedWith(promise, Error, 'error message');1110 });1111 });1112 describe('returns a new Promise that will reject with the error associated with the first mapped Promise value in the specified Array of Promise(s) or value(s) that rejects', function() {1113 it('Array with one value', function() {1114 promise = Deferred.map([1], rejectFunction);1115 expect(promise instanceof ExtPromise).toBe(true);1116 eventuallyRejectedWith(promise, Error, 'error message');1117 });1118 it('Array of values', function() {1119 promise = Deferred.map([1, 2, 3], rejectFunction);1120 expect(promise instanceof ExtPromise).toBe(true);1121 eventuallyRejectedWith(promise, Error, 'error message');1122 });1123 it('Sparse Array', function() {1124 promise = Deferred.map([, 2, , 4, 5], rejectFunction);1125 expect(promise instanceof ExtPromise).toBe(true);1126 eventuallyRejectedWith(promise, Error, 'error message');1127 });1128 it('Array with one resolved Promise', function() {1129 promise = Deferred.map([Deferred.resolved(1)], rejectFunction);1130 expect(promise instanceof ExtPromise).toBe(true);1131 eventuallyRejectedWith(promise, Error, 'error message');1132 });1133 it('Array of resolved Promises', function() {1134 promise = Deferred.map([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], rejectFunction);1135 expect(promise instanceof ExtPromise).toBe(true);1136 eventuallyRejectedWith(promise, Error, 'error message');1137 });1138 it('Array of values and resolved Promises', function() {1139 promise = Deferred.map([1, Deferred.resolved(2), Deferred.resolved(3), 4], rejectFunction);1140 expect(promise instanceof ExtPromise).toBe(true);1141 eventuallyRejectedWith(promise, Error, 'error message');1142 });1143 });1144 describe('returns a new Promise that will reject with the error associated with the first mapped Promise value in the specified resolved Promise of an Array of Promise(s) or value(s) that rejects', function() {1145 it('Promise of an Array with one value', function() {1146 promise = Deferred.map(Deferred.resolved([1]), rejectFunction);1147 expect(promise instanceof ExtPromise).toBe(true);1148 eventuallyRejectedWith(promise, Error, 'error message');1149 });1150 it('Promise of an Array of values', function() {1151 promise = Deferred.map(Deferred.resolved([1, 2, 3]), rejectFunction);1152 expect(promise instanceof ExtPromise).toBe(true);1153 eventuallyRejectedWith(promise, Error, 'error message');1154 });1155 it('Promise of a sparse Array', function() {1156 promise = Deferred.map(Deferred.resolved([, 2, , 4, 5]), rejectFunction);1157 expect(promise instanceof ExtPromise).toBe(true);1158 eventuallyRejectedWith(promise, Error, 'error message');1159 });1160 it('Promise of an Array with one resolved Promise', function() {1161 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1)]), rejectFunction);1162 expect(promise instanceof ExtPromise).toBe(true);1163 eventuallyRejectedWith(promise, Error, 'error message');1164 });1165 it('Promise of an Array of resolved Promises', function() {1166 promise = Deferred.map(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), rejectFunction);1167 expect(promise instanceof ExtPromise).toBe(true);1168 eventuallyRejectedWith(promise, Error, 'error message');1169 });1170 it('Promise of an Array of values and resolved Promises', function() {1171 promise = Deferred.map(Deferred.resolved([1, Deferred.resolved(2), Deferred.resolved(3), 4]), rejectFunction);1172 expect(promise instanceof ExtPromise).toBe(true);1173 eventuallyRejectedWith(promise, Error, 'error message');1174 });1175 });1176 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an Array of Promise(s) or value(s)', function() {1177 it('Error: error message', function() {1178 promise = Deferred.map(Deferred.rejected(new Error('error message')), doubleFunction);1179 expect(promise instanceof ExtPromise).toBe(true);1180 eventuallyRejectedWith(promise, Error, 'error message');1181 });1182 });1183 describe('throws an Error if anything other than an Array or Promise of an Array and a function are specified', function() {1184 it('no parameters', function() {1185 expect(function() {1186 return Deferred.map();1187 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1188 });1189 it('a single non-Array parameter', function() {1190 expect(function() {1191 return Deferred.map(1);1192 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1193 });1194 it('multiple non-Array parameters', function() {1195 expect(function() {1196 return Deferred.map(1, 2, 3);1197 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1198 });1199 it('an Array and no function', function() {1200 expect(function() {1201 return Deferred.map([1, 2, 3]);1202 }).toThrow('Invalid parameter: expected a function.');1203 });1204 it('a Promise of an Array and no function', function() {1205 expect(function() {1206 return Deferred.map(Deferred.resolved([1, 2, 3]));1207 }).toThrow('Invalid parameter: expected a function.');1208 });1209 it('an Array and a non-function parameter', function() {1210 expect(function() {1211 return Deferred.map([1, 2, 3], 'not a function');1212 }).toThrow('Invalid parameter: expected a function.');1213 });1214 it('a Promise of a non-function parameter', function() {1215 expect(function() {1216 return Deferred.map(Deferred.resolved([1, 2, 3], 'not a function'));1217 }).toThrow('Invalid parameter: expected a function.');1218 });1219 });1220 }); // map1221 describe('reduce()', function() {1222 function sumFunction(previousValue, currentValue, index, array) {1223 expect(arguments.length).toBe(4);1224 expect(array instanceof Array).toBe(true);1225 expect(index).toBeGE(0);1226 expect(index).toBeLT(array.length);1227 1228 return previousValue + currentValue;1229 }1230 function sumPromiseFunction(previousValue, currentValue, index, array) {1231 expect(arguments.length).toBe(4);1232 expect(array instanceof Array).toBe(true);1233 expect(index).toBeGE(0);1234 expect(index).toBeLT(array.length);1235 1236 return Deferred.resolved(previousValue + currentValue);1237 }1238 function rejectFunction(previousValue, currentValue, index, array) {1239 expect(arguments.length).toBe(4);1240 expect(array instanceof Array).toBe(true);1241 expect(index).toBeGE(0);1242 expect(index).toBeLT(array.length);1243 1244 return Deferred.rejected(new Error('error message'));1245 }1246 describe('returns a Promise that will resolve with the value obtained by reducing the specified Array of Promise(s) or value(s) using the specified function and initial value', function() {1247 it('Empty Array and an initial value', function() {1248 promise = Deferred.reduce([], sumFunction, 0);1249 expect(promise instanceof ExtPromise).toBe(true);1250 eventuallyResolvesTo(promise, 0, true);1251 });1252 it('Empty Array and a resolved Promise of an initial value', function() {1253 promise = Deferred.reduce([], sumFunction, Deferred.resolved(0));1254 expect(promise instanceof ExtPromise).toBe(true);1255 eventuallyResolvesTo(promise, 0, true);1256 });1257 it('Array with one value', function() {1258 promise = Deferred.reduce([1], sumFunction);1259 expect(promise instanceof ExtPromise).toBe(true);1260 eventuallyResolvesTo(promise, 1, true);1261 });1262 it('Array with one value and an initial value', function() {1263 promise = Deferred.reduce([1], sumFunction, 10);1264 expect(promise instanceof ExtPromise).toBe(true);1265 eventuallyResolvesTo(promise, 11, true);1266 });1267 it('Array with one value and a resolved Promise of an initial value', function() {1268 promise = Deferred.reduce([1], sumFunction, Deferred.resolved(10));1269 expect(promise instanceof ExtPromise).toBe(true);1270 eventuallyResolvesTo(promise, 11, true);1271 });1272 it('Array of values', function() {1273 promise = Deferred.reduce([1, 2, 3, 4], sumFunction);1274 expect(promise instanceof ExtPromise).toBe(true);1275 eventuallyResolvesTo(promise, 10, true);1276 });1277 it('Array of values and an initial value', function() {1278 promise = Deferred.reduce([1, 2, 3, 4], sumFunction, 10);1279 expect(promise instanceof ExtPromise).toBe(true);1280 eventuallyResolvesTo(promise, 20, true);1281 });1282 it('Array of values and a resolved Promise of an initial value', function() {1283 promise = Deferred.reduce([1, 2, 3, 4], sumFunction, Deferred.resolved(10));1284 expect(promise instanceof ExtPromise).toBe(true);1285 eventuallyResolvesTo(promise, 20, true);1286 });1287 it('Sparse Array', function() {1288 promise = Deferred.reduce([, 2, , 4, 5], sumFunction);1289 expect(promise instanceof ExtPromise).toBe(true);1290 eventuallyResolvesTo(promise, 11, true);1291 });1292 it('Sparse Array and an initial value', function() {1293 promise = Deferred.reduce([, 2, , 4, 5], sumFunction, 10);1294 expect(promise instanceof ExtPromise).toBe(true);1295 eventuallyResolvesTo(promise, 21, true);1296 });1297 it('Sparse Array and a resolved Promise of an initial value', function() {1298 promise = Deferred.reduce([, 2, , 4, 5], sumFunction, Deferred.resolved(10));1299 expect(promise instanceof ExtPromise).toBe(true);1300 eventuallyResolvesTo(promise, 21, true);1301 });1302 it('Array with one resolved Promise', function() {1303 promise = Deferred.reduce([Deferred.resolved(1)], sumFunction);1304 expect(promise instanceof ExtPromise).toBe(true);1305 eventuallyResolvesTo(promise, 1, true);1306 });1307 it('Array with one resolved Promise and an initial value', function() {1308 promise = Deferred.reduce([Deferred.resolved(1)], sumFunction, 10);1309 expect(promise instanceof ExtPromise).toBe(true);1310 eventuallyResolvesTo(promise, 11, true);1311 });1312 it('Array with one resolved Promise and a resolved Promise of an initial value', function() {1313 promise = Deferred.reduce([Deferred.resolved(1)], sumFunction, Deferred.resolved(10));1314 expect(promise instanceof ExtPromise).toBe(true);1315 eventuallyResolvesTo(promise, 11, true);1316 });1317 it('Array of resolved Promises', function() {1318 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumFunction);1319 expect(promise instanceof ExtPromise).toBe(true);1320 eventuallyResolvesTo(promise, 6, true);1321 });1322 it('Array of resolved Promises and an initial value', function() {1323 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumFunction, 10);1324 expect(promise instanceof ExtPromise).toBe(true);1325 eventuallyResolvesTo(promise, 16, true);1326 });1327 it('Array of resolved Promises and a resolved Promise of an initial value', function() {1328 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumFunction, Deferred.resolved(10));1329 expect(promise instanceof ExtPromise).toBe(true);1330 eventuallyResolvesTo(promise, 16, true);1331 });1332 it('Array of values and resolved Promises', function() {1333 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumFunction);1334 expect(promise instanceof ExtPromise).toBe(true);1335 eventuallyResolvesTo(promise, 10, true);1336 });1337 it('Array of values and resolved Promises and an initial value', function() {1338 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumFunction, 10);1339 expect(promise instanceof ExtPromise).toBe(true);1340 eventuallyResolvesTo(promise, 20, true);1341 });1342 it('Array of values and resolved Promises and a resolved Promise of an initial value', function() {1343 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumFunction, Deferred.resolved(10));1344 expect(promise instanceof ExtPromise).toBe(true);1345 eventuallyResolvesTo(promise, 20, true);1346 });1347 });1348 describe('returns a Promise that will resolve with the value obtained by reducing the specified resolved Promise of an Array of Promise(s) or value(s) using the specified function and initial value', function() {1349 it('Promise of an empty Array and an initial value', function() {1350 promise = Deferred.reduce(Deferred.resolved([]), sumFunction, 0);1351 expect(promise instanceof ExtPromise).toBe(true);1352 eventuallyResolvesTo(promise, 0, true);1353 });1354 it('Promise of an empty Array and a resolved Promise of an initial value', function() {1355 promise = Deferred.reduce(Deferred.resolved([]), sumFunction, Deferred.resolved(0));1356 expect(promise instanceof ExtPromise).toBe(true);1357 eventuallyResolvesTo(promise, 0, true);1358 });1359 it('Promise of an Array with one value', function() {1360 promise = Deferred.reduce(Deferred.resolved([1]), sumFunction);1361 expect(promise instanceof ExtPromise).toBe(true);1362 eventuallyResolvesTo(promise, 1, true);1363 });1364 it('Promise of an Array with one value and an initial value', function() {1365 promise = Deferred.reduce(Deferred.resolved([1]), sumFunction, 10);1366 expect(promise instanceof ExtPromise).toBe(true);1367 eventuallyResolvesTo(promise, 11, true);1368 });1369 it('Promise of an Array with one value and a resolved Promise of an initial value', function() {1370 promise = Deferred.reduce(Deferred.resolved([1]), sumFunction, Deferred.resolved(10));1371 expect(promise instanceof ExtPromise).toBe(true);1372 eventuallyResolvesTo(promise, 11, true);1373 });1374 it('Promise of an Array of values', function() {1375 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumFunction);1376 expect(promise instanceof ExtPromise).toBe(true);1377 eventuallyResolvesTo(promise, 10, true);1378 });1379 it('Promise of an Array of values and an initial value', function() {1380 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumFunction, 10);1381 expect(promise instanceof ExtPromise).toBe(true);1382 eventuallyResolvesTo(promise, 20, true);1383 });1384 it('Promise of an Array of values and a resolved Promise of an initial value', function() {1385 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumFunction, Deferred.resolved(10));1386 expect(promise instanceof ExtPromise).toBe(true);1387 eventuallyResolvesTo(promise, 20, true);1388 });1389 it('Promise of a sparse Array', function() {1390 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumFunction);1391 expect(promise instanceof ExtPromise).toBe(true);1392 eventuallyResolvesTo(promise, 11, true);1393 });1394 it('Promise of a sparse Array and an initial value', function() {1395 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumFunction, 10);1396 expect(promise instanceof ExtPromise).toBe(true);1397 eventuallyResolvesTo(promise, 21, true);1398 });1399 it('Promise of a sparse Array and a resolved Promise of an initial value', function() {1400 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumFunction, Deferred.resolved(10));1401 expect(promise instanceof ExtPromise).toBe(true);1402 eventuallyResolvesTo(promise, 21, true);1403 });1404 it('Promise of an Array with one resolved Promise', function() {1405 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumFunction);1406 expect(promise instanceof ExtPromise).toBe(true);1407 eventuallyResolvesTo(promise, 1, true);1408 });1409 it('Promise of an Array with one resolved Promise and an initial value', function() {1410 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumFunction, 10);1411 expect(promise instanceof ExtPromise).toBe(true);1412 eventuallyResolvesTo(promise, 11, true);1413 });1414 it('Promise of an Array with one resolved Promise and a resolved Promise of an initial value', function() {1415 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumFunction, Deferred.resolved(10));1416 expect(promise instanceof ExtPromise).toBe(true);1417 eventuallyResolvesTo(promise, 11, true);1418 });1419 it('Promise of an Array of resolved Promises', function() {1420 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumFunction);1421 expect(promise instanceof ExtPromise).toBe(true);1422 eventuallyResolvesTo(promise, 6, true);1423 });1424 it('Promise of an Array of resolved Promises and an initial value', function() {1425 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumFunction, 10);1426 expect(promise instanceof ExtPromise).toBe(true);1427 eventuallyResolvesTo(promise, 16, true);1428 });1429 it('Promise of an Array of resolved Promises and a resolved Promise of an initial value', function() {1430 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumFunction, Deferred.resolved(10));1431 expect(promise instanceof ExtPromise).toBe(true);1432 eventuallyResolvesTo(promise, 16, true);1433 });1434 it('Promise of an Array of values and resolved Promises', function() {1435 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumFunction);1436 expect(promise instanceof ExtPromise).toBe(true);1437 eventuallyResolvesTo(promise, 10, true);1438 });1439 it('Promise of an Array of values and resolved Promises and an initial value', function() {1440 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumFunction, 10);1441 expect(promise instanceof ExtPromise).toBe(true);1442 eventuallyResolvesTo(promise, 20, true);1443 });1444 it('Promise of an Array of values and resolved Promises and a resolved Promise of an initial value', function() {1445 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumFunction, Deferred.resolved(10));1446 expect(promise instanceof ExtPromise).toBe(true);1447 eventuallyResolvesTo(promise, 20, true);1448 });1449 });1450 describe('returns a Promise that will resolve with the resolved Promise value obtained by reducing the specified Array of Promise(s) or value(s) using the specified function and initial value', function() {1451 it('Empty Array and an initial value', function() {1452 promise = Deferred.reduce([], sumPromiseFunction, 0);1453 expect(promise instanceof ExtPromise).toBe(true);1454 eventuallyResolvesTo(promise, 0, true);1455 });1456 it('Empty Array and a resolved Promise of an initial value', function() {1457 promise = Deferred.reduce([], sumPromiseFunction, Deferred.resolved(0));1458 expect(promise instanceof ExtPromise).toBe(true);1459 eventuallyResolvesTo(promise, 0, true);1460 });1461 it('Array with one value', function() {1462 promise = Deferred.reduce([1], sumPromiseFunction);1463 expect(promise instanceof ExtPromise).toBe(true);1464 eventuallyResolvesTo(promise, 1, true);1465 });1466 it('Array with one value and an initial value', function() {1467 promise = Deferred.reduce([1], sumPromiseFunction, 10);1468 expect(promise instanceof ExtPromise).toBe(true);1469 eventuallyResolvesTo(promise, 11, true);1470 });1471 it('Array with one value and a resolved Promise of an initial value', function() {1472 promise = Deferred.reduce([1], sumPromiseFunction, Deferred.resolved(10));1473 expect(promise instanceof ExtPromise).toBe(true);1474 eventuallyResolvesTo(promise, 11, true);1475 });1476 it('Array of values', function() {1477 promise = Deferred.reduce([1, 2, 3, 4], sumPromiseFunction);1478 expect(promise instanceof ExtPromise).toBe(true);1479 eventuallyResolvesTo(promise, 10, true);1480 });1481 it('Array of values and an initial value', function() {1482 promise = Deferred.reduce([1, 2, 3, 4], sumPromiseFunction, 10);1483 expect(promise instanceof ExtPromise).toBe(true);1484 eventuallyResolvesTo(promise, 20, true);1485 });1486 it('Array of values and a resolved Promise of an initial value', function() {1487 promise = Deferred.reduce([1, 2, 3, 4], sumPromiseFunction, Deferred.resolved(10));1488 expect(promise instanceof ExtPromise).toBe(true);1489 eventuallyResolvesTo(promise, 20, true);1490 });1491 it('Sparse Array', function() {1492 promise = Deferred.reduce([, 2, , 4, 5], sumPromiseFunction);1493 expect(promise instanceof ExtPromise).toBe(true);1494 eventuallyResolvesTo(promise, 11, true);1495 });1496 it('Sparse Array and an initial value', function() {1497 promise = Deferred.reduce([, 2, , 4, 5], sumPromiseFunction, 10);1498 expect(promise instanceof ExtPromise).toBe(true);1499 eventuallyResolvesTo(promise, 21, true);1500 });1501 it('Sparse Array and a resolved Promise of an initial value', function() {1502 promise = Deferred.reduce([, 2, , 4, 5], sumPromiseFunction, Deferred.resolved(10));1503 expect(promise instanceof ExtPromise).toBe(true);1504 eventuallyResolvesTo(promise, 21, true);1505 });1506 it('Array with one resolved Promise', function() {1507 promise = Deferred.reduce([Deferred.resolved(1)], sumPromiseFunction);1508 expect(promise instanceof ExtPromise).toBe(true);1509 eventuallyResolvesTo(promise, 1, true);1510 });1511 it('Array with one resolved Promise and an initial value', function() {1512 promise = Deferred.reduce([Deferred.resolved(1)], sumPromiseFunction, 10);1513 expect(promise instanceof ExtPromise).toBe(true);1514 eventuallyResolvesTo(promise, 11, true);1515 });1516 it('Array with one resolved Promise and a resolved Promise of an initial value', function() {1517 promise = Deferred.reduce([Deferred.resolved(1)], sumPromiseFunction, Deferred.resolved(10));1518 expect(promise instanceof ExtPromise).toBe(true);1519 eventuallyResolvesTo(promise, 11, true);1520 });1521 it('Array of resolved Promises', function() {1522 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumPromiseFunction);1523 expect(promise instanceof ExtPromise).toBe(true);1524 eventuallyResolvesTo(promise, 6, true);1525 });1526 it('Array of resolved Promises and an initial value', function() {1527 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumPromiseFunction, 10);1528 expect(promise instanceof ExtPromise).toBe(true);1529 eventuallyResolvesTo(promise, 16, true);1530 });1531 it('Array of resolved Promises and a resolved Promise of an initial value', function() {1532 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], sumPromiseFunction, Deferred.resolved(10));1533 expect(promise instanceof ExtPromise).toBe(true);1534 eventuallyResolvesTo(promise, 16, true);1535 });1536 it('Array of values and resolved Promises', function() {1537 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumPromiseFunction);1538 expect(promise instanceof ExtPromise).toBe(true);1539 eventuallyResolvesTo(promise, 10, true);1540 });1541 it('Array of values and resolved Promises and an initial value', function() {1542 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumPromiseFunction, 10);1543 expect(promise instanceof ExtPromise).toBe(true);1544 eventuallyResolvesTo(promise, 20, true);1545 });1546 it('Array of values and resolved Promises and a resolved Promise of an initial value', function() {1547 promise = Deferred.reduce([1, Deferred.resolved(2), 3, Deferred.resolved(4)], sumPromiseFunction, Deferred.resolved(10));1548 expect(promise instanceof ExtPromise).toBe(true);1549 eventuallyResolvesTo(promise, 20, true);1550 });1551 });1552 describe('returns a Promise that will resolve with the resolved Promise value obtained by reducing the specified resolved Promise of an Array of Promise(s) or value(s) using the specified function and initial value', function() {1553 it('Promise of an empty Array and an initial value', function() {1554 promise = Deferred.reduce(Deferred.resolved([]), sumPromiseFunction, 0);1555 expect(promise instanceof ExtPromise).toBe(true);1556 eventuallyResolvesTo(promise, 0, true);1557 });1558 it('Promise of an empty Array and a resolved Promise of an initial value', function() {1559 promise = Deferred.reduce(Deferred.resolved([]), sumPromiseFunction, Deferred.resolved(0));1560 expect(promise instanceof ExtPromise).toBe(true);1561 eventuallyResolvesTo(promise, 0, true);1562 });1563 it('Promise of an Array with one value', function() {1564 promise = Deferred.reduce(Deferred.resolved([1]), sumPromiseFunction);1565 expect(promise instanceof ExtPromise).toBe(true);1566 eventuallyResolvesTo(promise, 1, true);1567 });1568 it('Promise of an Array with one value and an initial value', function() {1569 promise = Deferred.reduce(Deferred.resolved([1]), sumPromiseFunction, 10);1570 expect(promise instanceof ExtPromise).toBe(true);1571 eventuallyResolvesTo(promise, 11, true);1572 });1573 it('Promise of an Array with one value and a resolved Promise of an initial value', function() {1574 promise = Deferred.reduce(Deferred.resolved([1]), sumPromiseFunction, Deferred.resolved(10));1575 expect(promise instanceof ExtPromise).toBe(true);1576 eventuallyResolvesTo(promise, 11, true);1577 });1578 it('Promise of an Array of values', function() {1579 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumPromiseFunction);1580 expect(promise instanceof ExtPromise).toBe(true);1581 eventuallyResolvesTo(promise, 10, true);1582 });1583 it('Promise of an Array of values and an initial value', function() {1584 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumPromiseFunction, 10);1585 expect(promise instanceof ExtPromise).toBe(true);1586 eventuallyResolvesTo(promise, 20, true);1587 });1588 it('Promise of an Array of values and a resolved Promise of an initial value', function() {1589 promise = Deferred.reduce(Deferred.resolved([1, 2, 3, 4]), sumPromiseFunction, Deferred.resolved(10));1590 expect(promise instanceof ExtPromise).toBe(true);1591 eventuallyResolvesTo(promise, 20, true);1592 });1593 it('Promise of a sparse Array', function() {1594 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumPromiseFunction);1595 expect(promise instanceof ExtPromise).toBe(true);1596 eventuallyResolvesTo(promise, 11, true);1597 });1598 it('Promise of a sparse Array and an initial value', function() {1599 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumPromiseFunction, 10);1600 expect(promise instanceof ExtPromise).toBe(true);1601 eventuallyResolvesTo(promise, 21, true);1602 });1603 it('Promise of a sparse Array and a resolved Promise of an initial value', function() {1604 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), sumPromiseFunction, Deferred.resolved(10));1605 expect(promise instanceof ExtPromise).toBe(true);1606 eventuallyResolvesTo(promise, 21, true);1607 });1608 it('Promise of an Array with one resolved Promise', function() {1609 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumPromiseFunction);1610 expect(promise instanceof ExtPromise).toBe(true);1611 eventuallyResolvesTo(promise, 1, true);1612 });1613 it('Promise of an Array with one resolved Promise and an initial value', function() {1614 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumPromiseFunction, 10);1615 expect(promise instanceof ExtPromise).toBe(true);1616 eventuallyResolvesTo(promise, 11, true);1617 });1618 it('Promise of an Array with one resolved Promise and a resolved Promise of an initial value', function() {1619 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), sumPromiseFunction, Deferred.resolved(10));1620 expect(promise instanceof ExtPromise).toBe(true);1621 eventuallyResolvesTo(promise, 11, true);1622 });1623 it('Promise of an Array of resolved Promises', function() {1624 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumPromiseFunction);1625 expect(promise instanceof ExtPromise).toBe(true);1626 eventuallyResolvesTo(promise, 6, true);1627 });1628 it('Promise of an Array of resolved Promises and an initial value', function() {1629 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumPromiseFunction, 10);1630 expect(promise instanceof ExtPromise).toBe(true);1631 eventuallyResolvesTo(promise, 16, true);1632 });1633 it('Promise of an Array of resolved Promises and a resolved Promise of an initial value', function() {1634 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), sumPromiseFunction, Deferred.resolved(10));1635 expect(promise instanceof ExtPromise).toBe(true);1636 eventuallyResolvesTo(promise, 16, true);1637 });1638 it('Promise of an Array of values and resolved Promises', function() {1639 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumPromiseFunction);1640 expect(promise instanceof ExtPromise).toBe(true);1641 eventuallyResolvesTo(promise, 10, true);1642 });1643 it('Promise of an Array of values and resolved Promises and an initial value', function() {1644 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumPromiseFunction, 10);1645 expect(promise instanceof ExtPromise).toBe(true);1646 eventuallyResolvesTo(promise, 20, true);1647 });1648 it('Promise of an Array of values and resolved Promises and a resolved Promise of an initial value', function() {1649 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), 3, Deferred.resolved(4)]), sumPromiseFunction, Deferred.resolved(10));1650 expect(promise instanceof ExtPromise).toBe(true);1651 eventuallyResolvesTo(promise, 20, true);1652 });1653 });1654 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified Array of Promise(s) or value(s) that rejects', function() {1655 it('Array with one rejected Promise', function() {1656 promise = Deferred.reduce([Deferred.rejected(new Error('error message'))], sumFunction);1657 expect(promise instanceof ExtPromise).toBe(true);1658 eventuallyRejectedWith(promise, Error, 'error message');1659 });1660 it('Array of values and a rejected Promise', function() {1661 promise = Deferred.reduce([1, Deferred.rejected(new Error('error message')), 3], sumFunction);1662 expect(promise instanceof ExtPromise).toBe(true);1663 eventuallyRejectedWith(promise, Error, 'error message');1664 });1665 it('Array of resolved Promises and a rejected Promise', function() {1666 promise = Deferred.reduce([Deferred.resolved(1), Deferred.rejected(new Error('error message')), Deferred.resolved(3)], sumFunction);1667 expect(promise instanceof ExtPromise).toBe(true);1668 eventuallyRejectedWith(promise, Error, 'error message');1669 });1670 it('Array of values, pending and resolved Promises and a rejected Promise', function() {1671 promise = Deferred.reduce([1, 2, Deferred.rejected(new Error('error message')), Deferred.resolved(4), new Deferred().promise], sumFunction);1672 expect(promise instanceof ExtPromise).toBe(true);1673 eventuallyRejectedWith(promise, Error, 'error message');1674 });1675 });1676 describe('returns a new Promise that will reject with the error associated with the first Promise in the specified resolved Promise of an Array of Promise(s) or value(s) that rejects', function() {1677 it('Promise of an Array with one rejected Promise', function() {1678 promise = Deferred.reduce(Deferred.resolved([Deferred.rejected(new Error('error message'))]), sumFunction);1679 expect(promise instanceof ExtPromise).toBe(true);1680 eventuallyRejectedWith(promise, Error, 'error message');1681 });1682 it('Promise of an Array of values and a rejected Promise', function() {1683 promise = Deferred.reduce(Deferred.resolved([1, Deferred.rejected(new Error('error message')), 3]), sumFunction);1684 expect(promise instanceof ExtPromise).toBe(true);1685 eventuallyRejectedWith(promise, Error, 'error message');1686 });1687 it('Promise of an Array of resolved Promises and a rejected Promise', function() {1688 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.rejected(new Error('error message')), Deferred.resolved(3)]), sumFunction);1689 expect(promise instanceof ExtPromise).toBe(true);1690 eventuallyRejectedWith(promise, Error, 'error message');1691 });1692 it('Promise of an Array of values, pending and resolved Promises and a rejected Promise', function() {1693 promise = Deferred.reduce(Deferred.resolved([1, 2, Deferred.rejected(new Error('error message')), Deferred.resolved(4), new Deferred().promise]), sumFunction);1694 expect(promise instanceof ExtPromise).toBe(true);1695 eventuallyRejectedWith(promise, Error, 'error message');1696 });1697 });1698 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an Array of Promise(s) or value(s)', function() {1699 it('Error: error message', function() {1700 promise = Deferred.reduce(Deferred.rejected(new Error('error message')), sumFunction);1701 expect(promise instanceof ExtPromise).toBe(true);1702 eventuallyRejectedWith(promise, Error, 'error message');1703 });1704 });1705 describe('returns a new Promise that will reject with the error associated with the first rejected Promise returned by the specified function for the the specified Array of Promise(s) or value(s)', function() {1706 it('Array with one value', function() {1707 promise = Deferred.reduce([1], rejectFunction, 10);1708 expect(promise instanceof ExtPromise).toBe(true);1709 eventuallyRejectedWith(promise, Error, 'error message');1710 });1711 it('Array of values', function() {1712 promise = Deferred.reduce([1, 2, 3], rejectFunction, 10);1713 expect(promise instanceof ExtPromise).toBe(true);1714 eventuallyRejectedWith(promise, Error, 'error message');1715 });1716 it('Sparse Array', function() {1717 promise = Deferred.reduce([, 2, , 4, 5], rejectFunction, 10);1718 expect(promise instanceof ExtPromise).toBe(true);1719 eventuallyRejectedWith(promise, Error, 'error message');1720 });1721 it('Array with one resolved Promise', function() {1722 promise = Deferred.reduce([Deferred.resolved(1)], rejectFunction, 10);1723 expect(promise instanceof ExtPromise).toBe(true);1724 eventuallyRejectedWith(promise, Error, 'error message');1725 });1726 it('Array of resolved Promises', function() {1727 promise = Deferred.reduce([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)], rejectFunction, 10);1728 expect(promise instanceof ExtPromise).toBe(true);1729 eventuallyRejectedWith(promise, Error, 'error message');1730 });1731 it('Array of values and resolved Promises', function() {1732 promise = Deferred.reduce([1, Deferred.resolved(2), Deferred.resolved(3), 4], rejectFunction, 10);1733 expect(promise instanceof ExtPromise).toBe(true);1734 eventuallyRejectedWith(promise, Error, 'error message');1735 });1736 });1737 describe('returns a new Promise that will reject with the error associated with the first rejected Promise returned by the specified function for the the specified resolved Promise of an Array of Promise(s) or value(s)', function() {1738 it('Promise of an Array with one value', function() {1739 promise = Deferred.reduce(Deferred.resolved([1]), rejectFunction, 10);1740 expect(promise instanceof ExtPromise).toBe(true);1741 eventuallyRejectedWith(promise, Error, 'error message');1742 });1743 it('Promise of an Array of values', function() {1744 promise = Deferred.reduce(Deferred.resolved([1, 2, 3]), rejectFunction, 10);1745 expect(promise instanceof ExtPromise).toBe(true);1746 eventuallyRejectedWith(promise, Error, 'error message');1747 });1748 it('Promise of a sparse Array', function() {1749 promise = Deferred.reduce(Deferred.resolved([, 2, , 4, 5]), rejectFunction, 10);1750 expect(promise instanceof ExtPromise).toBe(true);1751 eventuallyRejectedWith(promise, Error, 'error message');1752 });1753 it('Promise of an Array with one resolved Promise', function() {1754 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1)]), rejectFunction, 10);1755 expect(promise instanceof ExtPromise).toBe(true);1756 eventuallyRejectedWith(promise, Error, 'error message');1757 });1758 it('Promise of an Array of resolved Promises', function() {1759 promise = Deferred.reduce(Deferred.resolved([Deferred.resolved(1), Deferred.resolved(2), Deferred.resolved(3)]), rejectFunction, 10);1760 expect(promise instanceof ExtPromise).toBe(true);1761 eventuallyRejectedWith(promise, Error, 'error message');1762 });1763 it('Promise of an Array of values and resolved Promises', function() {1764 promise = Deferred.reduce(Deferred.resolved([1, Deferred.resolved(2), Deferred.resolved(3), 4]), rejectFunction, 10);1765 expect(promise instanceof ExtPromise).toBe(true);1766 eventuallyRejectedWith(promise, Error, 'error message');1767 });1768 });1769 describe('returns a new Promise that will reject with the error associated with the rejected Promise of an initial value', function() {1770 it('Error: error message', function() {1771 promise = Deferred.reduce([1, 2, 3], sumFunction, Deferred.rejected(new Error('error message')));1772 expect(promise instanceof ExtPromise).toBe(true);1773 eventuallyRejectedWith(promise, Error, 'error message');1774 });1775 });1776 describe('returns a new Promise that will reject if reduce is attempted on an empty Array with no initial value specified', function() {1777 it('Empty Array', function() {1778 promise = Deferred.reduce([], sumFunction);1779 expect(promise instanceof ExtPromise).toBe(true);1780 eventuallyRejectedWith(promise, TypeError);1781 });1782 it('Promise of an empty Array', function() {1783 promise = Deferred.reduce(Deferred.resolved([]), sumFunction);1784 expect(promise instanceof ExtPromise).toBe(true);1785 eventuallyRejectedWith(promise, TypeError);1786 });1787 });1788 describe('throws an Error if anything other than an Array or Promise of an Array and a function are specified as the first two parameters', function() {1789 it('no parameters', function() {1790 expect(function() {1791 return Deferred.reduce();1792 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1793 });1794 it('a single non-Array parameter', function() {1795 expect(function() {1796 return Deferred.reduce(1);1797 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1798 });1799 it('multiple non-Array parameters', function() {1800 expect(function() {1801 return Deferred.reduce(1, 2, 3);1802 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');1803 });1804 it('an Array and no function', function() {1805 expect(function() {1806 return Deferred.reduce([1, 2, 3]);1807 }).toThrow('Invalid parameter: expected a function.');1808 });1809 it('a Promise of an Array and no function', function() {1810 expect(function() {1811 return Deferred.reduce(Deferred.resolved([1, 2, 3]));1812 }).toThrow('Invalid parameter: expected a function.');1813 });1814 it('an Array and a non-function parameter', function() {1815 expect(function() {1816 return Deferred.reduce([1, 2, 3], 'not a function');1817 }).toThrow('Invalid parameter: expected a function.');1818 });1819 it('a Promise of a non-function parameter', function() {1820 expect(function() {1821 return Deferred.reduce(Deferred.resolved([1, 2, 3], 'not a function'));1822 }).toThrow('Invalid parameter: expected a function.');1823 });1824 });1825 }); // reduce1826 describe('then()', function() {1827 describe('with a progress handler', function() {1828 var progressHandler;1829 describe('attaches a progress handler that will be called on progress updates', function() {1830 it('called with progress update when updated', function(done) {1831 progressHandler = jasmine.createSpy();1832 deferred = new Deferred();1833 promise = deferred.promise;1834 promise.then(null, null, progressHandler);1835 Ext.asap(function() {1836 deferred.update('progress');1837 expect(progressHandler.callCount).toBe(1);1838 expect(progressHandler.calls[0].args).toEqual(['progress']);1839 done();1840 });1841 });1842 it('called with progress update in specified scope when updated', function(done) {1843 progressHandler = jasmine.createSpy();1844 deferred = new Deferred();1845 promise = deferred.promise;1846 promise.then(null, null, progressHandler, targetScope);1847 Ext.asap(function() {1848 deferred.update('progress');1849 expect(progressHandler.callCount).toBe(1);1850 expect(progressHandler.calls[0].args).toEqual(['progress']);1851 expect(progressHandler.calls[0].scope).toBe(targetScope);1852 done();1853 });1854 });1855 });1856 describe('propagates transformed progress updates that originate from this Promise', function() {1857 it('propagates progress updates to subsequent Promises in the chain if a progress handler is omitted', function(done) {1858 progressHandler = jasmine.createSpy();1859 deferred = new Deferred();1860 promise = deferred.promise;1861 promise.then().then(null, null, progressHandler);1862 Ext.asap(function() {1863 deferred.update('progress');1864 expect(progressHandler.callCount).toBe(1);1865 expect(progressHandler.calls[0].args).toEqual(['progress']);1866 done();1867 });1868 });1869 it('propagates transformed progress updates to subsequent Promises in the chain if a progress handler transforms the progress update', function(done) {1870 //var deferred, progressHandler, promise, transformedProgressHandler, transformedTransformedProgressHandler;1871 //progressHandler = sinon.stub().returns('transformed progress');1872 progressHandler = jasmine.createSpy();1873 progressHandler.andReturn('transformed progress');1874 var transformedProgressHandler = jasmine.createSpy();1875 transformedProgressHandler.andReturn('transformed transformed progress');1876 var transformedTransformedProgressHandler = jasmine.createSpy();1877 deferred = new Deferred();1878 promise = deferred.promise;1879 promise.then(null, null, progressHandler).1880 then(null, null, transformedProgressHandler).1881 then(null, null, transformedTransformedProgressHandler);1882 Ext.asap(function() {1883 deferred.update('progress');1884 expect(progressHandler.callCount).toBe(1);1885 expect(progressHandler.calls[0].args).toEqual(['progress']);1886 expect(transformedProgressHandler.callCount).toBe(1);1887 expect(transformedProgressHandler.calls[0].args).toEqual(['transformed progress']);1888 expect(transformedTransformedProgressHandler.callCount).toBe(1);1889 expect(transformedTransformedProgressHandler.calls[0].args).1890 toEqual(['transformed transformed progress']);1891 done();1892 });1893 });1894 });1895 });1896 describe('with parameters specified via a configuration object', function() {1897 describe('attaches an onResolved callback to this Promise that will be called when it resolves', function() {1898 describe('when only a success handler is specified', function() {1899 it('called with resolved value when resolved', function(done) {1900 var onResolved = jasmine.createSpy();1901 promise = Deferred.resolved('resolved value');1902 promise.then({1903 success: onResolved1904 });1905 Ext.asap(function() {1906 expect(onResolved.callCount).toBe(1);1907 expect(onResolved.calls[0].args).toEqual(['resolved value']);1908 done();1909 });1910 });1911 it('called with resolved value in the specified scope when resolved', function(done) {1912 var onResolved = jasmine.createSpy();1913 promise = Deferred.resolved('resolved value');1914 promise.then({1915 success: onResolved,1916 scope: targetScope1917 });1918 Ext.asap(function() {1919 expect(onResolved.callCount).toBe(1);1920 expect(onResolved.calls[0].args).toEqual(['resolved value']);1921 expect(onResolved.calls[0].scope).toBe(targetScope);1922 done();1923 });1924 });1925 });1926 describe('when success, failure and progress handlers are specified', function() {1927 it('called with resolved value when resolved', function(done) {1928 var onResolved = jasmine.createSpy();1929 var onRejected = jasmine.createSpy();1930 var onProgress = jasmine.createSpy();1931 promise = Deferred.resolved('resolved value');1932 promise.then({1933 success: onResolved,1934 failure: onRejected,1935 progress: onProgress1936 });1937 Ext.asap(function() {1938 expect(onResolved.callCount).toBe(1);1939 expect(onResolved.calls[0].args).toEqual(['resolved value']);1940 expect(onRejected.callCount).toBe(0);1941 expect(onProgress.callCount).toBe(0);1942 done();1943 });1944 });1945 it('called with resolved value in the specified scope when resolved', function(done) {1946 var onResolved = jasmine.createSpy();1947 var onRejected = jasmine.createSpy();1948 var onProgress = jasmine.createSpy();1949 promise = Deferred.resolved('resolved value');1950 promise.then({1951 success: onResolved,1952 failure: onRejected,1953 progress: onProgress,1954 scope: targetScope1955 });1956 Ext.asap(function() {1957 expect(onResolved.callCount).toBe(1);1958 expect(onResolved.calls[0].args).toEqual(['resolved value']);1959 expect(onResolved.calls[0].scope).toBe(targetScope);1960 expect(onRejected.callCount).toBe(0);1961 expect(onProgress.callCount).toBe(0);1962 done();1963 });1964 });1965 });1966 });1967 describe('attaches an onRejected callback to this Promise that will be called when it rejects', function() {1968 describe('when only a failure handler is specified', function() {1969 it('called with rejection reason when rejected', function(done) {1970 var onRejected = jasmine.createSpy();1971 promise = Deferred.rejected('rejection reason');1972 promise.then({1973 failure: onRejected1974 });1975 Ext.asap(function() {1976 expect(onRejected.callCount).toBe(1);1977 expect(onRejected.calls[0].args).toEqual(['rejection reason']);1978 done();1979 });1980 });1981 it('called with rejection reason in specified scope when rejected', function(done) {1982 var onRejected = jasmine.createSpy();1983 promise = Deferred.rejected('rejection reason');1984 promise.then({1985 failure: onRejected,1986 scope: targetScope1987 });1988 Ext.asap(function() {1989 expect(onRejected.callCount).toBe(1);1990 expect(onRejected.calls[0].args).toEqual(['rejection reason']);1991 expect(onRejected.calls[0].scope).toBe(targetScope);1992 done();1993 });1994 });1995 });1996 describe('when success, failure and progress handlers are specified', function() {1997 it('called with rejection reason when rejected', function(done) {1998 var onResolved = jasmine.createSpy();1999 var onRejected = jasmine.createSpy();2000 var onProgress = jasmine.createSpy();2001 promise = Deferred.rejected('rejection reason');2002 promise.then({2003 success: onResolved,2004 failure: onRejected,2005 progress: onProgress2006 });2007 Ext.asap(function() {2008 expect(onResolved.callCount).toBe(0);2009 expect(onRejected.callCount).toBe(1);2010 expect(onRejected.calls[0].args).toEqual(['rejection reason']);2011 expect(onProgress.callCount).toBe(0);2012 done();2013 });2014 });2015 it('called with rejection reason in specified scope when rejected', function(done) {2016 var onProgress, onRejected, onResolved;2017 onResolved = jasmine.createSpy();2018 onRejected = jasmine.createSpy();2019 onProgress = jasmine.createSpy();2020 promise = Deferred.rejected('rejection reason');2021 promise.then({2022 success: onResolved,2023 failure: onRejected,2024 progress: onProgress,2025 scope: targetScope2026 });2027 Ext.asap(function() {2028 expect(onResolved.callCount).toBe(0);2029 expect(onRejected.callCount).toBe(1);2030 expect(onRejected.calls[0].args).toEqual(['rejection reason']);2031 expect(onRejected.calls[0].scope).toBe(targetScope);2032 expect(onProgress.callCount).toBe(0);2033 done();2034 });2035 });2036 });2037 });2038 describe('attaches an onProgress callback to this Promise that will be called when it resolves', function() {2039 describe('when only a progress handler is specified', function() {2040 it('called with progress update when updated', function(done) {2041 var onProgress = jasmine.createSpy();2042 deferred = new Deferred();2043 promise = deferred.promise;2044 promise.then({2045 progress: onProgress2046 });2047 Ext.asap(function() {2048 deferred.update('progress');2049 expect(onProgress.callCount).toBe(1);2050 expect(onProgress.calls[0].args).toEqual(['progress']);2051 done();2052 });2053 });2054 it('called with progress update in specified scope when updated', function(done) {2055 var onProgress = jasmine.createSpy();2056 deferred = new Deferred();2057 promise = deferred.promise;2058 promise.then({2059 progress: onProgress,2060 scope: targetScope2061 });2062 Ext.asap(function() {2063 deferred.update('progress');2064 expect(onProgress.callCount).toBe(1);2065 expect(onProgress.calls[0].args).toEqual(['progress']);2066 expect(onProgress.calls[0].scope).toBe(targetScope);2067 done();2068 });2069 });2070 });2071 describe('when success, failure and progress handlers are specified', function() {2072 it('called with progress update when updated', function(done) {2073 var onResolved = jasmine.createSpy();2074 var onRejected = jasmine.createSpy();2075 var onProgress = jasmine.createSpy();2076 deferred = new Deferred();2077 promise = deferred.promise;2078 promise.then({2079 success: onResolved,2080 failure: onRejected,2081 progress: onProgress2082 });2083 Ext.asap(function() {2084 deferred.update('progress');2085 expect(onProgress.callCount).toBe(1);2086 expect(onProgress.calls[0].args).toEqual(['progress']);2087 expect(onResolved.callCount).toBe(0);2088 expect(onRejected.callCount).toBe(0);2089 done();2090 });2091 });2092 it('called with progress update in specified scope when updated', function(done) {2093 var onResolved = jasmine.createSpy();2094 var onRejected = jasmine.createSpy();2095 var onProgress = jasmine.createSpy();2096 deferred = new Deferred();2097 promise = deferred.promise;2098 promise.then({2099 success: onResolved,2100 failure: onRejected,2101 progress: onProgress,2102 scope: targetScope2103 });2104 Ext.asap(function() {2105 deferred.update('progress');2106 expect(onResolved.callCount).toBe(0);2107 expect(onRejected.callCount).toBe(0);2108 expect(onProgress.callCount).toBe(1);2109 expect(onProgress.calls[0].args).toEqual(['progress']);2110 expect(onProgress.calls[0].scope).toBe(targetScope);2111 done();2112 });2113 });2114 });2115 });2116 });2117 }); // then2118 describe('otherwise()', function() {2119 describe('attaches a callback that will be called if this Promise is rejected', function() {2120 describe('with parameters specified via function arguments', function() {2121 it('called if rejected', function(done) {2122 var me = this;2123 var onRejected = jasmine.createSpy();2124 var error = new Error('error message');2125 promise = Deferred.rejected(error);2126 promise.otherwise(onRejected);2127 promise.then(null, function() {2128 try {2129 expect(onRejected.callCount).toBe(1);2130 expect(onRejected.calls[0].args.length).toBe(1);2131 expect(onRejected.calls[0].args[0]).toBe(error);2132 done();2133 } catch (e) {2134 me.fail(e);2135 done();2136 }2137 });2138 });2139 it('called in specified scope if rejected', function(done) {2140 var me = this;2141 var onRejected = jasmine.createSpy();2142 var error = new Error('error message');2143 promise = Deferred.rejected(error);2144 promise.otherwise(onRejected, targetScope);2145 promise.then(null, function() {2146 try {2147 expect(onRejected.callCount).toBe(1);2148 expect(onRejected.calls[0].args.length).toBe(1);2149 expect(onRejected.calls[0].args[0]).toBe(error);2150 expect(onRejected.calls[0].scope).toBe(targetScope);2151 done();2152 } catch (e) {2153 me.fail(e);2154 done();2155 }2156 });2157 });2158 it('not called if resolved', function(done) {2159 var me = this;2160 var onRejected = jasmine.createSpy();2161 promise = Deferred.resolved('value');2162 promise.otherwise(onRejected);2163 promise.then(function() {2164 try {2165 expect(onRejected.callCount).toBe(0);2166 done();2167 } catch (e) {2168 me.fail(e);2169 done();2170 }2171 });2172 });2173 });2174 describe('with parameters specified via a configuration object', function() {2175 it('called if rejected', function(done) {2176 var me = this;2177 var onRejected = jasmine.createSpy();2178 var error = new Error('error message');2179 promise = Deferred.rejected(error);2180 promise.otherwise({2181 fn: onRejected2182 });2183 promise.then(null, function() {2184 try {2185 expect(onRejected.callCount).toBe(1);2186 expect(onRejected.calls[0].args.length).toBe(1);2187 expect(onRejected.calls[0].args[0]).toBe(error);2188 done();2189 } catch (e) {2190 me.fail(e);2191 done();2192 }2193 });2194 });2195 it('called in specified scope if rejected', function(done) {2196 var me = this;2197 var onRejected = jasmine.createSpy();2198 var error = new Error('error message');2199 promise = Deferred.rejected(error);2200 promise.otherwise({2201 fn: onRejected,2202 scope: targetScope2203 });2204 promise.then(null, function() {2205 try {2206 expect(onRejected.callCount).toBe(1);2207 expect(onRejected.calls[0].args.length).toBe(1);2208 expect(onRejected.calls[0].args[0]).toBe(error);2209 expect(onRejected.calls[0].scope).toBe(targetScope);2210 done();2211 } catch (e) {2212 me.fail(e);2213 done();2214 }2215 });2216 });2217 it('not called if resolved', function(done) {2218 var me = this;2219 var onRejected = jasmine.createSpy();2220 promise = Deferred.resolved('value');2221 promise.otherwise({2222 fn: onRejected2223 });2224 promise.then(function() {2225 try {2226 expect(onRejected.callCount).toBe(0);2227 done();2228 } catch (e) {2229 me.fail(e);2230 done();2231 }2232 });2233 });2234 });2235 });2236 describe('returns a Promise of the transformed future value', function() {2237 it('resolves with the returned value if callback returns a value', function() {2238 var onRejected = function() {2239 return 'returned value';2240 };2241 promise = Deferred.rejected(new Error('error message')).otherwise(onRejected);2242 expect(promise instanceof ExtPromise).toBe(true);2243 eventuallyResolvesTo(promise, 'returned value', true);2244 });2245 it('resolves with the resolved value if callback returns a Promise that resolves with value', function() {2246 var onRejected = function() {2247 return Deferred.resolved('resolved value');2248 };2249 promise = Deferred.rejected(new Error('error message')).otherwise(onRejected);2250 expect(promise instanceof ExtPromise).toBe(true);2251 eventuallyResolvesTo(promise, 'resolved value', true);2252 });2253 it('rejects with the thrown Error if callback throws an Error', function() {2254 var onRejected = function() {2255 throw new Error('thrown error message');2256 };2257 promise = Deferred.rejected(new Error('error message')).otherwise(onRejected);2258 expect(promise instanceof ExtPromise).toBe(true);2259 eventuallyRejectedWith(promise, Error, 'thrown error message');2260 });2261 it('rejects with the rejection reason if callback returns a Promise that rejects with a reason', function() {2262 var onRejected = function() {2263 return Deferred.rejected(new Error('rejection reason'));2264 };2265 promise = Deferred.rejected(new Error('original error message')).otherwise(onRejected);2266 expect(promise instanceof ExtPromise).toBe(true);2267 eventuallyRejectedWith(promise, Error, 'rejection reason');2268 });2269 });2270 }); // otherwise2271 describe('always()', function() {2272 describe('attaches a callback to this Promise that will be called when it resolves or rejects', function() {2273 describe('with parameters specified via function arguments', function() {2274 it('called with no parameters when resolved', function(done) {2275 var me = this;2276 var onComplete = jasmine.createSpy();2277 promise = Deferred.resolved('value');2278 promise.always(onComplete);2279 promise.then(function() {2280 try {2281 expect(onComplete.callCount).toBeGT(0);2282 done();2283 } catch (e) {2284 me.fail(e);2285 done();2286 }2287 });2288 });2289 it('called with no parameters in the specified scope when resolved', function(done) {2290 var me = this;2291 var onComplete = jasmine.createSpy();2292 promise = Deferred.resolved('value');2293 promise.always(onComplete, targetScope);2294 promise.then(function() {2295 try {2296 expect(onComplete.callCount).toBeGT(0);2297 done();2298 } catch (e) {2299 me.fail(e);2300 done();2301 }2302 });2303 });2304 it('called with no parameters when rejected', function(done) {2305 var me = this;2306 var onComplete = jasmine.createSpy();2307 promise = Deferred.rejected(new Error('error message'));2308 promise.always(onComplete);2309 promise.then(null, function() {2310 try {2311 expect(onComplete.callCount).toBeGT(0);2312 done();2313 } catch (e) {2314 me.fail(e);2315 done();2316 }2317 });2318 });2319 it('called with no parameters in the specified scope when rejected', function(done) {2320 var me = this;2321 var onComplete = jasmine.createSpy();2322 promise = Deferred.rejected(new Error('error message'));2323 promise.always(onComplete, targetScope);2324 promise.then(null, function() {2325 try {2326 expect(onComplete.callCount).toBeGT(0);2327 done();2328 } catch (e) {2329 me.fail(e);2330 done();2331 }2332 });2333 });2334 });2335 describe('with parameters specified via a configuration object', function() {2336 it('called with no parameters when resolved', function(done) {2337 var me = this;2338 var onComplete = jasmine.createSpy();2339 promise = Deferred.resolved('value');2340 promise.always({2341 fn: onComplete2342 });2343 promise.then(function() {2344 var error;2345 try {2346 expect(onComplete.callCount).toBeGT(0);2347 done();2348 } catch (e) {2349 me.fail(e);2350 done();2351 }2352 });2353 });2354 it('called with no parameters in the specified scope when resolved', function(done) {2355 var me = this;2356 var onComplete = jasmine.createSpy();2357 promise = Deferred.resolved('value');2358 promise.always({2359 fn: onComplete,2360 scope: targetScope2361 });2362 promise.then(function() {2363 var error;2364 try {2365 expect(onComplete.callCount).toBeGT(0);2366 done();2367 } catch (e) {2368 me.fail(e);2369 done();2370 }2371 });2372 });2373 it('called with no parameters when rejected', function(done) {2374 var me = this;2375 var onComplete = jasmine.createSpy();2376 promise = Deferred.rejected(new Error('error message'));2377 promise.always({2378 fn: onComplete2379 });2380 promise.then(null, function() {2381 var error;2382 try {2383 expect(onComplete.callCount).toBeGT(0);2384 done();2385 } catch (e) {2386 me.fail(e);2387 done();2388 }2389 });2390 });2391 it('called with no parameters in the specified scope when rejected', function(done) {2392 var me = this;2393 var onComplete = jasmine.createSpy();2394 promise = Deferred.rejected(new Error('error message'));2395 promise.always({2396 fn: onComplete,2397 scope: targetScope2398 });2399 promise.then(null, function() {2400 var error;2401 try {2402 expect(onComplete.callCount).toBeGT(0);2403 done();2404 } catch (e) {2405 me.fail(e);2406 done();2407 }2408 });2409 });2410 });2411 });2412 describe('return a new "pass-through" Promise that resolves with the original value or rejects with the original reason', function() {2413 it('if the originating Promise resolves, ignores value returned by callback', function() {2414 function onComplete () {2415 return 'callback return value';2416 }2417 promise = Deferred.resolved('resolved value').always(onComplete);2418 expect(promise instanceof ExtPromise).toBe(true);2419 eventuallyResolvesTo(promise, 'resolved value', true);2420 });2421 xit('if the originating Promise resolves, ignores and later rethrows Error thrown by callback', function(done) {2422 var me = this;2423 function onComplete () {2424 throw new Error('callback error message');2425 }2426 promise = Deferred.resolved('resolved value').always(onComplete);2427 expect(promise instanceof ExtPromise).toBe(true);2428 promise.then(function(v) {2429 expect(v).toBe('resolved value');2430 }, function(reason) {2431 expect(reason.message).toBe('callback error message');2432 done();2433 });2434 /*2435 TODO - Not sure the right conversion of this:2436 assert.eventuallyThrows(new Error('callback error message'), function(error) {2437 if (error) {2438 throw error;2439 }2440 return promise.should.eventually.equal('resolved value').then(function(value) {2441 return done();2442 }, function(reason) {2443 return done(reason);2444 });2445 }, 100);2446 */2447 });2448 it('if the originating Promise rejects, ignores value returned by callback', function() {2449 function onComplete () {2450 return 'callback return value';2451 }2452 promise = Deferred.rejected(new Error('rejection reason')).always(onComplete);2453 expect(promise instanceof ExtPromise).toBe(true);2454 eventuallyRejectedWith(promise, Error, 'rejection reason');2455 });2456 xit('if the originating Promise rejects, ignores and later rethrows Error thrown by callback', function(done) {2457 var me = this;2458 function onComplete () {2459 throw new Error('callback error message');2460 }2461 promise = Deferred.rejected(new Error('rejection reason')).always(onComplete);2462 expect(promise instanceof ExtPromise).toBe(true);2463 promise.then(function(v) {2464 me.fail('should reject');2465 done();2466 }, function(reason) {2467 expect(reason.message).toBe('rejection value');2468 //?? expect(reason.message).toBe('callback error message');2469 done();2470 });2471 /*2472 TODO - Not sure the right conversion of this:2473 assert.eventuallyThrows(new Error('callback error message'), function(error) {2474 if (error) {2475 throw error;2476 }2477 return promise.should.be.rejectedWith(Error, 'rejection reason').then(function(value) {2478 return done();2479 }, function(reason) {2480 return done(reason);2481 });2482 }, 100);2483 */2484 });2485 });2486 }); // always2487 //TODO - not sure the conversion for these2488 xdescribe('done()', function() {2489 describe('terminates a Promise chain, ensuring that unhandled rejections will be thrown as Errors', function() {2490 it('rethrows the rejection as an error if the originating Promise rejects', function(done) {2491 promise = Deferred.rejected(new Error('rejection reason')).done();2492 assert.eventuallyThrows(new Error('rejection reason'), done, 100);2493 });2494 it('rethrows the rejection as an error if an ancestor Promise rejects and that rejection is unhandled', function(done) {2495 2496 this.slow(250);2497 promise = Deferred.rejected(new Error('rejection reason')).then(function(value) {2498 return value;2499 }).done();2500 assert.eventuallyThrows(new Error('rejection reason'), done, 100);2501 });2502 });2503 });2504 describe('cancel()', function() {2505 describe('cancels a Promise if it is still pending, triggering a rejection with a CancellationError that will propagate to any Promises originating from that Promise', function() {2506 it('rejects a pending Promise with a CancellationError', function() {2507 promise = new Deferred().promise;2508 promise.cancel();2509 eventuallyRejectedWith(promise, Ext.promise.Promise.CancellationError);2510 });2511 it('rejects a pending Promise with a CancellationError with a reason', function() {2512 promise = new Deferred().promise;2513 promise.cancel('cancellation reason');2514 eventuallyRejectedWith(promise, Ext.promise.Promise.CancellationError, 'cancellation reason');2515 });2516 it('ignores attempts to cancel a fulfilled Promise', function() {2517 promise = Deferred.resolved('resolved value');2518 promise.cancel();2519 eventuallyResolvesTo(promise, 'resolved value', true);2520 });2521 it('ignores attempts to cancel a rejected Promise', function() {2522 promise = Deferred.rejected(new Error('rejection reason'));2523 promise.cancel();2524 eventuallyRejectedWith(promise, Error, 'rejection reason');2525 });2526 it('propagates rejection with that CancellationError to Promises that originate from the cancelled Promise', function() {2527 promise = new Deferred().promise;2528 promise.cancel('cancellation reason');2529 eventuallyRejectedWith(promise.then(),2530 Ext.promise.Promise.CancellationError, 'cancellation reason');2531 });2532 });2533 });2534 xdescribe('log()', function() {2535 describe('logs the resolution or rejection of this Promise using Logger.log()', function() {2536 beforeEach(function() {2537 return jasmine.createSpy(Logger, 'log');2538 });2539 afterEach(function() {2540 return Logger.log.restore();2541 });2542 it('logs a fulfilled promise', function(done) {2543 var promise, value;2544 value = 'resolved value';2545 promise = Deferred.resolved(value).log();2546 expect(promise instanceof ExtPromise).toBe(true);2547 promise.always(function() {2548 var error;2549 try {2550 expect(Logger.log).to.be.calledOnce.and.calledWith("Promise resolved with value: " + value);2551 done();2552 } catch (e) {2553 me.fail(e);2554 done(e);2555 }2556 });2557 });2558 it('logs a fulfilled promise, with the optional name specified', function(done) {2559 var promise, value;2560 value = 'resolved value';2561 promise = Deferred.resolved(value).log('Test Promise');2562 expect(promise instanceof ExtPromise).toBe(true);2563 promise.always(function() {2564 var error;2565 try {2566 expect(Logger.log).to.be.calledOnce.and.calledWith("Test Promise resolved with value: " + value);2567 done();2568 } catch (e) {2569 me.fail(e);2570 done();2571 }2572 });2573 });2574 it('logs a rejected promise', function(done) {2575 var promise, reason;2576 reason = new Error('rejection reason');2577 promise = Deferred.rejected(reason).log();2578 expect(promise instanceof ExtPromise).toBe(true);2579 promise.always(function() {2580 var error;2581 try {2582 expect(Logger.log).to.be.calledOnce.and.calledWith("Promise rejected with reason: " + reason);2583 done();2584 } catch (e) {2585 me.fail(e);2586 done();2587 }2588 });2589 });2590 it('logs a rejected promise, with the optional name specified', function(done) {2591 var promise, reason;2592 reason = new Error('rejection reason');2593 promise = Deferred.rejected(reason).log('Test Promise');2594 expect(promise instanceof ExtPromise).toBe(true);2595 promise.always(function() {2596 var error;2597 try {2598 expect(Logger.log).to.be.calledOnce.and.calledWith("Test Promise rejected with reason: " + reason);2599 done();2600 } catch (e) {2601 me.fail(e);2602 done();2603 }2604 });2605 });2606 });2607 describe('return a new "pass-through" Promise that resolves with the original value or rejects with the original reason', function() {2608 it('resolves if the originating Promise resolves', function() {2609 2610 promise = Deferred.resolved('resolved value').log();2611 expect(promise instanceof ExtPromise).toBe(true);2612 eventuallyResolvesTo(promise, 'resolved value', true);2613 });2614 it('rejects if the originating Promise rejects', function() {2615 2616 promise = Deferred.rejected(new Error('rejection reason')).log();2617 expect(promise instanceof ExtPromise).toBe(true);2618 eventuallyRejectedWith(promise, Error, 'rejection reason');2619 });2620 });2621 }); // log2622 2623 describe('Extras', function() {2624 function verifyScope (fn, expectedScope) {2625 return function() {2626 expect(this).toBe(expectedScope);2627 return fn.apply(this, arguments);2628 };2629 }2630 function verifyArgs (fn, expectedArgs) {2631 return function() {2632 var args = Ext.Array.slice(arguments, 0);2633 expect(args).toEqual(expectedArgs);2634 return fn.apply(this, arguments);2635 };2636 }2637 describe('sequence()', function() {2638 var fn1 = function() {2639 ++fn1.callCount;2640 expect(fn2.callCount).toBe(0);2641 expect(fn3.callCount).toBe(0);2642 return 1;2643 };2644 var fn2 = function() {2645 ++fn2.callCount;2646 expect(fn1.callCount).toBe(1);2647 expect(fn3.callCount).toBe(0);2648 return 2;2649 };2650 var fn3 = function() {2651 ++fn3.callCount;2652 expect(fn1.callCount).toBe(1);2653 expect(fn2.callCount).toBe(1);2654 return 3;2655 };2656 beforeEach(function() {2657 fn1.callCount = fn2.callCount = fn3.callCount = 0;2658 });2659 describe('returns a new Promise that will resolve with an Array of the results returned by calling the specified functions in sequence order', function() {2660 it('Empty Array', function() {2661 var fns = [];2662 promise = Deferred.sequence(fns);2663 expect(promise instanceof ExtPromise).toBe(true);2664 eventuallyResolvesTo(promise, [], true);2665 });2666 it('Empty Array with the optional scope specified', function() {2667 var fns = [];2668 promise = Deferred.sequence(fns, targetScope);2669 expect(promise instanceof ExtPromise).toBe(true);2670 eventuallyResolvesTo(promise, [], true);2671 });2672 it('Empty Array with the optional scope and arguments specified', function() {2673 var args = ['a', 'b', 'c'];2674 var fns = [];2675 promise = Deferred.sequence(fns, targetScope, 'a', 'b', 'c');2676 expect(promise instanceof ExtPromise).toBe(true);2677 eventuallyResolvesTo(promise, [], true);2678 });2679 it('Array with one function', function() {2680 var fns = [fn1];2681 promise = Deferred.sequence(fns);2682 expect(promise instanceof ExtPromise).toBe(true);2683 eventuallyResolvesTo(promise, [1], true);2684 });2685 it('Array with one function with the optional scope specified', function() {2686 var fns = [verifyScope(fn1, targetScope)];2687 promise = Deferred.sequence(fns, targetScope);2688 expect(promise instanceof ExtPromise).toBe(true);2689 eventuallyResolvesTo(promise, [1], true);2690 });2691 it('Array with one function with the optional scope and arguments specified', function() {2692 var args, fns;2693 args = ['a', 'b', 'c'];2694 fns = [verifyArgs(verifyScope(fn1, targetScope), args)];2695 promise = Deferred.sequence(fns, targetScope, 'a', 'b', 'c');2696 expect(promise instanceof ExtPromise).toBe(true);2697 eventuallyResolvesTo(promise, [1], true);2698 });2699 it('Array of two functions', function() {2700 var fns;2701 fns = [fn1, fn2];2702 promise = Deferred.sequence(fns);2703 expect(promise instanceof ExtPromise).toBe(true);2704 eventuallyResolvesTo(promise, [1, 2], true);2705 });2706 it('Array of two functions with the optional scope specified', function() {2707 var fns;2708 fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope)];2709 promise = Deferred.sequence(fns, targetScope);2710 expect(promise instanceof ExtPromise).toBe(true);2711 eventuallyResolvesTo(promise, [1, 2], true);2712 });2713 it('Array of two functions with the optional scope and arguments specified', function() {2714 var args, fns;2715 args = ['a', 'b', 'c'];2716 fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args)];2717 promise = Deferred.sequence(fns, targetScope, 'a', 'b', 'c');2718 expect(promise instanceof ExtPromise).toBe(true);2719 eventuallyResolvesTo(promise, [1, 2], true);2720 });2721 it('Array of three functions', function() {2722 var fns;2723 fns = [fn1, fn2, fn3];2724 promise = Deferred.sequence(fns);2725 expect(promise instanceof ExtPromise).toBe(true);2726 eventuallyResolvesTo(promise, [1, 2, 3], true);2727 });2728 it('Array of three functions with the optional scope specified', function() {2729 var fns;2730 fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope), verifyScope(fn3, targetScope)];2731 promise = Deferred.sequence(fns, targetScope);2732 expect(promise instanceof ExtPromise).toBe(true);2733 eventuallyResolvesTo(promise, [1, 2, 3], true);2734 });2735 it('Array of three functions with the optional scope and arguments specified', function() {2736 var args, fns;2737 args = ['a', 'b', 'c'];2738 fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args), verifyArgs(verifyScope(fn3, targetScope), args)];2739 promise = Deferred.sequence(fns, targetScope, 'a', 'b', 'c');2740 expect(promise instanceof ExtPromise).toBe(true);2741 eventuallyResolvesTo(promise, [1, 2, 3], true);2742 });2743 });2744 describe('returns a new Promise that will resolve with an Array of the results returned by calling the specified resolved Promise of an Array of functions in sequence order', function() {2745 it('Promise of an empty Array', function() {2746 var fns;2747 fns = [];2748 promise = Deferred.sequence(Deferred.resolved(fns));2749 expect(promise instanceof ExtPromise).toBe(true);2750 eventuallyResolvesTo(promise, [], true);2751 });2752 it('Promise of an empty Array with the optional scope specified', function() {2753 var fns;2754 fns = [];2755 promise = Deferred.sequence(Deferred.resolved(fns), targetScope);2756 expect(promise instanceof ExtPromise).toBe(true);2757 eventuallyResolvesTo(promise, [], true);2758 });2759 it('Promise of an empty Array with the optional scope and arguments specified', function() {2760 var args, fns;2761 args = ['a', 'b', 'c'];2762 fns = [];2763 promise = Deferred.sequence(fns, targetScope, 'a', 'b', 'c');2764 expect(promise instanceof ExtPromise).toBe(true);2765 eventuallyResolvesTo(promise, [], true);2766 });2767 it('Promise of an Array with one function', function() {2768 var fns = [fn1];2769 promise = Deferred.sequence(Deferred.resolved(fns));2770 expect(promise instanceof ExtPromise).toBe(true);2771 eventuallyResolvesTo(promise, [1], true);2772 });2773 it('Promise of an Array with one function with the optional scope specified', function() {2774 var fns;2775 fns = [verifyScope(fn1, targetScope)];2776 promise = Deferred.sequence(Deferred.resolved(fns), targetScope);2777 expect(promise instanceof ExtPromise).toBe(true);2778 eventuallyResolvesTo(promise, [1], true);2779 });2780 it('Promise of an Array with one function with the optional scope and arguments specified', function() {2781 var args, fns;2782 args = ['a', 'b', 'c'];2783 fns = [verifyArgs(verifyScope(fn1, targetScope), args)];2784 promise = Deferred.sequence(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');2785 expect(promise instanceof ExtPromise).toBe(true);2786 eventuallyResolvesTo(promise, [1], true);2787 });2788 it('Promise of an Array of two functions', function() {2789 var fns;2790 fns = [fn1, fn2];2791 promise = Deferred.sequence(Deferred.resolved(fns));2792 expect(promise instanceof ExtPromise).toBe(true);2793 eventuallyResolvesTo(promise, [1, 2], true);2794 });2795 it('Promise of an Array of two functions with the optional scope specified', function() {2796 var fns;2797 fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope)];2798 promise = Deferred.sequence(Deferred.resolved(fns), targetScope);2799 expect(promise instanceof ExtPromise).toBe(true);2800 eventuallyResolvesTo(promise, [1, 2], true);2801 });2802 it('Promise of an Array of two functions with the optional scope and arguments specified', function() {2803 var args, fns;2804 args = ['a', 'b', 'c'];2805 fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args)];2806 promise = Deferred.sequence(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');2807 expect(promise instanceof ExtPromise).toBe(true);2808 eventuallyResolvesTo(promise, [1, 2], true);2809 });2810 it('Promise of an Array of three functions', function() {2811 var fns;2812 fns = [fn1, fn2, fn3];2813 promise = Deferred.sequence(Deferred.resolved(fns));2814 expect(promise instanceof ExtPromise).toBe(true);2815 eventuallyResolvesTo(promise, [1, 2, 3], true);2816 });2817 it('Promise of an Array of three functions with the optional scope specified', function() {2818 var fns;2819 fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope), verifyScope(fn3, targetScope)];2820 promise = Deferred.sequence(Deferred.resolved(fns), targetScope);2821 expect(promise instanceof ExtPromise).toBe(true);2822 eventuallyResolvesTo(promise, [1, 2, 3], true);2823 });2824 it('Promise of an Array of three functions with the optional scope and arguments specified', function() {2825 var args, fns;2826 args = ['a', 'b', 'c'];2827 fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args), verifyArgs(verifyScope(fn3, targetScope), args)];2828 promise = Deferred.sequence(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');2829 expect(promise instanceof ExtPromise).toBe(true);2830 eventuallyResolvesTo(promise, [1, 2, 3], true);2831 });2832 });2833 describe('returns a new Promise that will reject with the Error associated with the specified rejected Promise of an Array of functions', function() {2834 it('Error: error message', function() {2835 2836 promise = Deferred.sequence(Deferred.rejected(new Error('error message')));2837 expect(promise instanceof ExtPromise).toBe(true);2838 eventuallyRejectedWith(promise, Error, 'error message');2839 });2840 });2841 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions throws an Error', function() {2842 function brokenFn () {2843 throw new Error('Error message');2844 }2845 it('Array with one function that throws an Error', function() {2846 var fns;2847 fns = [brokenFn];2848 promise = Deferred.sequence(fns);2849 expect(promise instanceof ExtPromise).toBe(true);2850 eventuallyRejectedWith(promise, Error, 'Error message');2851 });2852 it('Array with one function and one function that throws an Error', function() {2853 var fns;2854 fns = [fn1, brokenFn];2855 promise = Deferred.sequence(fns);2856 expect(promise instanceof ExtPromise).toBe(true);2857 eventuallyRejectedWith(promise, Error, 'Error message');2858 });2859 it('Array with two functions and one function that throws an Error', function() {2860 var fns;2861 fns = [fn1, fn2, brokenFn];2862 promise = Deferred.sequence(fns);2863 expect(promise instanceof ExtPromise).toBe(true);2864 eventuallyRejectedWith(promise, Error, 'Error message');2865 });2866 });2867 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions throws an Error', function() {2868 function brokenFn () {2869 throw new Error('Error message');2870 }2871 it('Promise of an Array with one function that throws an Error', function() {2872 var fns;2873 fns = [brokenFn];2874 promise = Deferred.sequence(Deferred.resolved(fns));2875 expect(promise instanceof ExtPromise).toBe(true);2876 eventuallyRejectedWith(promise, Error, 'Error message');2877 });2878 it('Promise of an Array with one function and one function that throws an Error', function() {2879 var fns;2880 fns = [fn1, brokenFn];2881 promise = Deferred.sequence(Deferred.resolved(fns));2882 expect(promise instanceof ExtPromise).toBe(true);2883 eventuallyRejectedWith(promise, Error, 'Error message');2884 });2885 it('Promise of an Array with two functions and one function that throws an Error', function() {2886 var fns;2887 fns = [fn1, fn2, brokenFn];2888 promise = Deferred.sequence(Deferred.resolved(fns));2889 expect(promise instanceof ExtPromise).toBe(true);2890 eventuallyRejectedWith(promise, Error, 'Error message');2891 });2892 });2893 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions returns a rejected Promise', function() {2894 function rejectFn () {2895 return Deferred.rejected(new Error('Error message'));2896 }2897 it('Array with one function that returns a rejected Promise', function() {2898 var fns;2899 fns = [rejectFn];2900 promise = Deferred.sequence(fns);2901 expect(promise instanceof ExtPromise).toBe(true);2902 eventuallyRejectedWith(promise, Error, 'Error message');2903 });2904 it('Array with one function and one function that returns a rejected Promise', function() {2905 var fns;2906 fns = [fn1, rejectFn];2907 promise = Deferred.sequence(fns);2908 expect(promise instanceof ExtPromise).toBe(true);2909 eventuallyRejectedWith(promise, Error, 'Error message');2910 });2911 it('Array with two functions and one function that returns a rejected Promise', function() {2912 var fns;2913 fns = [fn1, fn2, rejectFn];2914 promise = Deferred.sequence(fns);2915 expect(promise instanceof ExtPromise).toBe(true);2916 eventuallyRejectedWith(promise, Error, 'Error message');2917 });2918 });2919 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions returns a rejected Promise', function() {2920 function rejectFn () {2921 return Deferred.rejected(new Error('Error message'));2922 }2923 it('Promise of an Array with one function that returns a rejected Promise', function() {2924 var fns;2925 fns = [rejectFn];2926 promise = Deferred.sequence(Deferred.resolved(fns));2927 expect(promise instanceof ExtPromise).toBe(true);2928 eventuallyRejectedWith(promise, Error, 'Error message');2929 });2930 it('Promise of an Array with one function and one function that returns a rejected Promise', function() {2931 var fns;2932 fns = [fn1, rejectFn];2933 promise = Deferred.sequence(Deferred.resolved(fns));2934 expect(promise instanceof ExtPromise).toBe(true);2935 eventuallyRejectedWith(promise, Error, 'Error message');2936 });2937 it('Promise of an Array with two functions and one function that returns a rejected Promise', function() {2938 var fns;2939 fns = [fn1, fn2, rejectFn];2940 promise = Deferred.sequence(Deferred.resolved(fns));2941 expect(promise instanceof ExtPromise).toBe(true);2942 eventuallyRejectedWith(promise, Error, 'Error message');2943 });2944 });2945 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified Array is not a function', function() {2946 it('Array with one non-function value', function() {2947 var fns;2948 fns = [1];2949 promise = Deferred.sequence(fns);2950 expect(promise instanceof ExtPromise).toBe(true);2951 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2952 });2953 it('Array with one function and one non-function value', function() {2954 var fns;2955 fns = [fn1, 1];2956 promise = Deferred.sequence(fns);2957 expect(promise instanceof ExtPromise).toBe(true);2958 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2959 });2960 it('Array with two functions and one non-function value', function() {2961 var fns;2962 fns = [fn1, fn2, 1];2963 promise = Deferred.sequence(fns);2964 expect(promise instanceof ExtPromise).toBe(true);2965 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2966 });2967 });2968 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified resolved Promise of an Array is not a function ', function() {2969 it('Promise of an Array with one non-function value', function() {2970 var fns;2971 fns = [1];2972 promise = Deferred.sequence(Deferred.resolved(fns));2973 expect(promise instanceof ExtPromise).toBe(true);2974 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2975 });2976 it('Promise of an Array with one function and one non-function value', function() {2977 var fns;2978 fns = [fn1, 1];2979 promise = Deferred.sequence(Deferred.resolved(fns));2980 expect(promise instanceof ExtPromise).toBe(true);2981 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2982 });2983 it('Promise of an Array with two functions and one non-function value', function() {2984 var fns;2985 fns = [fn1, fn2, 1];2986 promise = Deferred.sequence(Deferred.resolved(fns));2987 expect(promise instanceof ExtPromise).toBe(true);2988 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');2989 });2990 });2991 describe('throws an Error if anything other than Array or Promise of an Array is specified as the first parameter', function() {2992 it('No parameters', function() {2993 expect(function() {2994 return Deferred.sequence();2995 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');2996 });2997 it('A non-Array parameter', function() {2998 expect(function() {2999 return Deferred.sequence(1);3000 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');3001 });3002 });3003 }); // sequence3004 describe('parallel()', function() {3005 var fn1 = function() {3006 ++fn1.callCount;3007 return 1;3008 };3009 var fn2 = function() {3010 ++fn2.callCount;3011 return 2;3012 };3013 var fn3 = function() {3014 ++fn3.callCount;3015 return 3;3016 };3017 beforeEach(function() {3018 fn1.callCount = fn2.callCount = fn3.callCount = 0;3019 });3020 describe('returns a new Promise that will resolve with an Array of the results returned by calling the specified functions in parallel', function() {3021 it('Empty Array', function() {3022 var fns = [];3023 promise = Deferred.parallel(fns);3024 expect(promise instanceof ExtPromise).toBe(true);3025 eventuallyResolvesTo(promise, [], true);3026 });3027 it('Empty Array with the optional scope specified', function() {3028 var fns = [];3029 promise = Deferred.parallel(fns, targetScope);3030 expect(promise instanceof ExtPromise).toBe(true);3031 eventuallyResolvesTo(promise, [], true);3032 });3033 it('Empty Array with the optional scope and arguments specified', function() {3034 var args = ['a', 'b', 'c'];3035 var fns = [];3036 promise = Deferred.parallel(fns, targetScope, 'a', 'b', 'c');3037 expect(promise instanceof ExtPromise).toBe(true);3038 eventuallyResolvesTo(promise, [], true);3039 });3040 it('Array with one function', function() {3041 var fns = [fn1];3042 promise = Deferred.parallel(fns);3043 expect(promise instanceof ExtPromise).toBe(true);3044 eventuallyResolvesTo(promise, [1], true);3045 });3046 it('Array with one function with the optional scope specified', function() {3047 var fns = [verifyScope(fn1, targetScope)];3048 promise = Deferred.parallel(fns, targetScope);3049 expect(promise instanceof ExtPromise).toBe(true);3050 eventuallyResolvesTo(promise, [1], true);3051 });3052 it('Array with one function with the optional scope and arguments specified', function() {3053 var args = ['a', 'b', 'c'];3054 var fns = [verifyArgs(verifyScope(fn1, targetScope), args)];3055 promise = Deferred.parallel(fns, targetScope, 'a', 'b', 'c');3056 expect(promise instanceof ExtPromise).toBe(true);3057 eventuallyResolvesTo(promise, [1], true);3058 });3059 it('Array of two functions', function() {3060 var fns = [fn1, fn2];3061 promise = Deferred.parallel(fns);3062 expect(promise instanceof ExtPromise).toBe(true);3063 eventuallyResolvesTo(promise, [1, 2], true);3064 });3065 it('Array of two functions with the optional scope specified', function() {3066 var fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope)];3067 promise = Deferred.parallel(fns, targetScope);3068 expect(promise instanceof ExtPromise).toBe(true);3069 eventuallyResolvesTo(promise, [1, 2], true);3070 });3071 it('Array of two functions with the optional scope and arguments specified', function() {3072 var args = ['a', 'b', 'c'];3073 var fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args)];3074 promise = Deferred.parallel(fns, targetScope, 'a', 'b', 'c');3075 expect(promise instanceof ExtPromise).toBe(true);3076 eventuallyResolvesTo(promise, [1, 2], true);3077 });3078 it('Array of three functions', function() {3079 var fns = [fn1, fn2, fn3];3080 promise = Deferred.parallel(fns);3081 expect(promise instanceof ExtPromise).toBe(true);3082 eventuallyResolvesTo(promise, [1, 2, 3], true);3083 });3084 it('Array of three functions with the optional scope specified', function() {3085 var fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope), verifyScope(fn3, targetScope)];3086 promise = Deferred.parallel(fns, targetScope);3087 expect(promise instanceof ExtPromise).toBe(true);3088 eventuallyResolvesTo(promise, [1, 2, 3], true);3089 });3090 it('Array of three functions with the optional scope and arguments specified', function() {3091 var args = ['a', 'b', 'c'];3092 var fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args), verifyArgs(verifyScope(fn3, targetScope), args)];3093 promise = Deferred.parallel(fns, targetScope, 'a', 'b', 'c');3094 expect(promise instanceof ExtPromise).toBe(true);3095 eventuallyResolvesTo(promise, [1, 2, 3], true);3096 });3097 });3098 describe('returns a new Promise that will resolve with an Array of the results returned by calling the specified resolved Promise of an Array of functions in parallel', function() {3099 it('Promise of an empty Array', function() {3100 var fns = [];3101 promise = Deferred.parallel(Deferred.resolved(fns));3102 expect(promise instanceof ExtPromise).toBe(true);3103 eventuallyResolvesTo(promise, [], true);3104 });3105 it('Promise of an empty Array with the optional scope specified', function() {3106 var fns = [];3107 promise = Deferred.parallel(Deferred.resolved(fns), targetScope);3108 expect(promise instanceof ExtPromise).toBe(true);3109 eventuallyResolvesTo(promise, [], true);3110 });3111 it('Promise of an empty Array with the optional scope and arguments specified', function() {3112 var args = ['a', 'b', 'c'];3113 var fns = [];3114 promise = Deferred.parallel(fns, targetScope, 'a', 'b', 'c');3115 expect(promise instanceof ExtPromise).toBe(true);3116 eventuallyResolvesTo(promise, [], true);3117 });3118 it('Promise of an Array with one function', function() {3119 var fns = [fn1];3120 promise = Deferred.parallel(Deferred.resolved(fns));3121 expect(promise instanceof ExtPromise).toBe(true);3122 eventuallyResolvesTo(promise, [1], true);3123 });3124 it('Promise of an Array with one function with the optional scope specified', function() {3125 var fns = [verifyScope(fn1, targetScope)];3126 promise = Deferred.parallel(Deferred.resolved(fns), targetScope);3127 expect(promise instanceof ExtPromise).toBe(true);3128 eventuallyResolvesTo(promise, [1], true);3129 });3130 it('Promise of an Array with one function with the optional scope and arguments specified', function() {3131 var args = ['a', 'b', 'c'];3132 var fns = [verifyArgs(verifyScope(fn1, targetScope), args)];3133 promise = Deferred.parallel(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');3134 expect(promise instanceof ExtPromise).toBe(true);3135 eventuallyResolvesTo(promise, [1], true);3136 });3137 it('Promise of an Array of two functions', function() {3138 var fns = [fn1, fn2];3139 promise = Deferred.parallel(Deferred.resolved(fns));3140 expect(promise instanceof ExtPromise).toBe(true);3141 eventuallyResolvesTo(promise, [1, 2], true);3142 });3143 it('Promise of an Array of two functions with the optional scope specified', function() {3144 var fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope)];3145 promise = Deferred.parallel(Deferred.resolved(fns), targetScope);3146 expect(promise instanceof ExtPromise).toBe(true);3147 eventuallyResolvesTo(promise, [1, 2], true);3148 });3149 it('Promise of an Array of two functions with the optional scope and arguments specified', function() {3150 var args = ['a', 'b', 'c'];3151 var fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args)];3152 promise = Deferred.parallel(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');3153 expect(promise instanceof ExtPromise).toBe(true);3154 eventuallyResolvesTo(promise, [1, 2], true);3155 });3156 it('Promise of an Array of three functions', function() {3157 var fns = [fn1, fn2, fn3];3158 promise = Deferred.parallel(Deferred.resolved(fns));3159 expect(promise instanceof ExtPromise).toBe(true);3160 eventuallyResolvesTo(promise, [1, 2, 3], true);3161 });3162 it('Promise of an Array of three functions with the optional scope specified', function() {3163 var fns = [verifyScope(fn1, targetScope), verifyScope(fn2, targetScope), verifyScope(fn3, targetScope)];3164 promise = Deferred.parallel(Deferred.resolved(fns), targetScope);3165 expect(promise instanceof ExtPromise).toBe(true);3166 eventuallyResolvesTo(promise, [1, 2, 3], true);3167 });3168 it('Promise of an Array of three functions with the optional scope and arguments specified', function() {3169 var args = ['a', 'b', 'c'];3170 var fns = [verifyArgs(verifyScope(fn1, targetScope), args), verifyArgs(verifyScope(fn2, targetScope), args), verifyArgs(verifyScope(fn3, targetScope), args)];3171 promise = Deferred.parallel(Deferred.resolved(fns), targetScope, 'a', 'b', 'c');3172 expect(promise instanceof ExtPromise).toBe(true);3173 eventuallyResolvesTo(promise, [1, 2, 3], true);3174 });3175 });3176 describe('returns a new Promise that will reject with the Error associated with the specified rejected Promise of an Array of functions', function() {3177 it('Error: error message', function() {3178 promise = Deferred.parallel(Deferred.rejected(new Error('error message')));3179 expect(promise instanceof ExtPromise).toBe(true);3180 eventuallyRejectedWith(promise, Error, 'error message');3181 });3182 });3183 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions throws an Error', function() {3184 function brokenFn () {3185 throw new Error('Error message');3186 }3187 it('Array with one function that throws an Error', function() {3188 var fns = [brokenFn];3189 promise = Deferred.parallel(fns);3190 expect(promise instanceof ExtPromise).toBe(true);3191 eventuallyRejectedWith(promise, Error, 'Error message');3192 });3193 it('Array with one function and one function that throws an Error', function() {3194 var fns = [fn1, brokenFn];3195 promise = Deferred.parallel(fns);3196 expect(promise instanceof ExtPromise).toBe(true);3197 eventuallyRejectedWith(promise, Error, 'Error message');3198 });3199 it('Array with two functions and one function that throws an Error', function() {3200 var fns = [fn1, fn2, brokenFn];3201 promise = Deferred.parallel(fns);3202 expect(promise instanceof ExtPromise).toBe(true);3203 eventuallyRejectedWith(promise, Error, 'Error message');3204 });3205 });3206 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions throws an Error', function() {3207 function brokenFn () {3208 throw new Error('Error message');3209 }3210 it('Promise of an Array with one function that throws an Error', function() {3211 var fns = [brokenFn];3212 promise = Deferred.parallel(Deferred.resolved(fns));3213 expect(promise instanceof ExtPromise).toBe(true);3214 eventuallyRejectedWith(promise, Error, 'Error message');3215 });3216 it('Promise of an Array with one function and one function that throws an Error', function() {3217 var fns = [fn1, brokenFn];3218 promise = Deferred.parallel(Deferred.resolved(fns));3219 expect(promise instanceof ExtPromise).toBe(true);3220 eventuallyRejectedWith(promise, Error, 'Error message');3221 });3222 it('Promise of an Array with two functions and one function that throws an Error', function() {3223 var fns = [fn1, fn2, brokenFn];3224 promise = Deferred.parallel(Deferred.resolved(fns));3225 expect(promise instanceof ExtPromise).toBe(true);3226 eventuallyRejectedWith(promise, Error, 'Error message');3227 });3228 });3229 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions returns a rejected Promise', function() {3230 function rejectFn () {3231 return Deferred.rejected(new Error('Error message'));3232 }3233 it('Array with one function that returns a rejected Promise', function() {3234 var fns = [rejectFn];3235 promise = Deferred.parallel(fns);3236 expect(promise instanceof ExtPromise).toBe(true);3237 eventuallyRejectedWith(promise, Error, 'Error message');3238 });3239 it('Array with one function and one function that returns a rejected Promise', function() {3240 var fns = [fn1, rejectFn];3241 promise = Deferred.parallel(fns);3242 expect(promise instanceof ExtPromise).toBe(true);3243 eventuallyRejectedWith(promise, Error, 'Error message');3244 });3245 it('Array with two functions and one function that returns a rejected Promise', function() {3246 var fns = [fn1, fn2, rejectFn];3247 promise = Deferred.parallel(fns);3248 expect(promise instanceof ExtPromise).toBe(true);3249 eventuallyRejectedWith(promise, Error, 'Error message');3250 });3251 });3252 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions returns a rejected Promise', function() {3253 function rejectFn () {3254 return Deferred.rejected(new Error('Error message'));3255 }3256 it('Promise of an Array with one function that returns a rejected Promise', function() {3257 var fns = [rejectFn];3258 promise = Deferred.parallel(Deferred.resolved(fns));3259 expect(promise instanceof ExtPromise).toBe(true);3260 eventuallyRejectedWith(promise, Error, 'Error message');3261 });3262 it('Promise of an Array with one function and one function that returns a rejected Promise', function() {3263 var fns = [fn1, rejectFn];3264 promise = Deferred.parallel(Deferred.resolved(fns));3265 expect(promise instanceof ExtPromise).toBe(true);3266 eventuallyRejectedWith(promise, Error, 'Error message');3267 });3268 it('Promise of an Array with two functions and one function that returns a rejected Promise', function() {3269 var fns = [fn1, fn2, rejectFn];3270 promise = Deferred.parallel(Deferred.resolved(fns));3271 expect(promise instanceof ExtPromise).toBe(true);3272 eventuallyRejectedWith(promise, Error, 'Error message');3273 });3274 });3275 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified Array is not a function', function() {3276 it('Array with one non-function value', function() {3277 var fns = [1];3278 promise = Deferred.parallel(fns);3279 expect(promise instanceof ExtPromise).toBe(true);3280 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3281 });3282 it('Array with one function and one non-function value', function() {3283 var fns = [fn1, 1];3284 promise = Deferred.parallel(fns);3285 expect(promise instanceof ExtPromise).toBe(true);3286 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3287 });3288 it('Array with two functions and one non-function value', function() {3289 var fns = [fn1, fn2, 1];3290 promise = Deferred.parallel(fns);3291 expect(promise instanceof ExtPromise).toBe(true);3292 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3293 });3294 });3295 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified resolved Promise of an Array is not a function ', function() {3296 it('Promise of an Array with one non-function value', function() {3297 var fns = [1];3298 promise = Deferred.parallel(Deferred.resolved(fns));3299 expect(promise instanceof ExtPromise).toBe(true);3300 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3301 });3302 it('Promise of an Array with one function and one non-function value', function() {3303 var fns = [fn1, 1];3304 promise = Deferred.parallel(Deferred.resolved(fns));3305 expect(promise instanceof ExtPromise).toBe(true);3306 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3307 });3308 it('Promise of an Array with two functions and one non-function value', function() {3309 var fns = [fn1, fn2, 1];3310 promise = Deferred.parallel(Deferred.resolved(fns));3311 expect(promise instanceof ExtPromise).toBe(true);3312 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3313 });3314 });3315 describe('throws an Error if anything other than Array or Promise of an Array is specified as the first parameter', function() {3316 it('No parameters', function() {3317 expect(function() {3318 return Deferred.parallel();3319 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');3320 });3321 it('A non-Array parameter', function() {3322 expect(function() {3323 return Deferred.parallel(1);3324 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');3325 });3326 });3327 }); // parallel3328 describe('pipeline()', function() {3329 function createAppenderFn (v) {3330 return function(x) {3331 return x ? x + v : v;3332 };3333 }3334 describe('returns a new Promise that will resolve with the result returned by calling the specified Array of functions as a pipeline', function() {3335 it('Empty Array', function() {3336 var fns = [];3337 promise = Deferred.pipeline(fns);3338 expect(promise instanceof ExtPromise).toBe(true);3339 eventuallyResolvesTo(promise, void 0, true);3340 });3341 it('Empty Array with an initial value', function() {3342 var fns = [];3343 promise = Deferred.pipeline(fns, 'initial value');3344 expect(promise instanceof ExtPromise).toBe(true);3345 eventuallyResolvesTo(promise, 'initial value', true);3346 });3347 it('Empty Array with an initial value and scope', function() {3348 var fns = [];3349 promise = Deferred.pipeline(fns, 'initial value');3350 expect(promise instanceof ExtPromise).toBe(true);3351 eventuallyResolvesTo(promise, 'initial value', true);3352 });3353 it('Array with one function', function() {3354 var fns = [createAppenderFn('a')];3355 promise = Deferred.pipeline(fns);3356 expect(promise instanceof ExtPromise).toBe(true);3357 eventuallyResolvesTo(promise, 'a', true);3358 });3359 it('Array with one function with an initial value', function() {3360 var fns = [createAppenderFn('b')];3361 promise = Deferred.pipeline(fns, 'a');3362 expect(promise instanceof ExtPromise).toBe(true);3363 eventuallyResolvesTo(promise, 'ab', true);3364 });3365 it('Array with one function with an initial value and scope', function() {3366 var fns = [verifyScope(createAppenderFn('b'), targetScope)];3367 promise = Deferred.pipeline(fns, 'a', targetScope);3368 expect(promise instanceof ExtPromise).toBe(true);3369 eventuallyResolvesTo(promise, 'ab', true);3370 });3371 it('Array of two functions', function() {3372 var fns = [createAppenderFn('a'), createAppenderFn('b')];3373 promise = Deferred.pipeline(fns);3374 expect(promise instanceof ExtPromise).toBe(true);3375 eventuallyResolvesTo(promise, 'ab', true);3376 });3377 it('Array of two functions with an initial value', function() {3378 var fns = [createAppenderFn('b'), createAppenderFn('c')];3379 promise = Deferred.pipeline(fns, 'a');3380 expect(promise instanceof ExtPromise).toBe(true);3381 eventuallyResolvesTo(promise, 'abc', true);3382 });3383 it('Array of two functions with an initial value and scope', function() {3384 var fns = [verifyScope(createAppenderFn('b'), targetScope), verifyScope(createAppenderFn('c'), targetScope)];3385 promise = Deferred.pipeline(fns, 'a', targetScope);3386 expect(promise instanceof ExtPromise).toBe(true);3387 eventuallyResolvesTo(promise, 'abc', true);3388 });3389 it('Array of three functions', function() {3390 var fns = [createAppenderFn('a'), createAppenderFn('b'), createAppenderFn('c')];3391 promise = Deferred.pipeline(fns);3392 expect(promise instanceof ExtPromise).toBe(true);3393 eventuallyResolvesTo(promise, 'abc', true);3394 });3395 it('Array of three functions with an initial value', function() {3396 var fns = [createAppenderFn('b'), createAppenderFn('c'), createAppenderFn('d')];3397 promise = Deferred.pipeline(fns, 'a');3398 expect(promise instanceof ExtPromise).toBe(true);3399 eventuallyResolvesTo(promise, 'abcd', true);3400 });3401 it('Array of three functions with an initial value', function() {3402 var fns = [verifyScope(createAppenderFn('b'), targetScope), verifyScope(createAppenderFn('c'), targetScope), verifyScope(createAppenderFn('d'), targetScope)];3403 promise = Deferred.pipeline(fns, 'a', targetScope);3404 expect(promise instanceof ExtPromise).toBe(true);3405 eventuallyResolvesTo(promise, 'abcd', true);3406 });3407 });3408 describe('returns a new Promise that will resolve with the result returned by calling the specified Promise of an Array of functions as a pipeline', function() {3409 it('Promise of an empty Array', function() {3410 var fns = [];3411 promise = Deferred.pipeline(Deferred.resolved(fns));3412 expect(promise instanceof ExtPromise).toBe(true);3413 eventuallyResolvesTo(promise, void 0, true);3414 });3415 it('Promise of an empty Array with an initial value', function() {3416 var fns = [];3417 promise = Deferred.pipeline(Deferred.resolved(fns), 'initial value');3418 expect(promise instanceof ExtPromise).toBe(true);3419 eventuallyResolvesTo(promise, 'initial value', true);3420 });3421 it('Promise of an empty Array with an initial value and scope', function() {3422 var fns = [];3423 promise = Deferred.pipeline(Deferred.resolved(fns), 'initial value');3424 expect(promise instanceof ExtPromise).toBe(true);3425 eventuallyResolvesTo(promise, 'initial value', true);3426 });3427 it('Promise of an Array with one function', function() {3428 var fns = [createAppenderFn('a')];3429 promise = Deferred.pipeline(Deferred.resolved(fns));3430 expect(promise instanceof ExtPromise).toBe(true);3431 eventuallyResolvesTo(promise, 'a', true);3432 });3433 it('Promise of an Array with one function with an initial value', function() {3434 var fns = [createAppenderFn('b')];3435 promise = Deferred.pipeline(Deferred.resolved(fns), 'a');3436 expect(promise instanceof ExtPromise).toBe(true);3437 eventuallyResolvesTo(promise, 'ab', true);3438 });3439 it('Promise of an Array with one function with an initial value and scope', function() {3440 var fns = [verifyScope(createAppenderFn('b'), targetScope)];3441 promise = Deferred.pipeline(Deferred.resolved(fns), 'a', targetScope);3442 expect(promise instanceof ExtPromise).toBe(true);3443 eventuallyResolvesTo(promise, 'ab', true);3444 });3445 it('Promise of an Array of two functions', function() {3446 var fns = [createAppenderFn('a'), createAppenderFn('b')];3447 promise = Deferred.pipeline(Deferred.resolved(fns));3448 expect(promise instanceof ExtPromise).toBe(true);3449 eventuallyResolvesTo(promise, 'ab', true);3450 });3451 it('Promise of an Array of two functions with an initial value', function() {3452 var fns = [createAppenderFn('b'), createAppenderFn('c')];3453 promise = Deferred.pipeline(Deferred.resolved(fns), 'a');3454 expect(promise instanceof ExtPromise).toBe(true);3455 eventuallyResolvesTo(promise, 'abc', true);3456 });3457 it('Promise of an Array of two functions with an initial value and scope', function() {3458 var fns = [verifyScope(createAppenderFn('b'), targetScope), verifyScope(createAppenderFn('c'), targetScope)];3459 promise = Deferred.pipeline(Deferred.resolved(fns), 'a', targetScope);3460 expect(promise instanceof ExtPromise).toBe(true);3461 eventuallyResolvesTo(promise, 'abc', true);3462 });3463 it('Promise of an Array of three functions', function() {3464 var fns = [createAppenderFn('a'), createAppenderFn('b'), createAppenderFn('c')];3465 promise = Deferred.pipeline(Deferred.resolved(fns));3466 expect(promise instanceof ExtPromise).toBe(true);3467 eventuallyResolvesTo(promise, 'abc', true);3468 });3469 it('Promise of an Array of three functions with an initial value', function() {3470 var fns = [createAppenderFn('b'), createAppenderFn('c'), createAppenderFn('d')];3471 promise = Deferred.pipeline(Deferred.resolved(fns), 'a');3472 expect(promise instanceof ExtPromise).toBe(true);3473 eventuallyResolvesTo(promise, 'abcd', true);3474 });3475 it('Promise of an Array of three functions with an initial value', function() {3476 var fns = [verifyScope(createAppenderFn('b'), targetScope), verifyScope(createAppenderFn('c'), targetScope), verifyScope(createAppenderFn('d'), targetScope)];3477 promise = Deferred.pipeline(Deferred.resolved(fns), 'a', targetScope);3478 expect(promise instanceof ExtPromise).toBe(true);3479 eventuallyResolvesTo(promise, 'abcd', true);3480 });3481 });3482 describe('returns a new Promise that will reject with the Error associated with the specified rejected Promise of an Array of functions', function() {3483 it('Error: error message', function() {3484 promise = Deferred.pipeline(Deferred.rejected(new Error('error message')));3485 expect(promise instanceof ExtPromise).toBe(true);3486 eventuallyRejectedWith(promise, Error, 'error message');3487 });3488 });3489 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions throws an Error', function() {3490 function brokenFn () {3491 throw new Error('Error message');3492 }3493 it('Array with one function that throws an Error', function() {3494 var fns = [brokenFn];3495 promise = Deferred.pipeline(fns);3496 expect(promise instanceof ExtPromise).toBe(true);3497 eventuallyRejectedWith(promise, Error, 'Error message');3498 });3499 it('Array with one function and one function that throws an Error', function() {3500 var fns = [createAppenderFn('a'), brokenFn];3501 promise = Deferred.pipeline(fns);3502 expect(promise instanceof ExtPromise).toBe(true);3503 eventuallyRejectedWith(promise, Error, 'Error message');3504 });3505 it('Array with two functions and one function that throws an Error', function() {3506 var fns = [createAppenderFn('a'), createAppenderFn('b'), brokenFn];3507 promise = Deferred.pipeline(fns);3508 expect(promise instanceof ExtPromise).toBe(true);3509 eventuallyRejectedWith(promise, Error, 'Error message');3510 });3511 });3512 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions throws an Error', function() {3513 function brokenFn () {3514 throw new Error('Error message');3515 }3516 it('Promise of an Array with one function that throws an Error', function() {3517 var fns = [brokenFn];3518 promise = Deferred.pipeline(Deferred.resolved(fns));3519 expect(promise instanceof ExtPromise).toBe(true);3520 eventuallyRejectedWith(promise, Error, 'Error message');3521 });3522 it('Promise of an Array with one function and one function that throws an Error', function() {3523 var fns = [createAppenderFn('a'), brokenFn];3524 promise = Deferred.pipeline(Deferred.resolved(fns));3525 expect(promise instanceof ExtPromise).toBe(true);3526 eventuallyRejectedWith(promise, Error, 'Error message');3527 });3528 it('Promise of an Array with two functions and one function that throws an Error', function() {3529 var fns = [createAppenderFn('a'), createAppenderFn('b'), brokenFn];3530 promise = Deferred.pipeline(Deferred.resolved(fns));3531 expect(promise instanceof ExtPromise).toBe(true);3532 eventuallyRejectedWith(promise, Error, 'Error message');3533 });3534 });3535 describe('returns a new Promise that will reject with the associated Error if any of the specified Array of functions returns a rejected Promise', function() {3536 function rejectFn () {3537 return Deferred.rejected(new Error('Error message'));3538 }3539 it('Array with one function that returns a rejected Promise', function() {3540 var fns = [rejectFn];3541 promise = Deferred.pipeline(fns);3542 expect(promise instanceof ExtPromise).toBe(true);3543 eventuallyRejectedWith(promise, Error, 'Error message');3544 });3545 it('Array with one function and one function that returns a rejected Promise', function() {3546 var fns = [createAppenderFn('a'), rejectFn];3547 promise = Deferred.pipeline(fns);3548 expect(promise instanceof ExtPromise).toBe(true);3549 eventuallyRejectedWith(promise, Error, 'Error message');3550 });3551 it('Array with two functions and one function that returns a rejected Promise', function() {3552 var fns = [createAppenderFn('a'), createAppenderFn('b'), rejectFn];3553 promise = Deferred.pipeline(fns);3554 expect(promise instanceof ExtPromise).toBe(true);3555 eventuallyRejectedWith(promise, Error, 'Error message');3556 });3557 });3558 describe('returns a new Promise that will reject with the associated Error if any of the specified Promise of an Array of functions returns a rejected Promise', function() {3559 function rejectFn () {3560 return Deferred.rejected(new Error('Error message'));3561 }3562 it('Array with one function that returns a rejected Promise', function() {3563 var fns = [rejectFn];3564 promise = Deferred.pipeline(Deferred.resolved(fns));3565 expect(promise instanceof ExtPromise).toBe(true);3566 eventuallyRejectedWith(promise, Error, 'Error message');3567 });3568 it('Array with one function and one function that returns a rejected Promise', function() {3569 var fns = [createAppenderFn('a'), rejectFn];3570 promise = Deferred.pipeline(Deferred.resolved(fns));3571 expect(promise instanceof ExtPromise).toBe(true);3572 eventuallyRejectedWith(promise, Error, 'Error message');3573 });3574 it('Array with two functions and one function that returns a rejected Promise', function() {3575 var fns = [createAppenderFn('a'), createAppenderFn('b'), rejectFn];3576 promise = Deferred.pipeline(Deferred.resolved(fns));3577 expect(promise instanceof ExtPromise).toBe(true);3578 eventuallyRejectedWith(promise, Error, 'Error message');3579 });3580 });3581 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified Array is not a function', function() {3582 it('Array with one non-function value', function() {3583 var fns = [1];3584 promise = Deferred.pipeline(fns);3585 expect(promise instanceof ExtPromise).toBe(true);3586 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3587 });3588 it('Array with one function and one non-function value', function() {3589 var fns = [createAppenderFn('a'), 1];3590 promise = Deferred.pipeline(fns);3591 expect(promise instanceof ExtPromise).toBe(true);3592 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3593 });3594 it('Array with two functions and one non-function value', function() {3595 var fns = [createAppenderFn('a'), createAppenderFn('b'), 1];3596 promise = Deferred.pipeline(fns);3597 expect(promise instanceof ExtPromise).toBe(true);3598 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3599 });3600 });3601 describe('returns a new Promise that will reject with the associated Error if any of the items in the specified resolved Promise of an Array is not a function ', function() {3602 it('Promise of an Array with one non-function value', function() {3603 var fns = [1];3604 promise = Deferred.pipeline(Deferred.resolved(fns));3605 expect(promise instanceof ExtPromise).toBe(true);3606 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3607 });3608 it('Promise of an Array with one function and one non-function value', function() {3609 var fns = [createAppenderFn('a'), 1];3610 promise = Deferred.pipeline(Deferred.resolved(fns));3611 expect(promise instanceof ExtPromise).toBe(true);3612 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3613 });3614 it('Promise of an Array with two functions and one non-function value', function() {3615 var fns = [createAppenderFn('a'), createAppenderFn('b'), 1];3616 promise = Deferred.pipeline(Deferred.resolved(fns));3617 expect(promise instanceof ExtPromise).toBe(true);3618 eventuallyRejectedWith(promise, Error, 'Invalid parameter: expected a function.');3619 });3620 });3621 describe('throws an Error if anything other than Array or Promise of an Array is specified as the first parameter', function() {3622 it('No parameters', function() {3623 expect(function() {3624 return Deferred.pipeline();3625 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');3626 });3627 it('A non-Array parameter', function() {3628 expect(function() {3629 return Deferred.pipeline(1);3630 }).toThrow('Invalid parameter: expected an Array or Promise of an Array.');3631 });3632 });3633 }); // pipeline3634 }); // Extras...

Full Screen

Full Screen

plot_promise_test.js

Source:plot_promise_test.js Github

copy

Full Screen

1var Plotly = require('@lib/index');2var Events = require('@src/lib/events');3var createGraphDiv = require('../assets/create_graph_div');4var destroyGraphDiv = require('../assets/destroy_graph_div');5describe('Plotly.___ methods', function() {6 'use strict';7 afterEach(destroyGraphDiv);8 describe('Plotly.plot promise', function() {9 var promise,10 promiseGd;11 beforeEach(function(done) {12 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }];13 promise = Plotly.plot(createGraphDiv(), data, {});14 promise.then(function(gd) {15 promiseGd = gd;16 done();17 });18 });19 it('should be returned with the graph div as an argument', function() {20 expect(promiseGd).toBeDefined();21 expect(typeof promiseGd).toBe('object');22 expect(promiseGd.data).toBeDefined();23 expect(promiseGd.layout).toBeDefined();24 });25 });26 describe('Plotly.plot promise', function() {27 var gd,28 promise,29 promiseRejected = false;30 beforeEach(function(done) {31 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }];32 gd = createGraphDiv();33 Events.init(gd);34 gd.on('plotly_beforeplot', function() {35 return false;36 });37 promise = Plotly.plot(gd, data, {});38 promise.then(null, function() {39 promiseRejected = true;40 done();41 });42 });43 it('should be rejected when plotly_beforeplot event handlers return false', function() {44 expect(promiseRejected).toBe(true);45 });46 });47 describe('Plotly.plot promise', function() {48 var gd,49 promise,50 promiseRejected = false;51 beforeEach(function(done) {52 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }];53 gd = createGraphDiv();54 gd._dragging = true;55 promise = Plotly.plot(gd, data, {});56 promise.then(null, function() {57 promiseRejected = true;58 done();59 });60 });61 it('should reject the promise when graph is being dragged', function() {62 expect(promiseRejected).toBe(true);63 });64 });65 describe('Plotly.redraw promise', function() {66 var promise,67 promiseGd;68 beforeEach(function(done) {69 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],70 initialDiv = createGraphDiv();71 Plotly.plot(initialDiv, data, {});72 promise = Plotly.redraw(initialDiv);73 promise.then(function(gd) {74 promiseGd = gd;75 done();76 });77 });78 it('should be returned with the graph div as an argument', function() {79 expect(promiseGd).toBeDefined();80 expect(typeof promiseGd).toBe('object');81 expect(promiseGd.data).toBeDefined();82 expect(promiseGd.layout).toBeDefined();83 });84 });85 describe('Plotly.newPlot promise', function() {86 var promise,87 promiseGd;88 beforeEach(function(done) {89 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }];90 promise = Plotly.newPlot(createGraphDiv(), data, {});91 promise.then(function(gd) {92 promiseGd = gd;93 done();94 });95 });96 it('should be returned with the graph div as an argument', function() {97 expect(promiseGd).toBeDefined();98 expect(typeof promiseGd).toBe('object');99 expect(promiseGd.data).toBeDefined();100 expect(promiseGd.layout).toBeDefined();101 });102 });103 describe('Plotly.extendTraces promise', function() {104 var promise,105 promiseGd;106 beforeEach(function(done) {107 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],108 initialDiv = createGraphDiv();109 Plotly.plot(initialDiv, data, {});110 promise = Plotly.extendTraces(initialDiv, { y: [[2]] }, [0], 3);111 promise.then(function(gd) {112 promiseGd = gd;113 done();114 });115 });116 it('should be returned with the graph div as an argument', function() {117 expect(promiseGd).toBeDefined();118 expect(typeof promiseGd).toBe('object');119 expect(promiseGd.data).toBeDefined();120 expect(promiseGd.layout).toBeDefined();121 });122 });123 describe('Plotly.prependTraces promise', function() {124 var promise,125 promiseGd;126 beforeEach(function(done) {127 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],128 initialDiv = createGraphDiv();129 Plotly.plot(initialDiv, data, {});130 promise = Plotly.prependTraces(initialDiv, { y: [[2]] }, [0], 3);131 promise.then(function(gd) {132 promiseGd = gd;133 done();134 });135 });136 it('should be returned with the graph div as an argument', function() {137 expect(promiseGd).toBeDefined();138 expect(typeof promiseGd).toBe('object');139 expect(promiseGd.data).toBeDefined();140 expect(promiseGd.layout).toBeDefined();141 });142 });143 describe('Plotly.addTraces promise', function() {144 var promise,145 promiseGd;146 beforeEach(function(done) {147 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],148 initialDiv = createGraphDiv();149 Plotly.plot(initialDiv, data, {});150 promise = Plotly.addTraces(initialDiv, [{ x: [1, 2, 3], y: [1, 2, 3] }], [1]);151 promise.then(function(gd) {152 promiseGd = gd;153 done();154 });155 });156 it('should be returned with the graph div as an argument', function() {157 expect(promiseGd).toBeDefined();158 expect(typeof promiseGd).toBe('object');159 expect(promiseGd.data).toBeDefined();160 expect(promiseGd.layout).toBeDefined();161 });162 });163 describe('Plotly.deleteTraces promise', function() {164 var promise,165 promiseGd;166 beforeEach(function(done) {167 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],168 initialDiv = createGraphDiv();169 Plotly.plot(initialDiv, data, {});170 promise = Plotly.deleteTraces(initialDiv, [0]);171 promise.then(function(gd) {172 promiseGd = gd;173 done();174 });175 });176 it('should be returned with the graph div as an argument', function() {177 expect(promiseGd).toBeDefined();178 expect(typeof promiseGd).toBe('object');179 expect(promiseGd.data).toBeDefined();180 expect(promiseGd.layout).toBeDefined();181 });182 });183 describe('Plotly.deleteTraces promise', function() {184 var promise,185 promiseGd;186 beforeEach(function(done) {187 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],188 initialDiv = createGraphDiv();189 Plotly.plot(initialDiv, data, {});190 promise = Plotly.deleteTraces(initialDiv, [0]);191 promise.then(function(gd) {192 promiseGd = gd;193 done();194 });195 });196 it('should be returned with the graph div as an argument', function() {197 expect(promiseGd).toBeDefined();198 expect(typeof promiseGd).toBe('object');199 expect(promiseGd.data).toBeDefined();200 expect(promiseGd.layout).toBeDefined();201 });202 });203 describe('Plotly.moveTraces promise', function() {204 var promise,205 promiseGd;206 beforeEach(function(done) {207 var data = [208 { x: [1, 2, 3], y: [4, 5, 6] },209 { x: [1, 2, 3], y: [6, 5, 4] }210 ],211 initialDiv = createGraphDiv();212 Plotly.plot(initialDiv, data, {});213 promise = Plotly.moveTraces(initialDiv, 0, 1);214 promise.then(function(gd) {215 promiseGd = gd;216 done();217 });218 });219 it('should be returned with the graph div as an argument', function() {220 expect(promiseGd).toBeDefined();221 expect(typeof promiseGd).toBe('object');222 expect(promiseGd.data).toBeDefined();223 expect(promiseGd.layout).toBeDefined();224 });225 });226 describe('Plotly.restyle promise', function() {227 var promise,228 promiseGd;229 beforeEach(function(done) {230 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],231 initialDiv = createGraphDiv();232 Plotly.plot(initialDiv, data, {});233 promise = Plotly.restyle(initialDiv, 'marker.color', 'rgb(255,0,0)');234 promise.then(function(gd) {235 promiseGd = gd;236 done();237 });238 });239 it('should be returned with the graph div as an argument', function() {240 expect(promiseGd).toBeDefined();241 expect(typeof promiseGd).toBe('object');242 expect(promiseGd.data).toBeDefined();243 expect(promiseGd.layout).toBeDefined();244 });245 });246 describe('Plotly.restyle promise', function() {247 var promise,248 promiseRejected = false;249 beforeEach(function(done) {250 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],251 initialDiv = createGraphDiv();252 Plotly.plot(initialDiv, data, {});253 promise = Plotly.restyle(initialDiv, undefined, '');254 promise.then(null, function() {255 promiseRejected = true;256 done();257 });258 });259 it('should be rejected when the attribute is missing', function() {260 expect(promiseRejected).toBe(true);261 });262 });263 describe('Plotly.relayout promise', function() {264 var promise,265 promiseGd;266 beforeEach(function(done) {267 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],268 layout = {hovermode: 'closest'},269 initialDiv = createGraphDiv();270 Plotly.plot(initialDiv, data, layout);271 promise = Plotly.relayout(initialDiv, 'hovermode', false);272 promise.then(function(gd) {273 promiseGd = gd;274 done();275 });276 });277 it('should be returned with the graph div as an argument', function() {278 expect(promiseGd).toBeDefined();279 expect(typeof promiseGd).toBe('object');280 expect(promiseGd.data).toBeDefined();281 expect(promiseGd.layout).toBeDefined();282 });283 });284 describe('Plotly.relayout promise', function() {285 var promise,286 promiseGd;287 beforeEach(function(done) {288 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],289 layout = {hovermode: 'closest'},290 initialDiv = createGraphDiv();291 Plotly.plot(initialDiv, data, layout);292 promise = Plotly.relayout(initialDiv, 'hovermode', false);293 promise.then(function(gd) {294 promiseGd = gd;295 done();296 });297 });298 it('should be returned with the graph div as an argument', function() {299 expect(promiseGd).toBeDefined();300 expect(typeof promiseGd).toBe('object');301 expect(promiseGd.data).toBeDefined();302 expect(promiseGd.layout).toBeDefined();303 });304 });305 describe('Plotly.relayout promise', function() {306 var promise,307 promiseGd;308 beforeEach(function(done) {309 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],310 layout = {hovermode: 'closest'},311 initialDiv = createGraphDiv();312 Plotly.plot(initialDiv, data, layout);313 initialDiv.framework = { isPolar: true };314 promise = Plotly.relayout(initialDiv, 'hovermode', false);315 promise.then(function(gd) {316 promiseGd = gd;317 done();318 });319 });320 it('should be returned with the graph div unchanged when the framework is polar', function() {321 expect(promiseGd).toBeDefined();322 expect(typeof promiseGd).toBe('object');323 expect(promiseGd.changed).toBeFalsy();324 });325 });326 describe('Plotly.relayout promise', function() {327 var promise,328 promiseRejected = false;329 beforeEach(function(done) {330 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }],331 layout = {hovermode: 'closest'},332 initialDiv = createGraphDiv();333 Plotly.plot(initialDiv, data, layout);334 promise = Plotly.relayout(initialDiv, undefined, false);335 promise.then(null, function() {336 promiseRejected = true;337 done();338 });339 });340 it('should be rejected when the attribute is missing', function() {341 expect(promiseRejected).toBe(true);342 });343 });344 describe('Plotly.Plots.resize promise', function() {345 var initialDiv;346 beforeEach(function(done) {347 var data = [{ x: [1, 2, 3], y: [4, 5, 6] }];348 initialDiv = createGraphDiv();349 Plotly.plot(initialDiv, data, {}).then(done);350 });351 it('should return a resolved promise of the gd', function(done) {352 Plotly.Plots.resize(initialDiv).then(function(gd) {353 expect(gd).toBeDefined();354 expect(typeof gd).toBe('object');355 expect(gd.layout).toBeDefined();356 }).then(done);357 });358 it('should return a rejected promise with no argument', function(done) {359 Plotly.Plots.resize().then(null, function(err) {360 expect(err).toBeDefined();361 expect(err.message).toBe('Resize must be passed a plot div element.');362 }).then(done);363 });364 });...

Full Screen

Full Screen

promise-spec.js

Source:promise-spec.js Github

copy

Full Screen

1describe("KISSY.Defer", function () {2 var S = KISSY,3 Promise = S.Promise;4 it("works for simple value", function () {5 var r, r2;6 Promise.when(1,7 function (v) {8 r = v;9 return r + 1;10 }).then(function (v) {11 r2 = v;12 });13 waitsFor(function () {14 return r == 1 && r2 == 2;15 }, 100);16 });17 it("works simply when fulfilled", function () {18 var d = S.Defer(),19 p = d.promise,20 r;21 expect(Promise.isPromise(d)).toBe(false);22 expect(Promise.isPromise(p)).toBe(true);23 expect(Promise.isResolved(p)).toBe(false);24 expect(Promise.isRejected(p)).toBe(false);25 p.then(function (v) {26 r = v;27 });28 waits(100);29 runs(function () {30 d.resolve(1);31 });32 waits(100);33 runs(function () {34 expect(r).toBe(1);35 expect(Promise.isResolved(p)).toBe(true);36 });37 });38 it("can access value after resolved", function () {39 var d = S.Defer(),40 r,41 p = d.promise;42 d.resolve(1);43 waits(100);44 runs(function () {45 expect(Promise.isResolved(p)).toBe(true);46 p.then(function (v) {47 r = v;48 });49 });50 waits(100);51 runs(function () {52 expect(r).toBe(1);53 expect(Promise.isResolved(p)).toBe(true);54 });55 });56 it("can access error after resolved", function () {57 var d = S.Defer(),58 r,59 p = d.promise;60 d.reject(1);61 waits(100);62 runs(function () {63 expect(Promise.isResolved(p)).toBe(false);64 expect(Promise.isRejected(p)).toBe(true);65 p.fail(function (v) {66 r = v;67 });68 });69 waits(100);70 runs(function () {71 expect(r).toBe(1);72 expect(Promise.isResolved(p)).toBe(false);73 expect(Promise.isRejected(p)).toBe(true);74 });75 });76 it("can transform returned value by chained promise", function () {77 var d = S.Defer(),78 p = d.promise,79 r;80 p.then(81 function (v) {82 return v + 1;83 }).then(function (v) {84 r = v;85 });86 waits(100);87 runs(function () {88 d.resolve(1);89 });90 waits(100);91 runs(function () {92 expect(r).toBe(2);93 });94 });95 it("should support promise chained promise", function () {96 var defer = S.Defer(),97 p = defer.promise,98 p2,99 v1, v2;100 p2 = p.then(101 function (v) {102 v1 = v;103 var d2 = S.Defer();104 setTimeout(function () {105 d2.resolve(1);106 }, 50);107 return d2.promise;108 }).then(109 function (v) {110 v2 = v;111 });112 waits(100);113 runs(function () {114 defer.resolve(2);115 });116 waits(20);117 runs(function () {118 expect(Promise.isResolved(p)).toBe(true);119 // p2 is waiting for d2120 expect(Promise.isResolved(p2)).toBe(false);121 });122 waits(100);123 runs(function () {124 expect(v1).toBe(2);125 expect(v2).toBe(1);126 expect(Promise.isResolved(p)).toBe(true);127 expect(Promise.isResolved(p2)).toBe(true);128 });129 });130 it("should propagate error reason", function () {131 var d = S.Defer(),132 order = [],133 p = d.promise;134 var p2 = p.then(135 function (v) {136 order.push("e1 :" + v);137 throw "e1";138 },139 function (r) {140 order.push("e2 :" + r);141 return "e2";142 });143 var p3 = p2.then(144 function (v) {145 order.push("e3 :" + v);146 throw "e3";147 },148 function (r) {149 order.push("e4 :" + r);150 throw "e4";151 });152 var p4 = p3.then(function (v) {153 order.push("e5 :" + v);154 throw "e5";155 }, function (r) {156 order.push("e6 :" + r);157 throw "e6";158 });159 waits(100);160 runs(function () {161 d.resolve(1);162 });163 waits(50);164 runs(function () {165 expect(Promise.isRejected(p)).toBe(false);166 expect(Promise.isResolved(p)).toBe(true);167 expect(Promise.isRejected(p2)).toBe(true);168 expect(Promise.isResolved(p2)).toBe(false);169 // p2 rethrow170 expect(Promise.isRejected(p3)).toBe(true);171 expect(Promise.isResolved(p3)).toBe(false);172 });173 waits(100);174 runs(function () {175 expect(order).toEqual(['e1 :1', 'e4 :e1', 'e6 :e4'])176 });177 });178 it("should support error recovery", function () {179 var d = S.Defer(),180 order = [],181 p = d.promise;182 var p2 = p.then(183 function (v) {184 order.push("e1 :" + v);185 throw "e1";186 },187 function (r) {188 order.push("e2 :" + r);189 return "e2";190 });191 var p3 = p2.then(192 function (v) {193 order.push("e3 :" + v);194 throw "e3";195 },196 function (r) {197 order.push("e4 :" + r);198 return "e4";199 });200 var p4 = p3.then(function (v) {201 order.push("e5 :" + v);202 throw "e5";203 }, function (r) {204 order.push("e6 :" + r);205 throw "e6";206 });207 waits(100);208 runs(function () {209 d.resolve(1);210 });211 waits(50);212 runs(function () {213 expect(Promise.isRejected(p)).toBe(false);214 expect(Promise.isResolved(p)).toBe(true);215 expect(Promise.isRejected(p2)).toBe(true);216 expect(Promise.isResolved(p2)).toBe(false);217 // p2.error recovery218 expect(Promise.isRejected(p3)).toBe(false);219 expect(Promise.isResolved(p3)).toBe(true);220 });221 waits(100);222 runs(function () {223 expect(order).toEqual(['e1 :1', 'e4 :e1', 'e5 :e4'])224 });225 });226 it("should propagate error reason by default", function () {227 var d = S.Defer(),228 order = [],229 p = d.promise;230 var p2 = p.then(231 function (v) {232 order.push("e1 :" + v);233 throw "e1";234 });235 var p3 = p2.then(236 function (v) {237 order.push("e3 :" + v);238 throw "e3";239 });240 var p4 = p3.then(function (v) {241 order.push("e5 :" + v);242 throw "e5";243 }, function (r) {244 order.push("e6 :" + r);245 throw "e6";246 });247 waits(100);248 runs(function () {249 d.resolve(1);250 });251 waits(100);252 runs(function () {253 expect(order).toEqual(['e1 :1', 'e6 :e1'])254 });255 });256 it("all works", function () {257 var defer1 = S.Defer();258 var defer2 = S.Defer();259 var r = [];260 var p = Promise.all([defer1.promise, defer2.promise]);261 p.then(function (v) {262 r = v;263 });264 waits(50);265 runs(function () {266 defer1.resolve(1);267 });268 waits(50);269 runs(function () {270 expect(Promise.isResolved(defer1.promise)).toBe(true);271 expect(Promise.isResolved(defer2.promise)).toBe(false);272 expect(r).toEqual([]);273 expect(Promise.isResolved(p)).toBe(false);274 });275 waits(50);276 runs(function () {277 defer2.resolve(2);278 });279 waits(50);280 runs(function () {281 expect(Promise.isResolved(defer1.promise)).toBe(true);282 expect(Promise.isResolved(defer2.promise)).toBe(true);283 expect(r).toEqual([1, 2]);284 expect(Promise.isResolved(p)).toBe(true);285 });286 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-promise'));4const chai = require('chai');5const chaiAsPromised = require('chai-as-promised');6chai.use(chaiAsPromised);7const assert = require('assert');8const assertAsPromised = require('assert-as-promised');9assertAsPromised(assert);10describe('promise', function () {11 it('should be resolved', function () {12 return expect.promise(function (run) {13 run(function () {14 return Promise.resolve('foo');15 });16 }).to.be.fulfilled();17 });18 it('should be rejected', function () {19 return expect.promise(function (run) {20 run(function () {21 return Promise.reject(new Error('foo'));22 });23 }).to.be.rejectedWith('foo');24 });25 it('should be resolved with chai-as-promised', function () {26 return chai.expect(Promise.resolve('foo')).to.be.fulfilled;27 });28 it('should be rejected with chai-as-promised', function () {29 return chai.expect(Promise.reject(new Error('foo'))).to.be.rejectedWith('foo');30 });31 it('should be resolved with assert-as-promised', function () {32 return assertAsPromised(Promise.resolve('foo'));33 });34 it('should be rejected with assert-as-promised', function () {35 return assertAsPromised(Promise.reject(new Error('foo')));36 });37});38{39 "scripts": {40 },41 "dependencies": {42 }43}

Full Screen

Using AI Code Generation

copy

Full Screen

1var expect = require('unexpected').clone().use(require('unexpected-promise'));2expect.addAssertion('<any> to be fulfilled with <any>', function (expect, subject, value) {3 return expect(subject, 'to be fulfilled with', value);4});5expect.addAssertion('<any> to be fulfilled with <any>', function (expect, subject, value) {6 return expect(subject, 'to be fulfilled with', value);7});8expect.addAssertion('<any> to be rejected with <any>', function (expect, subject, value) {9 return expect(subject, 'to be rejected with', value);10});11expect.addAssertion('<any> to be rejected with <any>', function (expect, subject, value) {12 return expect(subject, 'to be rejected with', value);13});14it('should return 1', function () {15 var promise = Promise.resolve(1);16 return expect(promise, 'to be fulfilled with', 1);17});18it('should return 1', function () {19 var promise = Promise.resolve(1);20 return expect(promise, 'to be fulfilled with', 1);21});22it('should return 1', function () {23 var promise = Promise.resolve(1);24 return expect(promise, 'to be fulfilled with', 1);25});26it('should return 1', function () {27 var promise = Promise.resolve(1);28 return expect(promise, 'to be fulfilled with', 1);29});30it('should return 1', function () {31 var promise = Promise.resolve(1);32 return expect(promise, 'to be fulfilled with', 1);33});34it('should return 1', function () {35 var promise = Promise.resolve(1);36 return expect(promise, 'to be fulfilled with', 1);37});38it('should return 1', function () {39 var promise = Promise.resolve(1);40 return expect(promise, 'to be fulfilled with', 1);41});42it('should return 1', function () {43 var promise = Promise.resolve(1);44 return expect(promise, 'to be fulfilled with', 1);45});46it('should return 1', function () {47 var promise = Promise.resolve(1);48 return expect(promise, 'to be fulfilled with', 1);49});50it('should return 1', function () {51 var promise = Promise.resolve(1);

Full Screen

Using AI Code Generation

copy

Full Screen

1var expect = require('unexpected').clone().use(require('unexpected-promise'));2var chai = require('chai');3chai.use(require('chai-as-promised'));4var chaiAsPromised = require('chai-as-promised');5chaiAsPromised.transferPromiseness = function (assertion, promise) {6 assertion.then = promise.then.bind(promise);7 assertion.catch = promise.catch.bind(promise);8 assertion.finally = promise.finally.bind(promise);9};10var expectChai = require('unexpected').clone().use(require('unexpected-chai'));11expectChai.use(chaiAsPromised);12chai.use(chaiAsPromised);13expect.promise('to be fulfilled', function (run) {14 run(function () {15 return new Promise(function (resolve, reject) {16 setTimeout(function () {17 resolve('test');18 }, 1000);19 });20 });21});22expectChai('test').to.be.fulfilled;23chai.expect('test').to.be.fulfilled;

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected').clone().use(require('unexpected-promise'));2describe('test', function () {3 it('should succeed', function () {4 return expect('foo', 'to equal', 'foo');5 });6 it('should fail', function () {7 return expect('foo', 'to equal', 'bar');8 });9});10const expect = require('unexpected').clone().use(require('unexpected-promise'));11describe('test', function () {12 it('should succeed', function () {13 return expect(Promise.reject(new Error('foo')), 'to be rejected with', new Error('foo'));14 });15 it('should fail', function () {16 return expect(Promise.reject(new Error('foo')), 'to be rejected with', new Error('bar'));17 });18});19AssertionError: expected Promise { <rejected> Error: foo } to be rejected with Error: bar20AssertionError: expected Promise { <rejected> Error: foo } to be rejected with Error: bar21const expect = require('unexpected').clone().use(require('unexpected-promise'));22describe('test', function () {23 it('should succeed', function () {24 return expect(Promise.reject

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run unexpected automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful