]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
New test helpers to acquire and release charts
authorSimon Brunel <simonbrunel@users.noreply.github.com>
Mon, 25 Apr 2016 17:58:12 +0000 (19:58 +0200)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Tue, 26 Apr 2016 10:46:31 +0000 (12:46 +0200)
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

index 38755362cfe0f48ad5f6beccfed3f198744c1f83..17d9ad5d8fbe91c4c6145e73d1e30a72741fc9a7 100644 (file)
@@ -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 :(
        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' +
+               '}');
+})();