123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import Chart from 'chart.js';
- describe('events', function() {
- beforeEach(function() {
- this.data = {
- labels: [1, 2, 3],
- datasets: [{
- data: [1, 2, 3]
- }, {
- data: [4, 5, 6]
- }]
- };
- });
- describe('`enter` handlers', function() {
- it('should be called when the mouse moves inside the label', function() {
- var spy = jasmine.createSpy('spy');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- plugins: {
- datalabels: {
- listeners: {
- enter: spy
- }
- }
- }
- }
- });
- var ds0 = chart.getDatasetMeta(0);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
- expect(spy.calls.count()).toBe(1);
- jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[2]);
- expect(spy.calls.count()).toBe(2);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
- expect(spy.calls.argsFor(1)[0].dataIndex).toBe(2);
- expect(spy.calls.argsFor(1)[0].datasetIndex).toBe(0);
- });
- });
- describe('`leave` handlers', function() {
- it('should be called when the mouse moves outside the label', function() {
- var spy = jasmine.createSpy('spy');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- plugins: {
- datalabels: {
- listeners: {
- leave: spy
- }
- }
- }
- }
- });
- var ds0 = chart.getDatasetMeta(0);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[2]);
- expect(spy.calls.count()).toBe(1);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
- });
- it('should be called when the mouse moves out the canvas', function() {
- var spy = jasmine.createSpy('spy');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- plugins: {
- datalabels: {
- listeners: {
- leave: spy
- }
- }
- }
- }
- });
- var ds0 = chart.getDatasetMeta(0);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'mousemove', ds0.data[1]);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'mouseout');
- expect(spy.calls.count()).toBe(1);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
- });
- });
- describe('`click` handlers', function() {
- it('should be called when user click a label', function() {
- var spy = jasmine.createSpy('spy');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- plugins: {
- datalabels: {
- listeners: {
- click: spy
- }
- }
- }
- }
- });
- var ds0 = chart.getDatasetMeta(0);
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- expect(spy.calls.count()).toBe(1);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
- });
- });
- describe('`listeners` option', function() {
- it('should ignore events if empty', function() {
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data
- });
- expect(chart.$datalabels.listened).toBeFalsy();
- });
- it('should call handlers for any labels if at the options level', function() {
- var spy = jasmine.createSpy('spy');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- plugins: {
- datalabels: {
- listeners: {
- click: spy
- }
- }
- }
- }
- });
- var ds0 = chart.getDatasetMeta(0);
- var ds1 = chart.getDatasetMeta(1);
- expect(chart.$datalabels.listened).toBeTruthy();
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- jasmine.triggerMouseEvent(chart, 'click', ds1.data[2]);
- expect(spy.calls.count()).toBe(2);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(1);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(0);
- expect(spy.calls.argsFor(1)[0].dataIndex).toBe(2);
- expect(spy.calls.argsFor(1)[0].datasetIndex).toBe(1);
- });
- it('should call handlers only for labels of the same dataset if at the dataset level', function() {
- var spy = jasmine.createSpy('spy');
- var data = Chart.helpers.clone(this.data);
- data.datasets[1].datalabels = {
- listeners: {
- click: spy
- }
- };
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: data
- });
- var ds0 = chart.getDatasetMeta(0);
- var ds1 = chart.getDatasetMeta(1);
- expect(chart.$datalabels.listened).toBeTruthy();
- expect(spy.calls.count()).toBe(0);
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- jasmine.triggerMouseEvent(chart, 'click', ds1.data[2]);
- expect(spy.calls.count()).toBe(1);
- expect(spy.calls.argsFor(0)[0].dataIndex).toBe(2);
- expect(spy.calls.argsFor(0)[0].datasetIndex).toBe(1);
- });
- });
- describe('handlers', function() {
- it('should update label when explicitly returning `true`', function() {
- var options = {
- opacity: function(context) {
- return context.foobar ? 1 : 0.5;
- },
- listeners: {
- click: function(context) {
- context.foobar = !context.foobar;
- return true;
- }
- }
- };
- spyOn(options, 'opacity');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- hover: false,
- plugins: {
- datalabels: options
- }
- }
- });
- spyOn(chart, 'render');
- var ds0 = chart.getDatasetMeta(0);
- expect(chart.render).not.toHaveBeenCalled();
- expect(options.opacity).toHaveBeenCalled();
- expect(options.opacity.calls.argsFor(0)[0].foobar).toBeUndefined();
- options.opacity.calls.reset();
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- expect(chart.render).toHaveBeenCalled();
- expect(options.opacity).toHaveBeenCalled();
- expect(options.opacity.calls.argsFor(0)[0].foobar).toBeTruthy();
- options.opacity.calls.reset();
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- expect(chart.render).toHaveBeenCalled();
- expect(options.opacity).toHaveBeenCalled();
- expect(options.opacity.calls.argsFor(0)[0].foobar).toBeFalsy();
- });
- it('should not update label when returning not `true`', function() {
- var options = {
- opacity: function(context) {
- return context.foobar ? 1 : 0.5;
- },
- listeners: {
- click: function(context) {
- context.foobar = !context.foobar;
- // WE DO NOT RETURN TRUE // return true;
- }
- }
- };
- spyOn(options, 'opacity');
- var chart = jasmine.chart.acquire({
- type: 'line',
- data: this.data,
- options: {
- hover: false,
- plugins: {
- datalabels: options
- }
- }
- });
- spyOn(chart, 'render');
- var ds0 = chart.getDatasetMeta(0);
- expect(chart.render).not.toHaveBeenCalled();
- expect(options.opacity).toHaveBeenCalled();
- options.opacity.calls.reset();
- jasmine.triggerMouseEvent(chart, 'click', ds0.data[1]);
- expect(chart.render).not.toHaveBeenCalled();
- expect(options.opacity).not.toHaveBeenCalled();
- });
- });
- });
|