s-object.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. describe('Object', function () {
  2. "use strict";
  3. describe("Object.keys", function () {
  4. var obj = {
  5. "str": "boz",
  6. "obj": { },
  7. "arr": [],
  8. "bool": true,
  9. "num": 42,
  10. "null": null,
  11. "undefined": undefined
  12. };
  13. var loopedValues = [];
  14. for (var k in obj) {
  15. loopedValues.push(k);
  16. }
  17. var keys = Object.keys(obj);
  18. it('should have correct length', function () {
  19. expect(keys.length).toBe(7);
  20. });
  21. it('should return an Array', function () {
  22. expect(Array.isArray(keys)).toBe(true);
  23. });
  24. it('should return names which are own properties', function () {
  25. keys.forEach(function (name) {
  26. expect(obj.hasOwnProperty(name)).toBe(true);
  27. });
  28. });
  29. it('should return names which are enumerable', function () {
  30. keys.forEach(function (name) {
  31. expect(loopedValues.indexOf(name)).toNotBe(-1);
  32. })
  33. });
  34. it('should throw error for non object', function () {
  35. var e = {};
  36. expect(function () {
  37. try {
  38. Object.keys(42)
  39. } catch (err) {
  40. throw e;
  41. }
  42. }).toThrow(e);
  43. });
  44. });
  45. describe("Object.isExtensible", function () {
  46. var obj = { };
  47. it('should return true if object is extensible', function () {
  48. expect(Object.isExtensible(obj)).toBe(true);
  49. });
  50. it('should return false if object is not extensible', function () {
  51. expect(Object.isExtensible(Object.preventExtensions(obj))).toBe(false);
  52. });
  53. it('should return false if object is seal', function () {
  54. expect(Object.isExtensible(Object.seal(obj))).toBe(false);
  55. });
  56. it('should return false if object is freeze', function () {
  57. expect(Object.isExtensible(Object.freeze(obj))).toBe(false);
  58. });
  59. it('should throw error for non object', function () {
  60. var e1 = {};
  61. expect(function () {
  62. try {
  63. Object.isExtensible(42)
  64. } catch (err) {
  65. throw e1;
  66. }
  67. }).toThrow(e1);
  68. });
  69. });
  70. describe("Object.defineProperty", function () {
  71. var obj;
  72. beforeEach(function() {
  73. obj = {};
  74. Object.defineProperty(obj, 'name', {
  75. value : 'Testing',
  76. configurable: true,
  77. enumerable: true,
  78. writable: true
  79. });
  80. });
  81. it('should return the initial value', function () {
  82. expect(obj.hasOwnProperty('name')).toBeTruthy();
  83. expect(obj.name).toBe('Testing');
  84. });
  85. it('should be setable', function () {
  86. obj.name = 'Other';
  87. expect(obj.name).toBe('Other');
  88. });
  89. it('should return the parent initial value', function () {
  90. var child = Object.create(obj, {});
  91. expect(child.name).toBe('Testing');
  92. expect(child.hasOwnProperty('name')).toBeFalsy();
  93. });
  94. it('should not override the parent value', function () {
  95. var child = Object.create(obj, {});
  96. Object.defineProperty(child, 'name', {
  97. value : 'Other'
  98. });
  99. expect(obj.name).toBe('Testing');
  100. expect(child.name).toBe('Other');
  101. });
  102. it('should throw error for non object', function () {
  103. expect(function () {
  104. Object.defineProperty(42, 'name', {});
  105. }).toThrow();
  106. });
  107. });
  108. describe("Object.getOwnPropertyDescriptor", function () {
  109. it('should return undefined because the object does not own the property', function () {
  110. var descr = Object.getOwnPropertyDescriptor({}, 'name');
  111. expect(descr).toBeUndefined()
  112. });
  113. it('should return a data descriptor', function () {
  114. var descr = Object.getOwnPropertyDescriptor({name: 'Testing'}, 'name');
  115. expect(descr).not.toBeUndefined();
  116. expect(descr.value).toBe('Testing');
  117. expect(descr.writable).toBe(true);
  118. expect(descr.enumerable).toBe(true);
  119. expect(descr.configurable).toBe(true);
  120. });
  121. it('should return undefined because the object does not own the property', function () {
  122. var descr = Object.getOwnPropertyDescriptor(Object.create({name: 'Testing'}, {}), 'name');
  123. expect(descr).toBeUndefined()
  124. });
  125. it('should return a data descriptor', function () {
  126. var obj = Object.create({}, {
  127. name: {
  128. value : 'Testing',
  129. configurable: true,
  130. enumerable: true,
  131. writable: true
  132. }
  133. });
  134. var descr = Object.getOwnPropertyDescriptor(obj, 'name');
  135. expect(descr).not.toBeUndefined();
  136. expect(descr.value).toBe('Testing');
  137. expect(descr.writable).toBe(true);
  138. expect(descr.enumerable).toBe(true);
  139. expect(descr.configurable).toBe(true);
  140. });
  141. it('should throw error for non object', function () {
  142. expect(function () {
  143. Object.getOwnPropertyDescriptor(42, 'name');
  144. }).toThrow();
  145. });
  146. });
  147. });