Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f14938b

Browse files
committed
fixup! feat(ngMock): add $flushPendingTasks() and $verifyNoPendingTasks()
1 parent ebfa1a8 commit f14938b

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/ngMock/angular-mocks.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,14 @@ angular.mock.$Browser.prototype = {
244244
* @name $flushPendingTasks
245245
*
246246
* @description
247-
* Flushes all pending tasks and executes the corresponding callbacks.
247+
* Flushes all currently pending tasks and executes the corresponding callbacks.
248+
*
249+
* Optionally, you can also pass a `delay` argument to only flush tasks that are scheduled to be
250+
* executed within `delay` milliseconds. Currently, `delay` only applies to timeouts, since all
251+
* other tasks have a delay of 0 (i.e. they are scheduled to be executed as soon as possible, but
252+
* still asynchronously).
253+
*
254+
* If no delay is specified, it uses a delay such that all currently pending tasks are flushed.
248255
*
249256
* The types of tasks that are flushed include:
250257
*

test/ngMock/angular-mocksSpec.js

+61-4
Original file line numberDiff line numberDiff line change
@@ -626,16 +626,17 @@ describe('ngMock', function() {
626626

627627
it('should flush delayed', function() {
628628
browser.defer(logFn('A'));
629-
browser.defer(logFn('B'), 10, 'taskType');
630-
browser.defer(logFn('C'), 20);
629+
browser.defer(logFn('B'), 0, 'taskTypeB');
630+
browser.defer(logFn('C'), 10, 'taskTypeC');
631+
browser.defer(logFn('D'), 20);
631632
expect(log).toEqual('');
632633
expect(browser.defer.now).toEqual(0);
633634

634635
browser.defer.flush(0);
635-
expect(log).toEqual('A;');
636+
expect(log).toEqual('A;B;');
636637

637638
browser.defer.flush();
638-
expect(log).toEqual('A;B;C;');
639+
expect(log).toEqual('A;B;C;D;');
639640
});
640641

641642
it('should defer and flush over time', function() {
@@ -663,6 +664,62 @@ describe('ngMock', function() {
663664
it('should not throw an exception when passing a specific delay', function() {
664665
expect(function() {browser.defer.flush(100);}).not.toThrow();
665666
});
667+
668+
describe('tasks scheduled during flushing', function() {
669+
it('should be flushed if they do not exceed the target delay (when no delay specified)',
670+
function() {
671+
browser.defer(function() {
672+
logFn('1')();
673+
browser.defer(function() {
674+
logFn('3')();
675+
browser.defer(logFn('4'), 1);
676+
}, 2);
677+
}, 1);
678+
browser.defer(function() {
679+
logFn('2')();
680+
browser.defer(logFn('6'), 4);
681+
}, 2);
682+
browser.defer(logFn('5'), 5);
683+
684+
browser.defer.flush(0);
685+
expect(browser.defer.now).toEqual(0);
686+
expect(log).toEqual('');
687+
688+
browser.defer.flush();
689+
expect(browser.defer.now).toEqual(5);
690+
expect(log).toEqual('1;2;3;4;5;');
691+
}
692+
);
693+
694+
it('should be flushed if they do not exceed the specified delay',
695+
function() {
696+
browser.defer(function() {
697+
logFn('1')();
698+
browser.defer(function() {
699+
logFn('3')();
700+
browser.defer(logFn('4'), 1);
701+
}, 2);
702+
}, 1);
703+
browser.defer(function() {
704+
logFn('2')();
705+
browser.defer(logFn('6'), 4);
706+
}, 2);
707+
browser.defer(logFn('5'), 5);
708+
709+
browser.defer.flush(0);
710+
expect(browser.defer.now).toEqual(0);
711+
expect(log).toEqual('');
712+
713+
browser.defer.flush(4);
714+
expect(browser.defer.now).toEqual(4);
715+
expect(log).toEqual('1;2;3;4;');
716+
717+
browser.defer.flush(6);
718+
expect(browser.defer.now).toEqual(10);
719+
expect(log).toEqual('1;2;3;4;5;6;');
720+
}
721+
);
722+
});
666723
});
667724

668725
describe('defer.cancel', function() {

0 commit comments

Comments
 (0)