3

Struggling with acceptance tests. Started with basic login test:

import { test } from 'qunit';
import moduleForAcceptance from 'static/tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | authentication');

test('login', function(assert) {
  visit('/');
  click('.nav-bar__login-link');
  andThen(function() {
    assert.notOk(find('.login-form__submit-button').attr('disabled'));
  });

  fillIn('.login-form__email-block input', "ruz@email.com");
  fillIn('.login-form__password-block input', "qwe");
  click('.login-form__submit-button');

  andThen(function() {
    console.log("ftw");
    assert.equal(find('.nav-bar__profile-link').text(), "some");
  });
});

The problem is that andThen callback is called before authentication completes. It's jQuery ajax request and a few promises after. From what I can see ember waits for ajax query to complete, but doesn't wait for promises to get resolved/rejected. Should this test work out of the box? Do I have to write a custom waiter?

ruz
  • 482
  • 2
  • 7
  • you might want to take a look at the [following](https://medium.com/@chrisdmasters/testing-async-in-ember-js-part-one-7ec9bc070c0e) – feanor07 Apr 22 '17 at 12:24

1 Answers1

0

It sounds like your promises may not be setup right? But no, you should be able to write tests using the acceptance test helpers and not need to worry about async calls settling (or promises resolving) yourself

acorncom
  • 5,975
  • 1
  • 19
  • 31
  • I do not agree with that, `andThen` does not wait for even normal promises created via `new Ember.RSVP.Promise` but instead waits for `new Ember.Test.promise`s. Have you checked out [this stackoverflow question](http://stackoverflow.com/questions/26498845/using-ember-cli-how-do-i-get-an-acceptance-test-to-wait-for-a-promise) and [following discussion](https://medium.com/@chrisdmasters/testing-async-in-ember-js-part-one-7ec9bc070c0e). I would provide a simple case and be grateful if you can just make it work with right setup of promises. – feanor07 Apr 25 '17 at 06:34
  • Hmm, now you have me curious. https://github.com/emberjs/ember.js/blob/master/packages/ember-testing/lib/helpers/and_then.js indicates fairly clearly that andThen waits for promises, what is your setup that doesn't work? – acorncom Apr 25 '17 at 12:20
  • I will prepare a twiddle when I got home in the evening (UTC+03:00) hopefully. thx. – feanor07 Apr 25 '17 at 12:39
  • can you please check out the following [twiddle](https://ember-twiddle.com/a7d82abc565a0be25c8be0a7cb8ec10d?openFiles=routes.simple-ajax-route.js%2C&route=%2Frsvp-promise-route)? there are four routes that are identical to each other in terms of template. the difference lies in the promise usage upon an action firing after a button click. a simple ajax call without any Ember promise creation is simply waited by `andThen`. Similarly, `Ember.Test.Promise` works as expected. However; usage of `Ember.RSVP.Promise` and `window.setTimeout` are not being waited by `andThen`. – feanor07 Apr 25 '17 at 19:41
  • please un-comment the codes in `rsvp-promise-route-acceptance-test` and `simple-timeout-route-acceptance-test` files to make them fail. `andThen` will not wait for them and simply finish; hence they will fail. – feanor07 Apr 25 '17 at 19:44
  • In both the cases where you are having issues here, it looks like the central issue is the use of `window.setTimeout`. I switched those to the "Ember runloop aware" equivalent of `Ember.run.schedule` and things are running fine in [this twiddle](https://ember-twiddle.com/211e91357ce01bca31c745690a8f182c?openFiles=tests.acceptance.simple-timeout-route-acceptance-test.js%2C&route=%2Ftest-promise-route). Check [this revision](https://gist.github.com/acorncom/211e91357ce01bca31c745690a8f182c/revisions) for my changes. Does that help? – acorncom Apr 26 '17 at 00:40
  • i will never use `window.setTimeout` again :) thank you very much; now I am relieved. – feanor07 Apr 26 '17 at 06:15
  • Glad to hear it :-) – acorncom Apr 26 '17 at 11:42
  • Guys, thanks for attention. I thought that I'm crazy. @acorncom is it intentional that you put jQuery 1.x into deps? ajax test fails if you put 3.2.1 as a dependency. – ruz Apr 26 '17 at 16:42
  • It fails from time to time while using twiddle, so you should keep refreshing a twiddle. Check the following [twiddle](https://ember-twiddle.com/a553593f84a388ef55e493171355e0a8/45e2e963e99df4d9b3083df3299f54cb90b4c531?openFiles=twiddle.json%2C&route=%2Ftest-promise-route) – ruz Apr 26 '17 at 17:06
  • Tests I have around fail with jQuery 3.0.0+, 2.2.4 works fine. Looks like it's related to jquery's Deferred class becoming promise/a+ compliant. See [release notes](https://jquery.com/upgrade-guide/3.0/#breaking-change-and-feature-jquery-deferred-is-now-promises-a-compatible) that says "that Deferred .then() callbacks are always called asynchronously". See also, [promise/a+ spec](https://promisesaplus.com/), especially [point 2.2.4](https://promisesaplus.com/#point-34) and [note 3.1](https://promisesaplus.com/#notes). And of course deferred.js in 3.0.0+ has setTimeout call :) – ruz Apr 26 '17 at 18:34
  • Looks like this jQuery 3 issue is being discussed here: https://github.com/emberjs/ember.js/issues/14566 – acorncom Apr 27 '17 at 09:25