tests.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. 'use strict';
  2. var hasSymbols = require('has-symbols/shams')();
  3. var forEach = require('for-each');
  4. var has = require('has');
  5. var mockProperty = require('mock-property');
  6. module.exports = function (assign, t) {
  7. t.test('error cases', function (st) {
  8. st['throws'](function () { assign(null); }, TypeError, 'target must be an object');
  9. st['throws'](function () { assign(undefined); }, TypeError, 'target must be an object');
  10. st['throws'](function () { assign(null, {}); }, TypeError, 'target must be an object');
  11. st['throws'](function () { assign(undefined, {}); }, TypeError, 'target must be an object');
  12. st.end();
  13. });
  14. t.test('non-object target, no sources', function (st) {
  15. var bool = assign(true);
  16. st.equal(typeof bool, 'object', 'bool is object');
  17. st.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
  18. var number = assign(1);
  19. st.equal(typeof number, 'object', 'number is object');
  20. st.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
  21. var string = assign('1');
  22. st.equal(typeof string, 'object', 'number is object');
  23. st.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
  24. st.end();
  25. });
  26. t.test('non-object target, with sources', function (st) {
  27. var signal = {};
  28. st.test('boolean', function (st2) {
  29. var bool = assign(true, { a: signal });
  30. st2.equal(typeof bool, 'object', 'bool is object');
  31. st2.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
  32. st2.equal(bool.a, signal, 'source properties copied');
  33. st2.end();
  34. });
  35. st.test('number', function (st2) {
  36. var number = assign(1, { a: signal });
  37. st2.equal(typeof number, 'object', 'number is object');
  38. st2.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
  39. st2.equal(number.a, signal, 'source properties copied');
  40. st2.end();
  41. });
  42. st.test('string', function (st2) {
  43. var string = assign('1', { a: signal });
  44. st2.equal(typeof string, 'object', 'number is object');
  45. st2.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
  46. st2.equal(string.a, signal, 'source properties copied');
  47. st2.end();
  48. });
  49. st.end();
  50. });
  51. t.test('non-object sources', function (st) {
  52. st.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }, 'ignores null source');
  53. st.deepEqual(assign({ a: 1 }, { b: 2 }, undefined), { a: 1, b: 2 }, 'ignores undefined source');
  54. st.end();
  55. });
  56. t.test('returns the modified target object', function (st) {
  57. var target = {};
  58. var returned = assign(target, { a: 1 });
  59. st.equal(returned, target, 'returned object is the same reference as the target object');
  60. st.end();
  61. });
  62. t.test('has the right length', function (st) {
  63. st.equal(assign.length, 2, 'length is 2 => 2 required arguments');
  64. st.end();
  65. });
  66. t.test('merge two objects', function (st) {
  67. var target = { a: 1 };
  68. var returned = assign(target, { b: 2 });
  69. st.deepEqual(returned, { a: 1, b: 2 }, 'returned object has properties from both');
  70. st.end();
  71. });
  72. t.test('works with functions', function (st) {
  73. var target = function () {};
  74. target.a = 1;
  75. var returned = assign(target, { b: 2 });
  76. st.equal(target, returned, 'returned object is target');
  77. st.equal(returned.a, 1);
  78. st.equal(returned.b, 2);
  79. st.end();
  80. });
  81. t.test('works with primitives', function (st) {
  82. var target = 2;
  83. var source = { b: 42 };
  84. var returned = assign(target, source);
  85. st.equal(Object.prototype.toString.call(returned), '[object Number]', 'returned is object form of number primitive');
  86. st.equal(Number(returned), target, 'returned and target have same valueOf');
  87. st.equal(returned.b, source.b);
  88. st.end();
  89. });
  90. /* globals window */
  91. t.test('works with window.location', { skip: typeof window === 'undefined' }, function (st) {
  92. var target = {};
  93. assign(target, window.location);
  94. for (var prop in window.location) {
  95. if (has(window.location, prop)) {
  96. st.deepEqual(target[prop], window.location[prop], prop + ' is copied');
  97. }
  98. }
  99. st.end();
  100. });
  101. t.test('merge N objects', function (st) {
  102. var target = { a: 1 };
  103. var source1 = { b: 2 };
  104. var source2 = { c: 3 };
  105. var returned = assign(target, source1, source2);
  106. st.deepEqual(returned, { a: 1, b: 2, c: 3 }, 'returned object has properties from all sources');
  107. st.end();
  108. });
  109. t.test('only iterates over own keys', function (st) {
  110. var Foo = function () {};
  111. Foo.prototype.bar = true;
  112. var foo = new Foo();
  113. foo.baz = true;
  114. var target = { a: 1 };
  115. var returned = assign(target, foo);
  116. st.equal(returned, target, 'returned object is the same reference as the target object');
  117. st.deepEqual(target, { a: 1, baz: true }, 'returned object has only own properties from both');
  118. st.end();
  119. });
  120. t.test('includes enumerable symbols, after keys', { skip: !hasSymbols }, function (st) {
  121. var visited = [];
  122. var obj = {};
  123. Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
  124. var symbol = Symbol('enumerable');
  125. Object.defineProperty(obj, symbol, {
  126. enumerable: true,
  127. get: function () { visited.push(symbol); return Infinity; }
  128. });
  129. var nonEnumSymbol = Symbol('non-enumerable');
  130. Object.defineProperty(obj, nonEnumSymbol, {
  131. enumerable: false,
  132. get: function () { visited.push(nonEnumSymbol); return -Infinity; }
  133. });
  134. var target = assign({}, obj);
  135. st.deepEqual(visited, ['a', symbol], 'key is visited first, then symbol');
  136. st.equal(target.a, 42, 'target.a is 42');
  137. st.equal(target[symbol], Infinity, 'target[symbol] is Infinity');
  138. st.notEqual(target[nonEnumSymbol], -Infinity, 'target[nonEnumSymbol] is not -Infinity');
  139. st.end();
  140. });
  141. t.test('does not fail when symbols are not present', { skip: !Object.isFrozen || Object.isFrozen(Object) }, function (st) {
  142. st.teardown(mockProperty(Object, 'getOwnPropertySymbols', { 'delete': true }));
  143. var visited = [];
  144. var obj = {};
  145. Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
  146. var keys = ['a'];
  147. if (hasSymbols) {
  148. var symbol = Symbol('sym');
  149. Object.defineProperty(obj, symbol, {
  150. enumerable: true,
  151. get: function () { visited.push(symbol); return Infinity; }
  152. });
  153. keys.push(symbol);
  154. }
  155. var target = assign({}, obj);
  156. st.deepEqual(visited, keys, 'assign visits expected keys');
  157. st.equal(target.a, 42, 'target.a is 42');
  158. if (hasSymbols) {
  159. st.equal(target[symbol], Infinity);
  160. }
  161. st.end();
  162. });
  163. t.test('preserves correct property enumeration order', function (st) {
  164. var str = 'abcdefghijklmnopqrst';
  165. var letters = {};
  166. forEach(str.split(''), function (letter) {
  167. letters[letter] = letter;
  168. });
  169. var n = 5;
  170. st.comment('run the next test ' + n + ' times');
  171. var object = assign({}, letters);
  172. var actual = '';
  173. for (var k in object) {
  174. actual += k;
  175. }
  176. for (var i = 0; i < n; ++i) {
  177. st.equal(actual, str, 'property enumeration order should be followed');
  178. }
  179. st.end();
  180. });
  181. t.test('checks enumerability and existence, in case of modification during [[Get]]', { skip: !Object.defineProperty }, function (st) {
  182. var targetBvalue = {};
  183. var targetCvalue = {};
  184. var target = { b: targetBvalue, c: targetCvalue };
  185. var source = {};
  186. Object.defineProperty(source, 'a', {
  187. enumerable: true,
  188. get: function () {
  189. delete this.b;
  190. Object.defineProperty(this, 'c', { enumerable: false });
  191. return 'a';
  192. }
  193. });
  194. var sourceBvalue = {};
  195. var sourceCvalue = {};
  196. source.b = sourceBvalue;
  197. source.c = sourceCvalue;
  198. var result = assign(target, source);
  199. st.equal(result, target, 'sanity check: result is === target');
  200. st.equal(result.b, targetBvalue, 'target key not overwritten by deleted source key');
  201. st.equal(result.c, targetCvalue, 'target key not overwritten by non-enumerable source key');
  202. st.end();
  203. });
  204. };