Skip to content

Commit

Permalink
Merge pull request #34 from stalniy/spy.on
Browse files Browse the repository at this point in the history
Improved interface of spy.on
  • Loading branch information
keithamus committed Oct 16, 2015
2 parents 58f50ec + 790093f commit 0e847b6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -69,15 +69,19 @@ ee.on('some event', spy);
var spy_again = chai.spy();
ee.on('some other event', spy_again);

//or you can track object methods calls
// or you can track an object's method
var array = [ 1, 2, 3 ];
chai.spy.on(array, 'push');

// or you can track multiple object's methods
chai.spy.on(array, 'push', 'pop');

array.push(5);

//and you can reset the object calls
// and you can reset the object calls
array.push.reset();

//or you can create spy object
// or you can create spy object
var object = chai.spy.object([ 'push', 'pop' ]);
object.push(5);

Expand Down
14 changes: 9 additions & 5 deletions chai-spies.js
Expand Up @@ -115,15 +115,19 @@
* var spy = chai.spy.on(Array, 'isArray');
*
* @param {Object} object
* @param {String} method name to spy on
* @returns function to actually call
* @param {...String} method names to spy on
* @returns passed object
* @api public
*/

chai.spy.on = function (object, methodName) {
object[methodName] = chai.spy(object[methodName]);
chai.spy.on = function (object) {
var methodNames = Array.prototype.slice.call(arguments, 1);

methodNames.forEach(function(methodName) {
object[methodName] = chai.spy(object[methodName]);
});

return object[methodName];
return object;
};

/**
Expand Down
14 changes: 9 additions & 5 deletions lib/spy.js
Expand Up @@ -99,15 +99,19 @@ module.exports = function (chai, _) {
* var spy = chai.spy.on(Array, 'isArray');
*
* @param {Object} object
* @param {String} method name to spy on
* @returns function to actually call
* @param {...String} method names to spy on
* @returns passed object
* @api public
*/

chai.spy.on = function (object, methodName) {
object[methodName] = chai.spy(object[methodName]);
chai.spy.on = function (object) {
var methodNames = Array.prototype.slice.call(arguments, 1);

methodNames.forEach(function(methodName) {
object[methodName] = chai.spy(object[methodName]);
});

return object[methodName];
return object;
};

/**
Expand Down
13 changes: 9 additions & 4 deletions test/spies.js
Expand Up @@ -208,10 +208,8 @@ describe('Chai Spies', function () {
spyClean.should.have.length(0);
});

it('spies specified object method', function() {
var array = [];

chai.spy.on(array, 'push');
it('should spy specified object method', function () {
var array = chai.spy.on([], 'push');
array.push(1, 2);

array.push.should.be.a.spy;
Expand All @@ -226,6 +224,13 @@ describe('Chai Spies', function () {
spy().should.equal(value);
});

it('should spy multiple object methods passed as array', function () {
var array = chai.spy.on([], 'push', 'pop');

array.push.should.be.a.spy;
array.pop.should.be.a.spy;
});

describe('.with', function () {
it('should not interfere chai with' ,function () {
(1).should.be.with.a('number');
Expand Down

0 comments on commit 0e847b6

Please sign in to comment.