|
1 |
| -/* globals describe it Promise */ |
| 1 | +/* globals describe it */ |
2 | 2 | var expect = require('chai').expect
|
3 | 3 | var sinon = require('sinon')
|
4 |
| -var promise = require('../index') |
| 4 | +var Promise = require('../extra') |
5 | 5 |
|
6 | 6 | describe('async reject', function () {
|
7 |
| - it('should resolve values', function (done) { |
8 |
| - return promise(function (resolve, reject) { |
9 |
| - setTimeout(function () { |
10 |
| - reject(123) |
11 |
| - }, 0) |
12 |
| - }) |
13 |
| - .then(null, function (val) { |
14 |
| - expect(val).to.eql(123) |
15 |
| - done() |
16 |
| - }) |
| 7 | + it('should call reject', function () { |
| 8 | + var stub = sinon.stub() |
| 9 | + var d = Promise.defer() |
| 10 | + d.promise.then(null, stub) |
| 11 | + d.reject(123) |
| 12 | + expect(stub.calledWith(123)).to.eql(true) |
17 | 13 | })
|
18 |
| - it('should be chainable', function (done) { |
19 |
| - return promise(function (resolve, reject) { |
20 |
| - setTimeout(function () { |
21 |
| - reject(123) |
22 |
| - }, 0) |
23 |
| - }) |
24 |
| - .then(null, function () { |
25 |
| - return 234 |
26 |
| - }) |
27 |
| - .then(function (chained) { |
28 |
| - expect(chained).to.eql(234) |
29 |
| - done() |
30 |
| - }) |
| 14 | + it('should not call resolve', function () { |
| 15 | + var stub = sinon.stub() |
| 16 | + var d = Promise.defer() |
| 17 | + d.promise.then(stub, null) |
| 18 | + d.reject(123) |
| 19 | + expect(stub.called).to.eql(false) |
31 | 20 | })
|
32 |
| - it('should be really chainable', function (done) { |
33 |
| - return promise(function (resolve, reject) { |
34 |
| - setTimeout(function () { |
35 |
| - reject(123) |
36 |
| - }, 0) |
37 |
| - }) |
38 |
| - .then(null, null) |
39 |
| - .then(null, function () { |
40 |
| - return 234 |
41 |
| - }) |
42 |
| - .then(function (chained) { |
43 |
| - expect(chained).to.eql(234) |
44 |
| - done() |
45 |
| - }) |
| 21 | + it('should recover when caught', function () { |
| 22 | + var stub = sinon.stub() |
| 23 | + var d = Promise.defer() |
| 24 | + d.promise |
| 25 | + .then(null, function () { return 234 }) |
| 26 | + .then(stub, null) |
| 27 | + d.reject(123) |
| 28 | + expect(stub.calledWith(234)).to.eql(true) |
46 | 29 | })
|
47 |
| - it('should not change once rejected', function (done) { |
48 |
| - return promise(function (resolve, reject) { |
49 |
| - setTimeout(function () { |
50 |
| - reject(123) |
51 |
| - reject(234) |
52 |
| - }, 0) |
53 |
| - }) |
54 |
| - .then(null, function (val) { |
55 |
| - expect(val).to.eql(123) |
56 |
| - done() |
57 |
| - }) |
| 30 | + it('should be really chainable', function () { |
| 31 | + var stub = sinon.stub() |
| 32 | + var d = Promise.defer() |
| 33 | + d.promise |
| 34 | + .then(null, null) |
| 35 | + .then(null, function () { |
| 36 | + return 234 |
| 37 | + }) |
| 38 | + .then(stub) |
| 39 | + d.reject(123) |
| 40 | + expect(stub.calledWith(234)).to.eql(true) |
| 41 | + }) |
| 42 | + it('should be really really chainable', function () { |
| 43 | + var resolvedStub = sinon.stub() |
| 44 | + var rejectedStub = sinon.stub() |
| 45 | + var d = Promise.defer() |
| 46 | + d.promise |
| 47 | + .then(resolvedStub, null) |
| 48 | + .then(null, rejectedStub) |
| 49 | + d.reject(123) |
| 50 | + expect(resolvedStub.called).to.eql(false) |
| 51 | + expect(rejectedStub.calledWith(123)).to.eql(true) |
58 | 52 | })
|
59 |
| - it('should not call resolve', function (done) { |
| 53 | + it('should not change once rejected', function () { |
60 | 54 | var stub = sinon.stub()
|
61 |
| - return promise(function (resolve, reject) { |
62 |
| - setTimeout(function () { |
63 |
| - reject(123) |
64 |
| - }, 0) |
65 |
| - }) |
66 |
| - .then(stub, function () {}) |
67 |
| - .then(function () { |
68 |
| - expect(stub.called).to.eql(false) |
69 |
| - done() |
70 |
| - }) |
| 55 | + var d = Promise.defer() |
| 56 | + d.promise.then(null, stub) |
| 57 | + d.reject(123) |
| 58 | + d.reject(234) |
| 59 | + expect(d.promise.value).to.eql(123) |
71 | 60 | })
|
72 |
| - |
73 |
| - // requires native Promise api |
74 |
| - if (typeof Promise === 'undefined') return |
75 |
| - it('should not resolve promises', function (done) { |
76 |
| - return promise(function (resolve, reject) { |
77 |
| - setTimeout(function () { |
78 |
| - reject(Promise.resolve(123)) |
79 |
| - }, 0) |
80 |
| - }) |
81 |
| - .then(null, function (val) { |
82 |
| - expect(typeof val).to.eql('object') |
83 |
| - done() |
| 61 | + it('should reject an already fulfilled promise with the same reason', function () { |
| 62 | + var stub = sinon.stub() |
| 63 | + var d1 = Promise.defer() |
| 64 | + var d2 = Promise.defer() |
| 65 | + d1.promise.then(function () { |
| 66 | + return d2.promise |
84 | 67 | })
|
| 68 | + .then(null, stub) |
| 69 | + d1.resolve() |
| 70 | + d2.reject(123) |
| 71 | + expect(stub.calledWith(123)).to.eql(true) |
85 | 72 | })
|
86 |
| - it('should be compatible with the native promise api', function (done) { |
87 |
| - return Promise.all([promise(function (resolve, reject) { |
88 |
| - setTimeout(function () { |
89 |
| - reject(123) |
90 |
| - }, 0) |
91 |
| - })]) |
92 |
| - .then(null, function (reason) { |
93 |
| - expect(reason).to.eql(123) |
94 |
| - done() |
| 73 | + it('should reject an already rejected promise from a rejected promise with the same reason', function () { |
| 74 | + var stub = sinon.stub() |
| 75 | + var d1 = Promise.defer() |
| 76 | + var d2 = Promise.defer() |
| 77 | + d1.promise.then(null, function () { |
| 78 | + return d2.promise |
95 | 79 | })
|
| 80 | + .then(null, stub) |
| 81 | + d1.reject() |
| 82 | + d2.reject(123) |
| 83 | + expect(stub.calledWith(123)).to.eql(true) |
| 84 | + }) |
| 85 | + it('should not resolve promises', function () { |
| 86 | + var p = Promise.resolve(123) |
| 87 | + var stub = sinon.stub() |
| 88 | + var d = Promise.defer() |
| 89 | + d.promise.then(null, stub) |
| 90 | + d.reject(p) |
| 91 | + expect(stub.calledWith(p)).to.eql(true) |
96 | 92 | })
|
97 | 93 | })
|
0 commit comments