admin管理员组文章数量:1426451
I'm really not sure how to approach testing this? (spyOn?)
function reloadPage() {
$('#logo').click(function() {
location.reload();
})
}
Any advice would be great!
I'm really not sure how to approach testing this? (spyOn?)
function reloadPage() {
$('#logo').click(function() {
location.reload();
})
}
Any advice would be great!
Share Improve this question asked Apr 28, 2014 at 14:41 user3007294user3007294 9511 gold badge15 silver badges35 bronze badges1 Answer
Reset to default 4The reason you may be unsure about how to test this piece of code is because it's doing 2 different things and you should break it up into smaller chunks.
I see two distinct functions here:
- click event handling
- reload page
So why not break up the logic like so?
function reloadPage() {
location.reload();
}
function bindEvents() {
$('#logo').click(reloadPage);
}
Now you can test them separately using Spies:
describe('when the logo is clicked', function() {
var logo;
var handlers;
beforeEach(function() {
handlers = {
locationReload: location.reload, // handle for location.reload()
reloadPage: reloadPage // handle for your reloadPage()
};
logo = $('#logo').click(reloadPage);
// attach Spy on reloadPage() and let the function call through
spyOn(handlers, 'reloadPage').and.callThrough();
// attach Spy on location.reload()
spyOn(handlers, 'locationReload');
});
it('will execute reloadPage function', function() {
logo.trigger('click');
expect(handlers.reloadPage).toHaveBeenCalled();
});
it('will reload the page', function() {
logo.trigger('click');
expect(handlers.locationReload).toHaveBeenCalled();
});
afterEach(function() {
// clean up event bindings after each test
logo.off('click');
});
});
There's not much need to test that the reloadPage
handler was correctly added to the #logo
's click event because the test is simulating a .click()
and checking if reloadPage
gets called or not.
So most likely you'd only need to have the it('will reload the page')
spec, not both.
本文标签: javascriptLocation reload in JasmineStack Overflow
版权声明:本文标题:javascript - Location reload in Jasmine - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465306a2659507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论