You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. var define = require('../');
  3. var test = require('tape');
  4. var keys = require('object-keys');
  5. var arePropertyDescriptorsSupported = function () {
  6. var obj = { a: 1 };
  7. try {
  8. Object.defineProperty(obj, 'x', { value: obj });
  9. return obj.x === obj;
  10. } catch (e) { /* this is IE 8. */
  11. return false;
  12. }
  13. };
  14. var descriptorsSupported = !!Object.defineProperty && arePropertyDescriptorsSupported();
  15. var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
  16. test('defineProperties', function (dt) {
  17. dt.test('with descriptor support', { skip: !descriptorsSupported }, function (t) {
  18. var getDescriptor = function (value) {
  19. return {
  20. configurable: true,
  21. enumerable: false,
  22. value: value,
  23. writable: true
  24. };
  25. };
  26. var obj = {
  27. a: 1,
  28. b: 2,
  29. c: 3
  30. };
  31. t.deepEqual(keys(obj), ['a', 'b', 'c'], 'all literal-set keys start enumerable');
  32. define(obj, {
  33. b: 3,
  34. c: 4,
  35. d: 5
  36. });
  37. t.deepEqual(obj, {
  38. a: 1,
  39. b: 2,
  40. c: 3
  41. }, 'existing properties were not overridden');
  42. t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'd'), getDescriptor(5), 'new property "d" was added and is not enumerable');
  43. t.deepEqual(['a', 'b', 'c'], keys(obj), 'new keys are not enumerable');
  44. define(obj, {
  45. a: 2,
  46. b: 3,
  47. c: 4
  48. }, {
  49. a: function () { return true; },
  50. b: function () { return false; }
  51. });
  52. t.deepEqual(obj, {
  53. b: 2,
  54. c: 3
  55. }, 'properties only overriden when predicate exists and returns true');
  56. t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'd'), getDescriptor(5), 'existing property "d" remained and is not enumerable');
  57. t.deepEqual(Object.getOwnPropertyDescriptor(obj, 'a'), getDescriptor(2), 'existing property "a" was overridden and is not enumerable');
  58. t.deepEqual(['b', 'c'], keys(obj), 'overridden keys are not enumerable');
  59. t.end();
  60. });
  61. dt.test('without descriptor support', { skip: descriptorsSupported }, function (t) {
  62. var obj = {
  63. a: 1,
  64. b: 2,
  65. c: 3
  66. };
  67. define(obj, {
  68. b: 3,
  69. c: 4,
  70. d: 5
  71. });
  72. t.deepEqual(obj, {
  73. a: 1,
  74. b: 2,
  75. c: 3,
  76. d: 5
  77. }, 'existing properties were not overridden, new properties were added');
  78. define(obj, {
  79. a: 2,
  80. b: 3,
  81. c: 4
  82. }, {
  83. a: function () { return true; },
  84. b: function () { return false; }
  85. });
  86. t.deepEqual(obj, {
  87. a: 2,
  88. b: 2,
  89. c: 3,
  90. d: 5
  91. }, 'properties only overriden when predicate exists and returns true');
  92. t.end();
  93. });
  94. dt.end();
  95. });
  96. test('symbols', { skip: !hasSymbols }, function (t) {
  97. var sym = Symbol('foo');
  98. var obj = {};
  99. var aValue = {};
  100. var bValue = {};
  101. var properties = { a: aValue };
  102. properties[sym] = bValue;
  103. define(obj, properties);
  104. t.deepEqual(Object.keys(obj), [], 'object has no enumerable keys');
  105. t.deepEqual(Object.getOwnPropertyNames(obj), ['a'], 'object has non-enumerable "a" key');
  106. t.deepEqual(Object.getOwnPropertySymbols(obj), [sym], 'object has non-enumerable symbol key');
  107. t.equal(obj.a, aValue, 'string keyed value is defined');
  108. t.equal(obj[sym], bValue, 'symbol keyed value is defined');
  109. t.end();
  110. });