]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add some tests to improve coverage (#7164)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Fri, 28 Feb 2020 00:37:11 +0000 (02:37 +0200)
committerGitHub <noreply@github.com>
Fri, 28 Feb 2020 00:37:11 +0000 (19:37 -0500)
test/matchers.js
test/specs/core.helpers.tests.js
test/specs/helpers.collection.tests.js [new file with mode: 0644]
test/specs/helpers.color.tests.js [new file with mode: 0644]
test/specs/helpers.interpolation.tests.js [new file with mode: 0644]

index 7f1bcf55f511b790ef4ff2952f8889c62992d553..146e80318b7f49eea4b305a1744214ef6cadcd0e 100644 (file)
@@ -69,6 +69,19 @@ function toBeCloseToPixel() {
        };
 }
 
+function toBeCloseToPoint() {
+       function rnd(v) {
+               return Math.round(v * 100) / 100;
+       }
+       return {
+               compare: function(actual, expected) {
+                       return {
+                               pass: rnd(actual.x) === rnd(expected.x) && rnd(actual.y) === rnd(expected.y)
+                       };
+               }
+       };
+}
+
 function toEqualOneOf() {
        return {
                compare: function(actual, expecteds) {
@@ -197,9 +210,10 @@ function toEqualImageData() {
 }
 
 export default {
-       toBeCloseToPixel: toBeCloseToPixel,
-       toEqualOneOf: toEqualOneOf,
-       toBeValidChart: toBeValidChart,
-       toBeChartOfSize: toBeChartOfSize,
-       toEqualImageData: toEqualImageData
+       toBeCloseToPixel,
+       toBeCloseToPoint,
+       toEqualOneOf,
+       toBeValidChart,
+       toBeChartOfSize,
+       toEqualImageData
 };
index b2d963ce782bb90148342612299beaabb62f134a..17849812573f13bdbe5b80afabdcf9a1cf89f0d3 100644 (file)
@@ -13,47 +13,4 @@ describe('Core helper tests', function() {
                expect(helpers.uid()).toBe(uid + 2);
                expect(helpers.uid()).toBe(uid + 3);
        });
-
-       describe('Color helper', function() {
-               function isColorInstance(obj) {
-                       return typeof obj === 'object' && obj.valid;
-               }
-
-               it('should return a color when called with a color', function() {
-                       expect(isColorInstance(helpers.color('rgb(1, 2, 3)'))).toBe(true);
-               });
-       });
-
-       describe('Background hover color helper', function() {
-               it('should return a CanvasPattern when called with a CanvasPattern', function(done) {
-                       var dots = new Image();
-                       dots.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAD1BMVEUAAAD///////////////+PQt5oAAAABXRSTlMAHlFhZsfk/BEAAAAqSURBVHgBY2BgZGJmYmSAAUYWEIDzmcBcJhiXGcxlRpPFrhdmMiqgvX0AcGIBEUAo6UAAAAAASUVORK5CYII=';
-                       dots.onload = function() {
-                               var chartContext = document.createElement('canvas').getContext('2d');
-                               var patternCanvas = document.createElement('canvas');
-                               var patternContext = patternCanvas.getContext('2d');
-                               var pattern = patternContext.createPattern(dots, 'repeat');
-                               patternContext.fillStyle = pattern;
-
-                               var backgroundColor = helpers.getHoverColor(chartContext.createPattern(patternCanvas, 'repeat'));
-
-                               expect(backgroundColor instanceof CanvasPattern).toBe(true);
-
-                               done();
-                       };
-               });
-
-               it('should return a CanvasGradient when called with a CanvasGradient', function() {
-                       var context = document.createElement('canvas').getContext('2d');
-                       var gradient = context.createLinearGradient(0, 1, 2, 3);
-
-                       expect(helpers.getHoverColor(gradient) instanceof CanvasGradient).toBe(true);
-               });
-
-               it('should return a modified version of color when called with a color', function() {
-                       var originalColorRGB = 'rgb(70, 191, 189)';
-
-                       expect(helpers.getHoverColor('#46BFBD')).not.toEqual(originalColorRGB);
-               });
-       });
 });
diff --git a/test/specs/helpers.collection.tests.js b/test/specs/helpers.collection.tests.js
new file mode 100644 (file)
index 0000000..ba08bf7
--- /dev/null
@@ -0,0 +1,30 @@
+import {_lookup, _lookupByKey, _rlookupByKey} from '../../src/helpers/helpers.collection';
+
+describe('helpers.interpolation', function() {
+       it('Should do binary search', function() {
+               const data = [0, 2, 6, 9];
+               expect(_lookup(data, 0)).toEqual({lo: 0, hi: 1});
+               expect(_lookup(data, 1)).toEqual({lo: 0, hi: 1});
+               expect(_lookup(data, 3)).toEqual({lo: 1, hi: 2});
+               expect(_lookup(data, 6)).toEqual({lo: 1, hi: 2});
+               expect(_lookup(data, 9)).toEqual({lo: 2, hi: 3});
+       });
+
+       it('Should do binary search by key', function() {
+               const data = [{x: 0}, {x: 2}, {x: 6}, {x: 9}];
+               expect(_lookupByKey(data, 'x', 0)).toEqual({lo: 0, hi: 1});
+               expect(_lookupByKey(data, 'x', 1)).toEqual({lo: 0, hi: 1});
+               expect(_lookupByKey(data, 'x', 3)).toEqual({lo: 1, hi: 2});
+               expect(_lookupByKey(data, 'x', 6)).toEqual({lo: 1, hi: 2});
+               expect(_lookupByKey(data, 'x', 9)).toEqual({lo: 2, hi: 3});
+       });
+
+       it('Should do reverse binary search by key', function() {
+               const data = [{x: 10}, {x: 7}, {x: 3}, {x: 0}];
+               expect(_rlookupByKey(data, 'x', 0)).toEqual({lo: 2, hi: 3});
+               expect(_rlookupByKey(data, 'x', 3)).toEqual({lo: 2, hi: 3});
+               expect(_rlookupByKey(data, 'x', 5)).toEqual({lo: 1, hi: 2});
+               expect(_rlookupByKey(data, 'x', 8)).toEqual({lo: 0, hi: 1});
+               expect(_rlookupByKey(data, 'x', 10)).toEqual({lo: 0, hi: 1});
+       });
+});
diff --git a/test/specs/helpers.color.tests.js b/test/specs/helpers.color.tests.js
new file mode 100644 (file)
index 0000000..9805e2f
--- /dev/null
@@ -0,0 +1,44 @@
+import {color, getHoverColor} from '../../src/helpers/helpers.color';
+
+describe('Color helper', function() {
+       function isColorInstance(obj) {
+               return typeof obj === 'object' && obj.valid;
+       }
+
+       it('should return a color when called with a color', function() {
+               expect(isColorInstance(color('rgb(1, 2, 3)'))).toBe(true);
+       });
+});
+
+describe('Background hover color helper', function() {
+       it('should return a CanvasPattern when called with a CanvasPattern', function(done) {
+               var dots = new Image();
+               dots.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAD1BMVEUAAAD///////////////+PQt5oAAAABXRSTlMAHlFhZsfk/BEAAAAqSURBVHgBY2BgZGJmYmSAAUYWEIDzmcBcJhiXGcxlRpPFrhdmMiqgvX0AcGIBEUAo6UAAAAAASUVORK5CYII=';
+               dots.onload = function() {
+                       var chartContext = document.createElement('canvas').getContext('2d');
+                       var patternCanvas = document.createElement('canvas');
+                       var patternContext = patternCanvas.getContext('2d');
+                       var pattern = patternContext.createPattern(dots, 'repeat');
+                       patternContext.fillStyle = pattern;
+
+                       var backgroundColor = getHoverColor(chartContext.createPattern(patternCanvas, 'repeat'));
+
+                       expect(backgroundColor instanceof CanvasPattern).toBe(true);
+
+                       done();
+               };
+       });
+
+       it('should return a CanvasGradient when called with a CanvasGradient', function() {
+               var context = document.createElement('canvas').getContext('2d');
+               var gradient = context.createLinearGradient(0, 1, 2, 3);
+
+               expect(getHoverColor(gradient) instanceof CanvasGradient).toBe(true);
+       });
+
+       it('should return a modified version of color when called with a color', function() {
+               var originalColorRGB = 'rgb(70, 191, 189)';
+
+               expect(getHoverColor('#46BFBD')).not.toEqual(originalColorRGB);
+       });
+});
diff --git a/test/specs/helpers.interpolation.tests.js b/test/specs/helpers.interpolation.tests.js
new file mode 100644 (file)
index 0000000..4910f80
--- /dev/null
@@ -0,0 +1,35 @@
+import {_pointInLine, _steppedInterpolation, _bezierInterpolation} from '../../src/helpers/helpers.interpolation';
+
+describe('helpers.interpolation', function() {
+       it('Should interpolate a point in line', function() {
+               expect(_pointInLine({x: 10, y: 10}, {x: 20, y: 20}, 0)).toEqual({x: 10, y: 10});
+               expect(_pointInLine({x: 10, y: 10}, {x: 20, y: 20}, 0.5)).toEqual({x: 15, y: 15});
+               expect(_pointInLine({x: 10, y: 10}, {x: 20, y: 20}, 1)).toEqual({x: 20, y: 20});
+       });
+
+       it('Should intepolate a point in stepped line', function() {
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0, 'before')).toEqual({x: 10, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.4, 'before')).toEqual({x: 14, y: 20});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.5, 'before')).toEqual({x: 15, y: 20});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 1, 'before')).toEqual({x: 20, y: 20});
+
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0, 'middle')).toEqual({x: 10, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.4, 'middle')).toEqual({x: 14, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.5, 'middle')).toEqual({x: 15, y: 20});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 1, 'middle')).toEqual({x: 20, y: 20});
+
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0, 'after')).toEqual({x: 10, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.4, 'after')).toEqual({x: 14, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 0.5, 'after')).toEqual({x: 15, y: 10});
+               expect(_steppedInterpolation({x: 10, y: 10}, {x: 20, y: 20}, 1, 'after')).toEqual({x: 20, y: 20});
+       });
+
+       it('Should interpolate a point in curve', function() {
+               const pt1 = {x: 10, y: 10, controlPointNextX: 12, controlPointNextY: 12};
+               const pt2 = {x: 20, y: 30, controlPointPreviousX: 18, controlPointPreviousY: 28};
+
+               expect(_bezierInterpolation(pt1, pt2, 0)).toEqual({x: 10, y: 10});
+               expect(_bezierInterpolation(pt1, pt2, 0.2)).toBeCloseToPoint({x: 11.616, y: 12.656});
+               expect(_bezierInterpolation(pt1, pt2, 1)).toEqual({x: 20, y: 30});
+       });
+});