1

I'm not sure if I am doing the right approach to testing this?

Template.home.events({
    "click div#u-home-nav > button": function (event) {
    "use strict";
    var where = $(event.currentTarget).data("target").split("/")[1];

    if (where === "overview" || where === "frameworks" || where === "dashboard" || where === "dummy") {
        _.each(places, function (value) {
            if (value.main === where) {
                $("span#u-current-place").text(value.title);
            }
        });

        Router.go(where);
    }
});

I try following the solution in Location reload in Jasmine However I got the following: Error: No route found named undefined

describe("click routing", function () {
    "use strict";
    var element = $("div#u-home-nav.btn-group").find("button[data-target='/overview']")

    function goPage() {
        Router.go("/overview")
    }

    function bindEvents() {
        element.click(goPage);
    }

    describe('when the button is clicked', function () {
        var button;
        var handlers;
        beforeEach(function () {
            handlers = {
                location: Router.go(), // handle for Router.go()
                goPage: goPage         // handle for your goPage()
            };

            button = element.click(goPage);

            // attach Spy on goPage() and let the function call through
            spyOn(handlers, 'goPage').and.callThrough();

            // attach Spy on Router.go()
            spyOn(handlers, 'location');

        });

        it('will execute goPage function', function () {
            button.trigger('click');
            expect(handlers.goPage).toHaveBeenCalled();
        });

        it('will go to the page', function () {
            button.trigger('click');
            expect(handlers.location).toHaveBeenCalled();
        });

        afterEach(function () {
            // clean up event bindings after each test
            button.off('click');
        });

    });
});

Any advice would be great!

Community
  • 1
  • 1
Chris
  • 23
  • 3
  • When location is called, The method Router.go() is not going anywhere since there is no parameters in it.Therefore I tried to do Router.go("/overview"), however it give me location is not a method error. – Chris May 01 '15 at 16:20

1 Answers1

0

Try this:

describe("click routing", function () {
    "use strict";
    var element = $("div#u-home-nav.btn-group").find("button[data-target='/overview']");

    function goPage() {
        Router.go("/overview")
    }

    function bindEvents() {
        element.click(goPage);
    }

    describe('when the button is clicked', function () {
        var button;
        var handlers;
        beforeEach(function () {
            handlers = {
                location: Router.go(), // handle for Router.go()
                goPage: goPage         // handle for your goPage()
            };

            button = element;
            element.click(goPage);
            element.click();

            // attach Spy on goPage() and let the function call through
            spyOn(handlers, 'goPage').and.callThrough();

            // attach Spy on Router.go()
            spyOn(handlers, 'location');

        });

        it('will execute goPage function', function () {
            expect(handlers.goPage).toHaveBeenCalled();
        });

        it('will go to the page', function () {
            expect(handlers.location).toHaveBeenCalled();
        });

        afterEach(function () {
            // clean up event bindings after each test
            button.off('click');
        });

    });
});
marcel
  • 2,967
  • 1
  • 16
  • 25