Skip to content

Commit

Permalink
feat: add isMocked method to check if the member of the object is mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jun 11, 2016
1 parent c6e4500 commit 986493c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "1"
- "2"
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var mockMethod = require('./method');
var mockRequire = require('./require');


/**
* Mocks a method of an object.
* Mocks a member of an object.
*
* @param {Object|string} obj
* @param {!string|Object} key
Expand All @@ -17,13 +19,16 @@ var muk = module.exports = function mock(obj, key, method) {
}
};

/**
* check whether the member of the object is mocked
*/
muk.isMocked = mockMethod.isMocked;

/**
* Restore all mocks
*/
muk.restore = mockMethod.restore;


// delete this module from the cache so that the next time it gets
// require()'d it will be aware of the new parent
// in case it gets require()'d from a different directory
Expand Down
15 changes: 14 additions & 1 deletion lib/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// keep track of mocks
var mocks = [];
var cache = new Map();


/**
Expand Down Expand Up @@ -39,8 +40,15 @@ var method = module.exports = function mockMethod(obj, key, method) {
enumerable: true,
value: method
});
};

// set a flag that checks if it is mocked
var flag = cache.get(obj);
if (!flag) {
flag = new Set();
cache.set(obj, flag);
}
flag.add(key);
};

/**
* Restore all mocks
Expand Down Expand Up @@ -69,3 +77,8 @@ method.restore = function restoreMocks() {
requireMocks = [];
*/
};

method.isMocked = function isMocked(obj, key) {
var flag = cache.get(obj);
return flag ? flag.has(key) : false;
}
2 changes: 2 additions & 0 deletions lib/require.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var Module = require('module');
var path = require('path');

Expand Down
64 changes: 64 additions & 0 deletions test/method-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,70 @@ describe('Mock getter', function() {
});
});

describe('Mock check', function() {

afterEach(muk.restore);

it('Should check whether is mocked', function() {
var obj = {
a: 1,
};
assert.equal(muk.isMocked(obj, 'a'), false, 'obj should not be mocked');

muk(obj, 'a', 2);
assert.ok(muk.isMocked(obj, 'a'), 'obj should be mocked');
});

it('Should not be enumerable', function() {
var obj = {
a: 1,
};
muk(obj, 'a', 2);
assert.deepEqual(Object.keys(obj), ['a']);

var keys = [];
for (var key in obj) {
keys.push(key);
}
assert.deepEqual(keys, ['a']);
});

it('Should be restored', function() {
var obj = {
a: 1,
};
muk(obj, 'a', 2);
muk.restore();
assert.equal(obj.a, 1);
});

it('Should check different type', function() {
var obj = {
a: 1,
b: '1',
c: true,
d: {},
get e() {
return 1;
},
};
muk(obj, 'a', 2);
assert.ok(muk.isMocked(obj, 'a'));

muk(obj, 'b', '2');
assert.ok(muk.isMocked(obj, 'b'));

muk(obj, 'c', false);
assert.ok(muk.isMocked(obj, 'c'));

muk(obj, 'd', { d: 1 });
assert.ok(muk.isMocked(obj, 'd'));

muk(obj, 'e', 2);
assert.ok(muk.isMocked(obj, 'e'));
});
});

function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

0 comments on commit 986493c

Please sign in to comment.