javascript - 在meteor-jasmine中测试router.go的点击事件

标签 javascript jquery meteor jasmine iron-router

我不确定我是否采用了正确的方法来测试这个?

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);
    }
});

我尝试按照Location reload in Jasmine中的解决方案进行操作 但是我得到了以下信息: 错误:未找到名为未定义的路由

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');
        });

    });
});

任何建议都会很棒!

最佳答案

试试这个:

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');
        });

    });
});

关于javascript - 在meteor-jasmine中测试router.go的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29904768/

相关文章:

javascript - jQuery UI 日期选择器 : don't highlight today when it is also selected

javascript - 在 meteor 模板中访问父数据上下文

javascript - 将javascript中的长数字格式化为正确的单位的最简单方法?

javascript - `inline-block` 父元素宽度错误

javascript - d3.js - d3.select ("#id).remove() or .attr("类”, "hidden")不工作

javascript - 如何将不透明度应用于非事件 slider 元素?

javascript - jquery:ajax 未在文档 getready 中触发

javascript - ASP.NET Ajax 不调用 Url

android - Meteor+Cordova - Android 上的 "deviceready has not fired"

javascript - 在点击事件中调用父函数