From 42af8eada2803a54a98b4f792e60feb480d68a0c Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Sun, 25 Aug 2013 14:36:10 -0700 Subject: fix(mocks): $timeout#flush should not update time when empty When $timeout#flush is called with a delay and no task can be flushed within that delay, the current time should not be updated as that gets the mock into an inconsistent state. BREAKING CHANGE: if a tests was written around the buggy behavior the delays might be off now This would typically not be a problem, but because of the previous breaking change in $timeout.flush, the combination of two might be confusing and that's why we are documenting it. Old behavior: ``` doSomething(); //schedules task to execute in 500ms from now doOtherStuff(); //schedules task to execute in 600ms from now try { $timeout.flush(300); // throws "no task to be flushed" exception } catch(e) {}; $time.flush(200); //flushes only doSomething() task ``` New behavior: ``` doSomething(); //schedules task to execute in 500ms from now doOtherStuff(); //schedules task to execute in 600ms from now try { $timeout.flush(300); // throws "no task to be flushed" exception } catch(e) {}; $time.flush(200); // throws "no task to be flushed" exception again // because previous exception didn't move the time forward ``` Fixed test: ``` doSomething(); //schedules task to execute in 500ms from now doOtherStuff(); //schedules task to execute in 600ms from now try { $timeout.flush(300); // throws "no task to be flushed" exception } catch(e) {}; $time.flush(500); // flushes only doSomething() task ``` --- test/ngMock/angular-mocksSpec.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'test/ngMock/angular-mocksSpec.js') diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 88e1876f..67fd13a1 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -404,7 +404,7 @@ describe('ngMock', function() { expect(function() {$timeout.flush(100);}).toThrow(); expect(log).toEqual(['t1']); - $timeout.flush(900); + $timeout.flush(1000); expect(log).toEqual(['t1', 't2']); expect(function() {$timeout.flush();}).toThrow(); }); @@ -425,6 +425,21 @@ describe('ngMock', function() { }); + it('should not update the current time if an exception is thrown during a flush', function() { + $timeout(log.fn('t1'), 100); + $timeout(log.fn('t2'), 101); + + expect(function() { $timeout.flush(90); }).toThrow(); + expect(function() { $timeout.flush(90); }).toThrow(); + + $timeout.flush(100); + expect(log).toEqual(['t1']); + + $timeout.flush(1); + expect(log).toEqual(['t1', 't2']); + }); + + describe('verifyNoPendingTasks', function() { it('should throw an exception when not flushed', function() { -- cgit v1.2.3