diff --git a/Brocfile.js b/Brocfile.js index 40c178ce8..9c00a14c6 100644 --- a/Brocfile.js +++ b/Brocfile.js @@ -25,7 +25,7 @@ var lib = pickFiles('lib', { var tests = pickFiles('tests', { srcDir: '/', - files: ['test-support/*.js', '*.js'], + files: ['**/*.js'], destDir: '/tests' }); diff --git a/lib/ember-test-helpers/helpers/wait.js b/lib/ember-test-helpers/helpers/wait.js new file mode 100644 index 000000000..7d5c100de --- /dev/null +++ b/lib/ember-test-helpers/helpers/wait.js @@ -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); + }); +} diff --git a/tests/helpers/wait-test.js b/tests/helpers/wait-test.js new file mode 100644 index 000000000..c6fd0a01e --- /dev/null +++ b/tests/helpers/wait-test.js @@ -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'); + }); +});