1 /*global describe,it */
6 var assert = require('assert');
7 var MPromise = require('../');
13 describe('promise', function () {
14 it('events fire right after fulfill()', function (done) {
15 var promise = new MPromise()
18 promise.on('fulfill', function (a, b) {
24 promise.fulfill('1', '2');
26 promise.on('fulfill', function (a, b) {
32 assert.equal(2, called);
36 it('events fire right after reject()', function (done) {
37 var promise = new MPromise()
40 promise.on('reject', function (err) {
41 assert.ok(err instanceof Error);
45 promise.reject(new Error('booyah'));
47 promise.on('reject', function (err) {
48 assert.ok(err instanceof Error);
52 assert.equal(2, called);
56 describe('onResolve()', function () {
57 it('from constructor works', function (done) {
60 var promise = new MPromise(function (err) {
61 assert.ok(err instanceof Error);
65 promise.reject(new Error('dawg'));
67 assert.equal(1, called);
71 it('after fulfill()', function (done) {
72 var promise = new MPromise()
75 promise.fulfill('woot');
77 promise.onResolve(function (err, data) {
78 assert.equal(data, 'woot');
82 promise.onResolve(function (err) {
83 assert.strictEqual(err, null);
87 assert.equal(2, called);
92 describe('onFulfill shortcut', function () {
93 it('works', function (done) {
94 var promise = new MPromise()
97 promise.onFulfill(function (woot) {
98 assert.strictEqual(woot, undefined);
104 assert.equal(1, called);
109 describe('onReject shortcut', function () {
110 it('works', function (done) {
111 var promise = new MPromise()
114 promise.onReject(function (err) {
115 assert.ok(err instanceof Error);
119 promise.reject(new Error);
120 assert.equal(1, called);
125 describe('return values', function () {
126 it('on()', function (done) {
127 var promise = new MPromise();
128 assert.ok(promise.on('jump', function () {}) instanceof MPromise);
132 it('onFulfill()', function (done) {
133 var promise = new MPromise();
134 assert.ok(promise.onFulfill(function () {}) instanceof MPromise);
137 it('onReject()', function (done) {
138 var promise = new MPromise();
139 assert.ok(promise.onReject(function () {}) instanceof MPromise);
142 it('onResolve()', function (done) {
143 var promise = new MPromise();
144 assert.ok(promise.onResolve(function () {}) instanceof MPromise);
149 describe('casting errors', function () {
150 describe('reject()', function () {
151 it('does not cast arguments to Error', function (done) {
152 var p = new MPromise(function (err) {
153 assert.equal(3, err);
162 describe('then', function () {
163 describe('catching', function () {
164 it('should not catch returned promise fulfillments', function (done) {
167 p.then(function () { throw errorSentinal = new Error("boo!") });
174 it('should not catch returned promise fulfillments even async', function (done) {
177 p.then(function () { throw errorSentinal = new Error("boo!") });
179 setTimeout(function () {
186 it('can be disabled using .end()', function (done) {
187 if (process.version.indexOf('v0.8') == 0) return done();
190 , domain = require('domain').create();
192 domain.once('error', function (err) {
193 assert(err, errorSentinal);
194 clearTimeout(overTimeout);
198 domain.run(function () {
199 var p = new MPromise;
200 var p2 = p.then(function () {
201 throw errorSentinal = new Error('shucks')
207 overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 10);
211 it('can be disabled using .end() even when async', function (done) {
212 if (process.version.indexOf('v0.10') != 0) return done();
215 , domain = require('domain').create();
217 domain.on('error', function (err) {
218 assert(err, errorSentinal);
219 clearTimeout(overTimeout);
223 domain.run(function () {
224 var p = new MPromise;
225 var p2 = p.then(function () {
226 throw errorSentinal = new Error("boo!")
230 setTimeout(function () {p.fulfill();}, 10);
232 overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 20);
236 it('can be handled using .end() so no throwing', function (done) {
239 , domain = require('domain').create();
241 domain.run(function () {
242 var p = new MPromise;
243 var p2 = p.then(function () {
244 throw errorSentinal = new Error("boo!")
246 p2.end(function (err) {
247 assert.equal(err, errorSentinal);
248 clearTimeout(overTimeout);
252 setTimeout(function () {p.fulfill();}, 10);
254 overTimeout = setTimeout(function () { done(new Error('error was swallowed')); }, 20);
259 it('persistent', function (done) {
260 var p = new MPromise;
263 function ensure(val) {
265 assert.equal(v, val);
269 throw new Error('onReject should not be called');
272 p.then(ensure, guard).end();
276 p.reject(new Error('baz'));
278 p.then(ensure, guard).end();
284 it('accepts multiple completion values', function (done) {
285 var p = new MPromise;
287 p.then(function (a, b) {
288 assert.equal(2, arguments.length);
289 assert.equal('hi', a);
298 describe('fulfill values and splats', function () {
299 it('should handle multiple values', function (done) {
300 var p = new MPromise;
301 p.onFulfill(function (a, b, c) {
302 assert.equal('a', a);
303 assert.equal('b', b);
304 assert.equal('c', c);
307 p.fulfill('a', 'b', 'c');
310 it('should handle multiple values from a then', function (done) {
311 MPromise.fulfilled().then(
313 return MPromise.fulfilled().then(
315 var p = new MPromise;
316 p.fulfill('a', 'b', 'c');
323 assert.equal('a', a);
324 assert.equal('b', b);
325 assert.equal('c', c);
331 it('should work with `fulfilled` convenience method', function (done) {
332 MPromise.fulfilled('a', 'b', 'c').then(function (a, b, c) {
333 assert.equal('a', a);
334 assert.equal('b', b);
335 assert.equal('c', c);
342 describe('end', function () {
343 it("should return the promise", function (done) {
344 var p = new MPromise;
351 it("should throw for chain", function (done) {
352 var p = new MPromise;
353 p.then().then().then().then().end();
362 it("should not throw for chain with reject handler", function (done) {
363 var p = new MPromise;
364 p.then().then().then().then().end(function () {
376 describe('chain', function () {
377 it('should propagate fulfillment', function (done) {
378 var varSentinel = {a: 'a'};
379 var p1 = new MPromise;
380 p1.chain(new MPromise(function (err, doc) {
381 assert.equal(doc, varSentinel);
384 p1.fulfill(varSentinel);
388 it('should propagate rejection', function (done) {
389 var e = new Error("gaga");
390 var p1 = new MPromise;
391 p1.chain(new MPromise(function (err) {
392 assert.equal(err, e);
399 it('should propagate resolution err', function (done) {
400 var e = new Error("gaga");
401 var p1 = new MPromise;
402 p1.chain(new MPromise(function (err) {
403 assert.equal(err, e);
410 it('should propagate resolution val', function (done) {
411 var varSentinel = {a: 'a'};
412 var p1 = new MPromise;
413 p1.chain(new MPromise(function (err, val) {
414 assert.equal(val, varSentinel);
417 p1.resolve(null, varSentinel);
422 describe("all", function () {
423 it("works", function (done) {
425 var p = new MPromise;
426 var p2 = p.all(function () {
429 var p = new MPromise();
435 var p = new MPromise();
442 p2.then(function () {
443 assert.equal(count, 2);
450 it("handles rejects", function (done) {
452 var p = new MPromise;
453 var p2 = p.all(function () {
456 var p = new MPromise();
463 throw new Error("gaga");
467 p2.onReject(function (err) {
468 assert(err.message, "gaga");
469 assert.equal(count, 2);
477 describe("deferred", function () {
478 it("works", function (done) {
479 var d = MPromise.deferred();
480 assert.ok(d.promise instanceof MPromise);
481 assert.ok(d.reject instanceof Function);
482 assert.ok(d.resolve instanceof Function);
483 assert.ok(d.callback instanceof Function);
489 describe("hook", function () {
490 it("works", function (done) {
492 var l1 = function (ser, par) {
497 MPromise.hook([l1, l1, l1]).then(function () {
505 it("works with async serial hooks", function (done) {
508 var l1 = function (ser, par) {
510 setTimeout(function () {ser();}, 200);
513 MPromise.hook([l1, l1, l1]).then(function () {
520 it("works with async parallel hooks", function (done) {
523 var l1 = function (ser, par) {
526 setTimeout(function () {par();}, 200);
528 MPromise.hook([l1, l1, l1]).then(function () {
535 it("catches errors in hook logic", function (done) {
537 var l1 = function (ser, par) {
542 var l2 = function (ser, par) {
546 throw new Error("err")
548 MPromise.hook([l1, l2, l1]).end(function (err) {