For convenience, charts are now automatically released after each spec if they are not acquired using `persistent: true`. Also remove the confusing and error prone `chartInstance` global variable and make sure that chart instances are local to each spec.
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should be constructed', function() {
var chart = window.acquireChart({
type: 'bar',
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should be constructed', function() {
var chart = window.acquireChart({
type: 'bubble',
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should be constructed', function() {
var chart = window.acquireChart({
type: 'doughnut',
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should be constructed', function() {
var chart = window.acquireChart({
type: 'line',
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should be constructed', function() {
var chart = window.acquireChart({
type: 'polarArea',
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('Should be constructed', function() {
var chart = window.acquireChart({
type: 'radar',
tension: 0.1,
}));
- [
+ [
{ x: 256, y: 272, cppx: 256, cppy: 272, cpnx: 256, cpny: 272},
{ x: 256, y: 272, cppx: 256, cppy: 272, cpnx: 256, cpny: 272},
{ x: 256, y: 272, cppx: 256, cppy: 272, cpnx: 256, cpny: 272},
// Now update controller and ensure proper updates
meta.controller.update();
-
- [
+
+ [
{ x: 256, y: 133, cppx: 246, cppy: 133, cpnx: 272, cpny: 133 },
{ x: 464, y: 272, cppx: 464, cppy: 264, cpnx: 464, cpny: 278 },
{ x: 256, y: 272, cppx: 276.9, cppy: 272, cpnx: 250.4, cpny: 272 },
}));
// Since tension is now 0, we don't care about the control points
- [
+ [
{ x: 256, y: 133 },
{ x: 464, y: 272 },
{ x: 256, y: 272 },
}));
});
-
+
// Use custom styles for lines & first point
meta.dataset.custom = {
tension: 0.25,
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should fit a simple chart with 2 scales', function() {
var chart = window.acquireChart({
type: 'bar',
}
}
}, {
- height: '150px',
- width: '250px'
+ canvas: {
+ height: 150,
+ width: 250
+ }
});
expect(chart.chartArea.bottom).toBeCloseToPixel(112);
}
}
}, {
- height: '150px',
- width: '250px'
+ canvas: {
+ height: 150,
+ width: 250
+ }
});
expect(chart.chartArea.bottom).toBeCloseToPixel(150);
}
}
}, {
- height: '150px',
- width: '250px'
+ canvas: {
+ height: 150,
+ width: 250
+ }
});
expect(chart.chartArea.bottom).toBeCloseToPixel(102);
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('Should be constructed', function() {
var legend = new Chart.Legend({});
expect(legend).not.toBe(undefined);
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('Should display in label mode', function() {
var chartInstance = window.acquireChart({
type: 'line',
// Canvas injection helpers
var charts = {};
- function acquireChart(config, style) {
+ /**
+ * Injects a new canvas (and div wrapper) and creates teh associated Chart instance
+ * using the given config. Additional options allow tweaking elements generation.
+ * @param {object} config - Chart config.
+ * @param {object} options - Chart acquisition options.
+ * @param {object} options.canvas - Canvas attributes.
+ * @param {object} options.wrapper - Canvas wrapper attributes.
+ * @param {boolean} options.persistent - If true, the chart will not be released after the spec.
+ */
+ function acquireChart(config, options) {
var wrapper = document.createElement("div");
var canvas = document.createElement("canvas");
- wrapper.className = 'chartjs-wrapper';
+ var chart, key;
- style = style || { height: '512px', width: '512px' };
- for (var k in style) {
- wrapper.style[k] = style[k];
- canvas.style[k] = style[k];
+ options = options || {};
+ options.canvas = options.canvas || { height: 512, width: 512 };
+ options.wrapper = options.wrapper || { class: 'chartjs-wrapper' };
+
+ for (key in options.canvas) {
+ if (options.canvas.hasOwnProperty(key)) {
+ canvas.setAttribute(key, options.canvas[key]);
+ }
}
- canvas.height = canvas.style.height && parseInt(canvas.style.height);
- canvas.width = canvas.style.width && parseInt(canvas.style.width);
+ for (key in options.wrapper) {
+ if (options.wrapper.hasOwnProperty(key)) {
+ wrapper.setAttribute(key, options.wrapper[key]);
+ }
+ }
// 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';
+ config.options = config.options || {};
+ config.options.animation = config.options.animation === undefined? false : config.options.animation;
+ config.options.responsive = config.options.responsive === undefined? false : config.options.responsive;
+ config.options.defaultFontFamily = config.options.defaultFontFamily || 'Arial';
wrapper.appendChild(canvas);
window.document.body.appendChild(wrapper);
- var chart = new Chart(canvas.getContext("2d"), config);
+
+ chart = new Chart(canvas.getContext("2d"), config);
+ chart.__test_persistent = options.persistent;
charts[chart.id] = chart;
return chart;
}
delete chart;
}
- function releaseAllCharts(scope) {
+ afterEach(function() {
+ // Auto releasing acquired charts
for (var id in charts) {
var chart = charts[id];
- releaseChart(chart);
+ if (!chart.__test_persistent) {
+ releaseChart(chart);
+ }
}
- }
+ });
function injectCSS(css) {
// http://stackoverflow.com/q/3922139
window.acquireChart = acquireChart;
window.releaseChart = releaseChart;
- window.releaseAllCharts = releaseAllCharts;
// some style initialization to limit differences between browsers across different plateforms.
injectCSS(
describe('Linear Scale', function() {
- var chartInstance;
-
beforeEach(function() {
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- if (chartInstance)
- {
- releaseChart(chartInstance);
- }
- });
-
it('Should register the constructor with the scale service', function() {
var Constructor = Chart.scaleService.getScaleConstructor('linear');
expect(Constructor).not.toBe(undefined);
});
it('Should correctly determine the max & min data values', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(150);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-100);
+ expect(chart.scales.yScale0.max).toBe(150);
});
it('Should correctly determine the max & min of string data values', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(150);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-100);
+ expect(chart.scales.yScale0.max).toBe(150);
});
it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(80);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-100);
+ expect(chart.scales.yScale0.max).toBe(80);
});
it('Should correctly determine the max & min data values ignoring data that is NaN', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0.min).toBe(30);
- expect(chartInstance.scales.yScale0.max).toBe(90);
+ expect(chart.scales.yScale0.min).toBe(30);
+ expect(chart.scales.yScale0.max).toBe(90);
// Scale is now stacked
- chartInstance.scales.yScale0.options.stacked = true;
- chartInstance.update();
+ chart.scales.yScale0.options.stacked = true;
+ chart.update();
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(90);
+ expect(chart.scales.yScale0.min).toBe(0);
+ expect(chart.scales.yScale0.max).toBe(90);
});
it('Should correctly determine the max & min for scatter data', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
}
});
- chartInstance.update();
+ chart.update();
- expect(chartInstance.scales.xScale0.min).toBe(-20);
- expect(chartInstance.scales.xScale0.max).toBe(100);
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(100);
+ expect(chart.scales.xScale0.min).toBe(-20);
+ expect(chart.scales.xScale0.max).toBe(100);
+ expect(chart.scales.yScale0.min).toBe(0);
+ expect(chart.scales.yScale0.max).toBe(100);
});
it('Should correctly get the label for the given index', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
}
});
- chartInstance.update();
+ chart.update();
- expect(chartInstance.scales.yScale0.getLabelForIndex(3, 0)).toBe(7);
+ expect(chart.scales.yScale0.getLabelForIndex(3, 0)).toBe(7);
});
it('Should correctly determine the min and max data values when stacked mode is turned on', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
}
});
- chartInstance.update();
+ chart.update();
- expect(chartInstance.scales.yScale0.min).toBe(-150);
- expect(chartInstance.scales.yScale0.max).toBe(200);
+ expect(chart.scales.yScale0.min).toBe(-150);
+ expect(chart.scales.yScale0.max).toBe(200);
});
it('Should correctly determine the min and max data values when stacked mode is turned on and there are hidden datasets', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
}
});
- chartInstance.update();
+ chart.update();
- expect(chartInstance.scales.yScale0.min).toBe(-150);
- expect(chartInstance.scales.yScale0.max).toBe(200);
+ expect(chart.scales.yScale0.min).toBe(-150);
+ expect(chart.scales.yScale0.max).toBe(200);
});
it('Should correctly determine the min and max data values when stacked mode is turned on there are multiple types of datasets', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- chartInstance.scales.yScale0.determineDataLimits();
- expect(chartInstance.scales.yScale0.min).toBe(-105);
- expect(chartInstance.scales.yScale0.max).toBe(160);
+ chart.scales.yScale0.determineDataLimits();
+ expect(chart.scales.yScale0.min).toBe(-105);
+ expect(chart.scales.yScale0.max).toBe(160);
});
it('Should ensure that the scale has a max and min that are not equal', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [],
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-1);
- expect(chartInstance.scales.yScale0.max).toBe(1);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-1);
+ expect(chart.scales.yScale0.max).toBe(1);
});
it('Should ensure that the scale has a max and min that are not equal when beginAtZero is set', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [],
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(1);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(0);
+ expect(chart.scales.yScale0.max).toBe(1);
});
it('Should use the suggestedMin and suggestedMax options', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-10);
- expect(chartInstance.scales.yScale0.max).toBe(10);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-10);
+ expect(chart.scales.yScale0.max).toBe(10);
});
it('Should use the min and max options', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-1010);
- expect(chartInstance.scales.yScale0.max).toBe(1010);
- expect(chartInstance.scales.yScale0.ticks[0]).toBe('1010');
- expect(chartInstance.scales.yScale0.ticks[chartInstance.scales.yScale0.ticks.length - 1]).toBe('-1010');
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.min).toBe(-1010);
+ expect(chart.scales.yScale0.max).toBe(1010);
+ expect(chart.scales.yScale0.ticks[0]).toBe('1010');
+ expect(chart.scales.yScale0.ticks[chart.scales.yScale0.ticks.length - 1]).toBe('-1010');
});
it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20']);
+ expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+ expect(chart.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20']);
- chartInstance.scales.yScale0.options.ticks.beginAtZero = true;
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20', '15', '10', '5', '0']);
+ chart.scales.yScale0.options.ticks.beginAtZero = true;
+ chart.update();
+ expect(chart.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20', '15', '10', '5', '0']);
- chartInstance.data.datasets[0].data = [-20, -30, -40, -50];
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '-5', '-10', '-15', '-20', '-25', '-30', '-35', '-40', '-45', '-50']);
+ chart.data.datasets[0].data = [-20, -30, -40, -50];
+ chart.update();
+ expect(chart.scales.yScale0.ticks).toEqual(['0', '-5', '-10', '-15', '-20', '-25', '-30', '-35', '-40', '-45', '-50']);
- chartInstance.scales.yScale0.options.ticks.beginAtZero = false;
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['-20', '-25', '-30', '-35', '-40', '-45', '-50']);
+ chart.scales.yScale0.options.ticks.beginAtZero = false;
+ chart.update();
+ expect(chart.scales.yScale0.ticks).toEqual(['-20', '-25', '-30', '-35', '-40', '-45', '-50']);
});
it('Should generate tick marks in the correct order in reversed mode', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
});
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '10', '20', '30', '40', '50', '60', '70', '80']);
- expect(chartInstance.scales.yScale0.start).toBe(80);
- expect(chartInstance.scales.yScale0.end).toBe(0);
+ expect(chart.scales.yScale0.ticks).toEqual(['0', '10', '20', '30', '40', '50', '60', '70', '80']);
+ expect(chart.scales.yScale0.start).toBe(80);
+ expect(chart.scales.yScale0.end).toBe(0);
});
it('should use the correct number of decimal places in the default format function', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
}
}
});
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0.06', '0.05', '0.04', '0.03', '0.02', '0.01', '0']);
+ expect(chart.scales.yScale0.ticks).toEqual(['0.06', '0.05', '0.04', '0.03', '0.02', '0.01', '0']);
});
it('Should build labels using the user supplied callback', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
});
// Just the index
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
+ expect(chart.scales.yScale0.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
});
it('Should get the correct pixel value for a point', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(501); // right - paddingRight
expect(xScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(41); // left + paddingLeft
expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(271); // halfway*/
expect(xScale.getValueForPixel(41)).toBeCloseTo(-1, 1e-2);
expect(xScale.getValueForPixel(271)).toBeCloseTo(0, 1e-2);
- var yScale = chartInstance.scales.yScale0;
+ var yScale = chart.scales.yScale0;
expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(32); // right - paddingRight
expect(yScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(484); // left + paddingLeft
expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(258); // halfway*/
});
it('should fit correctly', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.paddingTop).toBeCloseToPixel(0);
expect(xScale.paddingBottom).toBeCloseToPixel(0);
expect(xScale.paddingLeft).toBeCloseToPixel(0);
expect(xScale.width).toBeCloseToPixel(471);
expect(xScale.height).toBeCloseToPixel(28);
- var yScale = chartInstance.scales.yScale0;
+ var yScale = chart.scales.yScale0;
expect(yScale.paddingTop).toBeCloseToPixel(0);
expect(yScale.paddingBottom).toBeCloseToPixel(0);
expect(yScale.paddingLeft).toBeCloseToPixel(0);
// Extra size when scale label showing
xScale.options.scaleLabel.display = true;
yScale.options.scaleLabel.display = true;
- chartInstance.update();
+ chart.update();
expect(xScale.paddingTop).toBeCloseToPixel(0);
expect(xScale.paddingBottom).toBeCloseToPixel(0);
});
it('should fit correctly when display is turned off', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
});
- var yScale = chartInstance.scales.yScale0;
+ var yScale = chart.scales.yScale0;
expect(yScale.width).toBeCloseToPixel(0);
});
});
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- window.releaseAllCharts();
- });
-
it('should register the constructor with the scale service', function() {
var Constructor = Chart.scaleService.getScaleConstructor('logarithmic');
expect(Constructor).not.toBe(undefined);
// Tests for the radial linear scale used by the polar area and radar charts
describe('Test the radial linear scale', function() {
- var chartInstance;
-
beforeEach(function() {
window.addDefaultMatchers(jasmine);
});
- afterEach(function() {
- if (chartInstance) {
- releaseChart(chartInstance);
- }
- });
-
it('Should register the constructor with the scale service', function() {
var Constructor = Chart.scaleService.getScaleConstructor('radialLinear');
expect(Constructor).not.toBe(undefined);
});
it('Should correctly determine the max & min data values', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
},
options: {
- scales: {
-
- }
+ scales: {}
}
});
- expect(chartInstance.scale.min).toBe(-100);
- expect(chartInstance.scale.max).toBe(150);
+ expect(chart.scale.min).toBe(-100);
+ expect(chart.scale.max).toBe(150);
});
it('Should correctly determine the max & min of string data values', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
},
options: {
- scales: {
-
- }
+ scales: {}
}
});
- expect(chartInstance.scale.min).toBe(-100);
- expect(chartInstance.scale.max).toBe(150);
+ expect(chart.scale.min).toBe(-100);
+ expect(chart.scale.max).toBe(150);
});
it('Should correctly determine the max & min data values when there are hidden datasets', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
},
options: {
- scales: {
-
- }
+ scales: {}
}
});
- expect(chartInstance.scale.min).toBe(-100);
- expect(chartInstance.scale.max).toBe(150);
+ expect(chart.scale.min).toBe(-100);
+ expect(chart.scale.max).toBe(150);
});
it('Should correctly determine the max & min data values when there is NaN data', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
},
options: {
- scales: {
-
- }
+ scales: {}
}
});
- expect(chartInstance.scale.min).toBe(50);
- expect(chartInstance.scale.max).toBe(70);
+ expect(chart.scale.min).toBe(50);
+ expect(chart.scale.max).toBe(70);
});
it('Should ensure that the scale has a max and min that are not equal', function() {
});
it('Should use the suggestedMin and suggestedMax options', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.min).toBe(-10);
- expect(chartInstance.scale.max).toBe(10);
+ expect(chart.scale.min).toBe(-10);
+ expect(chart.scale.max).toBe(10);
});
it('Should use the min and max options', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.min).toBe(-1010);
- expect(chartInstance.scale.max).toBe(1010);
- expect(chartInstance.scale.ticks).toEqual(['-1010', '-1000', '-500', '0', '500', '1000', '1010']);
+ expect(chart.scale.min).toBe(-1010);
+ expect(chart.scale.max).toBe(1010);
+ expect(chart.scale.ticks).toEqual(['-1010', '-1000', '-500', '0', '500', '1000', '1010']);
});
it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.ticks).toEqual(['20', '25', '30', '35', '40', '45', '50']);
+ expect(chart.scale.ticks).toEqual(['20', '25', '30', '35', '40', '45', '50']);
- chartInstance.scale.options.ticks.beginAtZero = true;
- chartInstance.update();
+ chart.scale.options.ticks.beginAtZero = true;
+ chart.update();
- expect(chartInstance.scale.ticks).toEqual(['0', '5', '10', '15', '20', '25', '30', '35', '40', '45', '50']);
+ expect(chart.scale.ticks).toEqual(['0', '5', '10', '15', '20', '25', '30', '35', '40', '45', '50']);
- chartInstance.data.datasets[0].data = [-20, -30, -40, -50];
- chartInstance.update();
+ chart.data.datasets[0].data = [-20, -30, -40, -50];
+ chart.update();
- expect(chartInstance.scale.ticks).toEqual(['-50', '-45', '-40', '-35', '-30', '-25', '-20', '-15', '-10', '-5', '0']);
+ expect(chart.scale.ticks).toEqual(['-50', '-45', '-40', '-35', '-30', '-25', '-20', '-15', '-10', '-5', '0']);
- chartInstance.scale.options.ticks.beginAtZero = false;
- chartInstance.update();
+ chart.scale.options.ticks.beginAtZero = false;
+ chart.update();
- expect(chartInstance.scale.ticks).toEqual(['-50', '-45', '-40', '-35', '-30', '-25', '-20']);
+ expect(chart.scale.ticks).toEqual(['-50', '-45', '-40', '-35', '-30', '-25', '-20']);
});
it('Should generate tick marks in the correct order in reversed mode', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.ticks).toEqual(['80', '70', '60', '50', '40', '30', '20', '10', '0']);
- expect(chartInstance.scale.start).toBe(80);
- expect(chartInstance.scale.end).toBe(0);
+ expect(chart.scale.ticks).toEqual(['80', '70', '60', '50', '40', '30', '20', '10', '0']);
+ expect(chart.scale.start).toBe(80);
+ expect(chart.scale.end).toBe(0);
});
it('Should build labels using the user supplied callback', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
- expect(chartInstance.scale.pointLabels).toEqual(['label1', 'label2', 'label3', 'label4', 'label5']);
+ expect(chart.scale.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
+ expect(chart.scale.pointLabels).toEqual(['label1', 'label2', 'label3', 'label4', 'label5']);
});
it('Should build point labels using the user supplied callback', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.pointLabels).toEqual(['0', '1', '2', '3', '4']);
+ expect(chart.scale.pointLabels).toEqual(['0', '1', '2', '3', '4']);
});
it('should correctly set the center point', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.drawingArea).toBe(225);
- expect(chartInstance.scale.xCenter).toBe(256);
- expect(chartInstance.scale.yCenter).toBe(272);
+ expect(chart.scale.drawingArea).toBe(225);
+ expect(chart.scale.xCenter).toBe(256);
+ expect(chart.scale.yCenter).toBe(272);
});
it('should correctly get the label for a given data index', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
}
});
- expect(chartInstance.scale.getLabelForIndex(1, 0)).toBe(5);
+ expect(chart.scale.getLabelForIndex(1, 0)).toBe(5);
});
it('should get the correct distance from the center point', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
}
});
- expect(chartInstance.scale.getDistanceFromCenterForValue(chartInstance.scale.min)).toBe(0);
- expect(chartInstance.scale.getDistanceFromCenterForValue(chartInstance.scale.max)).toBe(225);
- expect(chartInstance.scale.getPointPositionForValue(1, 5)).toEqual({
+ expect(chart.scale.getDistanceFromCenterForValue(chart.scale.min)).toBe(0);
+ expect(chart.scale.getDistanceFromCenterForValue(chart.scale.max)).toBe(225);
+ expect(chart.scale.getPointPositionForValue(1, 5)).toEqual({
x: 269,
y: 268,
});
- chartInstance.scale.options.reverse = true;
- chartInstance.update();
+ chart.scale.options.reverse = true;
+ chart.update();
- expect(chartInstance.scale.getDistanceFromCenterForValue(chartInstance.scale.min)).toBe(225);
- expect(chartInstance.scale.getDistanceFromCenterForValue(chartInstance.scale.max)).toBe(0);
+ expect(chart.scale.getDistanceFromCenterForValue(chart.scale.min)).toBe(225);
+ expect(chart.scale.getDistanceFromCenterForValue(chart.scale.max)).toBe(0);
});
it('should correctly get angles for all points', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'radar',
data: {
datasets: [{
var slice = 72; // (360 / 5)
for(var i = 0; i < 5; i++) {
- expect(radToNearestDegree(chartInstance.scale.getIndexAngle(i))).toBe(15 + (slice * i) - 90);
+ expect(radToNearestDegree(chart.scale.getIndexAngle(i))).toBe(15 + (slice * i) - 90);
}
- chartInstance.options.startAngle = 0;
- chartInstance.update();
+ chart.options.startAngle = 0;
+ chart.update();
for(var i = 0; i < 5; i++) {
- expect(radToNearestDegree(chartInstance.scale.getIndexAngle(i))).toBe((slice * i) - 90);
+ expect(radToNearestDegree(chart.scale.getIndexAngle(i))).toBe((slice * i) - 90);
}
});
});
// Time scale tests
describe('Time scale tests', function() {
- var chartInstance;
-
beforeEach(function() {
window.addDefaultMatchers(jasmine);
});
});
- afterEach(function() {
- if (chartInstance)
- {
- releaseChart(chartInstance);
- }
- });
-
it('Should load moment.js as a dependency', function() {
expect(window.moment).not.toBe(undefined);
});
return moment('01/01/2015 12:00', 'DD/MM/YYYY HH:mm').add(days, 'd').toDate();
}
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
});
// Counts down because the lines are drawn top to bottom
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 3, 2015', 'Jan 5, 2015', 'Jan 7, 2015', 'Jan 9, 2015', 'Jan 11, 2015' ]);
});
it('should allow custom time parsers', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
});
// Counts down because the lines are drawn top to bottom
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
// Counts down because the lines are drawn top to bottom
expect(xScale.ticks[0]).toEqualOneOf(['Nov 19, 1981', 'Nov 20, 1981', 'Nov 21, 1981']); // handle time zone changes
});
it('should get the correct pixel for a value', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.getPixelForValue('', 0, 0)).toBeCloseToPixel(78);
expect(xScale.getPixelForValue('', 6, 0)).toBeCloseToPixel(452);
expect(xScale.getPixelForValue('2015-01-01T20:00:00')).toBeCloseToPixel(78);
expect(xScale.getValueForPixel(78)).toBeCloseToTime({
- value: moment(chartInstance.data.labels[0]),
+ value: moment(chart.data.labels[0]),
unit: 'hour',
threshold: 0.75
});
expect(xScale.getValueForPixel(452)).toBeCloseToTime({
- value: moment(chartInstance.data.labels[6]),
+ value: moment(chart.data.labels[6]),
unit: 'hour'
});
});
it('should get the correct label for a data value', function() {
- chartInstance = window.acquireChart({
+ var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.getLabelForIndex(0, 0)).toBe('2015-01-01T20:00:00');
expect(xScale.getLabelForIndex(6, 0)).toBe('2015-01-10T12:00');
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
expect(xScale.getPixelForValue('', 0, 0)).toBeCloseToPixel(78);
expect(xScale.getValueForPixel(78)).toBeCloseToTime({
- value: moment(chartInstance.data.labels[0]),
+ value: moment(chart.data.labels[0]),
unit: 'day',
threshold: 0.75
});
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
var getOutOfBoundLabelMoment = function() {
xScale.getLabelMoment(12, 0);
expect(getOutOfBoundLabelMoment).not.toThrow();
});
-
+
it("should not throw an error if the datasetIndex or index are null", function() {
var chart = window.acquireChart({
type: 'line',
}
});
- var xScale = chartInstance.scales.xScale0;
+ var xScale = chart.scales.xScale0;
var getNullDatasetIndexLabelMoment = function() {
xScale.getLabelMoment(null, 1);
};
-
+
var getNullIndexLabelMoment = function() {
xScale.getLabelMoment(1, null);
};