-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest.js
More file actions
103 lines (88 loc) · 2.36 KB
/
test.js
File metadata and controls
103 lines (88 loc) · 2.36 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
/* global describe, it */
'use strict';
var assert = require('assert');
var Promise = require('./');
describe("Promise", function () {
it('should throw on invalid resolver type', function () {
assert.throws(function () {
new Promise('unicorns');
}, /Promise resolver unicorns is not a function/);
});
});
describe("Promise.all", function () {
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('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, 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);
});