Skip to content

Commit

Permalink
Add support for Ember Test registered waiters to wait helper
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed Jan 19, 2016
1 parent 474be00 commit d3270ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/ember-test-helpers/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function wait(_options) {
var options = _options || {};
var waitForTimers = options.hasOwnProperty('waitForTimers') ? options.waitForTimers : true;
var waitForAJAX = options.hasOwnProperty('waitForAJAX') ? options.waitForAJAX : true;
var waitForWaiters = options.hasOwnProperty('waitForWaiters') ? options.waitForWaiters : true;

return new Ember.RSVP.Promise(function(resolve) {
var watcher = self.setInterval(function() {
Expand All @@ -42,6 +43,10 @@ export default function wait(_options) {
return;
}

if (waitForWaiters && (Ember.Test.waiters && Ember.Test.waiters.any(([context, callback]) => !callback.call(context)))) {
return;
}

// Stop polling
self.clearInterval(watcher);

Expand Down
38 changes: 38 additions & 0 deletions tests/wait-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ moduleForComponent('wait helper tests', {

this.register('template:components/x-test-4', Ember.Handlebars.compile("{{internalValue}}"));

this.register('component:x-test-5', Ember.Component.extend({
internalValue: 'initial value',

ready: false,

isReady() {
return this.get('ready');
},

init() {
this._super.apply(this, arguments);
Ember.Test.registerWaiter(this, this.isReady);
Ember.run.later(() => {
this.setProperties({
internalValue: 'async value',
ready: true
});
}, 25);
},

willDestroy() {
this._super.apply(this, arguments);
Ember.Test.unregisterWaiter(this, this.isReady);
}

}));

this.register('template:components/x-test-5', Ember.Handlebars.compile('{{internalValue}}'));

this.server = new Pretender(function() {
this.get('/whazzits', function() {
return [
Expand Down Expand Up @@ -186,3 +215,12 @@ test('it can wait only for timers', function() {
equal(testContext.$().text(), 'Local Data!' );
});
});

test('it waits for Ember test waiters', function() {
this.render('{{x-test-5}}');

return wait({ waitForTimers: false })
.then(() => {
equal(this.$().text(), 'async value');
});
});

0 comments on commit d3270ac

Please sign in to comment.