Skip to content

Commit

Permalink
Added tests for CanceledError.rethrow method;
Browse files Browse the repository at this point in the history
Refactored;
  • Loading branch information
DigitalBrainJS committed Dec 13, 2020
1 parent 8c9746b commit e3d9f6f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
33 changes: 7 additions & 26 deletions lib/c-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const {
toGenerator,
toArray,
buildDecorator,
getFnType
getFnType,
lazyBindMethods
} = require('./utils');

const {now} = Date;
Expand Down Expand Up @@ -1670,6 +1671,11 @@ prototype.removeEventListener = prototype.off;
}
});

lazyBindMethods(CPromise, Object.getOwnPropertyNames(CPromise).map(prop => {
const {value} = Object.getOwnPropertyDescriptor(CPromise, prop);
return typeof value === 'function' ? prop : '';
}).filter(Boolean));

Object.defineProperties(CPromise, Object.entries({
CPromise,
CanceledError,
Expand Down Expand Up @@ -1891,31 +1897,6 @@ Object.entries(decorators).forEach(([name, initializer]) => {
})
})

const lazyBindMethods = (obj, props) => {
props.forEach(prop => {
const symbol = Symbol(`${prop}Bound`);
const {value: fn} = Object.getOwnPropertyDescriptor(obj, prop);

Object.defineProperty(obj, prop, {
get: function () {
return this[symbol] || (this[symbol] = fn.bind(this))
},

set: function (v) {
throw Error(`Can not rewrite method ${prop} with ${v}`);
},

enumerable: true,
configurable: true
})
})
}

lazyBindMethods(CPromise, Object.getOwnPropertyNames(CPromise).map(prop => {
const {value} = Object.getOwnPropertyDescriptor(CPromise, prop);
return typeof value === 'function' ? prop : '';
}).filter(Boolean));

exports.CPromise = CPromise;
/**
* CanceledError class
Expand Down
2 changes: 1 addition & 1 deletion lib/canceled-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const _code = Symbol('code');
class CanceledError extends Error {
/**
* Constructs a new error instance
* @param {String} reason
* @param {String} [reason]
*/
constructor(reason = E_REASON_CANCELED) {
super();
Expand Down
23 changes: 22 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ function buildDecorator(builder, name) {
}
}

const lazyBindMethods = (obj, props) => {
props.forEach(prop => {
const symbol = Symbol(`${prop}Bound`);
const {value: fn} = Object.getOwnPropertyDescriptor(obj, prop);

Object.defineProperty(obj, prop, {
get: function () {
return this[symbol] || (this[symbol] = fn.bind(this))
},

set: function (v) {
throw Error(`Can not rewrite method ${prop} with ${v}`);
},

enumerable: true,
configurable: true
})
})
}

module.exports={
isThenable,
setImmediate: _setImmediate,
Expand All @@ -127,5 +147,6 @@ module.exports={
toGenerator,
toArray,
getFnType,
buildDecorator
buildDecorator,
lazyBindMethods
};
32 changes: 32 additions & 0 deletions test/tests/CanceledError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const assert = require('assert');
const {CPromise, E_REASON_TIMEOUT, E_REASON_DISPOSED} = require('../../lib/c-promise');
const {CanceledError} = CPromise;

module.exports = {
'CanceledError.throw': {
'should throw the error that is instance of CanceledError': function () {
const originalErr = new CanceledError();
try {
CanceledError.rethrow(originalErr);
} catch (err) {
assert.strictEqual(err, originalErr);
}
},

'should throw the error only if some error code matched': function () {
const originalErr = new CanceledError(E_REASON_TIMEOUT);
try {
CanceledError.rethrow(originalErr, E_REASON_DISPOSED);
} catch (err) {
assert.fail('should not throw');
}

try {
CanceledError.rethrow(originalErr, E_REASON_TIMEOUT);
} catch (err) {
assert.strictEqual(err, originalErr);
}
}
}
}

0 comments on commit e3d9f6f

Please sign in to comment.