cartodb-4.42/lib/assets/test/spec/fixtures/builder/fake-promise.fixture.js
2024-04-06 05:25:13 +00:00

42 lines
872 B
JavaScript

var fakePromise = function (object, functionName) {
var resolveFn = null;
var rejectFn = null;
spyOn(object, functionName).and.callFake(function () {
return new Promise(function (resolve, reject) {
resolveFn = resolve;
rejectFn = resolve;
});
});
function resolve (value) {
if (resolveFn) {
resolveFn(value);
resolveFn = null;
} else {
throw new Error('Tried to resolve a promise that has not been called.');
}
}
function reject (value) {
if (rejectFn) {
rejectFn(value);
rejectFn = null;
} else {
throw new Error('Tried to reject a promise that has not been called.');
}
}
function hasBeenCalled () {
return !!resolveFn || !!rejectFn;
}
return {
resolve: resolve,
reject: reject,
hasBeenCalled: hasBeenCalled
};
};
module.exports = fakePromise;