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

Add test for issue #1026 #1030

Merged
merged 3 commits into from
Dec 27, 2016
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
2 changes: 1 addition & 1 deletion lib/sinon/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}

Object.getOwnPropertyNames(obj).forEach(function (k) {
if (!seen[k]) {
if (seen[k] !== true) {
seen[k] = true;
var target = typeof Object.getOwnPropertyDescriptor(obj, k).get === "function" ?
originalObj : obj;
Expand Down
32 changes: 32 additions & 0 deletions test/issues/issues-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@

sandbox.restore();
}
},

"#1026 - should stub watch method on any Object": function () {
// makes sure that Object.prototype.watch is set back to its old value
function restore(oldWatch) {
if (oldWatch) {
Object.prototype.watch = oldWatch; // eslint-disable-line no-extend-native
} else {
delete Object.prototype.watch;
}
}

try {
var oldWatch = Object.prototype.watch;

if (typeof Object.prototype.watch !== "function") {
Object.prototype.watch = function rolex() {}; // eslint-disable-line no-extend-native
}

var stubbedObject = sinon.stub({
watch: function () {}
});

stubbedObject.watch();

assert.isArray(stubbedObject.watch.args);
} catch (error) {
restore(oldWatch);
throw error;
}

restore(oldWatch);
}
});
}(this));
15 changes: 11 additions & 4 deletions test/walk-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
assert(iterator.alwaysCalledOn(rcvr));
assert(iterator.calledWith("world", "hello"));
assert(iterator.calledWith(15, "foo"));
} catch(e) {
} catch (e) {
err = e;
} finally {
Object.getOwnPropertyNames = getOwnPropertyNames;
Expand All @@ -143,15 +143,22 @@

"does not walk the same property twice": function () {
var parent = {
func: function () {}
func: function parentFunc() {}
};
var child = sinon.create(parent);
child.func = function () {};
child.func = function childFunc() {};
var iterator = sinon.spy();

sinon.walk(child, iterator);

assert.equals(iterator.callCount, 1);
var propertyNames = iterator.args.map(function (call) {
return call[1];
});

// make sure that each property name only exists once
propertyNames.forEach(function (name, index) {
assert.equals(index, propertyNames.lastIndexOf(name));
});
}
});
}(this));