admin管理员组文章数量:1389749
I'm writing a library that supports browser navigation with the help of history.pushState and also catches the popstate event that municates when navigation takes place in the browser. As I'm trying to write Jasmine tests for this library, I'm wondering how I can mock history.pushState
and also fake the emission of the popstate
signal from window
? The following code snippets should elucidate the problem:
Library code:
var lib = (function() {
function navigate(path) {
history.pushState(null, null, path);
}
function onPopState(event) {
if (lib.callback) {
lib.callback(document.location);
}
}
$(window).bind('popstate', onPopState);
return {
navigate: navigate
};
})();
Test code (Jasmine):
describe("Test navigate", function() {
it("Should invoke callback upon state change", function() {
var invokedWith;
function callback(url) {
invokedWith = url;
}
lib.callback = callback;
lib.navigate('/path');
// Doesn't work, callback invoked asynchronously
expect(invokedWith).toEqual('/path');
});
});
Basically, I want to mock the history.pushState
function and emit a fake popstate
event from window
, so as to test the popstate
handling in lib
.
See also my fiddle for "working" code.
I'm writing a library that supports browser navigation with the help of history.pushState and also catches the popstate event that municates when navigation takes place in the browser. As I'm trying to write Jasmine tests for this library, I'm wondering how I can mock history.pushState
and also fake the emission of the popstate
signal from window
? The following code snippets should elucidate the problem:
Library code:
var lib = (function() {
function navigate(path) {
history.pushState(null, null, path);
}
function onPopState(event) {
if (lib.callback) {
lib.callback(document.location);
}
}
$(window).bind('popstate', onPopState);
return {
navigate: navigate
};
})();
Test code (Jasmine):
describe("Test navigate", function() {
it("Should invoke callback upon state change", function() {
var invokedWith;
function callback(url) {
invokedWith = url;
}
lib.callback = callback;
lib.navigate('/path');
// Doesn't work, callback invoked asynchronously
expect(invokedWith).toEqual('/path');
});
});
Basically, I want to mock the history.pushState
function and emit a fake popstate
event from window
, so as to test the popstate
handling in lib
.
See also my fiddle for "working" code.
Share Improve this question asked Jan 1, 2013 at 13:41 aknuds1aknuds1 68.2k69 gold badges206 silver badges320 bronze badges1 Answer
Reset to default 4You can spy on history.pushState
like this:
spyOn(history, 'pushState');
As you use jquery to bind the event you can simply trigger popstate
by yourself.
$(window).trigger('popstate')
本文标签: javascriptJasmineHow can I mock historypushState and also fake event emissionStack Overflow
版权声明:本文标题:javascript - Jasmine - How can I mock history.pushState and also fake event emission? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744696908a2620335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论