testingember.jsember-testing

Ember -- component integration async tests aren't waiting until async calls are returned


I'm having a hard time testing async functionality in component integration tests. Input kicks off an async call to an endpoint, and when it returns, I send an action. I'm trying to test that the action sends the correct data.

I've tried putting my assertion in the wait() helper, but the assertion gets hit before the (dependent upon async) action is called.

Here's a twiddle showing that problem: https://ember-twiddle.com/79f9a80c639b642e538803ac64a1cf9d?openFiles=tests.integration.components.test-comp-test.js%2Ctemplates.components.test-comp.hbs

How can I correctly code my async component integration tests?


Solution

  • There are two things that fails your test:

    1. Firstly, never make use of setTimeout (window.setTimeout) to schedule some future work with Ember. Make use the Ember way of doing it; I mean Ember.run.later. For the same thing that happened to me with acceptance tests; please see the following question and look through comments on the answer. The reason is that; Ember's test helpers really cannot handle setTimeout as we expect it.
    2. You have a problem within the test itself; in the action handler you have written in test you need to change the name attribute instead of returning a promise.

    Anyway please see the following twiddle I have updated. Testing in general with Ember is kind of a pain; since I believe there is not a proper comprehensive documentation. Good luck!