Skip to content

Commit

Permalink
Check configurable on a prop before creating (fixes #1456) (#1462)
Browse files Browse the repository at this point in the history
* Fix: check configurable on a prop before creating (fixes #1456)
  • Loading branch information
gyandeeps authored and fatso83 committed Jun 20, 2017
1 parent e7dbfd7 commit ef2ecec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/sinon/default-behaviors.js
@@ -1,4 +1,7 @@
"use strict";

var getPropertyDescriptor = require("./util/core/get-property-descriptor");

var slice = [].slice;
var useLeftMostCallback = -1;
var useRightMostCallback = -2;
Expand All @@ -14,6 +17,12 @@ function throwsException(fake, error, message) {
}
}

function isPropertyConfigurable(obj, propName) {
var propertyDescriptor = getPropertyDescriptor(obj, propName);

return propertyDescriptor ? propertyDescriptor.configurable : true;
}

module.exports = {
callsFake: function callsFake(fake, fn) {
fake.fakeFn = fn;
Expand Down Expand Up @@ -181,7 +190,7 @@ module.exports = {

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

return fake;
Expand All @@ -192,7 +201,7 @@ module.exports = {

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

return fake;
Expand All @@ -204,7 +213,7 @@ module.exports = {
Object.defineProperty(rootStub.rootObj, rootStub.propName, {
value: newVal,
enumerable: true,
configurable: true
configurable: isPropertyConfigurable(rootStub.rootObj, rootStub.propName)
});

return fake;
Expand Down
21 changes: 21 additions & 0 deletions test/issues/issues-test.js
Expand Up @@ -2,6 +2,7 @@

var referee = require("referee");
var sinon = require("../../lib/sinon");
var sinonSandbox = require("../../lib/sinon/sandbox");
var configureLogError = require("../../lib/sinon/util/core/log_error.js");
var assert = referee.assert;
var refute = referee.refute;
Expand Down Expand Up @@ -248,4 +249,24 @@ describe("issues", function () {
});
});
});

if (typeof window !== "undefined") {
describe("#1456", function () {
var sandbox;

beforeEach(function () {
sandbox = sinonSandbox.create();
});

afterEach(function () {
sandbox.restore();
});

it("stub window innerHeight", function () {
sandbox.stub(window, "innerHeight").value(111);

assert.equals(window.innerHeight, 111);
});
});
}
});
13 changes: 13 additions & 0 deletions test/stub-test.js
Expand Up @@ -2672,5 +2672,18 @@ describe("stub", function () {

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

it("allows stubbing object props with configurable false", function () {
var myObj = {};
Object.defineProperty(myObj, "prop", {
configurable: false,
enumerable: true,
writable: true,
value: "static"
});

createStub(myObj, "prop").value("newString");
assert.equals(myObj.prop, "newString");
});
});
});

0 comments on commit ef2ecec

Please sign in to comment.