Testing Asynchronous Actions
Sometimes we need to test components that rely on asynchronous actions that happen at specific times. Angular provides a function called fakeAsync
which wraps our tests in a zone and gives us access to the tick
function, which will allow us to simulate the passage of time precisely.
Let's go back to the example of the QuoteComponent
component and rewrite the unit test using fakeAsync
:
Here we have a QuoteComponent
that has a getQuote
which triggers an asynchronous update. We have wrapped our entire test in fakeAsync
which will allow us to test the asynchronous behavior of our component (getQuote()
) using synchronous function calls by calling tick()
. We can then run detectChanges
and query the DOM for our expected result.
Last updated