Skip to content

Commit

Permalink
Fix sinonjs#1638: Make resetHistory work for props also
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick authored and Franck Romano committed Oct 1, 2019
1 parent ce00de3 commit 98c35f2
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 98c35f2

Please sign in to comment.