0

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.

bravely
  • 185
  • 3
  • 12

1 Answers1

0

Your visit syntax isn't quite right, should be:

test('visiting users.show route', function(assert) {
  visit('/example1').then(function() {
    assert.equal(currentPath(), 'users.show');
    assert.equal(find('#username').text(), 'example1');
  });
});
Jakeii
  • 1,273
  • 9
  • 16
  • andThen simply waits for all promises(as opposed to a specific promise) to finish. Your method still ends up with `currentPath()` being `undefined`, so I don't think this is it. – bravely Apr 29 '15 at 01:34