-
Notifications
You must be signed in to change notification settings - Fork 395
/
globalRejectionEvents-test.js
116 lines (91 loc) · 2.47 KB
/
globalRejectionEvents-test.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
/*global process, window, setTimeout*/
var buster = typeof window !== 'undefined' ? window.buster : require('buster');
var CorePromise = require('../lib/Promise');
var sentinel = { value: 'sentinel' };
buster.testCase('global rejection events', {
'on Node': {
'tearDown': function() {
if(typeof window !== 'undefined') {
return;
}
process.removeAllListeners('unhandledRejection');
process.removeAllListeners('rejectionHandled');
},
'should emit unhandledRejection': function(done) {
if(typeof window !== 'undefined') {
buster.assert(true);
done();
return;
}
function listener(e) {
buster.assert.same(e, sentinel);
done();
}
process.on('unhandledRejection', listener);
CorePromise.reject(sentinel);
},
'should emit rejectionHandled': function(done) {
if(typeof window !== 'undefined') {
buster.assert(true);
done();
return;
}
var r;
function unhandled(e, rejection) {
buster.assert.same(e, sentinel);
r = rejection;
}
function handled(rejection) {
buster.assert.same(rejection, r);
done();
}
process.on('unhandledRejection', unhandled);
process.on('rejectionHandled', handled);
var p = CorePromise.reject(sentinel);
setTimeout(function() {
p.catch(function() {});
}, 10);
}
},
'in Browser': {
'should emit unhandledRejection': function(done) {
if(typeof window === 'undefined') {
buster.assert(true);
done();
return;
}
function listener(e) {
window.removeEventListener('unhandledRejection', listener, false);
e.preventDefault();
buster.assert.same(e.detail.reason, sentinel);
done();
}
window.addEventListener('unhandledRejection', listener, false);
CorePromise.reject(sentinel);
},
'should emit rejectionHandled': function(done) {
if(typeof window === 'undefined') {
buster.assert(true);
done();
return;
}
var key;
function unhandled(e) {
window.removeEventListener('unhandledRejection', unhandled, false);
buster.assert.same(e.detail.reason, sentinel);
key = e.detail.key;
}
function handled(e) {
window.removeEventListener('rejectionHandled', handled, false);
buster.assert.same(e.detail.key, key);
done();
}
window.addEventListener('unhandledRejection', unhandled, false);
window.addEventListener('rejectionHandled', handled, false);
var p = CorePromise.reject(sentinel);
setTimeout(function() {
p.catch(function() {});
}, 10);
}
}
});