aurelia / testing

Simplifies the testing of UI components by providing an elegant, fluent interface for arranging test setups along with a number of runtime debug/test helpers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feature request: method to wait for task queue

manuel-guilbault opened this issue · comments

Many operations are asynchronous in the framework. For example, updating the target of a two-way binding is done asynchronously. When depending on such operations in a test, you need to enqueue the rest of the test on the task queue, which requires code that is repetitive and that adds noise to the tests.

For example:

import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
import {TaskQueue} from 'aurelia-framework';

describe('the my-component custom element', () => {
  let viewModel, component, taskQueue;

  beforeEach(() => {
    viewModel = { result = null };
    component = StageComponent
      .withResource('my-component')
      .inView('<my-component value.two-way="result"></my-component>')
      .boundTo(viewModel);
    component.bootstrap(aurelia => {
      aurelia.use.standardConfiguration();
      taskQueue = aurelia.container.get(TaskQueue);
    });
  });

  afterEach(() => {
    component.dispose();
  });

  it('should assign value when whatever', done => {
    //trigger something that should update the value property on the component
    taskQueue.queueMicroTask(() => {
      //assert something about viewModel.result
      done();
    });
  });
});

At the moment, we have to:

  • import the TaskQueue
  • define a custom bootstrap function to get the TaskQueue instance
  • manually queue a task

This last point is mostly annoying because it is not Promise-friendly. Since I use mostly jasmine-promises instead of the done callback, I am forced to wrap the call to queueMicroTask inside a Promise constructor.

All of this contribute to make tests bloated with details related to the Aurelia implementation. A new Promise-friendly method on ComponentTester that would wrap all of this could be nice. I could even quickly come up with a PR for this.

use .then(done) instead. Here's an example (needs to be split up, but you'll see what I mean): https://github.com/aurelia/validation/blob/master/test/basic.ts

Oh so the recommended solution is to use setTimeout instead of the task queue?

Using the task queue works, and would make your tests execute faster. You may want to wait two cycles though. Maybe the change and blur methods in the example should be exported from the testing repo?

Could be useful indeed.
And thanks for the example :)

So which is better? new Promise(setTimeout) or taskQueue.queueMicroTask()?

@RomkeVdMeulen https://github.com/aurelia/testing/pull/79/files see the use of flushMicroTaskQueue. I just couldn't merge it so far as it seems there is some bogus in getting it to work with our test-environment :(