admin管理员组文章数量:1355655
I have this statement in my code, and I was wondering how I could test the setInterval using Jasmine.
const x = setInterval(() => {
const countdown = getElementById('countdownWrapper');
const systemTime = ...
const now = new Date().getTime();
const endTime = systemTime - now;
countdown.querySelector('.countdown-time').innerHTML = time();
if (endTime < 0) {
clearInterval(x);
countdown.classList.remove('countdown-time--show');
}
}, 1000);
systemTime is fed from an epoch value in a DATA-CONTENT attribute in the HTML.
Any help would be greatly appreciated
I have this statement in my code, and I was wondering how I could test the setInterval using Jasmine.
const x = setInterval(() => {
const countdown = getElementById('countdownWrapper');
const systemTime = ...
const now = new Date().getTime();
const endTime = systemTime - now;
countdown.querySelector('.countdown-time').innerHTML = time();
if (endTime < 0) {
clearInterval(x);
countdown.classList.remove('countdown-time--show');
}
}, 1000);
systemTime is fed from an epoch value in a DATA-CONTENT attribute in the HTML.
Any help would be greatly appreciated
Share Improve this question asked Jun 15, 2018 at 22:43 TakuhiiTakuhii 9672 gold badges9 silver badges29 bronze badges 1- Possible duplicate of How jasmine clock works? – mooga Commented Jun 15, 2018 at 22:50
2 Answers
Reset to default 6beforeEach(function() {
timerCallback = jasmine.createSpyObj("setInterval");
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it("causes a timeout to be called", function() {
setTimeout(function() {
timerCallback();
}, 1000);
expect(setInterval).not.toHaveBeenCalled();
jasmine.clock().tick(1001);
expect(setInterval).toHaveBeenCalled();
});
Correction to Benny's answer:
timerCallback = jasmine.createSpyObj("test", {setInterval: 0});
AND
expect(timerCallback.setInterval).not.toHaveBeenCalled();
Also, this DOESN'T test setInterval, that's been mocked here. This tests setTimeout!
本文标签: javascriptCan I test setInterval with JasmineStack Overflow
版权声明:本文标题:javascript - Can I test setInterval with Jasmine? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744034043a2579435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论