diff --git a/karma.conf.js b/karma.conf.js index e601e25ff..bccda96ff 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -44,7 +44,10 @@ module.exports = function(karma) { chrome: { base: 'Chrome', flags: [ - '--disable-accelerated-2d-canvas' + '--disable-accelerated-2d-canvas', + '--disable-background-timer-throttling', + '--disable-backgrounding-occluded-windows', + '--disable-renderer-backgrounding' ] }, firefox: { diff --git a/src/annotation.js b/src/annotation.js index 9938d51b2..12e71a3b5 100644 --- a/src/annotation.js +++ b/src/annotation.js @@ -65,6 +65,7 @@ export default { } else if (isArray(annotationOptions)) { annotations.push(...annotationOptions); } + verifyScaleOptions(annotations, chart.scales); }, afterDataLimits(chart, args) { @@ -278,3 +279,13 @@ function getScaleLimits(scale, annotations) { } return {min, max}; } + +function verifyScaleOptions(annotations, scales) { + for (const annotation of annotations) { + for (const key of ['scaleID', 'xScaleID', 'yScaleID']) { + if (annotation[key] && !scales[annotation[key]]) { + throw new Error(`Non-existing scale '${annotation[key]}' defined as ${key} for annotation '${annotation.id}'. Configured scales: ${Object.keys(scales).join(', ')}`); + } + } + } +} diff --git a/test/fixtures/box/missingScale.js b/test/fixtures/box/missingScale.js deleted file mode 100644 index a57b2d226..000000000 --- a/test/fixtures/box/missingScale.js +++ /dev/null @@ -1,39 +0,0 @@ -module.exports = { - tolerance: 0.0055, - config: { - type: 'bar', - options: { - scales: { - x: { - labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], - }, - y: { - display: false, - min: 0, - max: 25 - } - }, - plugins: { - annotation: { - annotations: { - box: { - type: 'box', - xScaleID: 'x', - yScaleID: 'missing', - xMin: 'February', - xMax: 'May', - yMin: 5, - yMax: 18, - backgroundColor: 'rgba(33, 101, 171, 0.5)', - borderColor: 'rgb(33, 101, 171)', - borderWidth: 0, - }, - } - } - } - } - }, - options: { - spriteText: true - } -}; diff --git a/test/fixtures/box/missingScale.png b/test/fixtures/box/missingScale.png deleted file mode 100644 index 5ca51ecee..000000000 Binary files a/test/fixtures/box/missingScale.png and /dev/null differ diff --git a/test/specs/scales.spec.js b/test/specs/scales.spec.js new file mode 100644 index 000000000..ff6c2ab28 --- /dev/null +++ b/test/specs/scales.spec.js @@ -0,0 +1,29 @@ +describe('scale options', function() { + ['scaleID', 'xScaleID', 'yScaleID'].forEach(function(key) { + it(`should throw an exception when ${key} is not a defined scale`, function() { + function create() { + return acquireChart({ + type: 'line', + data: { + datasets: [{ + data: [1, 2, 3] + }] + }, + options: { + plugins: { + annotation: { + annotations: { + test: { + type: 'line', + [key]: 'missing' + } + } + } + } + } + }); + } + expect(create).toThrowError(`Non-existing scale 'missing' defined as ${key} for annotation 'test'. Configured scales: x, y`); + }); + }); +});