-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngMock) add atomic flushing strategy for $timeout #13183
Conversation
Add flag to `$timeout.flush()` function. If set to `true` — will handle only the tasks available at the moment of execution, otherwise will process the tasks that were added during execution as well (`false` by default).
@@ -103,8 +103,11 @@ angular.mock.$Browser = function() { | |||
* Flushes all pending requests and executes the defer callbacks. | |||
* | |||
* @param {number=} number of milliseconds to flush. See {@link #defer.now} | |||
* @param {boolean=} [atomic=false] If set to `true` — will handle only tasks | |||
* available at the moment of execution, otherwise will process tasks that was |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that were*
+1, using on one of my projects, works great |
@atogata, thanks. Spelling corrected. |
@elkeis - did you know that you can pass a |
Oh I just looked at the code. I see that you want to ensure that new calls to timeout that are triggered as part of flushing the current timeouts are not triggered... |
Interesting idea. I think this would need some more API thought as the current |
This is very vaguely related to #5420. It would be interesting to solve both at once... |
@elkeis once #14686 lands then as long as your timeouts have a non-zero delay, then they will indeed become separable in tests. Given your original example, if I add 1ms second delays: $timeout(function() {
action1();
$timeout(function() {
action2();
}, 1);
}, 1); Calling Is this enough for your testing? |
Closing as #14686 has landed and there has been activity on this PR for 10 months. |
Hi dear Angular community,
During writing unit tests I found one interesting issue. I need to write tests for some code like this:
And I'm expecting that I need to execute
$timeout.flush()
2 times. First time to reachaction1
function and the second time to executeaction2
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 .On 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:
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:
What do you think, guys?
P.S. this is a duplicate of #13146, I opened a new one because can't solve the certificate issue for commit with wrong email address.