Testing

Other topics

Waiting for promises in tests in elegant way

You can make function passed to test() method async - then you can use await keyword. Your test will wait until Promises resolve and testing asynchronous code becomes easier and more readable. In the following example call that returns a Promise is changeset.validate(). Please notice also wrapping set call in Ember.run. Setting quantity has asynchronous effects (observers, computed properties) and thus we need to wrap it in Ember.run.

test('quantity validation: greater than 0', async function (assert) {
    assert.expect(3);

    const model = this.subject({
        quantity: 1
    });

    const changeset = createChangeset(model);

    await changeset.validate();

    assert.ok(!changeset.get('error.quantity'));

    Ember.run(() => {
        changeset.set('quantity', -1);
    });

    await changeset.validate();

    assert.equal(changeset.get('error.quantity.validation.length'), 1);
    assert.ok(!changeset.get('isValid'));
});

Contributors

Topic Id: 10722

Example Ids: 32163

This site is not affiliated with any of the contributors.