-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
232 lines (197 loc) · 5.96 KB
/
test.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { PromiseStates } from './src/promise-states';
import { Vouch } from './src';
import { Deferrable } from './src/deferrable';
const assert = require('assert');
const PromiseAPlusTests = require('promises-aplus-tests');
describe('Deferrable', function() {
it('can resolves onResolve handler with resolved value', function () {
const deferrable = new Deferrable().then(
resolve => assert.equal(resolve, 'RESOLVED!')
);
deferrable.finalize('RESOLVED!', PromiseStates.Fulfilled);
});
describe('#constructor', function() {
it('has an initial state of pending', function() {
const deferrable = new Deferrable();
assert.equal(deferrable.state, PromiseStates.Pending);
});
});
});
describe("Quick Vouch Sanity Tests", function() {
it('has a `then` method', function () {
const vouch = new Vouch(() => {});
assert.equal(typeof vouch.then, 'function');
});
it('resolves when passed a resolving function', function(done) {
const willResolve = (resolve/*, reject*/) => resolve('hello');
const vouch = new Vouch(willResolve);
vouch.then(
() => assert.ok(true) || done(),
() => assert.ok(false) || done()
);
});
it('rejects when passed a rejecting function', function(done) {
const willReject = (resolve, reject) => reject('hello');
const vouch = new Vouch(willReject);
vouch.then(
() => assert.ok(false) || done(),
() => assert.ok(true) || done()
);
});
it('resolves with value passed', function(done) {
const expectedResolvedValue = "SPECIFIC VALUE";
const willResolve = (resolve) => resolve(expectedResolvedValue);
const vouch = new Vouch(willResolve);
vouch.then(resolvedValue => {
assert.equal(resolvedValue, expectedResolvedValue);
done();
},
() => assert.ok(false)
);
});
it('resolves with value passed through multiple `then` chains', function(done) {
const expectedResolvedValue = "SPECIFIC VALUE";
const willResolve = (resolve) => resolve(expectedResolvedValue);
const passThrough = value => value;
const vouch = new Vouch(willResolve);
vouch
.then(passThrough)
.then(
resolvedValue => {
assert.equal(resolvedValue, expectedResolvedValue);
done();
},
() => assert.ok(false)
);
});
it('resolves after passing through a reject', function(done) {
const willReject = (resolve, reject) => reject('REJECTED');
const vouch = new Vouch(willReject);
vouch
// should skip
.then()
// should be handled with reject handler
.then(
resolved => assert(false),
rejected => assert(rejected === 'REJECTED')
)
// should fall back to the success handler
.then(
resolved => {
assert(true);
done();
},
rejected => assert(false)
);
});
it('resolves with final resolution from nested returned thenables', function(done) {
const willResolve = resolve => resolve();
const vouch = new Vouch(willResolve)
.then(function () {
return {
then(onFullfill) {
return onFullfill({
then(onFullfill) {
return onFullfill('REACHED THE DEPTHS');
}
});
}
};
})
.then(result => {
assert.equal(result, 'REACHED THE DEPTHS');
done();
});
});
it('resolves with the value of a resolved promise', function(done) {
Vouch.resolve()
.then(() => Vouch.resolve('RESOLVED'))
.then(value => assert(value, 'RESOLVED') || done());
});
it('eventually resolves with an object with a then function', function(done) {
let resolve ;
let eventual = new Vouch(r => {
resolve = r;
});
let mockThen = {
then(onFullfill) {
onFullfill('FULFILLED');
}
}
Vouch
.resolve()
.then(() => eventual)
.then(value => {
assert.equal(value, 'FULFILLED');
done();
});
setTimeout(() => resolve(mockThen), 10);
});
it('resolves with the value of an eventually resolved promise', function(done) {
let resolveEventual;
const eventual = new Promise(r => {
resolveEventual = r
});
Vouch.resolve()
.then(() => eventual)
.then(value => {
assert(value, 'RESOLVED') || done()
});
resolveEventual('RESOLVED');
});
it('rejects within reject handler if an error is thrown', function(done) {
const willReject = (resolve, reject) => reject('REJECTED');
const vouch = new Vouch(willReject);
vouch
// this will re-throw the rejected reason
// continuing the rejection chain
.then(
() => assert(false),
rejected => { throw (rejected); }
)
.then(
() => assert(false),
rejected => {
assert.equal(rejected, 'REJECTED');
done();
}
);
});
});
describe('`Vouch` class', function () {
describe('`resolve` method', function () {
it('has a `resolve` method', function () {
assert.equal(typeof Vouch.resolve, 'function');
});
it('result from calling `resolve` has a `then` method', function () {
assert.equal(typeof Vouch.resolve(true).then, 'function');
});
});
describe('`reject` method', function () {
it('method exists', function () {
assert.equal(typeof Vouch.reject, 'function');
});
it('has a `then` method after being called', function () {
assert.equal(typeof Vouch.reject(true).then, 'function');
});
});
});
describe("Promises/A+ Test Suite", function () {
const mochaTestAdapter = {
resolved: Vouch.resolve,
rejected: Vouch.reject,
deferred() {
const d = new Deferrable();
return {
promise: d,
resolve: value => {
d.finalize(value, PromiseStates.Fulfilled);
},
reject: value => {
d.finalize(value, PromiseStates.Rejected);
}
};
}
};
PromiseAPlusTests.mocha(mochaTestAdapter);
});