button": function (event) {-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:24:07 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com