Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1638: resetHistory for props #1677

Merged
merged 2 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 26 additions & 15 deletions test/collection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,29 +458,40 @@ describe("collection", function () {
describe(".resetHistory", function () {
beforeEach(function () {
this.collection = Object.create(sinonCollection);
this.collection.fakes = [{
// this fake has a resetHistory method
resetHistory: sinonSpy()
}, {
// this fake has a resetHistory method
resetHistory: sinonSpy()
}, {
// this fake pretends to be a spy, which does not have resetHistory method
// but has a reset method
reset: sinonSpy()
}];
var spy1 = sinonSpy();
spy1();

var spy2 = sinonSpy();
spy2();

this.collection.fakes = [
spy1,
spy2
];
});

it("resets the history on all fakes", function () {
var fake0 = this.collection.fakes[0];
var fake1 = this.collection.fakes[1];
var fake2 = this.collection.fakes[2];

this.collection.resetHistory();

assert(fake0.resetHistory.called);
assert(fake1.resetHistory.called);
assert(fake2.reset.called);
refute(fake0.called);
refute(fake1.called);
});

it("calls reset on fake that do not have a resetHistory", function () {
var noop = function noop() {};

noop.reset = function reset() {
noop.reset.called = true;
};

this.collection.fakes.push(noop);

this.collection.resetHistory();

assert.isTrue(noop.reset.called);
});
});

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);
});
});
});