2015-08-29 04:32:56 +02:00
|
|
|
describe('Core helper tests', function() {
|
|
|
|
|
|
|
|
var helpers;
|
|
|
|
|
|
|
|
beforeAll(function() {
|
|
|
|
helpers = window.Chart.helpers;
|
|
|
|
});
|
|
|
|
|
2015-10-17 05:14:07 +02:00
|
|
|
it('should filter an array', function() {
|
2015-08-29 04:32:56 +02:00
|
|
|
var data = [-10, 0, 6, 0, 7];
|
2015-10-17 05:14:07 +02:00
|
|
|
var callback = function(item) {
|
2016-10-16 23:34:59 +02:00
|
|
|
return item > 2;
|
2015-10-17 05:14:07 +02:00
|
|
|
};
|
2015-08-29 04:32:56 +02:00
|
|
|
expect(helpers.findNextWhere(data, callback)).toEqual(6);
|
|
|
|
expect(helpers.findNextWhere(data, callback, 2)).toBe(7);
|
|
|
|
expect(helpers.findNextWhere(data, callback, 4)).toBe(undefined);
|
|
|
|
expect(helpers.findPreviousWhere(data, callback)).toBe(7);
|
|
|
|
expect(helpers.findPreviousWhere(data, callback, 3)).toBe(6);
|
|
|
|
expect(helpers.findPreviousWhere(data, callback, 0)).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
2016-04-26 22:44:53 +02:00
|
|
|
it('should generate integer ids', function() {
|
|
|
|
var uid = helpers.uid();
|
|
|
|
expect(uid).toEqual(jasmine.any(Number));
|
|
|
|
expect(helpers.uid()).toBe(uid + 1);
|
|
|
|
expect(helpers.uid()).toBe(uid + 2);
|
|
|
|
expect(helpers.uid()).toBe(uid + 3);
|
2015-08-29 04:32:56 +02:00
|
|
|
});
|
|
|
|
|
2016-06-03 21:15:29 +02:00
|
|
|
it('should return the width of the longest text in an Array and 2D Array', function() {
|
|
|
|
var context = window.createMockContext();
|
|
|
|
var font = "normal 12px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
|
2016-10-16 23:34:59 +02:00
|
|
|
var arrayOfThings1D = ['FooBar', 'Bar'];
|
|
|
|
var arrayOfThings2D = [['FooBar_1', 'Bar_2'], 'Foo_1'];
|
|
|
|
|
2016-06-03 21:15:29 +02:00
|
|
|
|
Spelling fixes (#3522)
Including: actually, aspect, bottom, changes, characters, datasets, divisible, downward, guidelines, instance, instances, interrupted, item, javascript, label, lifecycle, lines, plugins, manually, milliseconds, nearest, occurring, position, predefined, recalculate, tooltip, those
2016-10-27 21:18:31 +02:00
|
|
|
// Regardless 'FooBar' is the longest label it should return (characters * 10)
|
2016-10-16 23:34:59 +02:00
|
|
|
expect(helpers.longestText(context, font, arrayOfThings1D, {})).toEqual(60);
|
|
|
|
expect(helpers.longestText(context, font, arrayOfThings2D, {})).toEqual(80);
|
2016-06-03 21:15:29 +02:00
|
|
|
// We check to make sure we made the right calls to the canvas.
|
|
|
|
expect(context.getCalls()).toEqual([{
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['FooBar']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['Bar']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['FooBar_1']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['Bar_2']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['Foo_1']
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('compare text with current longest and update', function() {
|
|
|
|
var context = window.createMockContext();
|
|
|
|
var data = {};
|
|
|
|
var gc = [];
|
|
|
|
var longest = 70;
|
|
|
|
|
|
|
|
expect(helpers.measureText(context, data, gc, longest, 'foobar')).toEqual(70);
|
|
|
|
expect(helpers.measureText(context, data, gc, longest, 'foobar_')).toEqual(70);
|
|
|
|
expect(helpers.measureText(context, data, gc, longest, 'foobar_1')).toEqual(80);
|
|
|
|
// We check to make sure we made the right calls to the canvas.
|
|
|
|
expect(context.getCalls()).toEqual([{
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['foobar']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['foobar_']
|
|
|
|
}, {
|
|
|
|
name: 'measureText',
|
|
|
|
args: ['foobar_1']
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2016-04-12 17:36:56 +02:00
|
|
|
describe('Color helper', function() {
|
|
|
|
function isColorInstance(obj) {
|
2019-10-31 23:03:00 +01:00
|
|
|
return typeof obj === 'object' && Object.prototype.hasOwnProperty.call(obj, 'values') && Object.prototype.hasOwnProperty.call(obj.values, 'rgb');
|
2016-04-12 17:36:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should return a color when called with a color', function() {
|
|
|
|
expect(isColorInstance(helpers.color('rgb(1, 2, 3)'))).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-09 08:24:32 +02:00
|
|
|
describe('Background hover color helper', function() {
|
|
|
|
it('should return a CanvasPattern when called with a CanvasPattern', function(done) {
|
2016-05-07 23:24:00 +02:00
|
|
|
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;
|
|
|
|
|
2016-05-09 08:24:32 +02:00
|
|
|
var backgroundColor = helpers.getHoverColor(chartContext.createPattern(patternCanvas, 'repeat'));
|
2016-05-07 23:24:00 +02:00
|
|
|
|
|
|
|
expect(backgroundColor instanceof CanvasPattern).toBe(true);
|
|
|
|
|
|
|
|
done();
|
2016-10-16 23:34:59 +02:00
|
|
|
};
|
2016-05-07 23:24:00 +02:00
|
|
|
});
|
|
|
|
|
2018-11-28 01:54:03 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2016-05-09 08:24:32 +02:00
|
|
|
it('should return a modified version of color when called with a color', function() {
|
2016-05-07 23:24:00 +02:00
|
|
|
var originalColorRGB = 'rgb(70, 191, 189)';
|
|
|
|
|
2016-05-09 08:24:32 +02:00
|
|
|
expect(helpers.getHoverColor('#46BFBD')).not.toEqual(originalColorRGB);
|
2016-05-07 23:24:00 +02:00
|
|
|
});
|
|
|
|
});
|
2015-10-17 05:14:07 +02:00
|
|
|
});
|