forked from domenic/promises-unwrapping
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-tests.js
195 lines (163 loc) · 6.45 KB
/
run-tests.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
var adapter = require("./testable-implementation");
var Promise = adapter.Promise;
var assert = require("assert");
var sentinel = { sentinel: "SENTINEL" };
function fulfilledThenable(value) {
var thenable = {
timesCalled: 0,
timesGotten: 0
};
Object.defineProperty(thenable, "then", {
get: function () {
++thenable.timesGotten;
return function (onFulfilled, onRejected) {
++this.timesCalled;
onFulfilled(value);
};
}
});
return thenable;
}
describe("Memoization of thenables", function () {
specify("retrieving a value twice, in parallel, should only call `then` once.", function (done) {
var tuple = adapter.pending();
var thenable = fulfilledThenable(sentinel);
var derived = tuple.promise.then(function () { return thenable; });
tuple.fulfill();
setTimeout(function () {
assert.strictEqual(thenable.timesCalled, 0);
assert.strictEqual(thenable.timesGotten, 0);
var valuesGotten = 0;
adapter.done(derived, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
adapter.done(derived, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
setTimeout(function () {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(valuesGotten, 2);
done();
}, 50);
}, 50);
});
specify("retrieving a value twice, in sequence, should only call `then` once.", function (done) {
var tuple = adapter.pending();
var thenable = fulfilledThenable(sentinel);
var derived = tuple.promise.then(function () { return thenable; });
tuple.fulfill();
setTimeout(function () {
assert.strictEqual(thenable.timesCalled, 0);
assert.strictEqual(thenable.timesGotten, 0);
var valuesGotten = 0;
adapter.done(derived, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
setTimeout(function () {
adapter.done(derived, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
setTimeout(function () {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(valuesGotten, 2);
done();
}, 50);
}, 50);
}, 50);
});
specify("when multiple promises are resolved to the thenable", function (done) {
var tuple1 = adapter.pending();
var tuple2 = adapter.pending();
var thenable = fulfilledThenable(sentinel);
var derived1 = tuple1.promise.then(function () { return thenable; });
var derived2 = tuple2.promise.then(function () { return thenable; });
tuple1.fulfill();
tuple2.fulfill();
var valuesGotten = 0;
adapter.done(derived1, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
adapter.done(derived2, function (value) {
assert.strictEqual(thenable.timesCalled, 1);
assert.strictEqual(thenable.timesGotten, 1);
assert.strictEqual(value, sentinel);
++valuesGotten;
});
setTimeout(function () {
assert.strictEqual(valuesGotten, 2);
done();
}, 50);
});
});
// Does not work even in Node 0.11.7 --harmony, since Array is not iterable yet I guess? So skipping.
describe.skip("Promise.all", function () {
it("fulfills if passed an empty array", function (done) {
adapter.done(Promise.all([]), function (value) {
assert(Array.isArray(value));
assert.deepEqual(value, []);
done();
});
});
it("fulfills if passed an array of mixed fulfilled promises and values", function (done) {
adapter.done(Promise.all([0, Promise.resolve(1), 2, Promise.resolve(3)]), function (value) {
assert(Array.isArray(value));
assert.deepEqual(value, [0, 1, 2, 3]);
done();
});
});
it("rejects if any passed promise is rejected", function (done) {
var foreverPending = new Promise(function () {});
var error = new Error("Rejected");
var rejected = Promise.reject(error);
adapter.done(Promise.all([foreverPending, rejected]),
function (value) {
assert(false, "should never get here");
done();
},
function (reason) {
assert.strictEqual(reason, error);
done();
}
);
});
it("resolves foreign thenables", function (done) {
var normal = Promise.resolve(1);
var foreign = { then: function (f) { f(2); } };
adapter.done(Promise.all([normal, foreign]), function (value) {
assert.deepEqual(value, [1, 2]);
done();
});
});
it("fulfills when passed an sparse array, skipping the omitted values", function (done) {
adapter.done(Promise.all([Promise.resolve(0), , , Promise.resolve(1)]), function (value) {
assert.deepEqual(value, [0, 1]);
done();
});
});
it("does not modify the input array", function (done) {
var input = [0, 1];
adapter.done(Promise.all(input), function (value) {
assert.notStrictEqual(input, value);
done();
});
});
});
require("promises-aplus-tests").mocha(adapter);