helpers.core.tests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. 'use strict';
  2. describe('Chart.helpers.core', function() {
  3. var helpers = Chart.helpers;
  4. describe('noop', function() {
  5. it('should be callable', function() {
  6. expect(helpers.noop).toBeDefined();
  7. expect(typeof helpers.noop).toBe('function');
  8. expect(typeof helpers.noop.call).toBe('function');
  9. });
  10. it('should returns "undefined"', function() {
  11. expect(helpers.noop(42)).not.toBeDefined();
  12. expect(helpers.noop.call(this, 42)).not.toBeDefined();
  13. });
  14. });
  15. describe('isArray', function() {
  16. it('should return true if value is an array', function() {
  17. expect(helpers.isArray([])).toBeTruthy();
  18. expect(helpers.isArray([42])).toBeTruthy();
  19. expect(helpers.isArray(new Array())).toBeTruthy();
  20. expect(helpers.isArray(Array.prototype)).toBeTruthy();
  21. });
  22. it('should return false if value is not an array', function() {
  23. expect(helpers.isArray()).toBeFalsy();
  24. expect(helpers.isArray({})).toBeFalsy();
  25. expect(helpers.isArray(undefined)).toBeFalsy();
  26. expect(helpers.isArray(null)).toBeFalsy();
  27. expect(helpers.isArray(true)).toBeFalsy();
  28. expect(helpers.isArray(false)).toBeFalsy();
  29. expect(helpers.isArray(42)).toBeFalsy();
  30. expect(helpers.isArray('Array')).toBeFalsy();
  31. expect(helpers.isArray({__proto__: Array.prototype})).toBeFalsy();
  32. });
  33. });
  34. describe('isObject', function() {
  35. it('should return true if value is an object', function() {
  36. expect(helpers.isObject({})).toBeTruthy();
  37. expect(helpers.isObject({a: 42})).toBeTruthy();
  38. expect(helpers.isObject(new Object())).toBeTruthy();
  39. });
  40. it('should return false if value is not an object', function() {
  41. expect(helpers.isObject()).toBeFalsy();
  42. expect(helpers.isObject(undefined)).toBeFalsy();
  43. expect(helpers.isObject(null)).toBeFalsy();
  44. expect(helpers.isObject(true)).toBeFalsy();
  45. expect(helpers.isObject(false)).toBeFalsy();
  46. expect(helpers.isObject(42)).toBeFalsy();
  47. expect(helpers.isObject('Object')).toBeFalsy();
  48. expect(helpers.isObject([])).toBeFalsy();
  49. expect(helpers.isObject([42])).toBeFalsy();
  50. expect(helpers.isObject(new Array())).toBeFalsy();
  51. expect(helpers.isObject(new Date())).toBeFalsy();
  52. });
  53. });
  54. describe('isNullOrUndef', function() {
  55. it('should return true if value is null/undefined', function() {
  56. expect(helpers.isNullOrUndef(null)).toBeTruthy();
  57. expect(helpers.isNullOrUndef(undefined)).toBeTruthy();
  58. });
  59. it('should return false if value is not null/undefined', function() {
  60. expect(helpers.isNullOrUndef(true)).toBeFalsy();
  61. expect(helpers.isNullOrUndef(false)).toBeFalsy();
  62. expect(helpers.isNullOrUndef('')).toBeFalsy();
  63. expect(helpers.isNullOrUndef('String')).toBeFalsy();
  64. expect(helpers.isNullOrUndef(0)).toBeFalsy();
  65. expect(helpers.isNullOrUndef([])).toBeFalsy();
  66. expect(helpers.isNullOrUndef({})).toBeFalsy();
  67. expect(helpers.isNullOrUndef([42])).toBeFalsy();
  68. expect(helpers.isNullOrUndef(new Date())).toBeFalsy();
  69. });
  70. });
  71. describe('valueOrDefault', function() {
  72. it('should return value if defined', function() {
  73. var object = {};
  74. var array = [];
  75. expect(helpers.valueOrDefault(null, 42)).toBe(null);
  76. expect(helpers.valueOrDefault(false, 42)).toBe(false);
  77. expect(helpers.valueOrDefault(object, 42)).toBe(object);
  78. expect(helpers.valueOrDefault(array, 42)).toBe(array);
  79. expect(helpers.valueOrDefault('', 42)).toBe('');
  80. expect(helpers.valueOrDefault(0, 42)).toBe(0);
  81. });
  82. it('should return default if undefined', function() {
  83. expect(helpers.valueOrDefault(undefined, 42)).toBe(42);
  84. expect(helpers.valueOrDefault({}.foo, 42)).toBe(42);
  85. });
  86. });
  87. describe('valueAtIndexOrDefault', function() {
  88. it('should return the passed value if not an array', function() {
  89. expect(helpers.valueAtIndexOrDefault(0, 0, 42)).toBe(0);
  90. expect(helpers.valueAtIndexOrDefault('', 0, 42)).toBe('');
  91. expect(helpers.valueAtIndexOrDefault(null, 0, 42)).toBe(null);
  92. expect(helpers.valueAtIndexOrDefault(false, 0, 42)).toBe(false);
  93. expect(helpers.valueAtIndexOrDefault(98, 0, 42)).toBe(98);
  94. });
  95. it('should return the value at index if defined', function() {
  96. expect(helpers.valueAtIndexOrDefault([1, false, 'foo'], 1, 42)).toBe(false);
  97. expect(helpers.valueAtIndexOrDefault([1, false, 'foo'], 2, 42)).toBe('foo');
  98. });
  99. it('should return the default value if the passed value is undefined', function() {
  100. expect(helpers.valueAtIndexOrDefault(undefined, 0, 42)).toBe(42);
  101. });
  102. it('should return the default value if value at index is undefined', function() {
  103. expect(helpers.valueAtIndexOrDefault([1, false, 'foo'], 3, 42)).toBe(42);
  104. expect(helpers.valueAtIndexOrDefault([1, undefined, 'foo'], 1, 42)).toBe(42);
  105. });
  106. });
  107. describe('callback', function() {
  108. it('should return undefined if fn is not a function', function() {
  109. expect(helpers.callback()).not.toBeDefined();
  110. expect(helpers.callback(null)).not.toBeDefined();
  111. expect(helpers.callback(42)).not.toBeDefined();
  112. expect(helpers.callback([])).not.toBeDefined();
  113. expect(helpers.callback({})).not.toBeDefined();
  114. });
  115. it('should call fn with the given args', function() {
  116. var spy = jasmine.createSpy('spy');
  117. helpers.callback(spy);
  118. helpers.callback(spy, []);
  119. helpers.callback(spy, ['foo']);
  120. helpers.callback(spy, [42, 'bar']);
  121. expect(spy.calls.argsFor(0)).toEqual([]);
  122. expect(spy.calls.argsFor(1)).toEqual([]);
  123. expect(spy.calls.argsFor(2)).toEqual(['foo']);
  124. expect(spy.calls.argsFor(3)).toEqual([42, 'bar']);
  125. });
  126. it('should call fn with the given scope', function() {
  127. var spy = jasmine.createSpy('spy');
  128. var scope = {};
  129. helpers.callback(spy);
  130. helpers.callback(spy, [], null);
  131. helpers.callback(spy, [], undefined);
  132. helpers.callback(spy, [], scope);
  133. expect(spy.calls.all()[0].object).toBe(window);
  134. expect(spy.calls.all()[1].object).toBe(window);
  135. expect(spy.calls.all()[2].object).toBe(window);
  136. expect(spy.calls.all()[3].object).toBe(scope);
  137. });
  138. it('should return the value returned by fn', function() {
  139. expect(helpers.callback(helpers.noop, [41])).toBe(undefined);
  140. expect(helpers.callback(function(i) {
  141. return i + 1;
  142. }, [41])).toBe(42);
  143. });
  144. });
  145. describe('each', function() {
  146. it('should iterate over an array forward if reverse === false', function() {
  147. var scope = {};
  148. var scopes = [];
  149. var items = [];
  150. var keys = [];
  151. helpers.each(['foo', 'bar', 42], function(item, key) {
  152. scopes.push(this);
  153. items.push(item);
  154. keys.push(key);
  155. }, scope);
  156. expect(scopes).toEqual([scope, scope, scope]);
  157. expect(items).toEqual(['foo', 'bar', 42]);
  158. expect(keys).toEqual([0, 1, 2]);
  159. });
  160. it('should iterate over an array backward if reverse === true', function() {
  161. var scope = {};
  162. var scopes = [];
  163. var items = [];
  164. var keys = [];
  165. helpers.each(['foo', 'bar', 42], function(item, key) {
  166. scopes.push(this);
  167. items.push(item);
  168. keys.push(key);
  169. }, scope, true);
  170. expect(scopes).toEqual([scope, scope, scope]);
  171. expect(items).toEqual([42, 'bar', 'foo']);
  172. expect(keys).toEqual([2, 1, 0]);
  173. });
  174. it('should iterate over object properties', function() {
  175. var scope = {};
  176. var scopes = [];
  177. var items = [];
  178. helpers.each({a: 'foo', b: 'bar', c: 42}, function(item, key) {
  179. scopes.push(this);
  180. items[key] = item;
  181. }, scope);
  182. expect(scopes).toEqual([scope, scope, scope]);
  183. expect(items).toEqual(jasmine.objectContaining({a: 'foo', b: 'bar', c: 42}));
  184. });
  185. it('should not throw when called with a non iterable object', function() {
  186. expect(function() {
  187. helpers.each(undefined);
  188. }).not.toThrow();
  189. expect(function() {
  190. helpers.each(null);
  191. }).not.toThrow();
  192. expect(function() {
  193. helpers.each(42);
  194. }).not.toThrow();
  195. });
  196. });
  197. describe('arrayEquals', function() {
  198. it('should return false if arrays are not the same', function() {
  199. expect(helpers.arrayEquals([], [42])).toBeFalsy();
  200. expect(helpers.arrayEquals([42], ['42'])).toBeFalsy();
  201. expect(helpers.arrayEquals([1, 2, 3], [1, 2, 3, 4])).toBeFalsy();
  202. expect(helpers.arrayEquals(['foo', 'bar'], ['bar', 'foo'])).toBeFalsy();
  203. expect(helpers.arrayEquals([1, 2, 3], [1, 2, 'foo'])).toBeFalsy();
  204. expect(helpers.arrayEquals([1, 2, [3, 4]], [1, 2, [3, 'foo']])).toBeFalsy();
  205. expect(helpers.arrayEquals([{a: 42}], [{a: 42}])).toBeFalsy();
  206. });
  207. it('should return false if arrays are not the same', function() {
  208. var o0 = {};
  209. var o1 = {};
  210. var o2 = {};
  211. expect(helpers.arrayEquals([], [])).toBeTruthy();
  212. expect(helpers.arrayEquals([1, 2, 3], [1, 2, 3])).toBeTruthy();
  213. expect(helpers.arrayEquals(['foo', 'bar'], ['foo', 'bar'])).toBeTruthy();
  214. expect(helpers.arrayEquals([true, false, true], [true, false, true])).toBeTruthy();
  215. expect(helpers.arrayEquals([o0, o1, o2], [o0, o1, o2])).toBeTruthy();
  216. });
  217. });
  218. describe('clone', function() {
  219. it('should clone primitive values', function() {
  220. expect(helpers.clone()).toBe(undefined);
  221. expect(helpers.clone(null)).toBe(null);
  222. expect(helpers.clone(true)).toBe(true);
  223. expect(helpers.clone(42)).toBe(42);
  224. expect(helpers.clone('foo')).toBe('foo');
  225. });
  226. it('should perform a deep copy of arrays', function() {
  227. var o0 = {a: 42};
  228. var o1 = {s: 's'};
  229. var a0 = ['bar'];
  230. var a1 = [a0, o0, 2];
  231. var f0 = function() {};
  232. var input = [a1, o1, f0, 42, 'foo'];
  233. var output = helpers.clone(input);
  234. expect(output).toEqual(input);
  235. expect(output).not.toBe(input);
  236. expect(output[0]).not.toBe(a1);
  237. expect(output[0][0]).not.toBe(a0);
  238. expect(output[1]).not.toBe(o1);
  239. });
  240. it('should perform a deep copy of objects', function() {
  241. var a0 = ['bar'];
  242. var a1 = [1, 2, 3];
  243. var o0 = {a: a1, i: 42};
  244. var f0 = function() {};
  245. var input = {o: o0, a: a0, f: f0, s: 'foo', i: 42};
  246. var output = helpers.clone(input);
  247. expect(output).toEqual(input);
  248. expect(output).not.toBe(input);
  249. expect(output.o).not.toBe(o0);
  250. expect(output.o.a).not.toBe(a1);
  251. expect(output.a).not.toBe(a0);
  252. });
  253. });
  254. describe('merge', function() {
  255. it('should update target and return it', function() {
  256. var target = {a: 1};
  257. var result = helpers.merge(target, {a: 2, b: 'foo'});
  258. expect(target).toEqual({a: 2, b: 'foo'});
  259. expect(target).toBe(result);
  260. });
  261. it('should return target if not an object', function() {
  262. expect(helpers.merge(undefined, {a: 42})).toEqual(undefined);
  263. expect(helpers.merge(null, {a: 42})).toEqual(null);
  264. expect(helpers.merge('foo', {a: 42})).toEqual('foo');
  265. expect(helpers.merge(['foo', 'bar'], {a: 42})).toEqual(['foo', 'bar']);
  266. });
  267. it('should ignore sources which are not objects', function() {
  268. expect(helpers.merge({a: 42})).toEqual({a: 42});
  269. expect(helpers.merge({a: 42}, null)).toEqual({a: 42});
  270. expect(helpers.merge({a: 42}, 42)).toEqual({a: 42});
  271. });
  272. it('should recursively overwrite target with source properties', function() {
  273. expect(helpers.merge({a: {b: 1}}, {a: {c: 2}})).toEqual({a: {b: 1, c: 2}});
  274. expect(helpers.merge({a: {b: 1}}, {a: {b: 2}})).toEqual({a: {b: 2}});
  275. expect(helpers.merge({a: [1, 2]}, {a: [3, 4]})).toEqual({a: [3, 4]});
  276. expect(helpers.merge({a: 42}, {a: {b: 0}})).toEqual({a: {b: 0}});
  277. expect(helpers.merge({a: 42}, {a: null})).toEqual({a: null});
  278. expect(helpers.merge({a: 42}, {a: undefined})).toEqual({a: undefined});
  279. });
  280. it('should merge multiple sources in the correct order', function() {
  281. var t0 = {a: {b: 1, c: [1, 2]}};
  282. var s0 = {a: {d: 3}, e: {f: 4}};
  283. var s1 = {a: {b: 5}};
  284. var s2 = {a: {c: [6, 7]}, e: 'foo'};
  285. expect(helpers.merge(t0, [s0, s1, s2])).toEqual({a: {b: 5, c: [6, 7], d: 3}, e: 'foo'});
  286. });
  287. it('should deep copy merged values from sources', function() {
  288. var a0 = ['foo'];
  289. var a1 = [1, 2, 3];
  290. var o0 = {a: a1, i: 42};
  291. var output = helpers.merge({}, {a: a0, o: o0});
  292. expect(output).toEqual({a: a0, o: o0});
  293. expect(output.a).not.toBe(a0);
  294. expect(output.o).not.toBe(o0);
  295. expect(output.o.a).not.toBe(a1);
  296. });
  297. });
  298. describe('mergeIf', function() {
  299. it('should update target and return it', function() {
  300. var target = {a: 1};
  301. var result = helpers.mergeIf(target, {a: 2, b: 'foo'});
  302. expect(target).toEqual({a: 1, b: 'foo'});
  303. expect(target).toBe(result);
  304. });
  305. it('should return target if not an object', function() {
  306. expect(helpers.mergeIf(undefined, {a: 42})).toEqual(undefined);
  307. expect(helpers.mergeIf(null, {a: 42})).toEqual(null);
  308. expect(helpers.mergeIf('foo', {a: 42})).toEqual('foo');
  309. expect(helpers.mergeIf(['foo', 'bar'], {a: 42})).toEqual(['foo', 'bar']);
  310. });
  311. it('should ignore sources which are not objects', function() {
  312. expect(helpers.mergeIf({a: 42})).toEqual({a: 42});
  313. expect(helpers.mergeIf({a: 42}, null)).toEqual({a: 42});
  314. expect(helpers.mergeIf({a: 42}, 42)).toEqual({a: 42});
  315. });
  316. it('should recursively copy source properties in target only if they do not exist in target', function() {
  317. expect(helpers.mergeIf({a: {b: 1}}, {a: {c: 2}})).toEqual({a: {b: 1, c: 2}});
  318. expect(helpers.mergeIf({a: {b: 1}}, {a: {b: 2}})).toEqual({a: {b: 1}});
  319. expect(helpers.mergeIf({a: [1, 2]}, {a: [3, 4]})).toEqual({a: [1, 2]});
  320. expect(helpers.mergeIf({a: 0}, {a: {b: 2}})).toEqual({a: 0});
  321. expect(helpers.mergeIf({a: null}, {a: 42})).toEqual({a: null});
  322. expect(helpers.mergeIf({a: undefined}, {a: 42})).toEqual({a: undefined});
  323. });
  324. it('should merge multiple sources in the correct order', function() {
  325. var t0 = {a: {b: 1, c: [1, 2]}};
  326. var s0 = {a: {d: 3}, e: {f: 4}};
  327. var s1 = {a: {b: 5}};
  328. var s2 = {a: {c: [6, 7]}, e: 'foo'};
  329. expect(helpers.mergeIf(t0, [s0, s1, s2])).toEqual({a: {b: 1, c: [1, 2], d: 3}, e: {f: 4}});
  330. });
  331. it('should deep copy merged values from sources', function() {
  332. var a0 = ['foo'];
  333. var a1 = [1, 2, 3];
  334. var o0 = {a: a1, i: 42};
  335. var output = helpers.mergeIf({}, {a: a0, o: o0});
  336. expect(output).toEqual({a: a0, o: o0});
  337. expect(output.a).not.toBe(a0);
  338. expect(output.o).not.toBe(o0);
  339. expect(output.o.a).not.toBe(a1);
  340. });
  341. });
  342. describe('extend', function() {
  343. it('should merge object properties in target and return target', function() {
  344. var target = {a: 'abc', b: 56};
  345. var object = {b: 0, c: [2, 5, 6]};
  346. var result = helpers.extend(target, object);
  347. expect(result).toBe(target);
  348. expect(target).toEqual({a: 'abc', b: 0, c: [2, 5, 6]});
  349. });
  350. it('should merge multiple objects properties in target', function() {
  351. var target = {a: 0, b: 1};
  352. var o0 = {a: 2, c: 3, d: 4};
  353. var o1 = {a: 5, c: 6};
  354. var o2 = {a: 7, e: 8};
  355. helpers.extend(target, o0, o1, o2);
  356. expect(target).toEqual({a: 7, b: 1, c: 6, d: 4, e: 8});
  357. });
  358. it('should not deeply merge object properties in target', function() {
  359. var target = {a: {b: 0, c: 1}};
  360. var object = {a: {b: 2, d: 3}};
  361. helpers.extend(target, object);
  362. expect(target).toEqual({a: {b: 2, d: 3}});
  363. expect(target.a).toBe(object.a);
  364. });
  365. });
  366. describe('inherits', function() {
  367. it('should return a derived class', function() {
  368. var A = function() {};
  369. A.prototype.p0 = 41;
  370. A.prototype.p1 = function() {
  371. return '42';
  372. };
  373. A.inherits = helpers.inherits;
  374. var B = A.inherits({p0: 43, p2: [44]});
  375. var C = A.inherits({p3: 45, p4: [46]});
  376. var b = new B();
  377. expect(b instanceof A).toBeTruthy();
  378. expect(b instanceof B).toBeTruthy();
  379. expect(b instanceof C).toBeFalsy();
  380. expect(b.p0).toBe(43);
  381. expect(b.p1()).toBe('42');
  382. expect(b.p2).toEqual([44]);
  383. });
  384. });
  385. });