| 1 |
var identity = function (x) { return x; }; |
| 2 |
|
| 3 |
describe('Function', function () { |
| 4 |
describe('#name', function () { |
| 5 |
it('returns the name for named functions', function () { |
| 6 |
var foo = function bar() {}; |
| 7 |
expect(foo.name).to.equal('bar'); |
| 8 |
|
| 9 |
// pre-ES6, this property is nonconfigurable. |
| 10 |
var configurable = Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(foo, 'name').configurable : false; |
| 11 |
|
| 12 |
expect(foo).to.have.ownPropertyDescriptor('name', { |
| 13 |
configurable: !!configurable, |
| 14 |
enumerable: false, |
| 15 |
writable: false, |
| 16 |
value: 'bar' |
| 17 |
}); |
| 18 |
}); |
| 19 |
|
| 20 |
it('does not poison every name when accessed on Function.prototype', function () { |
| 21 |
expect((function foo() {}).name).to.equal('foo'); |
| 22 |
expect(Function.prototype.name).to.match(/^$|Empty/); |
| 23 |
expect((function foo() {}).name).to.equal('foo'); |
| 24 |
}); |
| 25 |
|
| 26 |
it('returns empty string for anonymous functions', function () { |
| 27 |
var anon = identity(function () {}); |
| 28 |
expect(anon.name).to.equal(''); |
| 29 |
|
| 30 |
// pre-ES6, this property is nonconfigurable. |
| 31 |
var configurable = Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(anon, 'name').configurable : false; |
| 32 |
|
| 33 |
expect(anon).to.have.ownPropertyDescriptor('name', { |
| 34 |
configurable: !!configurable, |
| 35 |
enumerable: false, |
| 36 |
writable: false, |
| 37 |
value: '' |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
it('returns "anomymous" for Function functions', function () { |
| 42 |
// eslint-disable-next-line no-new-func |
| 43 |
var func = identity(Function('')); |
| 44 |
expect(typeof func.name).to.equal('string'); |
| 45 |
expect(func.name === 'anonymous' || func.name === '').to.equal(true); |
| 46 |
|
| 47 |
// pre-ES6, this property is nonconfigurable. |
| 48 |
var configurable = Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(func, 'name').configurable : false; |
| 49 |
|
| 50 |
expect(func).to.have.ownPropertyDescriptor('name', { |
| 51 |
configurable: !!configurable, |
| 52 |
enumerable: false, |
| 53 |
writable: false, |
| 54 |
value: func.name |
| 55 |
}); |
| 56 |
}); |
| 57 |
}); |
| 58 |
}); |