jest-mock-promise
Advanced tools
Comparing version 1.0.12 to 1.0.13
@@ -78,15 +78,7 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__ = __webpack_require__(1); | ||
/** | ||
@@ -111,16 +103,8 @@ * Synchronous Promise, which gets settled (resolved or settled) in a synchronous manner. | ||
*/ | ||
var PromiseState; | ||
(function (PromiseState) { | ||
PromiseState[PromiseState["pending"] = 0] = "pending"; | ||
PromiseState[PromiseState["resolved"] = 1] = "resolved"; | ||
PromiseState[PromiseState["rejected"] = 2] = "rejected"; | ||
})(PromiseState || (PromiseState = {})); | ||
var JestMockPromise = function () { | ||
function JestMockPromise(callbackFn) { | ||
_classCallCheck(this, JestMockPromise); | ||
class JestMockPromise { | ||
constructor(callbackFn) { | ||
this.handlers = []; | ||
this.handlerIx = 0; | ||
this.state = PromiseState.pending; | ||
this.state = __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].pending; | ||
// if given, calling the given function | ||
@@ -135,171 +119,161 @@ if (callbackFn) { | ||
*/ | ||
_createClass(JestMockPromise, [{ | ||
key: "resolveFn", | ||
value: function resolveFn(data) { | ||
this.data = data; | ||
this.state = PromiseState.resolved; | ||
this.err = void 0; | ||
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) { | ||
var el = this.handlers[this.handlerIx]; | ||
var returnedValue; | ||
// stop the execution at first `catch` handler you run into | ||
if (el.catch) { | ||
break; | ||
} | ||
try { | ||
// calling a `then` handler | ||
returnedValue = el.then(this.data); | ||
} catch (ex) { | ||
// in case `then` handler throws an error | ||
// > pass it down to a first `catch` handler | ||
this.handlerIx++; | ||
this.rejectFn(ex); | ||
} | ||
if (returnedValue !== void 0) { | ||
// IF handler returned a value | ||
// > use it as the `data` for all the handlers which will be called next | ||
this.data = returnedValue; | ||
} | ||
resolveFn(data) { | ||
this.data = data; | ||
this.state = __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].resolved; | ||
this.err = void 0; | ||
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) { | ||
var el = this.handlers[this.handlerIx]; | ||
var returnedValue; | ||
// stop the execution at first `catch` handler you run into | ||
if (el.catch) { | ||
break; | ||
} | ||
; | ||
} | ||
}, { | ||
key: "rejectFn", | ||
/** | ||
* Rejects the given promise | ||
* @param err error object which is to be passed as a param to `catch` function | ||
*/ | ||
value: function rejectFn(err) { | ||
this.state = PromiseState.rejected; | ||
this.err = err; | ||
// find the first `catch` handler and call it | ||
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) { | ||
var el = this.handlers[this.handlerIx], | ||
returnedValue; | ||
if (el.catch) { | ||
returnedValue = el.catch(err); | ||
// try executing `then` handlers which follow | ||
this.handlerIx++; | ||
this.resolveFn(returnedValue); | ||
// stop the execution as soon as you run into a first catch element | ||
break; | ||
} | ||
try { | ||
// calling a `then` handler | ||
returnedValue = el.then(this.data); | ||
} | ||
; | ||
} | ||
/** | ||
* Appends fulfillment and rejection handlers to the promise, | ||
* and returns a new promise resolving to the return value of | ||
* the called handler, or to its original settled value if the | ||
* promise was not handled (i.e. if the relevant handler | ||
* onFulfilled or onRejected is not a function). | ||
* @param onFulfilled fulfillment handler function | ||
* @param onRejected rejection handler function | ||
*/ | ||
}, { | ||
key: "then", | ||
value: function then(onFulfilled, onRejected) { | ||
// if the promise is already settled (resolved or rejected) | ||
// > call the apropriate handler | ||
switch (this.state) { | ||
case PromiseState.rejected: | ||
if (onRejected) { | ||
onRejected(this.err); | ||
} | ||
break; | ||
case PromiseState.resolved: | ||
onFulfilled(this.data); | ||
break; | ||
default: | ||
this.handlers.push({ then: onFulfilled }); | ||
if (onRejected) { | ||
this.handlers.push({ catch: onRejected }); | ||
} | ||
catch (ex) { | ||
// in case `then` handler throws an error | ||
// > pass it down to a first `catch` handler | ||
this.handlerIx++; | ||
this.rejectFn(ex); | ||
} | ||
return this; | ||
if (returnedValue !== void 0) { | ||
// IF handler returned a value | ||
// > use it as the `data` for all the handlers which will be called next | ||
this.data = returnedValue; | ||
} | ||
} | ||
/** | ||
* Appends a rejection handler callback to the promise, | ||
* and returns a new promise resolving to the return | ||
* value of the callback if it is called, or to its | ||
* original fulfillment value if the promise is instead | ||
* fulfilled. | ||
* @param onRejected rejection handler function | ||
*/ | ||
}, { | ||
key: "catch", | ||
value: function _catch(onRejected) { | ||
// if the promise is already rejected | ||
// > call the handler right away | ||
if (this.state === PromiseState.resolved) { | ||
onRejected(this.err); | ||
} else { | ||
this.handlers.push({ catch: onRejected }); | ||
; | ||
} | ||
; | ||
/** | ||
* Rejects the given promise | ||
* @param err error object which is to be passed as a param to `catch` function | ||
*/ | ||
rejectFn(err) { | ||
this.state = __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].rejected; | ||
this.err = err; | ||
// find the first `catch` handler and call it | ||
for (var maxIx = this.handlers.length; this.handlerIx < maxIx; this.handlerIx++) { | ||
var el = this.handlers[this.handlerIx], returnedValue; | ||
if (el.catch) { | ||
returnedValue = el.catch(err); | ||
// try executing `then` handlers which follow | ||
this.handlerIx++; | ||
this.resolveFn(returnedValue); | ||
// stop the execution as soon as you run into a first catch element | ||
break; | ||
} | ||
return this; | ||
} | ||
/** | ||
* Resolves the promise with the given promise data. | ||
* This is a non-standard method, which should be the last | ||
* one to be called, after all the fulfillment and rejection | ||
* handlers have been registered. | ||
* @param {*} data | ||
*/ | ||
}, { | ||
key: "resolve", | ||
value: function resolve(data) { | ||
this.resolveFn(data); | ||
; | ||
} | ||
/** | ||
* Appends fulfillment and rejection handlers to the promise, | ||
* and returns a new promise resolving to the return value of | ||
* the called handler, or to its original settled value if the | ||
* promise was not handled (i.e. if the relevant handler | ||
* onFulfilled or onRejected is not a function). | ||
* @param onFulfilled fulfillment handler function | ||
* @param onRejected rejection handler function | ||
*/ | ||
then(onFulfilled, onRejected) { | ||
// if the promise is already settled (resolved or rejected) | ||
// > call the apropriate handler | ||
switch (this.state) { | ||
case __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].rejected: | ||
if (onRejected) { | ||
onRejected(this.err); | ||
} | ||
break; | ||
case __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].resolved: | ||
onFulfilled(this.data); | ||
break; | ||
default: | ||
this.handlers.push({ then: onFulfilled }); | ||
if (onRejected) { | ||
this.handlers.push({ catch: onRejected }); | ||
} | ||
} | ||
/** | ||
* Rejects the promise with the given promise with the given error object. | ||
* This is a non-standard method, which should be the last | ||
* one to be called, after all the fulfillment and rejection | ||
* handlers have been registered. | ||
* @param {*} data | ||
*/ | ||
}, { | ||
key: "reject", | ||
value: function reject(err) { | ||
this.rejectFn(err); | ||
return (this); | ||
} | ||
/** | ||
* Appends a rejection handler callback to the promise, | ||
* and returns a new promise resolving to the return | ||
* value of the callback if it is called, or to its | ||
* original fulfillment value if the promise is instead | ||
* fulfilled. | ||
* @param onRejected rejection handler function | ||
*/ | ||
catch(onRejected) { | ||
// if the promise is already rejected | ||
// > call the handler right away | ||
if (this.state === __WEBPACK_IMPORTED_MODULE_0__jest_mock_promise_types__["a" /* PromiseState */].resolved) { | ||
onRejected(this.err); | ||
} | ||
/** | ||
* Creates a resolved promise with the given data | ||
* @param data data which should be passed to `then` handler functions | ||
*/ | ||
}], [{ | ||
key: "resolve", | ||
value: function resolve(data) { | ||
console.warn('a promise created via `JestMockPromise.resolve` will be executed async ... for sync execution call `resolve` method on an instance of `Promise`'); | ||
return new JestMockPromise(function (resolve, reject) { | ||
setTimeout(resolve(data), 0); | ||
}); | ||
else { | ||
this.handlers.push({ catch: onRejected }); | ||
} | ||
/** | ||
* Creates a rejected promise with the given data | ||
* @param err error object which is to be passed as a param to `catch` function | ||
*/ | ||
return (this); | ||
} | ||
/** | ||
* Resolves the promise with the given promise data. | ||
* This is a non-standard method, which should be the last | ||
* one to be called, after all the fulfillment and rejection | ||
* handlers have been registered. | ||
* @param {*} data | ||
*/ | ||
resolve(data) { | ||
this.resolveFn(data); | ||
} | ||
/** | ||
* Rejects the promise with the given promise with the given error object. | ||
* This is a non-standard method, which should be the last | ||
* one to be called, after all the fulfillment and rejection | ||
* handlers have been registered. | ||
* @param {*} data | ||
*/ | ||
reject(err) { | ||
this.rejectFn(err); | ||
} | ||
/** | ||
* Creates a resolved promise with the given data | ||
* @param data data which should be passed to `then` handler functions | ||
*/ | ||
static resolve(data) { | ||
console.warn('a promise created via `JestMockPromise.resolve` will be executed async ... for sync execution call `resolve` method on an instance of `Promise`'); | ||
return (new JestMockPromise((resolve, reject) => { | ||
setTimeout(resolve(data), 0); | ||
})); | ||
} | ||
/** | ||
* Creates a rejected promise with the given data | ||
* @param err error object which is to be passed as a param to `catch` function | ||
*/ | ||
static reject(err) { | ||
console.warn('a promise created via `JestMockPromise.reject` will be executed async ... for sync execution call `reject` method on an instance of `Promise`'); | ||
return (new JestMockPromise((resolve, reject) => { | ||
setTimeout(reject(err), 0); | ||
})); | ||
} | ||
; | ||
} | ||
/* harmony default export */ __webpack_exports__["default"] = (JestMockPromise); | ||
}, { | ||
key: "reject", | ||
value: function reject(err) { | ||
console.warn('a promise created via `JestMockPromise.reject` will be executed async ... for sync execution call `reject` method on an instance of `Promise`'); | ||
return new JestMockPromise(function (resolve, reject) { | ||
setTimeout(reject(err), 0); | ||
}); | ||
} | ||
}]); | ||
return JestMockPromise; | ||
}(); | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
exports.default = JestMockPromise; | ||
"use strict"; | ||
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return PromiseState; }); | ||
var PromiseState; | ||
(function (PromiseState) { | ||
PromiseState[PromiseState["pending"] = 0] = "pending"; | ||
PromiseState[PromiseState["resolved"] = 1] = "resolved"; | ||
PromiseState[PromiseState["rejected"] = 2] = "rejected"; | ||
})(PromiseState || (PromiseState = {})); | ||
/***/ }) | ||
@@ -306,0 +280,0 @@ /******/ ]); |
{ | ||
"name": "jest-mock-promise", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "Synchronous Promise Mock for testing with Jest", | ||
"main": "dist/jest-mock-promise.js", | ||
"types": "dist/jest-mock-promise.d.ts", | ||
"scripts": { | ||
@@ -12,5 +13,2 @@ "build": "webpack -p" | ||
"devDependencies": { | ||
"awesome-typescript-loader": "^3.4.0", | ||
"babel-core": "^6.26.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"ts-loader": "^3.1.1", | ||
@@ -17,0 +15,0 @@ "typescript": "^2.6.1", |
@@ -6,3 +6,3 @@ { | ||
"declaration": true, | ||
"declarationDir": "./dist/" | ||
"declarationDir": "./" | ||
}, | ||
@@ -9,0 +9,0 @@ "exclude": [ |
@@ -29,9 +29,4 @@ const path = require('path'); | ||
test: /\.ts$/, | ||
loader: "awesome-typescript-loader", | ||
exclude: /node_modules/, | ||
options: { | ||
useBabel: true, | ||
declaration: true, | ||
declarationDir: "./dist" | ||
} | ||
loader: "ts-loader", | ||
exclude: /node_modules/ | ||
}, | ||
@@ -38,0 +33,0 @@ ] |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
32968
3
9
404
0