From 886956441e33699f60aac66a89a8b4e421059523 Mon Sep 17 00:00:00 2001 From: Simon Brunel Date: Mon, 25 Apr 2016 19:58:12 +0200 Subject: [PATCH] New test helpers to acquire and release charts Since we changed the way how meta data are stores, now unit tests need to work on real Chart instances. This commit brings some helpers to inject/cleanup HTML canvas and it's wrapper into/from the DOM. --- test/mockContext.js | 75 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/test/mockContext.js b/test/mockContext.js index 38755362c..17d9ad5d8 100644 --- a/test/mockContext.js +++ b/test/mockContext.js @@ -57,7 +57,7 @@ }, }); }; - + Context.prototype._initMethods = function() { // define methods to test here // no way to introspect so we have to do some extra work :( @@ -121,4 +121,75 @@ window.createMockContext = function() { return new Context(); }; -})(); \ No newline at end of file + + // Canvas injection helpers + var charts = {}; + + function acquireChart(config, style) { + var wrapper = document.createElement("div"); + var canvas = document.createElement("canvas"); + wrapper.className = 'chartjs-wrapper'; + + style = style || { height: '512px', width: '512px' }; + for (var k in style) { + wrapper.style[k] = style[k]; + canvas.style[k] = style[k]; + } + + canvas.height = canvas.style.height && parseInt(canvas.style.height); + canvas.width = canvas.style.width && parseInt(canvas.style.width); + + // by default, remove chart animation and auto resize + var options = config.options = config.options || {}; + options.animation = options.animation === undefined? false : options.animation; + options.responsive = options.responsive === undefined? false : options.responsive; + options.defaultFontFamily = options.defaultFontFamily || 'Arial'; + + wrapper.appendChild(canvas); + window.document.body.appendChild(wrapper); + var chart = new Chart(canvas.getContext("2d"), config); + charts[chart.id] = chart; + return chart; + } + + function releaseChart(chart) { + chart.chart.canvas.parentNode.remove(); + delete charts[chart.id]; + delete chart; + } + + function releaseAllCharts(scope) { + for (var id in charts) { + var chart = charts[id]; + releaseChart(chart); + } + } + + function injectCSS(css) { + // http://stackoverflow.com/q/3922139 + var head = document.getElementsByTagName('head')[0]; + var style = document.createElement('style'); + style.setAttribute('type', 'text/css'); + if (style.styleSheet) { // IE + style.styleSheet.cssText = css; + } else { + style.appendChild(document.createTextNode(css)); + } + head.appendChild(style); + } + + window.acquireChart = acquireChart; + window.releaseChart = releaseChart; + window.releaseAllCharts = releaseAllCharts; + + // some style initialization to limit differences between browsers across different plateforms. + injectCSS( + '.chartjs-wrapper, .chartjs-wrapper canvas {' + + 'border: 0;' + + 'margin: 0;' + + 'padding: 0;' + + '}' + + '.chartjs-wrapper {' + + 'position: absolute' + + '}'); +})(); -- 2.47.3