Comparing version 1.2.1 to 3.0.0
'use strict'; | ||
// Keep track of mocks | ||
var mocks = []; | ||
var cache = new Map(); | ||
let mocks = []; | ||
const cache = new Map(); | ||
/** | ||
* Mocks a method of an object. | ||
* Mocks a value of an object. | ||
* | ||
* @param {Object} obj | ||
* @param {string} key | ||
* @param {!Function} method | ||
* @param {!Function|Object} value | ||
*/ | ||
var method = module.exports = (obj, key, method) => { | ||
method = method === undefined ? () => {} : method; | ||
var hasOwnProperty = obj.hasOwnProperty(key); | ||
const method = module.exports = (obj, key, value) => { | ||
const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, key); | ||
mocks.push({ | ||
@@ -32,3 +31,3 @@ obj, | ||
// Set a flag that checks if it is mocked | ||
var flag = cache.get(obj); | ||
let flag = cache.get(obj); | ||
if (!flag) { | ||
@@ -40,9 +39,18 @@ flag = new Set(); | ||
Object.defineProperty(obj, key, { | ||
writable: true, | ||
const descriptor = { | ||
configurable: true, | ||
enumerable: true, | ||
value: method | ||
}); | ||
}; | ||
if (value && (value.get || value.set)) { | ||
// Default to undefined | ||
descriptor.get = value.get; | ||
descriptor.set = value.set; | ||
} else { | ||
// Without getter/setter mode | ||
descriptor.value = value; | ||
descriptor.writable = true; | ||
} | ||
Object.defineProperty(obj, key, descriptor); | ||
}; | ||
@@ -54,4 +62,4 @@ | ||
method.restore = () => { | ||
for (var i = mocks.length - 1; i >= 0; i--) { | ||
var m = mocks[i]; | ||
for (let i = mocks.length - 1; i >= 0; i--) { | ||
let m = mocks[i]; | ||
if (!m.hasOwnProperty) { | ||
@@ -69,5 +77,5 @@ // Delete the mock key, use key on the prototype | ||
method.isMocked = function isMocked(obj, key) { | ||
var flag = cache.get(obj); | ||
method.isMocked = (obj, key) => { | ||
let flag = cache.get(obj); | ||
return flag ? flag.has(key) : false; | ||
}; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "1.2.1", | ||
"version": "3.0.0", | ||
"repository": { | ||
@@ -25,6 +25,6 @@ "type": "git", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.0" | ||
"mocha": "^6.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
@@ -31,0 +31,0 @@ "license": "MIT", |
@@ -22,2 +22,17 @@ # muk-prop.js | ||
Object props mocking with setter/getter. | ||
```js | ||
const muk = require('muk-prop'); | ||
const obj = { _a: 1 }; | ||
muk(obj, 'a', { | ||
set: (val) => obj._a = val * 2, | ||
get: (val) => obj._a, | ||
}); | ||
obj.a = 2; | ||
console.log(obj.a); // 4 | ||
``` | ||
Check if member has been mocked. | ||
@@ -29,3 +44,3 @@ | ||
Restore all mocked methods after tests. | ||
Restore all mocked methods/props after tests. | ||
@@ -32,0 +47,0 @@ ```js |
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
4512
67
65
4