From eb48c836491b504963b7e01107c7843d19f4af3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Girardi?= Date: Thu, 28 Aug 2014 20:58:30 -0400 Subject: [PATCH] Add specs for intervals that "clear themselves" Add specs to test if issue #655 is present: the handler of an interval cannot successfully clear the same interval that generated it's invocation. The most direct test consist in setting an interval with a handler that calls clearInterval over that same interval and make the clock tick for double of it's period. If the issue is present the interval's handler will be called twice. If the issue is not present, the first invocation of the handler will avoid a second one (because of the clearInterval). Another test is included in order to check if recurring scheduled functions are rescheduled before being called. Doing this in the reverse order is the exact cause of the issue. --- spec/core/ClockSpec.js | 18 ++++++++++++++++++ spec/core/DelayedFunctionSchedulerSpec.js | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/spec/core/ClockSpec.js b/spec/core/ClockSpec.js index e7f92811..18d281bf 100644 --- a/spec/core/ClockSpec.js +++ b/spec/core/ClockSpec.js @@ -323,6 +323,24 @@ describe("Clock (acceptance)", function() { expect(clearedFn).not.toHaveBeenCalled(); }); + it("can clear a previously set interval using that interval's handler", function() { + var spy = jasmine.createSpy('spy'), + delayedFunctionScheduler = new j$.DelayedFunctionScheduler(), + mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} }, + clock = new j$.Clock({setInterval: function() {}}, delayedFunctionScheduler, mockDate), + intervalId; + + clock.install(); + + intervalId = clock.setInterval(function() { + spy(); + clock.clearInterval(intervalId); + }, 100); + clock.tick(200); + + expect(spy.calls.count()).toEqual(1); + }); + it("correctly schedules functions after the Clock has advanced", function() { var delayedFn1 = jasmine.createSpy('delayedFn1'), delayedFunctionScheduler = new j$.DelayedFunctionScheduler(), diff --git a/spec/core/DelayedFunctionSchedulerSpec.js b/spec/core/DelayedFunctionSchedulerSpec.js index 7048b9e8..37dfa766 100644 --- a/spec/core/DelayedFunctionSchedulerSpec.js +++ b/spec/core/DelayedFunctionSchedulerSpec.js @@ -242,5 +242,18 @@ describe("DelayedFunctionScheduler", function() { expect(innerFn).toHaveBeenCalled(); }); + it("executes recurring functions after rescheduling them", function () { + var scheduler = new j$.DelayedFunctionScheduler(), + recurring = function() { + expect(scheduler.scheduleFunction).toHaveBeenCalled(); + }; + + scheduler.scheduleFunction(recurring, 10, [], true); + + spyOn(scheduler, "scheduleFunction"); + + scheduler.tick(10); + }); + });