/
test.js
519 lines (426 loc) · 12.6 KB
/
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import Ember from "ember-metal/core";
import emberRun from "ember-metal/run_loop";
import { create } from "ember-metal/platform";
import compare from "ember-runtime/compare";
import RSVP from "ember-runtime/ext/rsvp";
import setupForTesting from "ember-testing/setup_for_testing";
import EmberApplication from "ember-application/system/application";
/**
@module ember
@submodule ember-testing
*/
var slice = [].slice;
var helpers = {};
var injectHelpersCallbacks = [];
/**
This is a container for an assortment of testing related functionality:
* Choose your default test adapter (for your framework of choice).
* Register/Unregister additional test helpers.
* Setup callbacks to be fired when the test helpers are injected into
your application.
@class Test
@namespace Ember
*/
var Test = {
/**
Hash containing all known test helpers.
@property _helpers
@private
@since 1.7.0
*/
_helpers: helpers,
/**
`registerHelper` is used to register a test helper that will be injected
when `App.injectTestHelpers` is called.
The helper method will always be called with the current Application as
the first parameter.
For example:
```javascript
Ember.Test.registerHelper('boot', function(app) {
Ember.run(app, app.advanceReadiness);
});
```
This helper can later be called without arguments because it will be
called with `app` as the first parameter.
```javascript
App = Ember.Application.create();
App.injectTestHelpers();
boot();
```
@public
@method registerHelper
@param {String} name The name of the helper method to add.
@param {Function} helperMethod
@param options {Object}
*/
registerHelper: function(name, helperMethod) {
helpers[name] = {
method: helperMethod,
meta: { wait: false }
};
},
/**
`registerAsyncHelper` is used to register an async test helper that will be injected
when `App.injectTestHelpers` is called.
The helper method will always be called with the current Application as
the first parameter.
For example:
```javascript
Ember.Test.registerAsyncHelper('boot', function(app) {
Ember.run(app, app.advanceReadiness);
});
```
The advantage of an async helper is that it will not run
until the last async helper has completed. All async helpers
after it will wait for it complete before running.
For example:
```javascript
Ember.Test.registerAsyncHelper('deletePost', function(app, postId) {
click('.delete-' + postId);
});
// ... in your test
visit('/post/2');
deletePost(2);
visit('/post/3');
deletePost(3);
```
@public
@method registerAsyncHelper
@param {String} name The name of the helper method to add.
@param {Function} helperMethod
@since 1.2.0
*/
registerAsyncHelper: function(name, helperMethod) {
helpers[name] = {
method: helperMethod,
meta: { wait: true }
};
},
/**
Remove a previously added helper method.
Example:
```javascript
Ember.Test.unregisterHelper('wait');
```
@public
@method unregisterHelper
@param {String} name The helper to remove.
*/
unregisterHelper: function(name) {
delete helpers[name];
delete Test.Promise.prototype[name];
},
/**
Used to register callbacks to be fired whenever `App.injectTestHelpers`
is called.
The callback will receive the current application as an argument.
Example:
```javascript
Ember.Test.onInjectHelpers(function() {
Ember.$(document).ajaxSend(function() {
Test.pendingAjaxRequests++;
});
Ember.$(document).ajaxComplete(function() {
Test.pendingAjaxRequests--;
});
});
```
@public
@method onInjectHelpers
@param {Function} callback The function to be called.
*/
onInjectHelpers: function(callback) {
injectHelpersCallbacks.push(callback);
},
/**
This returns a thenable tailored for testing. It catches failed
`onSuccess` callbacks and invokes the `Ember.Test.adapter.exception`
callback in the last chained then.
This method should be returned by async helpers such as `wait`.
@public
@method promise
@param {Function} resolver The function used to resolve the promise.
*/
promise: function(resolver) {
return new Test.Promise(resolver);
},
/**
Used to allow ember-testing to communicate with a specific testing
framework.
You can manually set it before calling `App.setupForTesting()`.
Example:
```javascript
Ember.Test.adapter = MyCustomAdapter.create()
```
If you do not set it, ember-testing will default to `Ember.Test.QUnitAdapter`.
@public
@property adapter
@type {Class} The adapter to be used.
@default Ember.Test.QUnitAdapter
*/
adapter: null,
/**
Replacement for `Ember.RSVP.resolve`
The only difference is this uses
an instance of `Ember.Test.Promise`
@public
@method resolve
@param {Mixed} The value to resolve
@since 1.2.0
*/
resolve: function(val) {
return Test.promise(function(resolve) {
return resolve(val);
});
},
/**
This allows ember-testing to play nicely with other asynchronous
events, such as an application that is waiting for a CSS3
transition or an IndexDB transaction.
For example:
```javascript
Ember.Test.registerWaiter(function() {
return myPendingTransactions() == 0;
});
```
The `context` argument allows you to optionally specify the `this`
with which your callback will be invoked.
For example:
```javascript
Ember.Test.registerWaiter(MyDB, MyDB.hasPendingTransactions);
```
@public
@method registerWaiter
@param {Object} context (optional)
@param {Function} callback
@since 1.2.0
*/
registerWaiter: function(context, callback) {
if (arguments.length === 1) {
callback = context;
context = null;
}
if (!this.waiters) {
this.waiters = Ember.A();
}
this.waiters.push([context, callback]);
},
/**
`unregisterWaiter` is used to unregister a callback that was
registered with `registerWaiter`.
@public
@method unregisterWaiter
@param {Object} context (optional)
@param {Function} callback
@since 1.2.0
*/
unregisterWaiter: function(context, callback) {
var pair;
if (!this.waiters) { return; }
if (arguments.length === 1) {
callback = context;
context = null;
}
pair = [context, callback];
this.waiters = Ember.A(this.waiters.filter(function(elt) {
return compare(elt, pair)!==0;
}));
}
};
function helper(app, name) {
var fn = helpers[name].method;
var meta = helpers[name].meta;
return function() {
var args = slice.call(arguments);
var lastPromise = Test.lastPromise;
args.unshift(app);
// some helpers are not async and
// need to return a value immediately.
// example: `find`
if (!meta.wait) {
return fn.apply(app, args);
}
if (!lastPromise) {
// It's the first async helper in current context
lastPromise = fn.apply(app, args);
} else {
// wait for last helper's promise to resolve
// and then execute
run(function() {
lastPromise = Test.resolve(lastPromise).then(function() {
return fn.apply(app, args);
});
});
}
return lastPromise;
};
}
function run(fn) {
if (!emberRun.currentRunLoop) {
emberRun(fn);
} else {
fn();
}
}
EmberApplication.reopen({
/**
This property contains the testing helpers for the current application. These
are created once you call `injectTestHelpers` on your `Ember.Application`
instance. The included helpers are also available on the `window` object by
default, but can be used from this object on the individual application also.
@property testHelpers
@type {Object}
@default {}
*/
testHelpers: {},
/**
This property will contain the original methods that were registered
on the `helperContainer` before `injectTestHelpers` is called.
When `removeTestHelpers` is called, these methods are restored to the
`helperContainer`.
@property originalMethods
@type {Object}
@default {}
@private
@since 1.3.0
*/
originalMethods: {},
/**
This property indicates whether or not this application is currently in
testing mode. This is set when `setupForTesting` is called on the current
application.
@property testing
@type {Boolean}
@default false
@since 1.3.0
*/
testing: false,
/**
This hook defers the readiness of the application, so that you can start
the app when your tests are ready to run. It also sets the router's
location to 'none', so that the window's location will not be modified
(preventing both accidental leaking of state between tests and interference
with your testing framework).
Example:
```
App.setupForTesting();
```
@method setupForTesting
*/
setupForTesting: function() {
setupForTesting();
this.testing = true;
this.Router.reopen({
location: 'none'
});
},
/**
This will be used as the container to inject the test helpers into. By
default the helpers are injected into `window`.
@property helperContainer
@type {Object} The object to be used for test helpers.
@default window
@since 1.2.0
*/
helperContainer: window,
/**
This injects the test helpers into the `helperContainer` object. If an object is provided
it will be used as the helperContainer. If `helperContainer` is not set it will default
to `window`. If a function of the same name has already been defined it will be cached
(so that it can be reset if the helper is removed with `unregisterHelper` or
`removeTestHelpers`).
Any callbacks registered with `onInjectHelpers` will be called once the
helpers have been injected.
Example:
```
App.injectTestHelpers();
```
@method injectTestHelpers
*/
injectTestHelpers: function(helperContainer) {
if (helperContainer) { this.helperContainer = helperContainer; }
this.testHelpers = {};
for (var name in helpers) {
this.originalMethods[name] = this.helperContainer[name];
this.testHelpers[name] = this.helperContainer[name] = helper(this, name);
protoWrap(Test.Promise.prototype, name, helper(this, name), helpers[name].meta.wait);
}
for(var i = 0, l = injectHelpersCallbacks.length; i < l; i++) {
injectHelpersCallbacks[i](this);
}
},
/**
This removes all helpers that have been registered, and resets and functions
that were overridden by the helpers.
Example:
```javascript
App.removeTestHelpers();
```
@public
@method removeTestHelpers
*/
removeTestHelpers: function() {
for (var name in helpers) {
this.helperContainer[name] = this.originalMethods[name];
delete this.testHelpers[name];
delete this.originalMethods[name];
}
}
});
// This method is no longer needed
// But still here for backwards compatibility
// of helper chaining
function protoWrap(proto, name, callback, isAsync) {
proto[name] = function() {
var args = arguments;
if (isAsync) {
return callback.apply(this, args);
} else {
return this.then(function() {
return callback.apply(this, args);
});
}
};
}
Test.Promise = function() {
RSVP.Promise.apply(this, arguments);
Test.lastPromise = this;
};
Test.Promise.prototype = create(RSVP.Promise.prototype);
Test.Promise.prototype.constructor = Test.Promise;
// Patch `then` to isolate async methods
// specifically `Ember.Test.lastPromise`
var originalThen = RSVP.Promise.prototype.then;
Test.Promise.prototype.then = function(onSuccess, onFailure) {
return originalThen.call(this, function(val) {
return isolate(onSuccess, val);
}, onFailure);
};
// This method isolates nested async methods
// so that they don't conflict with other last promises.
//
// 1. Set `Ember.Test.lastPromise` to null
// 2. Invoke method
// 3. Return the last promise created during method
// 4. Restore `Ember.Test.lastPromise` to original value
function isolate(fn, val) {
var value, lastPromise;
// Reset lastPromise for nested helpers
Test.lastPromise = null;
value = fn(val);
lastPromise = Test.lastPromise;
// If the method returned a promise
// return that promise. If not,
// return the last async helper's promise
if ((value && (value instanceof Test.Promise)) || !lastPromise) {
return value;
} else {
run(function() {
lastPromise = Test.resolve(lastPromise).then(function() {
return value;
});
});
return lastPromise;
}
}
export default Test;