function.js 2.0 KB

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