Chart.js/test/jasmine.index.js
Simon Brunel 1ca0ffb5d5 Introduce unit test based on image comparison (#3988)
Attempt to make easier the creation of unit tests that check the drawing output. Until now, this was done by checking calls on a 'fake' context, which is hard to maintain (need to update pixel values by hands) and also not reliable when optimizing code (i.e. different calls sequence but same result).

As of now, it's possible to define 'auto' tests based on JSON/PNG fixtures: chart is generated from the JSON file and compared to the associated PNG image. The image diff is done using `pixelmatch`. As an example (and in preparation of the `filler` plugin), add auto tests for the line element `fill` options.
2017-03-05 09:49:12 -07:00

56 lines
1.3 KiB
JavaScript

var Context = require('./jasmine.context');
var matchers = require('./jasmine.matchers');
var utils = require('./jasmine.utils');
(function() {
// Keep track of all acquired charts to automatically release them after each specs
var charts = {};
function acquireChart() {
var chart = utils.acquireChart.apply(utils, arguments);
charts[chart.id] = chart;
return chart;
}
function releaseChart(chart) {
utils.releaseChart.apply(utils, arguments);
delete charts[chart.id];
}
function createMockContext() {
return new Context();
}
window.acquireChart = acquireChart;
window.releaseChart = releaseChart;
window.createMockContext = createMockContext;
// some style initialization to limit differences between browsers across different plateforms.
utils.injectCSS(
'.chartjs-wrapper, .chartjs-wrapper canvas {' +
'border: 0;' +
'margin: 0;' +
'padding: 0;' +
'}' +
'.chartjs-wrapper {' +
'position: absolute' +
'}');
jasmine.specsFromFixtures = utils.specsFromFixtures;
beforeEach(function() {
jasmine.addMatchers(matchers);
});
afterEach(function() {
// Auto releasing acquired charts
Object.keys(charts).forEach(function(id) {
var chart = charts[id];
if (!(chart.$test || {}).persistent) {
releaseChart(chart);
}
});
});
}());