Comparing version 1.0.3 to 2.0.0
@@ -31,3 +31,3 @@ /** | ||
* important because accessor properties have functions that will still execute | ||
* after Eos inherits from that object, effectively keeping functionality alive | ||
* after Leche inherits from that object, effectively keeping functionality alive | ||
* on the fake object. | ||
@@ -54,2 +54,36 @@ * @param {Object} object The object to check. | ||
/** | ||
* Determines if a given property is a data property in ES5. This is | ||
* important because we can overwrite data properties with getters in ES5, | ||
* but not in ES3. | ||
* @param {Object} object The object to check. | ||
* @param {string} key The property name to check. | ||
* @returns {boolean} True if it's an ES5 data property, false if not. | ||
* @private | ||
*/ | ||
function isES5DataProperty(object, key) { | ||
var result = false; | ||
// make sure this works in older browsers without error | ||
if (Object.getOwnPropertyDescriptor && object.hasOwnProperty(key)) { | ||
var descriptor = Object.getOwnPropertyDescriptor(object, key); | ||
result = ('value' in descriptor) && (typeof descriptor.value !== 'function'); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Determines if a given property is a data property in ES3. | ||
* @param {Object} object The object to check. | ||
* @param {string} key The property name to check. | ||
* @returns {boolean} True if it's an ES5 data property, false if not. | ||
* @private | ||
*/ | ||
function isES3DataProperty(object, key) { | ||
return typeof object[key] !== 'function'; | ||
} | ||
/** | ||
* An abstraction of Object.create() to ensure that this library can also be used | ||
@@ -111,3 +145,3 @@ * in browsers. | ||
/** | ||
* @module eos | ||
* @module leche | ||
*/ | ||
@@ -168,2 +202,24 @@ module.exports = { | ||
} else if (isES5DataProperty(template, key)) { | ||
(function(propertyKey) { | ||
Object.defineProperty(fake, key, { | ||
get: function() { | ||
throw new Error('Unexpected use of property "' + propertyKey + '".'); | ||
}, | ||
set: function(value) { | ||
// when set, change into a data property - removes the getter and setter | ||
Object.defineProperty(fake, key, { value: value }); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
}(key)); | ||
} else if (isES3DataProperty(template, key)) { | ||
// can't do anything special for ES3, so just assign undefined | ||
fake[key] = undefined; | ||
} else if (typeof fake[key] === 'function') { | ||
@@ -170,0 +226,0 @@ fake[key] = (function(methodKey) { |
@@ -92,2 +92,5 @@ /** | ||
exec('git push origin master --tags'); | ||
// also publish to npm (requires authentication) | ||
exec('npm publish'); | ||
} | ||
@@ -94,0 +97,0 @@ |
{ | ||
"name": "leche", | ||
"author": "nzakas", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A JavaScript testing utility designed to work with Mocha and Sinon", | ||
@@ -6,0 +6,0 @@ "main": "./lib/leche.js", |
@@ -69,2 +69,4 @@ [![Build Status](https://travis-ci.org/box/leche.png?branch=master)](https://travis-ci.org/box/leche) | ||
Additionally, fakes contain all of the properties of the passed-in object. In ECMAScript 5 environments, accessing these properties without first setting them to a value will result in an errow. | ||
To create a fake, pass in the object you want to fake to `leche.fake()`, such as: | ||
@@ -88,9 +90,17 @@ | ||
// in a test | ||
var fakePerson = leche.fake(Person.prototype); | ||
var fakePerson = leche.fake(new Person('Jeff')); | ||
assert.ok(fakePerson instanceof Person); // passes | ||
assert.ok('sayName' in fakePerson); // passes | ||
assert.ok('name' in fakePerson); // passes | ||
// throws an error | ||
fakePerson.sayName(); | ||
// also throws an error | ||
var name = fakePerson.name; | ||
// won't throw an error because you've assigned a value | ||
fakePerson.name = 'Jeff'; | ||
var name = fakePerson.name; | ||
``` | ||
@@ -214,4 +224,2 @@ | ||
## Support | ||
@@ -218,0 +226,0 @@ |
@@ -105,3 +105,3 @@ /** | ||
it('should create an object whose properties remain unchanged when called on an object with only properties', function() { | ||
it('should create an object whose properties throw an error when accessed', function() { | ||
@@ -113,8 +113,23 @@ var template = { | ||
var fake = leche.fake(template); | ||
assert.equal(fake.name, 'leche'); | ||
assert.throws(function() { | ||
fake.name; // calls getter | ||
}, /Unexpected use of property "name"\./); | ||
}); | ||
it('should should create an object with a data property when called on an object with only an accessor property', function() { | ||
it('should create an object whose properties do not throw an error when set to a value', function() { | ||
var template = { | ||
name: 'leche' | ||
}; | ||
var fake = leche.fake(template); | ||
fake.name = 'box'; | ||
assert.equal(fake.name, 'box'); | ||
}); | ||
it('should create an object with a data property when called on an object with only an accessor property', function() { | ||
var template = { | ||
get data() { | ||
@@ -130,2 +145,3 @@ return this.foo.bar; | ||
}); | ||
@@ -132,0 +148,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42926
604
241