12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157 |
- describe('Chart', function() {
- // https://github.com/chartjs/Chart.js/issues/2481
- // See global.deprecations.tests.js for backward compatibility
- it('should be defined and prototype of chart instances', function() {
- var chart = acquireChart({});
- expect(Chart).toBeDefined();
- expect(Chart instanceof Object).toBeTruthy();
- expect(chart.constructor).toBe(Chart);
- expect(chart instanceof Chart).toBeTruthy();
- expect(Chart.prototype.isPrototypeOf(chart)).toBeTruthy();
- });
- describe('config initialization', function() {
- it('should create missing config.data properties', function() {
- var chart = acquireChart({});
- var data = chart.data;
- expect(data instanceof Object).toBeTruthy();
- expect(data.labels instanceof Array).toBeTruthy();
- expect(data.labels.length).toBe(0);
- expect(data.datasets instanceof Array).toBeTruthy();
- expect(data.datasets.length).toBe(0);
- });
- it('should not alter config.data references', function() {
- var ds0 = {data: [10, 11, 12, 13]};
- var ds1 = {data: [20, 21, 22, 23]};
- var datasets = [ds0, ds1];
- var labels = [0, 1, 2, 3];
- var data = {labels: labels, datasets: datasets};
- var chart = acquireChart({
- type: 'line',
- data: data
- });
- expect(chart.data).toBe(data);
- expect(chart.data.labels).toBe(labels);
- expect(chart.data.datasets).toBe(datasets);
- expect(chart.data.datasets[0]).toBe(ds0);
- expect(chart.data.datasets[1]).toBe(ds1);
- expect(chart.data.datasets[0].data).toBe(ds0.data);
- expect(chart.data.datasets[1].data).toBe(ds1.data);
- });
- it('should define chart.data as an alias for config.data', function() {
- var config = {data: {labels: [], datasets: []}};
- var chart = acquireChart(config);
- expect(chart.data).toBe(config.data);
- chart.data = {labels: [1, 2, 3], datasets: [{data: [4, 5, 6]}]};
- expect(config.data).toBe(chart.data);
- expect(config.data.labels).toEqual([1, 2, 3]);
- expect(config.data.datasets[0].data).toEqual([4, 5, 6]);
- config.data = {labels: [7, 8, 9], datasets: [{data: [10, 11, 12]}]};
- expect(chart.data).toBe(config.data);
- expect(chart.data.labels).toEqual([7, 8, 9]);
- expect(chart.data.datasets[0].data).toEqual([10, 11, 12]);
- });
- it('should initialize config with default options', function() {
- var callback = function() {};
- var defaults = Chart.defaults;
- defaults.global.responsiveAnimationDuration = 42;
- defaults.global.hover.onHover = callback;
- defaults.line.hover.mode = 'x-axis';
- defaults.line.spanGaps = true;
- var chart = acquireChart({
- type: 'line'
- });
- var options = chart.options;
- expect(options.defaultFontSize).toBe(defaults.global.defaultFontSize);
- expect(options.showLines).toBe(defaults.line.showLines);
- expect(options.spanGaps).toBe(true);
- expect(options.responsiveAnimationDuration).toBe(42);
- expect(options.hover.onHover).toBe(callback);
- expect(options.hover.mode).toBe('x-axis');
- });
- it('should override default options', function() {
- var defaults = Chart.defaults;
- defaults.global.responsiveAnimationDuration = 42;
- defaults.line.hover.mode = 'x-axis';
- defaults.line.spanGaps = true;
- var chart = acquireChart({
- type: 'line',
- options: {
- responsiveAnimationDuration: 4242,
- spanGaps: false,
- hover: {
- mode: 'dataset',
- },
- title: {
- position: 'bottom'
- }
- }
- });
- var options = chart.options;
- expect(options.responsiveAnimationDuration).toBe(4242);
- expect(options.spanGaps).toBe(false);
- expect(options.hover.mode).toBe('dataset');
- expect(options.title.position).toBe('bottom');
- });
- it('should override axis positions that are incorrect', function() {
- var chart = acquireChart({
- type: 'line',
- options: {
- scales: {
- xAxes: [{
- position: 'left',
- }],
- yAxes: [{
- position: 'bottom'
- }]
- }
- }
- });
- var scaleOptions = chart.options.scales;
- expect(scaleOptions.xAxes[0].position).toBe('bottom');
- expect(scaleOptions.yAxes[0].position).toBe('left');
- });
- it('should throw an error if the chart type is incorrect', function() {
- function createChart() {
- acquireChart({
- type: 'area',
- data: {
- datasets: [{
- label: 'first',
- data: [10, 20]
- }],
- labels: ['0', '1'],
- },
- options: {
- scales: {
- xAxes: [{
- position: 'left',
- }],
- yAxes: [{
- position: 'bottom'
- }]
- }
- }
- });
- }
- expect(createChart).toThrow(new Error('"area" is not a chart type.'));
- });
- });
- describe('config.options.responsive: false', function() {
- it('should not inject the resizer element', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- });
- var wrapper = chart.canvas.parentNode;
- expect(wrapper.childNodes.length).toBe(1);
- expect(wrapper.firstChild.tagName).toBe('CANVAS');
- });
- });
- describe('config.options.responsive: true (maintainAspectRatio: false)', function() {
- it('should fill parent width and height', function() {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: 'width: 150px; height: 245px'
- },
- wrapper: {
- style: 'width: 300px; height: 350px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 350,
- rw: 300, rh: 350,
- });
- });
- it('should resize the canvas when parent width changes', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'width: 300px; height: 350px; position: relative'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 350,
- rw: 300, rh: 350,
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.width = '455px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 350,
- rw: 455, rh: 350,
- });
- wrapper.style.width = '150px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 150, dh: 350,
- rw: 150, rh: 350,
- });
- done();
- });
- });
- });
- it('should resize the canvas when parent height changes', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'width: 300px; height: 350px; position: relative'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 350,
- rw: 300, rh: 350,
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.height = '455px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 455,
- rw: 300, rh: 455,
- });
- wrapper.style.height = '150px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 150,
- rw: 300, rh: 150,
- });
- done();
- });
- });
- });
- it('should not include parent padding when resizing the canvas', function(done) {
- var chart = acquireChart({
- type: 'line',
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'padding: 50px; width: 320px; height: 350px; position: relative'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 350,
- rw: 320, rh: 350,
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.height = '355px';
- wrapper.style.width = '455px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 355,
- rw: 455, rh: 355,
- });
- done();
- });
- });
- it('should resize the canvas when the canvas display style changes from "none" to "block"', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: 'display: none;'
- },
- wrapper: {
- style: 'width: 320px; height: 350px'
- }
- });
- var canvas = chart.canvas;
- canvas.style.display = 'block';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 350,
- rw: 320, rh: 350,
- });
- done();
- });
- });
- it('should resize the canvas when the wrapper display style changes from "none" to "block"', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'display: none; width: 460px; height: 380px'
- }
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.display = 'block';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 460, dh: 380,
- rw: 460, rh: 380,
- });
- done();
- });
- });
- // https://github.com/chartjs/Chart.js/issues/3790
- it('should resize the canvas if attached to the DOM after construction', function(done) {
- var canvas = document.createElement('canvas');
- var wrapper = document.createElement('div');
- var body = window.document.body;
- var chart = new Chart(canvas, {
- type: 'line',
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 0, dh: 0,
- rw: 0, rh: 0,
- });
- wrapper.style.cssText = 'width: 455px; height: 355px';
- wrapper.appendChild(canvas);
- body.appendChild(wrapper);
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 355,
- rw: 455, rh: 355,
- });
- body.removeChild(wrapper);
- chart.destroy();
- done();
- });
- });
- it('should resize the canvas when attached to a different parent', function(done) {
- var canvas = document.createElement('canvas');
- var wrapper = document.createElement('div');
- var body = window.document.body;
- var chart = new Chart(canvas, {
- type: 'line',
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 0, dh: 0,
- rw: 0, rh: 0,
- });
- wrapper.style.cssText = 'width: 455px; height: 355px';
- wrapper.appendChild(canvas);
- body.appendChild(wrapper);
- waitForResize(chart, function() {
- var resizer = wrapper.firstChild;
- expect(resizer.className).toBe('chartjs-size-monitor');
- expect(resizer.tagName).toBe('DIV');
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 355,
- rw: 455, rh: 355,
- });
- var target = document.createElement('div');
- target.style.cssText = 'width: 640px; height: 480px';
- target.appendChild(canvas);
- body.appendChild(target);
- waitForResize(chart, function() {
- expect(target.firstChild).toBe(resizer);
- expect(wrapper.firstChild).toBe(null);
- expect(chart).toBeChartOfSize({
- dw: 640, dh: 480,
- rw: 640, rh: 480,
- });
- body.removeChild(wrapper);
- body.removeChild(target);
- chart.destroy();
- done();
- });
- });
- });
- // https://github.com/chartjs/Chart.js/issues/3521
- it('should resize the canvas after the wrapper has been re-attached to the DOM', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'width: 320px; height: 350px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 350,
- rw: 320, rh: 350,
- });
- var wrapper = chart.canvas.parentNode;
- var parent = wrapper.parentNode;
- parent.removeChild(wrapper);
- parent.appendChild(wrapper);
- wrapper.style.height = '355px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 355,
- rw: 320, rh: 355,
- });
- parent.removeChild(wrapper);
- wrapper.style.width = '455px';
- parent.appendChild(wrapper);
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 355,
- rw: 455, rh: 355,
- });
- done();
- });
- });
- });
- // https://github.com/chartjs/Chart.js/issues/4737
- it('should resize the canvas when re-creating the chart', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true
- }
- }, {
- wrapper: {
- style: 'width: 320px'
- }
- });
- waitForResize(chart, function() {
- var canvas = chart.canvas;
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 320,
- rw: 320, rh: 320,
- });
- chart.destroy();
- chart = new Chart(canvas, {
- type: 'line',
- options: {
- responsive: true
- }
- });
- canvas.parentNode.style.width = '455px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 455, dh: 455,
- rw: 455, rh: 455,
- });
- done();
- });
- });
- });
- });
- describe('config.options.responsive: true (maintainAspectRatio: true)', function() {
- it('should resize the canvas with correct aspect ratio when parent width changes', function(done) {
- var chart = acquireChart({
- type: 'line', // AR == 2
- options: {
- responsive: true,
- maintainAspectRatio: true
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'width: 300px; height: 350px; position: relative'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 150,
- rw: 300, rh: 150,
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.width = '450px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 450, dh: 225,
- rw: 450, rh: 225,
- });
- wrapper.style.width = '150px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 150, dh: 75,
- rw: 150, rh: 75,
- });
- done();
- });
- });
- });
- it('should not resize the canvas when parent height changes', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: true
- }
- }, {
- canvas: {
- style: ''
- },
- wrapper: {
- style: 'width: 320px; height: 350px; position: relative'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 160,
- rw: 320, rh: 160,
- });
- var wrapper = chart.canvas.parentNode;
- wrapper.style.height = '455px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 160,
- rw: 320, rh: 160,
- });
- wrapper.style.height = '150px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 160,
- rw: 320, rh: 160,
- });
- done();
- });
- });
- });
- });
- describe('Retina scale (a.k.a. device pixel ratio)', function() {
- beforeEach(function() {
- this.devicePixelRatio = window.devicePixelRatio;
- window.devicePixelRatio = 3;
- });
- afterEach(function() {
- window.devicePixelRatio = this.devicePixelRatio;
- });
- // see https://github.com/chartjs/Chart.js/issues/3575
- it ('should scale the render size but not the "implicit" display size', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- width: 320,
- height: 240,
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 240,
- rw: 960, rh: 720,
- });
- });
- it ('should scale the render size but not the "explicit" display size', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 320px; height: 240px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 240,
- rw: 960, rh: 720,
- });
- });
- });
- describe('config.options.devicePixelRatio', function() {
- beforeEach(function() {
- this.devicePixelRatio = window.devicePixelRatio;
- window.devicePixelRatio = 1;
- });
- afterEach(function() {
- window.devicePixelRatio = this.devicePixelRatio;
- });
- // see https://github.com/chartjs/Chart.js/issues/3575
- it ('should scale the render size but not the "implicit" display size', function() {
- var chart = acquireChart({
- options: {
- responsive: false,
- devicePixelRatio: 3
- }
- }, {
- canvas: {
- width: 320,
- height: 240,
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 240,
- rw: 960, rh: 720,
- });
- });
- it ('should scale the render size but not the "explicit" display size', function() {
- var chart = acquireChart({
- options: {
- responsive: false,
- devicePixelRatio: 3
- }
- }, {
- canvas: {
- style: 'width: 320px; height: 240px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 320, dh: 240,
- rw: 960, rh: 720,
- });
- });
- });
- describe('controller.destroy', function() {
- it('should remove the resizer element when responsive: true', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true
- }
- });
- waitForResize(chart, function() {
- var wrapper = chart.canvas.parentNode;
- var resizer = wrapper.firstChild;
- expect(wrapper.childNodes.length).toBe(2);
- expect(resizer.className).toBe('chartjs-size-monitor');
- expect(resizer.tagName).toBe('DIV');
- chart.destroy();
- expect(wrapper.childNodes.length).toBe(1);
- expect(wrapper.firstChild.tagName).toBe('CANVAS');
- done();
- });
- });
- });
- describe('controller.reset', function() {
- it('should reset the chart elements', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 0]
- }]
- },
- options: {
- responsive: true
- }
- });
- var meta = chart.getDatasetMeta(0);
- // Verify that points are at their initial correct location,
- // then we will reset and see that they moved
- expect(meta.data[0]._model.y).toBeCloseToPixel(333);
- expect(meta.data[1]._model.y).toBeCloseToPixel(183);
- expect(meta.data[2]._model.y).toBe(32);
- expect(meta.data[3]._model.y).toBe(484);
- chart.reset();
- // For a line chart, the animation state is the bottom
- expect(meta.data[0]._model.y).toBe(484);
- expect(meta.data[1]._model.y).toBe(484);
- expect(meta.data[2]._model.y).toBe(484);
- expect(meta.data[3]._model.y).toBe(484);
- });
- });
- describe('config update', function() {
- it ('should update options', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true
- }
- });
- chart.options = {
- responsive: false,
- scales: {
- yAxes: [{
- ticks: {
- min: 0,
- max: 10
- }
- }]
- }
- };
- chart.update();
- var yScale = chart.scales['y-axis-0'];
- expect(yScale.options.ticks.min).toBe(0);
- expect(yScale.options.ticks.max).toBe(10);
- });
- it ('should update scales options', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true
- }
- });
- chart.options.scales.yAxes[0].ticks.min = 0;
- chart.options.scales.yAxes[0].ticks.max = 10;
- chart.update();
- var yScale = chart.scales['y-axis-0'];
- expect(yScale.options.ticks.min).toBe(0);
- expect(yScale.options.ticks.max).toBe(10);
- });
- it ('should update scales options from new object', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true
- }
- });
- var newScalesConfig = {
- yAxes: [{
- ticks: {
- min: 0,
- max: 10
- }
- }]
- };
- chart.options.scales = newScalesConfig;
- chart.update();
- var yScale = chart.scales['y-axis-0'];
- expect(yScale.options.ticks.min).toBe(0);
- expect(yScale.options.ticks.max).toBe(10);
- });
- it ('should remove discarded scale', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true,
- scales: {
- yAxes: [{
- id: 'yAxis0',
- ticks: {
- min: 0,
- max: 10
- }
- }]
- }
- }
- });
- var newScalesConfig = {
- yAxes: [{
- ticks: {
- min: 0,
- max: 10
- }
- }]
- };
- chart.options.scales = newScalesConfig;
- chart.update();
- var yScale = chart.scales.yAxis0;
- expect(yScale).toBeUndefined();
- var newyScale = chart.scales['y-axis-0'];
- expect(newyScale.options.ticks.min).toBe(0);
- expect(newyScale.options.ticks.max).toBe(10);
- });
- it ('should update tooltip options', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true
- }
- });
- var newTooltipConfig = {
- mode: 'dataset',
- intersect: false
- };
- chart.options.tooltips = newTooltipConfig;
- chart.update();
- expect(chart.tooltip._options).toEqual(jasmine.objectContaining(newTooltipConfig));
- });
- it ('should reset the tooltip on update', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true,
- tooltip: {
- mode: 'nearest'
- }
- }
- });
- // Trigger an event over top of a point to
- // put an item into the tooltip
- var meta = chart.getDatasetMeta(0);
- var point = meta.data[1];
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var evt = new MouseEvent('mousemove', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: rect.left + point._model.x,
- clientY: 0
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check and see if tooltip was displayed
- var tooltip = chart.tooltip;
- expect(chart.lastActive).toEqual([point]);
- expect(tooltip._lastActive).toEqual([]);
- // Update and confirm tooltip is reset
- chart.update();
- expect(chart.lastActive).toEqual([]);
- expect(tooltip._lastActive).toEqual([]);
- });
- it ('should update the metadata', function() {
- var cfg = {
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- type: 'line',
- data: [10, 20, 30, 0]
- }]
- },
- options: {
- responsive: true,
- scales: {
- xAxes: [{
- type: 'time'
- }],
- yAxes: [{
- scaleLabel: {
- display: true,
- labelString: 'Value'
- }
- }]
- }
- }
- };
- var chart = acquireChart(cfg);
- var meta = chart.getDatasetMeta(0);
- expect(meta.type).toBe('line');
- // change the dataset to bar and check that meta was updated
- chart.config.data.datasets[0].type = 'bar';
- chart.update();
- meta = chart.getDatasetMeta(0);
- expect(meta.type).toBe('bar');
- });
- });
- describe('plugin.extensions', function() {
- it ('should notify plugin in correct order', function(done) {
- var plugin = this.plugin = {};
- var sequence = [];
- var hooks = {
- init: [
- 'beforeInit',
- 'afterInit'
- ],
- update: [
- 'beforeUpdate',
- 'beforeLayout',
- 'afterLayout',
- 'beforeDatasetsUpdate',
- 'beforeDatasetUpdate',
- 'afterDatasetUpdate',
- 'afterDatasetsUpdate',
- 'afterUpdate',
- ],
- render: [
- 'beforeRender',
- 'beforeDraw',
- 'beforeDatasetsDraw',
- 'beforeDatasetDraw',
- 'afterDatasetDraw',
- 'afterDatasetsDraw',
- 'beforeTooltipDraw',
- 'afterTooltipDraw',
- 'afterDraw',
- 'afterRender',
- ],
- resize: [
- 'resize'
- ],
- destroy: [
- 'destroy'
- ]
- };
- Object.keys(hooks).forEach(function(group) {
- hooks[group].forEach(function(name) {
- plugin[name] = function() {
- sequence.push(name);
- };
- });
- });
- var chart = window.acquireChart({
- type: 'line',
- data: {datasets: [{}]},
- plugins: [plugin],
- options: {
- responsive: true
- }
- }, {
- wrapper: {
- style: 'width: 300px'
- }
- });
- chart.canvas.parentNode.style.width = '400px';
- waitForResize(chart, function() {
- chart.destroy();
- expect(sequence).toEqual([].concat(
- hooks.init,
- hooks.update,
- hooks.render,
- hooks.resize,
- hooks.update,
- hooks.render,
- hooks.destroy
- ));
- done();
- });
- });
- it('should not notify before/afterDatasetDraw if dataset is hidden', function() {
- var sequence = [];
- var plugin = this.plugin = {
- beforeDatasetDraw: function(chart, args) {
- sequence.push('before-' + args.index);
- },
- afterDatasetDraw: function(chart, args) {
- sequence.push('after-' + args.index);
- }
- };
- window.acquireChart({
- type: 'line',
- data: {datasets: [{}, {hidden: true}, {}]},
- plugins: [plugin]
- });
- expect(sequence).toEqual([
- 'before-2', 'after-2',
- 'before-0', 'after-0'
- ]);
- });
- });
- describe('controller.update', function() {
- beforeEach(function() {
- this.chart = acquireChart({
- type: 'doughnut',
- options: {
- animation: {
- easing: 'linear',
- duration: 500
- }
- }
- });
- this.addAnimationSpy = spyOn(Chart.animationService, 'addAnimation');
- });
- it('should add an animation with the default options', function() {
- this.chart.update();
- expect(this.addAnimationSpy).toHaveBeenCalledWith(
- this.chart,
- jasmine.objectContaining({easing: 'linear'}),
- undefined,
- undefined
- );
- });
- it('should add an animation with the provided options', function() {
- this.chart.update({
- duration: 800,
- easing: 'easeOutBounce',
- lazy: false,
- });
- expect(this.addAnimationSpy).toHaveBeenCalledWith(
- this.chart,
- jasmine.objectContaining({easing: 'easeOutBounce'}),
- 800,
- false
- );
- });
- });
- });
|