Search code examples
ember.jsember-simple-authember-testing

Ember Simple Auth 1.0 Testing Helpers


I've recently upgraded from 0.8 to 1.0 and my app is working correctly.

One thing that surprised me though and I still don't understand is how the new acceptance test helpers should be used.

Previously (0.8) I could write my test like this and they would pass:

test('sign in and sign out', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

  authenticateSession();

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });

  invalidateSession();

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });
});

However, after upgrading and rewriting them in the new format:

import { authenticateSession, invalidateSession } from 'instatube-app/tests/helpers/ember-simple-auth';

test('sign in and sign out', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

  authenticateSession(application);

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });

  invalidateSession(application);

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });
});

Only the first assertion now passes.

If I split them up into separate tests ie:

test('when signed out display sign in button', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

});

test('when signed in display sign out button', function(assert) {
  authenticateSession(application);
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });
});

Then these individual tests pass, but I cannot seem to get them working for actual use cases.

Any ideas why this is happening and how to fix it would be very much appreciated.


Solution

  • So in case anyone else runs into this issue I managed to solve it by including the helpers in the andThen waiters, like this:

    import { authenticateSession, invalidateSession } from 'instatube-app/tests/helpers/ember-simple-auth';
    
    test('sign in and sign out', function(assert) {
      visit('/');
    
      andThen(function() {
        assert.ok(find(':contains("Sign In")').length,
          'expected to see "Sign In"');
        authenticateSession(application);
      });
    
      andThen(function() {
        assert.ok(find(':contains("Sign Out")').length,
          'expected to see "Sign Out"');
        invalidateSession(application);
      });
    
      andThen(function() {
        assert.ok(find(':contains("Sign In")').length,
          'expected to see "Sign In"');
      });
    });