@@ -142,7 +142,6 @@ QUnit.test('rejections like jqXHR which have errorThrown property work', functio
142
142
}
143
143
} ) ;
144
144
145
-
146
145
QUnit . test ( 'rejections where the errorThrown is a string should wrap the sting in an error object' , function ( ) {
147
146
expect ( 2 ) ;
148
147
@@ -167,3 +166,128 @@ QUnit.test('rejections where the errorThrown is a string should wrap the sting i
167
166
Ember . testing = wasEmberTesting ;
168
167
}
169
168
} ) ;
169
+
170
+ var wasTesting ;
171
+ var reason = 'i failed' ;
172
+ QUnit . module ( 'Ember.test: rejection assertions' , {
173
+ before ( ) {
174
+ wasTesting = Ember . testing ;
175
+ Ember . testing = true ;
176
+ } ,
177
+ after ( ) {
178
+ Ember . testing = wasTesting ;
179
+ }
180
+ } ) ;
181
+
182
+ function ajax ( something ) {
183
+ return RSVP . Promise ( function ( resolve ) {
184
+ QUnit . stop ( ) ;
185
+ setTimeout ( function ( ) {
186
+ QUnit . start ( ) ;
187
+ resolve ( ) ;
188
+ } , 0 ) ; // fake true / foreign async
189
+ } ) ;
190
+ }
191
+
192
+ QUnit . test ( 'unambigiously unhandled rejection' , function ( ) {
193
+ QUnit . throws ( function ( ) {
194
+ Ember . run ( function ( ) {
195
+ RSVP . Promise . reject ( reason ) ;
196
+ } ) ; // something is funky, we should likely assert
197
+ } , reason ) ;
198
+ } ) ;
199
+
200
+ QUnit . test ( 'sync handled' , function ( ) {
201
+ Ember . run ( function ( ) {
202
+ RSVP . Promise . reject ( reason ) . catch ( function ( ) { } ) ;
203
+ } ) ; // handled, we shouldn't need to assert.
204
+ ok ( true , 'reached end of test' ) ;
205
+ } ) ;
206
+
207
+ QUnit . test ( 'handled within the same micro-task (via Ember.RVP.Promise)' , function ( ) {
208
+ Ember . run ( function ( ) {
209
+ var rejection = RSVP . Promise . reject ( reason ) ;
210
+ RSVP . Promise . resolve ( 1 ) . then ( ( ) => rejection . catch ( function ( ) { } ) ) ;
211
+ } ) ; // handled, we shouldn't need to assert.
212
+ ok ( true , 'reached end of test' ) ;
213
+ } ) ;
214
+
215
+ QUnit . test ( 'handled within the same micro-task (via direct run-loop)' , function ( ) {
216
+ Ember . run ( function ( ) {
217
+ var rejection = RSVP . Promise . reject ( reason ) ;
218
+
219
+ Ember . run . schedule ( 'afterRender' , ( ) => rejection . catch ( function ( ) { } ) ) ;
220
+ } ) ; // handled, we shouldn't need to assert.
221
+ ok ( true , 'reached end of test' ) ;
222
+ } ) ;
223
+
224
+ QUnit . test ( 'handled in the next microTask queue flush (Ember.run.next)' , function ( ) {
225
+ expect ( 2 ) ;
226
+
227
+ QUnit . throws ( function ( ) {
228
+ Ember . run ( function ( ) {
229
+ var rejection = RSVP . Promise . reject ( reason ) ;
230
+
231
+ QUnit . stop ( ) ;
232
+ Ember . run . next ( ( ) => {
233
+ QUnit . start ( ) ;
234
+ rejection . catch ( function ( ) { } ) ;
235
+ ok ( true , 'reached end of test' ) ;
236
+ } ) ;
237
+ } ) ;
238
+ } , reason ) ;
239
+
240
+ // a promise rejection survived a full flush of the run-loop without being handled
241
+ // this is very likely an issue.
242
+ } ) ;
243
+
244
+ QUnit . test ( 'handled in the same microTask Queue flush do to data locality' , function ( ) {
245
+ // an ambiguous scenario, this may or may not assert
246
+ // it depends on the locality of `user#1`
247
+ var store = {
248
+ find ( ) {
249
+ return Promise . resolve ( 1 ) ;
250
+ }
251
+ } ;
252
+
253
+ Ember . run ( function ( ) {
254
+ var rejection = RSVP . Promise . reject ( reason ) ;
255
+
256
+ store . find ( 'user' , 1 ) . then ( ( ) => rejection . catch ( function ( ) { } ) ) ;
257
+ } ) ;
258
+
259
+ ok ( true , 'reached end of test' ) ;
260
+ } ) ;
261
+
262
+ QUnit . test ( 'handled in a different microTask Queue flush do to data locality' , function ( ) {
263
+ // an ambiguous scenario, this may or may not assert
264
+ // it depends on the locality of `user#1`
265
+ var store = {
266
+ find ( ) {
267
+ return ajax ( ) ;
268
+ }
269
+ } ;
270
+
271
+ QUnit . throws ( function ( ) {
272
+ Ember . run ( function ( ) {
273
+ var rejection = RSVP . Promise . reject ( reason ) ;
274
+
275
+ store . find ( 'user' , 1 ) . then ( ( ) => {
276
+ rejection . catch ( function ( ) { } ) ;
277
+ ok ( true , 'reached end of test' ) ;
278
+ } ) ;
279
+ } ) ;
280
+ } , reason ) ;
281
+ } ) ;
282
+
283
+ QUnit . test ( 'handled in the next microTask queue flush (ajax example)' , function ( ) {
284
+ QUnit . throws ( function ( ) {
285
+ Ember . run ( function ( ) {
286
+ var rejection = RSVP . Promise . reject ( reason ) ;
287
+ ajax ( '/something/' ) . then ( ( ) => {
288
+ rejection . catch ( function ( ) { } ) ;
289
+ ok ( true , 'reached end of test' ) ;
290
+ } ) ;
291
+ } ) ;
292
+ } , reason ) ;
293
+ } ) ;
0 commit comments