Skip to content

Commit

Permalink
Fix #1638: Make resetHistory work for props also
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Jan 26, 2018
1 parent b37820a commit 3551fdf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/sinon/collection.js
Expand Up @@ -47,12 +47,29 @@ var collection = {
},

resetHistory: function resetHistory() {
function privateResetHistory(f) {
var method = f.resetHistory || f.reset;
if (method) {
method.call(f);
}
}

getFakes(this).forEach(function (fake) {
var method = fake.resetHistory || fake.reset;
if (typeof fake === "function") {
privateResetHistory(fake);
return;
}

if (method) {
method.call(fake);
var methods = [];
if (fake.get) {
methods.push(fake.get);
}

if (fake.set) {
methods.push(fake.set);
}

methods.forEach(privateResetHistory);
});
},

Expand Down
30 changes: 30 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -390,4 +390,34 @@ describe("issues", function () {
mock.verify();
});
});

describe("#1648 - resetHistory ", function () {
it("should reset property spies", function () {
var obj = {
func: function () {},
get prop() {
return 1;
}
};

var sandbox = sinon.createSandbox();
var spyFunc = sandbox.spy(obj, "func");
var spyProp = sandbox.spy(obj, "prop", ["get"]);

refute.isTrue(spyFunc.called);
refute.isTrue(spyProp.get.called);

obj.func();
//eslint-disable-next-line no-unused-expressions
obj.prop;

assert.isTrue(spyFunc.called);
assert.isTrue(spyProp.get.called);

sandbox.resetHistory();

refute.isTrue(spyFunc.called);
refute.isTrue(spyProp.get.called);
});
});
});

0 comments on commit 3551fdf

Please sign in to comment.