tests.js 7.7 KB

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