-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest.js
More file actions
171 lines (148 loc) · 3.98 KB
/
test.js
File metadata and controls
171 lines (148 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* global describe, it */
'use strict';
var assert = require('assert');
var Promise = require('./');
describe('Promise', function () {
it('should throw without new', function () {
assert.throws(function () {
/* eslint-disable new-cap */
var promise = Promise(function () {});
/* eslint-enable new-cap */
assert.ok(promise);
}, /Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function\./);
});
it('should throw on invalid resolver type', function () {
assert.throws(function () {
var promise = new Promise('unicorns');
assert.ok(promise);
}, /Promise resolver unicorns is not a function/);
});
it('should reject on exception in resolver', function (done) {
new Promise(function () {
throw new Error('Bang!');
})
.catch(function (err) {
assert.equal(err.message, 'Bang!');
done();
});
});
it('should reject on exception in then', function (done) {
Promise.resolve(1)
.then(function () {
throw new Error('Bang!');
})
.catch(function (err) {
assert.equal(err.message, 'Bang!');
done();
});
});
it('should return Promise from resolve value', function (done) {
Promise.resolve(Promise.resolve(1))
.then(function (value) {
assert.equal(value, 1);
done();
});
});
// Is it really so? Seems like a bug
it('should resolve thenable in resolve', function (done) {
var thenable = {
then: function (cb) {
cb(thenable);
}
};
Promise.resolve(thenable).then(function (v) {
assert.equal(thenable, v);
done();
});
});
});
describe('Promise.all', function () {
it('should throw error on invalid argument', function () {
assert.throws(function () {
Promise.all('unicorns');
}, /You must pass an array to Promise.all()./);
});
it('should resolve empty array to empty array', function (done) {
Promise.all([]).then(function (value) {
assert.deepEqual(value, []);
done();
});
});
it('should resolve values to array', function (done) {
Promise.all([1, 2, 3]).then(function (value) {
assert.deepEqual(value, [1, 2, 3]);
done();
});
});
it('should resolve promises to array', function (done) {
Promise.all([1, 2, 3].map(Promise.resolve)).then(function (value) {
assert.deepEqual(value, [1, 2, 3]);
done();
});
});
it('should pass first rejected promise to onReject', function (done) {
Promise.all([Promise.resolve(1), Promise.reject(2), Promise.reject(3)]).then(function () {
done('onFullfil called');
}, function (reason) {
assert.deepEqual(reason, 2);
done();
});
});
});
function delayedResolve() {
return new Promise(function (resolve) {
setTimeout(resolve, 10);
});
}
describe('Promise.race', function () {
it('should throw error on invalid argument', function () {
assert.throws(function () {
Promise.race('unicorns');
}, /You must pass an array to Promise.race()./);
});
it('empty array should be pending', function (done) {
var p = Promise.race([]);
setTimeout(function () {
assert.deepEqual(p._state, 'pending');
done();
}, 5);
});
it('should resolve first value', function (done) {
Promise.race([1, 2, 3]).then(function (value) {
assert.deepEqual(value, 1);
done();
});
});
it('should resolve first promise', function (done) {
Promise.race([1, 2, 3].map(Promise.resolve)).then(function (value) {
assert.deepEqual(value, 1);
done();
});
});
it('should pass first rejected promise to onReject', function (done) {
Promise.race([delayedResolve(), delayedResolve(), Promise.reject(3)]).then(function () {
done('onFullfil called');
}, function (reason) {
assert.deepEqual(reason, 3);
done();
});
});
});
describe('Promises/A+ Tests', function () {
var adapter = {
deferred: function () {
var resolve;
var reject;
var promise = new Promise(function (res, rej) {
resolve = res;
reject = rej;
});
return {
promise: promise,
resolve: resolve,
reject: reject
};
}
};
require('promises-aplus-tests').mocha(adapter);
});