global.deprecations.tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. describe('Deprecations', function() {
  2. describe('Version 2.8.0', function() {
  3. describe('Chart.layoutService', function() {
  4. it('should be defined and an alias of Chart.layouts', function() {
  5. expect(Chart.layoutService).toBeDefined();
  6. expect(Chart.layoutService).toBe(Chart.layouts);
  7. });
  8. });
  9. });
  10. describe('Version 2.7.0', function() {
  11. describe('Chart.Controller.update(duration, lazy)', function() {
  12. it('should add an animation with the provided options', function() {
  13. var chart = acquireChart({
  14. type: 'doughnut',
  15. options: {
  16. animation: {
  17. easing: 'linear',
  18. duration: 500
  19. }
  20. }
  21. });
  22. spyOn(Chart.animationService, 'addAnimation');
  23. chart.update(800, false);
  24. expect(Chart.animationService.addAnimation).toHaveBeenCalledWith(
  25. chart,
  26. jasmine.objectContaining({easing: 'linear'}),
  27. 800,
  28. false
  29. );
  30. });
  31. });
  32. describe('Chart.Controller.render(duration, lazy)', function() {
  33. it('should add an animation with the provided options', function() {
  34. var chart = acquireChart({
  35. type: 'doughnut',
  36. options: {
  37. animation: {
  38. easing: 'linear',
  39. duration: 500
  40. }
  41. }
  42. });
  43. spyOn(Chart.animationService, 'addAnimation');
  44. chart.render(800, true);
  45. expect(Chart.animationService.addAnimation).toHaveBeenCalledWith(
  46. chart,
  47. jasmine.objectContaining({easing: 'linear'}),
  48. 800,
  49. true
  50. );
  51. });
  52. });
  53. describe('Chart.helpers.indexOf', function() {
  54. it('should be defined and a function', function() {
  55. expect(Chart.helpers.indexOf).toBeDefined();
  56. expect(typeof Chart.helpers.indexOf).toBe('function');
  57. });
  58. it('should returns the correct index', function() {
  59. expect(Chart.helpers.indexOf([1, 2, 42], 42)).toBe(2);
  60. expect(Chart.helpers.indexOf([1, 2, 42], 3)).toBe(-1);
  61. expect(Chart.helpers.indexOf([1, 42, 2, 42], 42, 2)).toBe(3);
  62. expect(Chart.helpers.indexOf([1, 42, 2, 42], 3, 2)).toBe(-1);
  63. });
  64. });
  65. describe('Chart.helpers.clear', function() {
  66. it('should be defined and an alias of Chart.helpers.canvas.clear', function() {
  67. expect(Chart.helpers.clear).toBeDefined();
  68. expect(Chart.helpers.clear).toBe(Chart.helpers.canvas.clear);
  69. });
  70. });
  71. describe('Chart.helpers.getValueOrDefault', function() {
  72. it('should be defined and an alias of Chart.helpers.valueOrDefault', function() {
  73. expect(Chart.helpers.getValueOrDefault).toBeDefined();
  74. expect(Chart.helpers.getValueOrDefault).toBe(Chart.helpers.valueOrDefault);
  75. });
  76. });
  77. describe('Chart.helpers.getValueAtIndexOrDefault', function() {
  78. it('should be defined and an alias of Chart.helpers.valueAtIndexOrDefault', function() {
  79. expect(Chart.helpers.getValueAtIndexOrDefault).toBeDefined();
  80. expect(Chart.helpers.getValueAtIndexOrDefault).toBe(Chart.helpers.valueAtIndexOrDefault);
  81. });
  82. });
  83. describe('Chart.helpers.easingEffects', function() {
  84. it('should be defined and an alias of Chart.helpers.easing.effects', function() {
  85. expect(Chart.helpers.easingEffects).toBeDefined();
  86. expect(Chart.helpers.easingEffects).toBe(Chart.helpers.easing.effects);
  87. });
  88. });
  89. describe('Chart.helpers.drawRoundedRectangle', function() {
  90. it('should be defined and a function', function() {
  91. expect(Chart.helpers.drawRoundedRectangle).toBeDefined();
  92. expect(typeof Chart.helpers.drawRoundedRectangle).toBe('function');
  93. });
  94. it('should call Chart.helpers.canvas.roundedRect', function() {
  95. var ctx = window.createMockContext();
  96. spyOn(Chart.helpers.canvas, 'roundedRect');
  97. Chart.helpers.drawRoundedRectangle(ctx, 10, 20, 30, 40, 5);
  98. var calls = ctx.getCalls();
  99. expect(calls[0]).toEqual({name: 'beginPath', args: []});
  100. expect(calls[calls.length - 1]).toEqual({name: 'closePath', args: []});
  101. expect(Chart.helpers.canvas.roundedRect).toHaveBeenCalledWith(ctx, 10, 20, 30, 40, 5);
  102. });
  103. });
  104. describe('Chart.helpers.addEvent', function() {
  105. it('should be defined and a function', function() {
  106. expect(Chart.helpers.addEvent).toBeDefined();
  107. expect(typeof Chart.helpers.addEvent).toBe('function');
  108. });
  109. it('should correctly add event listener', function() {
  110. var listener = jasmine.createSpy('spy');
  111. Chart.helpers.addEvent(window, 'test', listener);
  112. window.dispatchEvent(new Event('test'));
  113. expect(listener).toHaveBeenCalled();
  114. });
  115. });
  116. describe('Chart.helpers.removeEvent', function() {
  117. it('should be defined and a function', function() {
  118. expect(Chart.helpers.removeEvent).toBeDefined();
  119. expect(typeof Chart.helpers.removeEvent).toBe('function');
  120. });
  121. it('should correctly remove event listener', function() {
  122. var listener = jasmine.createSpy('spy');
  123. Chart.helpers.addEvent(window, 'test', listener);
  124. Chart.helpers.removeEvent(window, 'test', listener);
  125. window.dispatchEvent(new Event('test'));
  126. expect(listener).not.toHaveBeenCalled();
  127. });
  128. });
  129. });
  130. describe('Version 2.6.0', function() {
  131. // https://github.com/chartjs/Chart.js/issues/2481
  132. describe('Chart.Controller', function() {
  133. it('should be defined and an alias of Chart', function() {
  134. expect(Chart.Controller).toBeDefined();
  135. expect(Chart.Controller).toBe(Chart);
  136. });
  137. it('should be prototype of chart instances', function() {
  138. var chart = acquireChart({});
  139. expect(chart.constructor).toBe(Chart.Controller);
  140. expect(chart instanceof Chart.Controller).toBeTruthy();
  141. expect(Chart.Controller.prototype.isPrototypeOf(chart)).toBeTruthy();
  142. });
  143. });
  144. describe('chart.chart', function() {
  145. it('should be defined and an alias of chart', function() {
  146. var chart = acquireChart({});
  147. var proxy = chart.chart;
  148. expect(proxy).toBeDefined();
  149. expect(proxy).toBe(chart);
  150. });
  151. it('should defined previously existing properties', function() {
  152. var chart = acquireChart({}, {
  153. canvas: {
  154. style: 'width: 140px; height: 320px'
  155. }
  156. });
  157. var proxy = chart.chart;
  158. expect(proxy.config instanceof Object).toBeTruthy();
  159. expect(proxy.controller instanceof Chart.Controller).toBeTruthy();
  160. expect(proxy.canvas instanceof HTMLCanvasElement).toBeTruthy();
  161. expect(proxy.ctx instanceof CanvasRenderingContext2D).toBeTruthy();
  162. expect(proxy.currentDevicePixelRatio).toBe(window.devicePixelRatio || 1);
  163. expect(proxy.aspectRatio).toBe(140 / 320);
  164. expect(proxy.height).toBe(320);
  165. expect(proxy.width).toBe(140);
  166. });
  167. });
  168. describe('Chart.Animation.animationObject', function() {
  169. it('should be defined and an alias of Chart.Animation', function(done) {
  170. var animation = null;
  171. acquireChart({
  172. options: {
  173. animation: {
  174. duration: 50,
  175. onComplete: function(arg) {
  176. animation = arg;
  177. }
  178. }
  179. }
  180. });
  181. setTimeout(function() {
  182. expect(animation).not.toBeNull();
  183. expect(animation.animationObject).toBeDefined();
  184. expect(animation.animationObject).toBe(animation);
  185. done();
  186. }, 200);
  187. });
  188. });
  189. describe('Chart.Animation.chartInstance', function() {
  190. it('should be defined and an alias of Chart.Animation.chart', function(done) {
  191. var animation = null;
  192. var chart = acquireChart({
  193. options: {
  194. animation: {
  195. duration: 50,
  196. onComplete: function(arg) {
  197. animation = arg;
  198. }
  199. }
  200. }
  201. });
  202. setTimeout(function() {
  203. expect(animation).not.toBeNull();
  204. expect(animation.chartInstance).toBeDefined();
  205. expect(animation.chartInstance).toBe(chart);
  206. done();
  207. }, 200);
  208. });
  209. });
  210. describe('Chart.elements.Line: fill option', function() {
  211. it('should decode "zero", "top" and "bottom" as "origin", "start" and "end"', function() {
  212. var chart = window.acquireChart({
  213. type: 'line',
  214. data: {
  215. datasets: [
  216. {fill: 'zero'},
  217. {fill: 'bottom'},
  218. {fill: 'top'},
  219. ]
  220. }
  221. });
  222. ['origin', 'start', 'end'].forEach(function(expected, index) {
  223. var meta = chart.getDatasetMeta(index);
  224. expect(meta.$filler).toBeDefined();
  225. expect(meta.$filler.fill).toBe(expected);
  226. });
  227. });
  228. });
  229. describe('Chart.helpers.callCallback', function() {
  230. it('should be defined and an alias of Chart.helpers.callback', function() {
  231. expect(Chart.helpers.callCallback).toBeDefined();
  232. expect(Chart.helpers.callCallback).toBe(Chart.helpers.callback);
  233. });
  234. });
  235. describe('Time Axis: unitStepSize option', function() {
  236. it('should use the stepSize property', function() {
  237. var chart = window.acquireChart({
  238. type: 'line',
  239. data: {
  240. labels: ['2015-01-01T20:00:00', '2015-01-01T21:00:00'],
  241. },
  242. options: {
  243. scales: {
  244. xAxes: [{
  245. id: 'time',
  246. type: 'time',
  247. bounds: 'ticks',
  248. time: {
  249. unit: 'hour',
  250. unitStepSize: 2
  251. }
  252. }]
  253. }
  254. }
  255. });
  256. var ticks = chart.scales.time.getTicks().map(function(tick) {
  257. return tick.label;
  258. });
  259. expect(ticks).toEqual(['8PM', '10PM']);
  260. });
  261. });
  262. });
  263. describe('Version 2.5.0', function() {
  264. describe('Chart.PluginBase', function() {
  265. it('should exist and extendable', function() {
  266. expect(Chart.PluginBase).toBeDefined();
  267. expect(Chart.PluginBase.extend).toBeDefined();
  268. });
  269. });
  270. describe('IPlugin.afterScaleUpdate', function() {
  271. it('should be called after the chart as been layed out', function() {
  272. var sequence = [];
  273. var plugin = {};
  274. var hooks = [
  275. 'beforeLayout',
  276. 'afterScaleUpdate',
  277. 'afterLayout'
  278. ];
  279. var override = Chart.layouts.update;
  280. Chart.layouts.update = function() {
  281. sequence.push('layoutUpdate');
  282. override.apply(this, arguments);
  283. };
  284. hooks.forEach(function(name) {
  285. plugin[name] = function() {
  286. sequence.push(name);
  287. };
  288. });
  289. window.acquireChart({plugins: [plugin]});
  290. expect(sequence).toEqual([].concat(
  291. 'beforeLayout',
  292. 'layoutUpdate',
  293. 'afterScaleUpdate',
  294. 'afterLayout'
  295. ));
  296. });
  297. });
  298. });
  299. describe('Version 2.4.0', function() {
  300. describe('x-axis mode', function() {
  301. it ('behaves like index mode with intersect: false', function() {
  302. var data = {
  303. datasets: [{
  304. label: 'Dataset 1',
  305. data: [10, 20, 30],
  306. pointHoverBorderColor: 'rgb(255, 0, 0)',
  307. pointHoverBackgroundColor: 'rgb(0, 255, 0)'
  308. }, {
  309. label: 'Dataset 2',
  310. data: [40, 40, 40],
  311. pointHoverBorderColor: 'rgb(0, 0, 255)',
  312. pointHoverBackgroundColor: 'rgb(0, 255, 255)'
  313. }],
  314. labels: ['Point 1', 'Point 2', 'Point 3']
  315. };
  316. var chart = window.acquireChart({
  317. type: 'line',
  318. data: data
  319. });
  320. var meta0 = chart.getDatasetMeta(0);
  321. var meta1 = chart.getDatasetMeta(1);
  322. var evt = {
  323. type: 'click',
  324. chart: chart,
  325. native: true, // needed otherwise things its a DOM event
  326. x: 0,
  327. y: 0
  328. };
  329. var elements = Chart.Interaction.modes['x-axis'](chart, evt);
  330. expect(elements).toEqual([meta0.data[0], meta1.data[0]]);
  331. });
  332. });
  333. });
  334. describe('Version 2.1.5', function() {
  335. // https://github.com/chartjs/Chart.js/pull/2752
  336. describe('Chart.pluginService', function() {
  337. it('should be defined and an alias of Chart.plugins', function() {
  338. expect(Chart.pluginService).toBeDefined();
  339. expect(Chart.pluginService).toBe(Chart.plugins);
  340. });
  341. });
  342. describe('Chart.Legend', function() {
  343. it('should be defined and an instance of Chart.Element', function() {
  344. var legend = new Chart.Legend({});
  345. expect(Chart.Legend).toBeDefined();
  346. expect(legend).not.toBe(undefined);
  347. expect(legend instanceof Chart.Element).toBeTruthy();
  348. });
  349. });
  350. describe('Chart.Title', function() {
  351. it('should be defined and an instance of Chart.Element', function() {
  352. var title = new Chart.Title({});
  353. expect(Chart.Title).toBeDefined();
  354. expect(title).not.toBe(undefined);
  355. expect(title instanceof Chart.Element).toBeTruthy();
  356. });
  357. });
  358. });
  359. });