2

Having the following controller and tests:

app/controllers/application.js

import Controller from '@ember/controller';
import { action } from '@ember/object';

export default class ApplicationController extends Controller {
  flag = false;

  @action
  raiseFlag() {
    this.flag = true;
  }

  @action
  async raiseFlagAsync() {
    await new Promise(resolve => setTimeout(resolve, 1000));
    this.flag = true;
  }
}

tests/unit/controllers/application-test.js

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Controller | application', function(hooks) {
  setupTest(hooks);

  test('it raises flag', function(assert) {
    let controller = this.owner.lookup('controller:application');
    assert.equal(controller.flag, false);
    controller.send('raiseFlag');
    assert.equal(controller.flag, true);
  });

  test('it raises flag asyncronously', async function(assert) {
    let controller = this.owner.lookup('controller:application');
    assert.equal(controller.flag, false);
    await controller.send('raiseFlagAsync');
    assert.equal(controller.flag, true);
  });
});

The first test case passes. The second test case fails (the async one)

What is the ember-octane way of waiting for the async action?

2 Answers2

2

The trick here is to not use send! Generally I would use send only if you need to bubble actions in the route chain. Its a bit a old concept and it has no return value. So await controller.send will not work.

You should just invoke the action directly:

test('it raises flag asyncronously', async function(assert) {
  let controller = this.owner.lookup('controller:application');
  assert.equal(controller.flag, false);
  await controller.raiseFlagAsync();
  assert.equal(controller.flag, true);
});
Lux
  • 17,835
  • 5
  • 43
  • 73
0

Not sure why it was so hard to find this information, maybe bad SEO.

import { waitUntil } from '@ember/test-helpers';

test('it raises flag asyncronously', async function(assert) {
  let controller = this.owner.lookup('controller:application');
  assert.equal(controller.flag, false);
  controller.send('raiseFlagAsync');

  await waitUntil(() => controller.flag === true);
});

If anyone comes up with a more ember-ish answer I'll accept that once instead