I have this statement in my code, and I was wondering how I could test the setInterval using Jasmine.
JavaScript
x
14
14
1
const x = setInterval(() => {
2
const countdown = getElementById('countdownWrapper');
3
const systemTime =
4
const now = new Date().getTime();
5
const endTime = systemTime - now;
6
7
countdown.querySelector('.countdown-time').innerHTML = time();
8
9
if (endTime < 0) {
10
clearInterval(x);
11
countdown.classList.remove('countdown-time--show');
12
}
13
}, 1000);
14
systemTime is fed from an epoch value in a DATA-CONTENT attribute in the HTML.
Any help would be greatly appreciated
Advertisement
Answer
JavaScript
1
21
21
1
beforeEach(function() {
2
timerCallback = jasmine.createSpyObj("setInterval");
3
jasmine.clock().install();
4
});
5
6
afterEach(function() {
7
jasmine.clock().uninstall();
8
});
9
10
it("causes a timeout to be called", function() {
11
setTimeout(function() {
12
timerCallback();
13
}, 1000);
14
15
expect(setInterval).not.toHaveBeenCalled();
16
17
jasmine.clock().tick(1001);
18
19
expect(setInterval).toHaveBeenCalled();
20
});
21