-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
147187d
commit fc2f9ca
Showing
5 changed files
with
122 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
|
||
var Promise = _interopDefault(require('promise-polyfill')); | ||
|
||
const root = typeof window === 'undefined' ? global : window; | ||
|
||
|
||
function PromiseMock() { | ||
Promise.apply(this, arguments); | ||
} | ||
PromiseMock.prototype = Object.create(Promise.prototype); | ||
Object.keys(Promise).forEach(function (key) { | ||
PromiseMock[key] = Promise[key]; | ||
}); | ||
|
||
// Queue of waiting callbacks | ||
PromiseMock.waiting = []; | ||
|
||
|
||
/** | ||
* Execute a pending Promise | ||
*/ | ||
PromiseMock.run = function run(count) { | ||
var runTimes = count ? count : 1; | ||
|
||
if (PromiseMock.waiting.length === 0) { | ||
throw new Error('No Promises waiting. Can\'t Promise.run()') | ||
} | ||
|
||
while (runTimes > 0 && PromiseMock.waiting.length > 0) { | ||
PromiseMock.waiting.shift()(); | ||
runTimes--; | ||
} | ||
}; | ||
|
||
/** | ||
* Execute all pending Promises | ||
*/ | ||
PromiseMock.runAll = function runAll() { | ||
if (PromiseMock.waiting.length === 0) { | ||
throw new Error('No Promises waiting. Can\'t Promise.run()') | ||
} | ||
|
||
while (PromiseMock.waiting.length > 0) { | ||
PromiseMock.run(); | ||
} | ||
}; | ||
|
||
PromiseMock._orginal = null; | ||
PromiseMock.install = function install() { | ||
PromiseMock._original = root.Promise; | ||
PromiseMock._originalImmediate = Promise._immediateFn; | ||
// Update the immediate function to push to queue | ||
Promise._immediateFn = function mockImmediateFn(fn) { | ||
PromiseMock.waiting.push(fn); | ||
}; | ||
|
||
root.Promise = PromiseMock; | ||
}; | ||
|
||
PromiseMock.uninstall = function uninstall() { | ||
PromiseMock.clear(); | ||
if (PromiseMock._original) { | ||
root.Promise = PromiseMock._original; | ||
} | ||
|
||
if (PromiseMock._originalImmediate) { | ||
Promise._immediateFn = PromiseMock._originalImmediate; | ||
} | ||
}; | ||
|
||
/** | ||
* Get the result of a Promise synchronously, throws on Promise reject | ||
* @param {Promise} promise | ||
* @returns {*} | ||
*/ | ||
PromiseMock.getResult = function result(promise) { | ||
var result, error; | ||
promise.then(function (promResult) { | ||
result = promResult; | ||
}, function (promError) { | ||
error = promError; | ||
}); | ||
PromiseMock.runAll(); | ||
if (error) { | ||
throw error; | ||
} | ||
return result; | ||
}; | ||
|
||
/** | ||
* Clear all pending Promises | ||
*/ | ||
PromiseMock.clear = function clear() { | ||
PromiseMock.waiting = []; | ||
}; | ||
|
||
module.exports = PromiseMock; |
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
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
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