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

feat(ngMock) add atomic flushing strategy for $timeout #13146

Closed
wants to merge 1 commit into from

Conversation

elkeis
Copy link

@elkeis elkeis commented Oct 21, 2015

Hi dear angular community,

During writing unit-tests I found one interesting issue. I need to write tests for some code like this:

$timeout(function() {
 action1();
 $timeout(function() {
  action2();
 });
});

And I'm expecting that i need to execute $timeout.flush() 2 times. First time to reach action1 function and the second time to execute action2 function (let's call this kind of flush "atomic" for example). But for now, for the first flush() it executes the both handlers. One after another. Please check this example: http://jsbin.com/fuziri/edit?js,output .

At the other hand we have some cases in tests when this buggy, for the first look, behaviour can simplify our lifes. In such case for example:

 var callback = jasmine.createSpy();
 $timeout(function() {}, 0, false).then(callback);
 $timeout.flush();
 expect(callback).toHaveBeenCalled();

Here $timeout register new defered task to process callback using $browser.defer during flushing. So to execute callback (if you are using that atomic flush strategy) you should call $timeout.flush() twise.

I think that the both strategies of flushing are pretty useful. And may be it will be a good idea to allow to switch between them, using some additional parameter.

I implemented solution which works for me. What do you think about this?

Add flag to `$timeout.flush()` function. If set to `true` — will handle only tasks
available at the moment of execution, otherwise will process tasks that was
added during execution as well (false by default).
@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.

@elkeis elkeis closed this Oct 21, 2015
@elkeis elkeis reopened this Oct 21, 2015
@elkeis elkeis closed this Oct 27, 2015
@elkeis elkeis deleted the timeout-flush-improvement branch October 27, 2015 20:24
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants