Skip to content

Commit 986493c

Browse files
committed
feat: add isMocked method to check if the member of the object is mocked
1 parent c6e4500 commit 986493c

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- "0.10"
43
- "0.12"
54
- "1"
65
- "2"

lib/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
'use strict';
2+
13
var mockMethod = require('./method');
24
var mockRequire = require('./require');
35

46

57
/**
6-
* Mocks a method of an object.
8+
* Mocks a member of an object.
79
*
810
* @param {Object|string} obj
911
* @param {!string|Object} key
@@ -17,13 +19,16 @@ var muk = module.exports = function mock(obj, key, method) {
1719
}
1820
};
1921

22+
/**
23+
* check whether the member of the object is mocked
24+
*/
25+
muk.isMocked = mockMethod.isMocked;
2026

2127
/**
2228
* Restore all mocks
2329
*/
2430
muk.restore = mockMethod.restore;
2531

26-
2732
// delete this module from the cache so that the next time it gets
2833
// require()'d it will be aware of the new parent
2934
// in case it gets require()'d from a different directory

lib/method.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// keep track of mocks
44
var mocks = [];
5+
var cache = new Map();
56

67

78
/**
@@ -39,8 +40,15 @@ var method = module.exports = function mockMethod(obj, key, method) {
3940
enumerable: true,
4041
value: method
4142
});
42-
};
4343

44+
// set a flag that checks if it is mocked
45+
var flag = cache.get(obj);
46+
if (!flag) {
47+
flag = new Set();
48+
cache.set(obj, flag);
49+
}
50+
flag.add(key);
51+
};
4452

4553
/**
4654
* Restore all mocks
@@ -69,3 +77,8 @@ method.restore = function restoreMocks() {
6977
requireMocks = [];
7078
*/
7179
};
80+
81+
method.isMocked = function isMocked(obj, key) {
82+
var flag = cache.get(obj);
83+
return flag ? flag.has(key) : false;
84+
}

lib/require.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
var Module = require('module');
24
var path = require('path');
35

test/method-test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,70 @@ describe('Mock getter', function() {
162162
});
163163
});
164164

165+
describe('Mock check', function() {
166+
167+
afterEach(muk.restore);
168+
169+
it('Should check whether is mocked', function() {
170+
var obj = {
171+
a: 1,
172+
};
173+
assert.equal(muk.isMocked(obj, 'a'), false, 'obj should not be mocked');
174+
175+
muk(obj, 'a', 2);
176+
assert.ok(muk.isMocked(obj, 'a'), 'obj should be mocked');
177+
});
178+
179+
it('Should not be enumerable', function() {
180+
var obj = {
181+
a: 1,
182+
};
183+
muk(obj, 'a', 2);
184+
assert.deepEqual(Object.keys(obj), ['a']);
185+
186+
var keys = [];
187+
for (var key in obj) {
188+
keys.push(key);
189+
}
190+
assert.deepEqual(keys, ['a']);
191+
});
192+
193+
it('Should be restored', function() {
194+
var obj = {
195+
a: 1,
196+
};
197+
muk(obj, 'a', 2);
198+
muk.restore();
199+
assert.equal(obj.a, 1);
200+
});
201+
202+
it('Should check different type', function() {
203+
var obj = {
204+
a: 1,
205+
b: '1',
206+
c: true,
207+
d: {},
208+
get e() {
209+
return 1;
210+
},
211+
};
212+
muk(obj, 'a', 2);
213+
assert.ok(muk.isMocked(obj, 'a'));
214+
215+
muk(obj, 'b', '2');
216+
assert.ok(muk.isMocked(obj, 'b'));
217+
218+
muk(obj, 'c', false);
219+
assert.ok(muk.isMocked(obj, 'c'));
220+
221+
muk(obj, 'd', { d: 1 });
222+
assert.ok(muk.isMocked(obj, 'd'));
223+
224+
muk(obj, 'e', 2);
225+
assert.ok(muk.isMocked(obj, 'e'));
226+
});
227+
});
228+
165229
function hasOwnProperty(obj, prop) {
166230
return Object.prototype.hasOwnProperty.call(obj, prop);
167231
}

0 commit comments

Comments
 (0)