Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests cleanup #3287

Merged
merged 3 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions test/controller.bar.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the bar controller
describe('Bar controller tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should be constructed', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down
9 changes: 0 additions & 9 deletions test/controller.bubble.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the bubble controller
describe('Bubble controller tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should be constructed', function() {
var chart = window.acquireChart({
type: 'bubble',
Expand Down
9 changes: 0 additions & 9 deletions test/controller.doughnut.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the bar controller
describe('Doughnut controller tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should be constructed', function() {
var chart = window.acquireChart({
type: 'doughnut',
Expand Down
9 changes: 0 additions & 9 deletions test/controller.line.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the line controller
describe('Line controller tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should be constructed', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down
9 changes: 0 additions & 9 deletions test/controller.polarArea.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the polar area controller
describe('Polar area controller tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should be constructed', function() {
var chart = window.acquireChart({
type: 'polarArea',
Expand Down
18 changes: 5 additions & 13 deletions test/controller.radar.tests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// Test the polar area controller
describe('Radar controller tests', function() {
beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('Should be constructed', function() {
var chart = window.acquireChart({
type: 'radar',
Expand Down Expand Up @@ -137,7 +129,7 @@ describe('Radar controller tests', function() {
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},
Expand All @@ -163,8 +155,8 @@ describe('Radar controller tests', function() {

// 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 },
Expand Down Expand Up @@ -225,7 +217,7 @@ describe('Radar controller tests', function() {
}));

// 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 },
Expand All @@ -245,7 +237,7 @@ describe('Radar controller tests', function() {
}));
});


// Use custom styles for lines & first point
meta.dataset.custom = {
tension: 0.25,
Expand Down
26 changes: 12 additions & 14 deletions test/core.layoutService.tests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// Tests of the scale service
describe('Test the layout service', function() {
beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('should fit a simple chart with 2 scales', function() {
var chart = window.acquireChart({
type: 'bar',
Expand All @@ -30,8 +22,10 @@ describe('Test the layout service', function() {
}
}
}, {
height: '150px',
width: '250px'
canvas: {
height: 150,
width: 250
}
});

expect(chart.chartArea.bottom).toBeCloseToPixel(112);
Expand Down Expand Up @@ -78,8 +72,10 @@ describe('Test the layout service', function() {
}
}
}, {
height: '150px',
width: '250px'
canvas: {
height: 150,
width: 250
}
});

expect(chart.chartArea.bottom).toBeCloseToPixel(150);
Expand Down Expand Up @@ -157,8 +153,10 @@ describe('Test the layout service', function() {
}
}
}, {
height: '150px',
width: '250px'
canvas: {
height: 150,
width: 250
}
});

expect(chart.chartArea.bottom).toBeCloseToPixel(102);
Expand Down
9 changes: 0 additions & 9 deletions test/core.legend.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the rectangle element
describe('Legend block tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('Should be constructed', function() {
var legend = new Chart.Legend({});
expect(legend).not.toBe(undefined);
Expand Down
9 changes: 0 additions & 9 deletions test/core.tooltip.tests.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// Test the rectangle element
describe('tooltip tests', function() {

beforeEach(function() {
window.addDefaultMatchers(jasmine);
});

afterEach(function() {
window.releaseAllCharts();
});

it('Should display in label mode', function() {
var chartInstance = window.acquireChart({
type: 'line',
Expand Down
58 changes: 39 additions & 19 deletions test/mockContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,39 +158,57 @@
};
}

window.addDefaultMatchers = function(jasmine) {
beforeEach(function() {
jasmine.addMatchers({
toBeCloseToPixel: toBeCloseToPixel,
toEqualOneOf: toEqualOneOf
});
}
});

// 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;

options = options || {};
options.canvas = options.canvas || { height: 512, width: 512 };
options.wrapper = options.wrapper || { class: 'chartjs-wrapper' };

style = style || { height: '512px', width: '512px' };
for (var k in style) {
wrapper.style[k] = style[k];
canvas.style[k] = style[k];
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;
}
Expand All @@ -201,12 +219,15 @@
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
Expand All @@ -223,7 +244,6 @@

window.acquireChart = acquireChart;
window.releaseChart = releaseChart;
window.releaseAllCharts = releaseAllCharts;

// some style initialization to limit differences between browsers across different plateforms.
injectCSS(
Expand Down
Loading