Skip to content

Instantly share code, notes, and snippets.

@machty
Created September 29, 2016 16:45
Show Gist options
  • Save machty/574457b1f2d993cc5959a1d6d6c74e5b to your computer and use it in GitHub Desktop.
Save machty/574457b1f2d993cc5959a1d6d6c74e5b to your computer and use it in GitHub Desktop.
export default Component.extend({
fooValue: 0,
looper: task(function * () {
while(true) {
yield timeout(500, "x-foo#looper-tick");
this.incrementProperty('fooValue');
}
}).on('init')
})
test('x-foo', function(assert) => {
visit('/');
return waitForTimer("x-foo#looper-tick").then((timer) => {
assert.equal(find('.fooValue').text(), 0);
return timer.fire();
}).then(() => {
assert.equal(find('.fooValue').text(), 1);
});
});
@morhook
Copy link

morhook commented Jan 12, 2018

I think another possible approach might be with bending-time solutions. Kind of with a special mode where the concurrency tasks can be controlled with a fake-timer, but the internal ember loop-timers can be executed to run correctly the visit, click, etc.

@viniciussbs
Copy link

Considering your example, could I use async/await?

test('x-foo', async function(assert) => {
  visit('/');

  let timer = await waitForTimer("x-foo#looper-tick");

  assert.equal(find('.fooValue').text(), 0);
  await timer.fire();
  assert.equal(find('.fooValue').text(), 1);
});

@sukima
Copy link

sukima commented Apr 6, 2022

Adding to the thread. the following worked for me. Caveat: I used sinon.useFakeTimers during unit/integration testing and left as is during acceptance tests.

// Alternative to ember-concurrency's timeout which does not register test
// waiters (settled). Use this for polling tasks to avoid hung acceptance tests
const optOutSettledTimeout = (delay) => new Promise((r) => setTimeout(r, delay));
hooks.beforeEach(function () {
  this.clock = sinon.useFakeTimers();
});

hooks.afterEach(function () {
  this.clock.restore();
});

test('…', async function (assert) {
  
  await this.clock.nextAsync();
  
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment