Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test your emberjs routes?

After a few months without looking at emberjs, I'm trying to go back into it now, and I'm therefore trying the new router. And I would like to test my routes.

Has anybody tried to write some routing tests with emberjs ?

Let's suppose the very basic following 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 test that loading the root.index route properly loads the HomeView ?

like image 318
Damien MATHIEU Avatar asked Dec 05 '22 15:12

Damien MATHIEU


1 Answers

Here is the full test, using Jasmine & 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 it helps.

like image 168
Shai Reznik - HiRez.io Avatar answered Jan 27 '23 15:01

Shai Reznik - HiRez.io