How do you test emberjs routes?

After a few months, without looking at emberjs, I am trying to return to it now, and therefore I am trying to use a new router. And I would like to check my routes.

Has anyone tried to write some routing tests with emberjs?

Suppose the main router:

App.Router = Ember.Router.extend({ root: Ember.Route.extend({ index: Ember.Route.extend({ route: '/', connectOutlets: function(router, context) { router.get('applicationController').connectOutlet({name: 'home'}); } }) }) }) 

How do you verify that loading the root.index route root.index correctly?

+4
source share
2 answers

Here's how Ember already tested connectOutlet for you:

https://github.com/emberjs/ember.js/blob/master/packages/ember-views/tests/system/controller_test.js

 test("connectOutlet instantiates a view, controller, and connects them", function() { var postController = Ember.Controller.create(); var appController = TestApp.ApplicationController.create({ controllers: { postController: postController }, namespace: { PostView: TestApp.PostView } }); var view = appController.connectOutlet('post'); ok(view instanceof TestApp.PostView, "the view is an instance of PostView"); equal(view.get('controller'), postController, "the controller is looked up on the parent controllers hash"); equal(appController.get('view'), view, "the app controller view is set"); }); 

Other routing tests can be found at https://github.com/emberjs/ember.js/tree/master/packages/ember-routing/tests

+4
source

Here is the complete test using Jasmine and Sinon :

code:

 describe("Given the Router", function(){ var router = null; beforeEach(function(){ router = Router.create(); }); afterEach(function(){ router = null; }); it("Should be defined", function(){ expect(router).toBeDefined(); }); it("Should have an root route", function(){ expect(router.get("root")).toBeDefined(); }); describe("its root route", function(){ var root = null; beforeEach(function(){ root = router.get("root").create(); }); afterEach(function(){ root = null; }); it("should have an index route", function(){ expect(root.get("index")).toBeDefined(); }); describe("its index route", function(){ var indexRoute = null; beforeEach(function(){ indexRoute = root.get("index").create(); }); it ("should have route of /", function(){ expect(indexRoute.get("route")).toEqual("/"); }); it ("should connect the outlets to home", function(){ var fakeRouter = Em.Object.create({applicationController: {connectOutlet: function(){} } }); var connectOutletSpy = sinon.spy(fakeRouter.applicationController, "connectOutlet"); var methodCall = connectOutletSpy.withArgs({name:"home"}); indexRoute.connectOutlets(fakeRouter); expect(methodCall.calledOnce).toBeTruthy(); }); }); }); }); 

Hope this helps.

+11
source

All Articles