Skip to content

Commit

Permalink
Allow replacing non-own object properties (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Apr 30, 2018
1 parent 7e2f05a commit 01cbe61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/sinon/sandbox.js
Expand Up @@ -165,7 +165,7 @@ function Sandbox() {
}

sandbox.replace = function replace(object, property, replacement) {
var descriptor = Object.getOwnPropertyDescriptor(object, property);
var descriptor = getPropertyDescriptor(object, property);

if (typeof descriptor === "undefined") {
throw new TypeError("Cannot replace non-existent own property " + valueToString(property));
Expand Down Expand Up @@ -196,7 +196,7 @@ function Sandbox() {
};

sandbox.replaceGetter = function replaceGetter(object, property, replacement) {
var descriptor = Object.getOwnPropertyDescriptor(object, property);
var descriptor = getPropertyDescriptor(object, property);

if (typeof descriptor === "undefined") {
throw new TypeError("Cannot replace non-existent own property " + valueToString(property));
Expand All @@ -222,7 +222,7 @@ function Sandbox() {
};

sandbox.replaceSetter = function replaceSetter(object, property, replacement) {
var descriptor = Object.getOwnPropertyDescriptor(object, property);
var descriptor = getPropertyDescriptor(object, property);

if (typeof descriptor === "undefined") {
throw new TypeError("Cannot replace non-existent own property " + valueToString(property));
Expand Down
40 changes: 40 additions & 0 deletions test/sandbox-test.js
Expand Up @@ -581,6 +581,25 @@ describe("Sandbox", function () {
assert.equals(actual, replacement);
});

it("should replace an inherited property", function () {
var expected = "baz";
var replacement = fake.returns(expected);
var existing = "existing";
var object = Object.create({
get foo() {
return existing;
}
});

this.sandbox.replaceGetter(object, "foo", replacement);

assert.equals(object.foo, expected);

this.sandbox.restore();

assert.equals(object.foo, existing);
});

describe("when called with a non-function replacement argument", function () {
it("should throw a TypeError", function () {
var sandbox = this.sandbox;
Expand Down Expand Up @@ -652,6 +671,27 @@ describe("Sandbox", function () {
assert.equals(actual, replacement);
});

it("should replace an inherited property", function () {
// eslint-disable-next-line accessor-pairs
var object = Object.create({
set foo(value) {
this.prop = value;
},
prop: "bar"
});
var replacement = function (value) {
this.prop = value + "blabla";
};

this.sandbox.replaceSetter(object, "foo", replacement);
object.foo = "doodle";
assert.equals(object.prop, "doodleblabla");

this.sandbox.restore();
object.foo = "doodle";
assert.equals(object.prop, "doodle");
});

describe("when called with a non-function replacement argument", function () {
it("should throw a TypeError", function () {
var sandbox = this.sandbox;
Expand Down

0 comments on commit 01cbe61

Please sign in to comment.