Last active
December 8, 2018 17:25
-
-
Save georgiee/fa2e9eec676cc0d6e63e66229595c51c to your computer and use it in GitHub Desktop.
One Angular Test can explain asynchronicity in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('setTimeout & tick & flushMicrotasks', fakeAsync(() => { | |
let state = []; | |
// add to the micro task queue — will be the first thing executed after this script block is done | |
Promise.resolve().then(function() { | |
state.push('promise result'); | |
}); | |
// add to the task queue — will be executed after this task and when the micro task queue is emptied | |
setTimeout(() => { state.push('timeout called'); }); | |
// add to the task queue after 2 seconds | |
setTimeout(() => { state.push('timeout called after 2s'); }, 2000); | |
expect(state).toEqual([]); | |
flushMicrotasks(); | |
expect(state).toEqual(['promise result']); | |
tick(); | |
expect(state).toEqual(['promise result', 'timeout called']); | |
tick(2000); | |
expect(state).toEqual(['promise result', 'timeout called', 'timeout called after 2s']); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment