Ember initializer async property undefined in integration test

335 views Asked by At

I'm trying to test my registration and login processes, the integration tests were passing perfectly prior to creating an initializer to extend the Ember-Simple-Auth Session object with the currentUser property.

It all works correctly in the browser, its just the tests now fail all in the sessionAuthenticationSucceeded action in the application route on the following line:

this.get('session.currentUser').then(function(user) {

with : TypeError: Cannot read property 'then' of undefined


/routes/application.js

import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  actions: {
    sessionAuthenticationSucceeded: function () {
      var self = this;
      this.get('session.currentUser').then(function(user) {
          if (user.get('account') && user.get('status') === 'complete'){
            self.transtionTo('home');
          } else {
            console.log('Need to complete Registration');
            self.transitionTo('me');
          }
      });
    }
  }
}

/initializers/custom-session.js

import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: 'custom-session',
  before: 'simple-auth',
  initialize: function(container) {
    // application.deferReadiness();
    Session.reopen({
      currentUser: function() {
        var id = this.get('user_id');
        if (!Ember.isEmpty(id)) {
          console.log('getting the current user');
          return container.lookup('store:main').find('user', id);
        }
      }.property('user_id')
    });
    // application.advanceReadiness();
  }
};

/tests/integration/visitor-signs-up-test.js

test('As a user with valid email and password', function(){
  var email = faker.internet.email();
  signUpUser(email, 'correctpassword', 'correctpassword');
  andThen(function(){
    equal(find('#logged-in-user').text(), email, 'User registered successfully as ' + email);
    equal(sessionIsAuthenticated(App), true, 'The session is Authenticated');
  });
});

test/helpers/registration-login.js

export function signUpUser(email, password, passwordConfirmation) {
  visit('/register').then(function(){
    fillIn('input.email', email);
    fillIn('input.password', password);
    fillIn('input.password-confirmation', passwordConfirmation);
    click('button.submit');
  });
}

I have tried using

application.deferReadiness()

as you can see commented out in the initializer (also pass in application in that instance) to ensure the async request has completed and user is available but that hasn't worked either.

I am using Pretender to intercept the api requests, but the call to api/v1/users/:id isn't being made at all during the tests.

The strange part is it works perfectly in the browser.

I'm trying to understand why this won't this work? Any guidance would be appreciated!

NB: I have also tried solution listed here and here with same outcome as above.

1

There are 1 answers

0
Obi On BEST ANSWER

I have figured out the problem, turns out I wasn't returning a user_id from the api/v1/users/sign_in request Pretender was intercepting hence when sessionAuthenticationSucceeded fired, there was no user_id available and thus currentUser was never being updated/triggered.

I'll leave all the code up there in case it helps somebody else. Comments or improvements to it are still very welcome!