mardi 28 avril 2015

Ember.js dynamic routes not resolving in test, but work in production

So, I'm trying to use the Twitter-style URL syntax, allowing a user to go to example.com/quaunaut to visit the user page of the user with the username 'quaunaut'. I was able to accomplish this via:

app/router.js

export default Router.map(function() {
  this.route('users.show', { path: '/:user_username' });
});

app/routes/users/show.js

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('user', { username: params.user_username }).then(function(result) {
      return result.get('firstObject');
    });
  },

  serialize: function(model) {
    return { user_username: model.get('username') };
  }
});

Now, when live or run via ember s, this works fantastically. However, in tests, it seems for some reason to not resolve.

var application, server, USERS;
USERS = {
  'example1': [{
    id: 1,
    username: 'example1'
  }],
  'example2': [{
    id: 2,
    username: 'example2'
  }]
};

module('Acceptance: UsersShow', {
  beforeEach: function() {
    application = startApp();

    server = new Pretender(function() {
      this.get('/api/users', function(request) {
        return [
          201,
          { 'content-type': 'application/javascript' },
          JSON.stringify(USERS[request.queryParams.username])
        ];
      });
    });
  },

  afterEach: function() {
    Ember.run(application, 'destroy');
    server.shutdown();
  }
});

test('visiting users.show route', function(assert) {
  visit('/example1');

  andThen(function() {
    assert.equal(currentPath(), 'users.show');
    assert.equal(find('#username').text(), 'example1');
  });
});

Which results in the following test results:

Acceptance: UsersShow: visiting users.show route
    ✘ failed
         expected users.show
    ✘ failed
         expected example1

So, any ideas why currentPath() isn't resolving? If you also have any recommendations for better means to implement what I'm looking to do here, I'm certainly open to it.




Aucun commentaire:

Enregistrer un commentaire