Search code examples
ember.jsember-testing

Ember.js - Test router didTransition event


I am trying to test a custom mixin that reopens the Ember router and adds a listener to the didTransition event. This is the code:

// my_mixin.js
...
initialize: function(container, application) {
      Ember.Router.reopen(App.MyMixin);
      var router = container.lookup('router:main');
      router.on('didTransition', function() {
         this.myFunctionFromMixin();
      });
    },
...

Then, the test (in Coffeescript):

test 'hooks myMixinFunction into router didTransition event', ->
  expect(1)
  myMixinStub = sinon.stub(router, 'myFunctionFromMixin')            
  router.transitionTo('some/known/route')
  # What goes here?
  ok(myMixinStub.calledOnce)

Obviously, this is not working, since there must be a way to do the assertion once the router has finished transitioning. How would you do this?

Note that this is not an integration test, but a unit test for the mixin.


Solution

  • I ended up with the following test and it's working:

    Ember.run ->
      router.trigger('didTransition')
      ok(myMixinStub.calledOnce)