Skip to content

Commit

Permalink
Throw error when non-existing scale id is used (chartjs#518)
Browse files Browse the repository at this point in the history
* Throw error when non-existing scale id is used

* Improve test
  • Loading branch information
kurkle committed Dec 2, 2021
1 parent 46ee11b commit f2e2481
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
5 changes: 4 additions & 1 deletion karma.conf.js
Expand Up @@ -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: {
Expand Down
11 changes: 11 additions & 0 deletions src/annotation.js
Expand Up @@ -65,6 +65,7 @@ export default {
} else if (isArray(annotationOptions)) {
annotations.push(...annotationOptions);
}
verifyScaleOptions(annotations, chart.scales);
},

afterDataLimits(chart, args) {
Expand Down Expand Up @@ -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(', ')}`);
}
}
}
}
39 changes: 0 additions & 39 deletions test/fixtures/box/missingScale.js

This file was deleted.

Binary file removed test/fixtures/box/missingScale.png
Binary file not shown.
29 changes: 29 additions & 0 deletions 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`);
});
});
});

0 comments on commit f2e2481

Please sign in to comment.