Last active
September 4, 2019 09:24
-
-
Save JakeGinnivan/1f1c5e05ab9d1cce12da97ec42731381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class PromiseTracker { | |
promises = [] | |
track(promise) { | |
this.promises.push(promise) | |
} | |
middleware() { | |
return () => next => (action) => { | |
const result = next(action) | |
// Promise.resolve must return the same promise if the arg is a promise | |
// http://www.ecma-international.org/ecma-262/6.0/#sec-promise.resolve | |
if (Promise.resolve(result) === result) { | |
this.track(result) | |
} | |
return result | |
} | |
} | |
hasWork() { | |
return this.promises.length > 0 | |
} | |
waitForCompletion() { | |
const all = Promise.all([...this.promises]) | |
// Use setTimeout to put the resolution of this | |
// promise back onto the event loop, this can fix | |
// issues where tests have not re-rendered before | |
// trying to find elements | |
.then(() => new Promise(setTimeout)) | |
this.promises = [] | |
return all | |
} | |
} | |
// Usage | |
const promiseTracker = new PromiseTracker() | |
const store = configureStore( | |
rootReducer, | |
{}, | |
promiseTracker.middleware(), | |
thunk | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment