123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- describe('Platform.dom', function() {
- describe('context acquisition', function() {
- var canvasId = 'chartjs-canvas';
- beforeEach(function() {
- var canvas = document.createElement('canvas');
- canvas.setAttribute('id', canvasId);
- window.document.body.appendChild(canvas);
- });
- afterEach(function() {
- document.getElementById(canvasId).remove();
- });
- // see https://github.com/chartjs/Chart.js/issues/2807
- it('should gracefully handle invalid item', function() {
- var chart = new Chart('foobar');
- expect(chart).not.toBeValidChart();
- chart.destroy();
- });
- it('should accept a DOM element id', function() {
- var canvas = document.getElementById(canvasId);
- var chart = new Chart(canvasId);
- expect(chart).toBeValidChart();
- expect(chart.canvas).toBe(canvas);
- expect(chart.ctx).toBe(canvas.getContext('2d'));
- chart.destroy();
- });
- it('should accept a canvas element', function() {
- var canvas = document.getElementById(canvasId);
- var chart = new Chart(canvas);
- expect(chart).toBeValidChart();
- expect(chart.canvas).toBe(canvas);
- expect(chart.ctx).toBe(canvas.getContext('2d'));
- chart.destroy();
- });
- it('should accept a canvas context2D', function() {
- var canvas = document.getElementById(canvasId);
- var context = canvas.getContext('2d');
- var chart = new Chart(context);
- expect(chart).toBeValidChart();
- expect(chart.canvas).toBe(canvas);
- expect(chart.ctx).toBe(context);
- chart.destroy();
- });
- it('should accept an array containing canvas', function() {
- var canvas = document.getElementById(canvasId);
- var chart = new Chart([canvas]);
- expect(chart).toBeValidChart();
- expect(chart.canvas).toBe(canvas);
- expect(chart.ctx).toBe(canvas.getContext('2d'));
- chart.destroy();
- });
- it('should accept a canvas from an iframe', function(done) {
- var iframe = document.createElement('iframe');
- iframe.onload = function() {
- var doc = iframe.contentDocument;
- doc.body.innerHTML += '<canvas id="chart"></canvas>';
- var canvas = doc.getElementById('chart');
- var chart = new Chart(canvas);
- expect(chart).toBeValidChart();
- expect(chart.canvas).toBe(canvas);
- expect(chart.ctx).toBe(canvas.getContext('2d'));
- chart.destroy();
- canvas.remove();
- iframe.remove();
- done();
- };
- document.body.appendChild(iframe);
- });
- });
- describe('config.options.aspectRatio', function() {
- it('should use default "global" aspect ratio for render and display sizes', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 620px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 620, dh: 310,
- rw: 620, rh: 310,
- });
- });
- it('should use default "chart" aspect ratio for render and display sizes', function() {
- var ratio = Chart.defaults.doughnut.aspectRatio;
- Chart.defaults.doughnut.aspectRatio = 1;
- var chart = acquireChart({
- type: 'doughnut',
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 425px'
- }
- });
- Chart.defaults.doughnut.aspectRatio = ratio;
- expect(chart).toBeChartOfSize({
- dw: 425, dh: 425,
- rw: 425, rh: 425,
- });
- });
- it('should use "user" aspect ratio for render and display sizes', function() {
- var chart = acquireChart({
- options: {
- responsive: false,
- aspectRatio: 3
- }
- }, {
- canvas: {
- style: 'width: 405px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 405, dh: 135,
- rw: 405, rh: 135,
- });
- });
- it('should not apply aspect ratio when height specified', function() {
- var chart = acquireChart({
- options: {
- responsive: false,
- aspectRatio: 3
- }
- }, {
- canvas: {
- style: 'width: 400px; height: 410px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 400, dh: 410,
- rw: 400, rh: 410,
- });
- });
- });
- describe('config.options.responsive: false', function() {
- it('should use default canvas size for render and display sizes', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: ''
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 150,
- rw: 300, rh: 150,
- });
- });
- it('should use canvas attributes for render and display sizes', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: '',
- width: 305,
- height: 245,
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 305, dh: 245,
- rw: 305, rh: 245,
- });
- });
- it('should use canvas style for render and display sizes (if no attributes)', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 345px; height: 125px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 345, dh: 125,
- rw: 345, rh: 125,
- });
- });
- it('should use attributes for the render size and style for the display size', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 345px; height: 125px;',
- width: 165,
- height: 85,
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 345, dh: 125,
- rw: 165, rh: 85,
- });
- });
- // https://github.com/chartjs/Chart.js/issues/3860
- it('should support decimal display width and/or height', function() {
- var chart = acquireChart({
- options: {
- responsive: false
- }
- }, {
- canvas: {
- style: 'width: 345.42px; height: 125.42px;'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 345, dh: 125,
- rw: 345, rh: 125,
- });
- });
- });
- describe('config.options.responsive: true (maintainAspectRatio: true)', function() {
- it('should fill parent width and use aspect ratio to calculate height', function() {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: true
- }
- }, {
- canvas: {
- style: 'width: 150px; height: 245px'
- },
- wrapper: {
- style: 'width: 300px; height: 350px'
- }
- });
- expect(chart).toBeChartOfSize({
- dw: 300, dh: 490,
- rw: 300, rh: 490,
- });
- });
- });
- describe('controller.destroy', function() {
- it('should reset context to default values', function() {
- var chart = acquireChart({});
- var context = chart.ctx;
- chart.destroy();
- // https://www.w3.org/TR/2dcontext/#conformance-requirements
- Chart.helpers.each({
- fillStyle: '#000000',
- font: '10px sans-serif',
- lineJoin: 'miter',
- lineCap: 'butt',
- lineWidth: 1,
- miterLimit: 10,
- shadowBlur: 0,
- shadowColor: 'rgba(0, 0, 0, 0)',
- shadowOffsetX: 0,
- shadowOffsetY: 0,
- strokeStyle: '#000000',
- textAlign: 'start',
- textBaseline: 'alphabetic'
- }, function(value, key) {
- expect(context[key]).toBe(value);
- });
- });
- it('should restore canvas initial values', function(done) {
- var chart = acquireChart({
- options: {
- responsive: true,
- maintainAspectRatio: false
- }
- }, {
- canvas: {
- width: 180,
- style: 'width: 512px; height: 480px'
- },
- wrapper: {
- style: 'width: 450px; height: 450px; position: relative'
- }
- });
- var canvas = chart.canvas;
- var wrapper = canvas.parentNode;
- wrapper.style.width = '475px';
- waitForResize(chart, function() {
- expect(chart).toBeChartOfSize({
- dw: 475, dh: 450,
- rw: 475, rh: 450,
- });
- chart.destroy();
- expect(canvas.getAttribute('width')).toBe('180');
- expect(canvas.getAttribute('height')).toBe(null);
- expect(canvas.style.width).toBe('512px');
- expect(canvas.style.height).toBe('480px');
- expect(canvas.style.display).toBe('');
- done();
- });
- });
- });
- describe('event handling', function() {
- it('should notify plugins about events', function() {
- var notifiedEvent;
- var plugin = {
- afterEvent: function(chart, e) {
- notifiedEvent = e;
- }
- };
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- },
- options: {
- responsive: true
- },
- plugins: [plugin]
- });
- var node = chart.canvas;
- var rect = node.getBoundingClientRect();
- var clientX = (rect.left + rect.right) / 2;
- var clientY = (rect.top + rect.bottom) / 2;
- var evt = new MouseEvent('click', {
- view: window,
- bubbles: true,
- cancelable: true,
- clientX: clientX,
- clientY: clientY
- });
- // Manually trigger rather than having an async test
- node.dispatchEvent(evt);
- // Check that notifiedEvent is correct
- expect(notifiedEvent).not.toBe(undefined);
- expect(notifiedEvent.native).toBe(evt);
- // Is type correctly translated
- expect(notifiedEvent.type).toBe(evt.type);
- // Relative Position
- expect(notifiedEvent.x).toBeCloseToPixel(chart.width / 2);
- expect(notifiedEvent.y).toBeCloseToPixel(chart.height / 2);
- });
- });
- });
|