// ... definitions omitted for brievety // This test case tests an asynchronous sequence of events. // The sequence is precisely controlled and the test itself reads chronologically top to bottom. describe('.remove', () => { it('removes a document from the local pouch', async () => { // "Block" those functions until we manually tell them to resume const localDBRemove = pauseOn(localDB.remove); const localDBPost = pauseOn(localDB.post); // Execute the function under test const result = syncDB.remove({...savedEntity}); // Wait for the function to hit the first hooked call await localDBRemove.then((resume) => { // Here we can make assertions while the function is awaiting on the result of the first call expect(localDB.remove).toHaveBeenLastCalledWith(savedEntity); // Resolve the first hooked call with some success resume.withSuccess({ id: savedEntity._id, ok: true, rev: savedEntity._rev }); }); // Wait for the function under test to hit the second hooked call await localDBPost.then((resume) => { expect(localDB.post).toHaveBeenLastCalledWith({ type: 'mutation', mutationType: 'delete', seq: 0, docId: savedEntity._id, docRev: savedEntity._rev }); resume.withSuccess({ id: 'mut1', ok: true, rev: '1.0' }); }); // We can now await the function to compute the final result. This will eventually resolve // because all of our blocking hooks have been dealt with. expect(await result).toEqual(savedEntity._id); }); // TODO: test all possible failure scenarios and assert recovery });