Skip to content

Commit

Permalink
Merge pull request #1030 from mroderick/add-test-for-issue-1026
Browse files Browse the repository at this point in the history
Add test for issue #1026
  • Loading branch information
mroderick authored Dec 27, 2016
2 parents ba631c6 + 2f04613 commit 45900b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
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));

0 comments on commit 45900b8

Please sign in to comment.