Skip to content

Commit

Permalink
Add wait helper.
Browse files Browse the repository at this point in the history
This allows component integration tests that need to deal with async
interactions to have an easy way to model their tests.

Example test (assuming the component does some async in an action
triggered by the button click):

```javascript
import { moduleForComponent, test } from 'ember-test-helpers';
import waitForPendingRunLoops from 'ember-test-helpers/helpers/wait';

test('it works when async exists in an event/action', function(assert) {
  this.render('{{my-foo}}');

  this.$('button').click();

  return waitForPendingRunLoops()
    .then(() => {
      // assertions for after async behavior
    });
});
```

The guts of this `wait` helper are pulled from the `wait` acceptance
test helper in Ember itself. Eventually we may be able to make Ember
itself provide an implementation that we can share here and in the
acceptance testing world.
  • Loading branch information
rwjblue committed Oct 19, 2015
1 parent ae46af8 commit e021c48
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var lib = pickFiles('lib', {

var tests = pickFiles('tests', {
srcDir: '/',
files: ['test-support/*.js', '*.js'],
files: ['**/*.js'],
destDir: '/tests'
});

Expand Down
18 changes: 18 additions & 0 deletions lib/ember-test-helpers/helpers/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Ember from 'ember';

export default function wait() {
return new Ember.RSVP.Promise(function(resolve) {
var watcher = self.setInterval(function() {
// If there are scheduled timers or we are inside of a run loop, keep polling
if (Ember.run.hasScheduledTimers() || Ember.run.currentRunLoop) {
return;
}

// Stop polling
self.clearInterval(watcher);

// Synchronously resolve the promise
Ember.run(null, resolve);
}, 10);
});
}
72 changes: 72 additions & 0 deletions tests/helpers/wait-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Ember from 'ember';
import { TestModuleForComponent } from 'ember-test-helpers';
import test from 'tests/test-support/qunit-test';
import qunitModuleFor from 'tests/test-support/qunit-module-for';
import { setResolverRegistry } from 'tests/test-support/resolver';

import wait from 'ember-test-helpers/helpers/wait';

function moduleForComponent(name, description, callbacks) {
var module = new TestModuleForComponent(name, description, callbacks);
qunitModuleFor(module);
}


moduleForComponent('wait helper tests', {
integration: true,
setup: function() {
this.register('component:x-test-1', Ember.Component.extend({
internalValue: 'initial value',

init: function() {
this._super.apply(this, arguments);

Ember.run.later(this, function() {
this.set('internalValue', 'async value');
}, 10);
}
}));

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

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

click: function() {
Ember.run.later(this, function() {
this.set('internalValue', 'async value');
}, 10);
}
}));

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

test('it works when async exists in `init`', function() {
var testContext = this;

this.render('{{x-test-1}}');

return wait()
.then(function() {
equal(testContext.$().text(), 'async value');
});
});

test('it works when async exists in an event/action', function() {
var testContext = this;

this.render('{{x-test-2}}');

equal(this.$().text(), 'initial value');

this.$('div').click();

return wait()
.then(function() {
equal(testContext.$().text(), 'async value');
});
});

0 comments on commit e021c48

Please sign in to comment.