forked from dexie/Dexie.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests-promise.js
369 lines (323 loc) · 10.5 KB
/
tests-promise.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import Dexie from 'dexie';
import {module, stop, start, asyncTest, equal, ok} from 'QUnit';
module("promise");
//Dexie.debug = "dexie";
function createDirectlyResolvedPromise() {
return new Dexie.Promise(function(resolve) {
resolve();
});
}
asyncTest("Promise basics", ()=>{
new Dexie.Promise(resolve => resolve("value"))
.then(value => {
equal(value, "value", "Promise should be resolved with 'value'");
}).then(()=>{
start();
});
});
asyncTest("return Promise.resolve() from Promise.then(...)", ()=>{
new Dexie.Promise(resolve => resolve("value"))
.then (value => {
return Dexie.Promise.resolve(value);
}).then (value => {
equal (value, "value", "returning Dexie.Promise.resolve() from then handler should work");
start();
})
});
asyncTest("return unresolved Promise from Promise.then(...)", ()=>{
new Dexie.Promise(resolve => resolve("value"))
.then (value => {
return new Dexie.Promise(resolve => setTimeout(resolve, 0, "value"));
}).then (value => {
equal (value, "value", "When unresolved promise is resolved, this promise should resolve with its value");
start();
})
});
asyncTest("Compatibility with other promises", ()=>{
Dexie.Promise.resolve().then(()=>{
return window.Promise.resolve(3);
}).then(x => {
equal(x, 3, "returning a window.Promise should be ok");
start();
})
});
asyncTest("When to promise resolve", ()=>{
var Promise = Dexie.Promise;
var res = [];
Promise.follow(()=>{
new Promise (resolve => resolve()).then(()=>res.push("B1"));
res.push("A1");
new Promise (resolve => resolve()).then(()=>res.push("B2"));
res.push("A2");
}).then(()=>{
equal(JSON.stringify(res), JSON.stringify([
"A1",
"A2",
"B1",
"B2"
]), "Resolves come in expected order.");
}).catch(e => {
ok(false, e.stack || e);
}).then(start);
});
asyncTest("Promise.follow()", ()=>{
var Promise = Dexie.Promise;
Promise.follow(() => {
Promise.resolve("test")
.then(x => x + ":")
.then(x => Promise.reject("rejection"))
.then(()=>ok(false, "Should not come here"))
.catch(e => equal(e, "rejection", "Should catch rejection"));
}).then(()=>ok(true, "Scope ended"))
.catch(e => ok(false, "Error: " + e.stack))
.then(start);
});
asyncTest("Promise.follow() 2", ()=>{
var Promise = Dexie.Promise;
Promise.follow(() => {
Promise.resolve("test")
.then(x => x + ":")
.then(x => Promise.reject("rejection"))
.then(()=>ok(false, "Should not come here"))
}).then(()=>ok(false, "Scope should not resolve"))
.catch(e => ok(true, "Got error: " + e.stack))
.then(start);
});
asyncTest("Promise.follow() 3 (empty)", ()=>{
Dexie.Promise.follow(()=>{})
.then(()=>ok(true, "Promise resolved when nothing was done"))
.then(start);
});
asyncTest ("Promise.follow chained", ()=>{
var Promise = Dexie.Promise;
//Promise._rootExec(()=>{
//Promise.scheduler = (fn, args) => setTimeout(fn, 0, args[0], args[1], args[2]);
Promise.follow(()=>{
new Promise(resolve => resolve()).then(()=>Promise.follow(()=>{
Promise.PSD.inner = true;
// Chains and rejection
new Promise(resolve => resolve())
.then(x => 3)
.then(null, e => "catched")
.then(x => {})
.then(()=>{throw new TypeError("oops");})
}).then(()=>ok(false, "Promise.follow() should not resolve since an unhandled rejection should have been detected"))
).then(()=>ok(false, "Promise.follow() should not resolve since an unhandled rejection should have been detected"))
.catch (TypeError, err => {
ok(true, "Got TypeError: " + err.stack);
});
}).then (()=> ok(true, "Outer Promise.follow() should resolve because inner was catched"))
.catch (err => {
ok(false, "Should have catched TypeError: " + err.stack);
}).then(()=>{
start();
});
//});
});
asyncTest("Promise.on.error should propagate once", 1, function(){
var Promise = Dexie.Promise;
function logErr (e) {
ok(true, e);
return false;
}
Promise.on('error', logErr);
var p = new Promise((resolve, reject)=>{
reject("apa");
}).finally(()=>{
}).finally(()=>{
});
var p2 = p.finally(()=>{});
var p3 = p.then(()=>{});
var p4 = p.then(()=>{
}).then(()=>{
});
Promise.all([p, p2, p3, p4]).finally(()=>{
setTimeout(()=>{
Promise.on('error').unsubscribe(logErr);
start();
}, 1);
});
});
asyncTest("Promise.on.error should not propagate if catched after finally", 1, function(){
var Promise = Dexie.Promise;
function logErr (e) {
ok(false, "Should already be catched:" + e);
}
Promise.on('error', logErr);
var p = new Promise((resolve, reject)=>{
reject("apa");
}).finally(()=>{
}).finally(()=>{
}).catch(e => {
ok(true, "Catching it here: " + e);
});
var p2 = p.finally(()=>{});
var p3 = p.then(()=>{});
var p4 = p.then(()=>{
}).then(()=>{
});
Promise.all([p, p2, p3, p4]).finally(()=>{
setTimeout(()=>{
Promise.on('error').unsubscribe(logErr);
start();
}, 1);
});
});
asyncTest("Issue#27(A) - Then handlers are called synchronously for already resolved promises", function () {
// Test with plain Dexie.Promise()
var expectedLog = ['1', '3', '2', 'a', 'c', 'b'];
var log = [];
var promise = createDirectlyResolvedPromise();
log.push('1');
promise.then(function() {
log.push('2');
log.push('a');
promise.then(function() {
log.push('b');
check();
});
log.push('c');
check();
});
log.push('3');
check();
function check() {
if (log.length == expectedLog.length) {
for (var i = 0; i < log.length; ++i) {
equal(log[i], expectedLog[i], "Position " + i + " is " + log[i] + " and was expected to be " + expectedLog[i]);
}
start();
}
}
});
asyncTest("Issue#27(B) - Then handlers are called synchronously for already resolved promises", function () {
// Test with a Promise returned from the Dexie library
var expectedLog = ['1', '3', '2', 'a', 'c', 'b'];
var log = [];
var db = new Dexie("Promise-test");
db.version(1).stores({ friends: '++id' });
db.on('populate', function () {
db.friends.add({ name: "one" });
db.friends.add({ name: "two" });
db.friends.add({ name: "three" });
});
db.delete().then(function () {
return db.open();
}).then(function () {
var promise = db.friends.toCollection().each(function() {});
log.push('1');
promise.then(function () {
log.push('2');
log.push('a');
promise.then(function() {
log.push('b');
check();
}).catch(function(e) {
ok(false, "error: " + e);
start();
});
log.push('c');
check();
}).catch(function(e) {
ok(false, "error: " + e);
start();
});
log.push('3');
check();
function check() {
if (log.length == expectedLog.length) {
for (var i = 0; i < log.length; ++i) {
equal(log[i], expectedLog[i], "Position " + i + " is " + log[i] + " and was expected to be " + expectedLog[i]);
}
db.delete().then(start);
}
}
});
});
asyncTest("Issue #97 A transaction may be lost after calling Dexie.Promise.resolve().then(...)", function() {
Dexie.Promise.newPSD(function () {
Dexie.Promise.PSD.hello = "promise land";
Dexie.Promise.resolve().then(function () {
ok(!!Dexie.Promise.PSD, "We should have a Dexie.Promise.PSD");
equal(Dexie.Promise.PSD.hello, "promise land");
}).catch(function(e) {
ok(false, "Error: " + e);
}).finally(start);
});
});
/*asyncTest("setTimeout vs setImmediate", ()=>{
var log=[];
setImmediate(()=>{
log.push("setImmediate");
if (log.length == 2) end();
});
setTimeout(()=>{
log.push("setTimeout");
if (log.length == 2) end();
}, 40);
function end() {
equal(log[0], "setImmediate", "setImmediate first");
equal(log[1], "setTimeout", "setTimeout second");
start();
}
});*/
asyncTest("Promise.on.error", ()=> {
var errors = [];
function onError(e, p) {
errors.push(e);
return false;
}
Dexie.Promise.on('error', onError);
new Dexie.Promise((resolve, reject) => {
reject ("error");
});
setTimeout(()=>{
equal(errors.length, 1, "Should be one error there");
equal(errors[0], "error", "Should be our error there");
Dexie.Promise.on.error.unsubscribe(onError);
start();
}, 40);
});
asyncTest("Promise.on.error2", ()=> {
var errors = [];
function onError(e, p) {
errors.push(e);
return false;
}
Dexie.Promise.on('error', onError);
new Dexie.Promise((resolve, reject) => {
new Dexie.Promise((resolve2, reject2) => {
reject2 ("error");
}).then(resolve, e => {
reject(e);
//return Dexie.Promise.reject(e);
});
});
setTimeout(()=>{
equal(errors.length, 1, "Should be one error there");
equal(errors[0], "error", "Should be our error there");
Dexie.Promise.on.error.unsubscribe(onError);
start();
}, 40);
});
asyncTest("Promise.on.error3", ()=> {
var errors = [];
function onError(e, p) {
errors.push(e);
return false;
}
Dexie.Promise.on('error', onError);
new Dexie.Promise((resolve, reject) => {
new Dexie.Promise((resolve2, reject2) => {
reject2 ("error");
}).then(resolve, e => {
reject(e);
//return Dexie.Promise.reject(e);
});
}).catch(()=>{});
setTimeout(()=>{
equal(errors.length, 0, "Should be zarro errors there");
Dexie.Promise.on.error.unsubscribe(onError);
start();
}, 40);
});