events.spec.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import Chart from 'chart.js';
  2. describe('events', function() {
  3. beforeEach(function() {
  4. this.data = {
  5. labels: [1, 2, 3],
  6. datasets: [{
  7. data: [1, 2, 3]
  8. }, {
  9. data: [4, 5, 6]
  10. }]
  11. };
  12. });
  13. describe('`enter` handlers', function() {
  14. it('should be called when the mouse moves inside the label', function() {
  15. var spy = jasmine.createSpy('spy');
  16. var chart = jasmine.chart.acquire({
  17. type: 'line',
  18. data: this.data,
  19. options: {
  20. plugins: {
  21. datalabels: {
  22. listeners: {
  23. enter: spy
  24. }
  25. }
  26. }
  27. }
  28. });
  29. var ds0 = chart.getDatasetMeta(0);
  30. expect(spy.calls.count()).toBe(0);
  31. jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
  32. expect(spy.calls.count()).toBe(1);
  33. jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[2]);
  34. expect(spy.calls.count()).toBe(2);
  35. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
  36. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
  37. expect(spy.calls.argsFor(1)[0].dataIndex).toBe(2);
  38. expect(spy.calls.argsFor(1)[0].datasetIndex).toBe(0);
  39. });
  40. });
  41. describe('`leave` handlers', function() {
  42. it('should be called when the mouse moves outside the label', function() {
  43. var spy = jasmine.createSpy('spy');
  44. var chart = jasmine.chart.acquire({
  45. type: 'line',
  46. data: this.data,
  47. options: {
  48. plugins: {
  49. datalabels: {
  50. listeners: {
  51. leave: spy
  52. }
  53. }
  54. }
  55. }
  56. });
  57. var ds0 = chart.getDatasetMeta(0);
  58. expect(spy.calls.count()).toBe(0);
  59. jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
  60. expect(spy.calls.count()).toBe(0);
  61. jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[2]);
  62. expect(spy.calls.count()).toBe(1);
  63. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
  64. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
  65. });
  66. it('should be called when the mouse moves out the canvas', function() {
  67. var spy = jasmine.createSpy('spy');
  68. var chart = jasmine.chart.acquire({
  69. type: 'line',
  70. data: this.data,
  71. options: {
  72. plugins: {
  73. datalabels: {
  74. listeners: {
  75. leave: spy
  76. }
  77. }
  78. }
  79. }
  80. });
  81. var ds0 = chart.getDatasetMeta(0);
  82. expect(spy.calls.count()).toBe(0);
  83. jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
  84. expect(spy.calls.count()).toBe(0);
  85. jasmine.triggerMouseEvent(chart, 'mouseout');
  86. expect(spy.calls.count()).toBe(1);
  87. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
  88. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
  89. });
  90. });
  91. describe('`click` handlers', function() {
  92. it('should be called when user click a label', function() {
  93. var spy = jasmine.createSpy('spy');
  94. var chart = jasmine.chart.acquire({
  95. type: 'line',
  96. data: this.data,
  97. options: {
  98. plugins: {
  99. datalabels: {
  100. listeners: {
  101. click: spy
  102. }
  103. }
  104. }
  105. }
  106. });
  107. var ds0 = chart.getDatasetMeta(0);
  108. expect(spy.calls.count()).toBe(0);
  109. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  110. expect(spy.calls.count()).toBe(1);
  111. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
  112. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
  113. });
  114. });
  115. describe('`listeners` option', function() {
  116. it('should ignore events if empty', function() {
  117. var chart = jasmine.chart.acquire({
  118. type: 'line',
  119. data: this.data
  120. });
  121. expect(chart.$datalabels.listened).toBeFalsy();
  122. });
  123. it('should call handlers for any labels if at the options level', function() {
  124. var spy = jasmine.createSpy('spy');
  125. var chart = jasmine.chart.acquire({
  126. type: 'line',
  127. data: this.data,
  128. options: {
  129. plugins: {
  130. datalabels: {
  131. listeners: {
  132. click: spy
  133. }
  134. }
  135. }
  136. }
  137. });
  138. var ds0 = chart.getDatasetMeta(0);
  139. var ds1 = chart.getDatasetMeta(1);
  140. expect(chart.$datalabels.listened).toBeTruthy();
  141. expect(spy.calls.count()).toBe(0);
  142. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  143. jasmine.triggerMouseEvent(chart, 'click', ds1.data[2]);
  144. expect(spy.calls.count()).toBe(2);
  145. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
  146. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
  147. expect(spy.calls.argsFor(1)[0].dataIndex).toBe(2);
  148. expect(spy.calls.argsFor(1)[0].datasetIndex).toBe(1);
  149. });
  150. it('should call handlers only for labels of the same dataset if at the dataset level', function() {
  151. var spy = jasmine.createSpy('spy');
  152. var data = Chart.helpers.clone(this.data);
  153. data.datasets[1].datalabels = {
  154. listeners: {
  155. click: spy
  156. }
  157. };
  158. var chart = jasmine.chart.acquire({
  159. type: 'line',
  160. data: data
  161. });
  162. var ds0 = chart.getDatasetMeta(0);
  163. var ds1 = chart.getDatasetMeta(1);
  164. expect(chart.$datalabels.listened).toBeTruthy();
  165. expect(spy.calls.count()).toBe(0);
  166. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  167. jasmine.triggerMouseEvent(chart, 'click', ds1.data[2]);
  168. expect(spy.calls.count()).toBe(1);
  169. expect(spy.calls.argsFor(0)[0].dataIndex).toBe(2);
  170. expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(1);
  171. });
  172. });
  173. describe('handlers', function() {
  174. it('should update label when explicitly returning `true`', function() {
  175. var options = {
  176. opacity: function(context) {
  177. return context.foobar ? 1 : 0.5;
  178. },
  179. listeners: {
  180. click: function(context) {
  181. context.foobar = !context.foobar;
  182. return true;
  183. }
  184. }
  185. };
  186. spyOn(options, 'opacity');
  187. var chart = jasmine.chart.acquire({
  188. type: 'line',
  189. data: this.data,
  190. options: {
  191. hover: false,
  192. plugins: {
  193. datalabels: options
  194. }
  195. }
  196. });
  197. spyOn(chart, 'render');
  198. var ds0 = chart.getDatasetMeta(0);
  199. expect(chart.render).not.toHaveBeenCalled();
  200. expect(options.opacity).toHaveBeenCalled();
  201. expect(options.opacity.calls.argsFor(0)[0].foobar).toBeUndefined();
  202. options.opacity.calls.reset();
  203. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  204. expect(chart.render).toHaveBeenCalled();
  205. expect(options.opacity).toHaveBeenCalled();
  206. expect(options.opacity.calls.argsFor(0)[0].foobar).toBeTruthy();
  207. options.opacity.calls.reset();
  208. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  209. expect(chart.render).toHaveBeenCalled();
  210. expect(options.opacity).toHaveBeenCalled();
  211. expect(options.opacity.calls.argsFor(0)[0].foobar).toBeFalsy();
  212. });
  213. it('should not update label when returning not `true`', function() {
  214. var options = {
  215. opacity: function(context) {
  216. return context.foobar ? 1 : 0.5;
  217. },
  218. listeners: {
  219. click: function(context) {
  220. context.foobar = !context.foobar;
  221. // WE DO NOT RETURN TRUE // return true;
  222. }
  223. }
  224. };
  225. spyOn(options, 'opacity');
  226. var chart = jasmine.chart.acquire({
  227. type: 'line',
  228. data: this.data,
  229. options: {
  230. hover: false,
  231. plugins: {
  232. datalabels: options
  233. }
  234. }
  235. });
  236. spyOn(chart, 'render');
  237. var ds0 = chart.getDatasetMeta(0);
  238. expect(chart.render).not.toHaveBeenCalled();
  239. expect(options.opacity).toHaveBeenCalled();
  240. options.opacity.calls.reset();
  241. jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
  242. expect(chart.render).not.toHaveBeenCalled();
  243. expect(options.opacity).not.toHaveBeenCalled();
  244. });
  245. });
  246. });