14 Testing asynchronous components
Testing components whose behavior is purely synchronous is straightforward. But components can have asynchronous hooks or event handlers, so testing them becomes a bit more complicated (and interesting) in these cases.
The main question the tester must face is how you know when all the asynchronous jobs have finished executing and the component has re-rendered. In this chapter, you’ll implement a nextTick()
function. This function returns a Promise
that resolves when all the pending jobs in the scheduler have finished executing. This way, by await
ing this Promise
, you can be sure that the component has re-rendered, and you can check what’s in the Document Object Model (DOM).
NOTE You can find all the listings in this chapter in the listings/ch14/ directory of the book’s repository (http://mng.bz/y82p). The code in this chapter can be checked out from the ch14 label (http://mng.bz/MZWQ): $
git
switch
--detach
ch14
.