forked from emberjs/ember.js
/
rsvp_test.js
254 lines (213 loc) · 6.41 KB
/
rsvp_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
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
import {
setOnerror,
getOnerror,
run
} from 'ember-metal';
import RSVP from '../../ext/rsvp';
import { isTesting, setTesting } from 'ember-debug';
const ORIGINAL_ONERROR = getOnerror();
QUnit.module('Ember.RSVP', {
teardown() {
setOnerror(ORIGINAL_ONERROR);
}
});
QUnit.test('Ensure that errors thrown from within a promise are sent to the console', function() {
let error = new Error('Error thrown in a promise for testing purposes.');
try {
run(function() {
new RSVP.Promise(function(resolve, reject) {
throw error;
});
});
ok(false, 'expected assertion to be thrown');
} catch (e) {
equal(e, error, 'error was re-thrown');
}
});
QUnit.test('TransitionAborted errors are not re-thrown', function() {
expect(1);
let fakeTransitionAbort = { name: 'TransitionAborted' };
run(RSVP, 'reject', fakeTransitionAbort);
ok(true, 'did not throw an error when dealing with TransitionAborted');
});
QUnit.test('Can reject with non-Error object', function(assert) {
let wasEmberTesting = isTesting();
setTesting(false);
expect(1);
try {
run(RSVP, 'reject', 'foo');
} catch (e) {
equal(e, 'foo', 'should throw with rejection message');
} finally {
setTesting(wasEmberTesting);
}
});
QUnit.test('Can reject with no arguments', function(assert) {
let wasEmberTesting = isTesting();
setTesting(false);
expect(1);
try {
run(RSVP, 'reject');
} catch (e) {
ok(false, 'should not throw');
} finally {
setTesting(wasEmberTesting);
}
ok(true);
});
QUnit.test('rejections like jqXHR which have errorThrown property work', function() {
expect(2);
let wasEmberTesting = isTesting();
let wasOnError = getOnerror();
try {
setTesting(false);
setOnerror(error => {
equal(error, actualError, 'expected the real error on the jqXHR');
equal(error.__reason_with_error_thrown__, jqXHR, 'also retains a helpful reference to the rejection reason');
});
let actualError = new Error('OMG what really happened');
let jqXHR = {
errorThrown: actualError
};
run(RSVP, 'reject', jqXHR);
} finally {
setOnerror(wasOnError);
setTesting(wasEmberTesting);
}
});
QUnit.test('rejections where the errorThrown is a string should wrap the sting in an error object', function() {
expect(2);
let wasEmberTesting = isTesting();
let wasOnError = getOnerror();
try {
setTesting(false);
setOnerror(error => {
equal(error.message, actualError, 'expected the real error on the jqXHR');
equal(error.__reason_with_error_thrown__, jqXHR, 'also retains a helpful reference to the rejection reason');
});
let actualError = 'OMG what really happened';
let jqXHR = {
errorThrown: actualError
};
run(RSVP, 'reject', jqXHR);
} finally {
setOnerror(wasOnError);
setTesting(wasEmberTesting);
}
});
QUnit.test('rejections can be serialized to JSON', function (assert) {
expect(2);
let wasEmberTesting = isTesting();
let wasOnError = getOnerror();
try {
setTesting(false);
setOnerror(error => {
assert.equal(error.message, 'a fail');
assert.ok(JSON.stringify(error), 'Error can be serialized');
});
let jqXHR = {
errorThrown: new Error('a fail')
};
run(RSVP, 'reject', jqXHR);
} finally {
setOnerror(wasOnError);
setTesting(wasEmberTesting);
}
});
const reason = 'i failed';
QUnit.module('Ember.test: rejection assertions');
function ajax(something) {
return RSVP.Promise(function(resolve) {
QUnit.stop();
setTimeout(function() {
QUnit.start();
resolve();
}, 0); // fake true / foreign async
});
}
QUnit.test('unambigiously unhandled rejection', function() {
QUnit.throws(function() {
run(function() {
RSVP.Promise.reject(reason);
}); // something is funky, we should likely assert
}, reason);
});
QUnit.test('sync handled', function() {
run(function() {
RSVP.Promise.reject(reason).catch(function() { });
}); // handled, we shouldn't need to assert.
ok(true, 'reached end of test');
});
QUnit.test('handled within the same micro-task (via Ember.RVP.Promise)', function() {
run(function() {
let rejection = RSVP.Promise.reject(reason);
RSVP.Promise.resolve(1).then(() => rejection.catch(function() { }));
}); // handled, we shouldn't need to assert.
ok(true, 'reached end of test');
});
QUnit.test('handled within the same micro-task (via direct run-loop)', function() {
run(function() {
let rejection = RSVP.Promise.reject(reason);
run.schedule('afterRender', () => rejection.catch(function() { }));
}); // handled, we shouldn't need to assert.
ok(true, 'reached end of test');
});
QUnit.test('handled in the next microTask queue flush (run.next)', function() {
expect(2);
QUnit.throws(function() {
run(function() {
let rejection = RSVP.Promise.reject(reason);
QUnit.stop();
run.next(() => {
QUnit.start();
rejection.catch(function() { });
ok(true, 'reached end of test');
});
});
}, reason);
// a promise rejection survived a full flush of the run-loop without being handled
// this is very likely an issue.
});
QUnit.test('handled in the same microTask Queue flush do to data locality', function() {
// an ambiguous scenario, this may or may not assert
// it depends on the locality of `user#1`
let store = {
find() {
return RSVP.Promise.resolve(1);
}
};
run(function() {
let rejection = RSVP.Promise.reject(reason);
store.find('user', 1).then(() => rejection.catch(function() { }));
});
ok(true, 'reached end of test');
});
QUnit.test('handled in a different microTask Queue flush do to data locality', function() {
// an ambiguous scenario, this may or may not assert
// it depends on the locality of `user#1`
let store = {
find() {
return ajax();
}
};
QUnit.throws(function() {
run(function() {
let rejection = RSVP.Promise.reject(reason);
store.find('user', 1).then(() => {
rejection.catch(function() { });
ok(true, 'reached end of test');
});
});
}, reason);
});
QUnit.test('handled in the next microTask queue flush (ajax example)', function() {
QUnit.throws(function() {
run(function() {
let rejection = RSVP.Promise.reject(reason);
ajax('/something/').then(() => {
rejection.catch(function() {});
ok(true, 'reached end of test');
});
});
}, reason);
});