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 restoring getters/setters/values for previously unexisting props #1419

Merged
merged 3 commits into from
May 24, 2017
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
20 changes: 7 additions & 13 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";
var getPropertyDescriptor = require("./util/core/get-property-descriptor");

var slice = [].slice;
var useLeftMostCallback = -1;
var useRightMostCallback = -2;
Expand Down Expand Up @@ -182,7 +180,8 @@ module.exports = {
var rootStub = fake.stub || fake;

Object.defineProperty(rootStub.rootObj, rootStub.propName, {
get: getterFunction
get: getterFunction,
configurable: true
});

return fake;
Expand All @@ -192,7 +191,8 @@ module.exports = {
var rootStub = fake.stub || fake;

Object.defineProperty(rootStub.rootObj, rootStub.propName, { // eslint-disable-line accessor-pairs
set: setterFunction
set: setterFunction,
configurable: true
});

return fake;
Expand All @@ -201,18 +201,12 @@ module.exports = {
value: function value(fake, newVal) {
var rootStub = fake.stub || fake;

var oldVal = getPropertyDescriptor(rootStub.rootObj, rootStub.propName).value;

Object.defineProperty(rootStub.rootObj, rootStub.propName, {
value: newVal
value: newVal,
enumerable: true,
configurable: true
});

fake.restore = function restore() {
Object.defineProperty(rootStub.rootObj, rootStub.propName, {
value: oldVal
});
};

return fake;
}
};
Expand Down
7 changes: 6 additions & 1 deletion lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ function stub(object, property, descriptor) {
s.rootObj = object;
s.propName = property;
s.restore = function restore() {
Object.defineProperty(object, property, actualDescriptor);
if (actualDescriptor !== undefined) {
Object.defineProperty(object, property, actualDescriptor);
return;
}

delete object[property];
};

return isStubbingNonFuncProperty ? s : wrapMethod(object, property, s);
Expand Down
40 changes: 39 additions & 1 deletion test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ var createStub = require("../lib/sinon/stub");
var createStubInstance = require("../lib/sinon/stub").createStubInstance;
var createSpy = require("../lib/sinon/spy");
var sinonMatch = require("../lib/sinon/match");
var getPropertyDescriptor = require("../lib/sinon/util/core/get-property-descriptor");
var deprecated = require("../lib/sinon/util/core/deprecated");
var assert = referee.assert;
var refute = referee.refute;
var fail = referee.fail;
var Promise = require("native-promise-only"); // eslint-disable-line no-unused-vars
var deprecated = require("../lib/sinon/util/core/deprecated");

describe("stub", function () {
it("is spy", function () {
Expand Down Expand Up @@ -2519,6 +2520,20 @@ describe("stub", function () {

assert.equals(myObj.prop, "bar");
});

it("can restore stubbed getters for previously undefined properties", function () {
var myObj = {};

var stub = createStub(myObj, "nonExisting");

stub.get(function getterFn() {
return "baz";
});

stub.restore();

assert.equals(getPropertyDescriptor(myObj, "nonExisting"), undefined);
});
});

describe(".set", function () {
Expand Down Expand Up @@ -2619,6 +2634,20 @@ describe("stub", function () {
myObj.prop = "foo";
assert.equals(myObj.otherProp, "bar");
});

it("can restore stubbed setters for previously undefined properties", function () {
var myObj = {};

var stub = createStub(myObj, "nonExisting");

stub.set(function setterFn() {
myObj.otherProp = "baz";
});

stub.restore();

assert.equals(getPropertyDescriptor(myObj, "nonExisting"), undefined);
});
});

describe(".value", function () {
Expand All @@ -2641,5 +2670,14 @@ describe("stub", function () {

assert.equals(myObj.prop, "rawString");
});

it("allows restoring previously undefined properties", function () {
var obj = {};
var stub = createStub(obj, "nonExisting").value(2);

stub.restore();

assert.equals(getPropertyDescriptor(obj, "nonExisting"), undefined);
});
});
});