2020-11-19 19:59:24 +01:00
|
|
|
const {toLineHeight, toPadding, toFont, resolve, toTRBLCorners} = Chart.helpers;
|
2017-07-20 19:20:54 +02:00
|
|
|
|
|
|
|
describe('Chart.helpers.options', function() {
|
|
|
|
describe('toLineHeight', function() {
|
|
|
|
it ('should support keyword values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toLineHeight('normal', 16)).toBe(16 * 1.2);
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|
|
|
|
it ('should support unitless values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toLineHeight(1.4, 16)).toBe(16 * 1.4);
|
|
|
|
expect(toLineHeight('1.4', 16)).toBe(16 * 1.4);
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|
|
|
|
it ('should support length values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toLineHeight('42px', 16)).toBe(42);
|
|
|
|
expect(toLineHeight('1.4em', 16)).toBe(16 * 1.4);
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|
|
|
|
it ('should support percentage values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toLineHeight('140%', 16)).toBe(16 * 1.4);
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|
|
|
|
it ('should fallback to default (1.2) for invalid values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toLineHeight(null, 16)).toBe(16 * 1.2);
|
|
|
|
expect(toLineHeight(undefined, 16)).toBe(16 * 1.2);
|
|
|
|
expect(toLineHeight('foobar', 16)).toBe(16 * 1.2);
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-21 15:03:03 +02:00
|
|
|
|
2020-10-26 15:05:24 +01:00
|
|
|
describe('toTRBLCorners', function() {
|
|
|
|
it('should support number values', function() {
|
|
|
|
expect(toTRBLCorners(4)).toEqual(
|
|
|
|
{topLeft: 4, topRight: 4, bottomLeft: 4, bottomRight: 4});
|
|
|
|
expect(toTRBLCorners(4.5)).toEqual(
|
|
|
|
{topLeft: 4.5, topRight: 4.5, bottomLeft: 4.5, bottomRight: 4.5});
|
|
|
|
});
|
|
|
|
it('should support string values', function() {
|
|
|
|
expect(toTRBLCorners('4')).toEqual(
|
|
|
|
{topLeft: 4, topRight: 4, bottomLeft: 4, bottomRight: 4});
|
|
|
|
expect(toTRBLCorners('4.5')).toEqual(
|
|
|
|
{topLeft: 4.5, topRight: 4.5, bottomLeft: 4.5, bottomRight: 4.5});
|
|
|
|
});
|
|
|
|
it('should support object values', function() {
|
|
|
|
expect(toTRBLCorners({topLeft: 1, topRight: 2, bottomLeft: 3, bottomRight: 4})).toEqual(
|
|
|
|
{topLeft: 1, topRight: 2, bottomLeft: 3, bottomRight: 4});
|
|
|
|
expect(toTRBLCorners({topLeft: 1.5, topRight: 2.5, bottomLeft: 3.5, bottomRight: 4.5})).toEqual(
|
|
|
|
{topLeft: 1.5, topRight: 2.5, bottomLeft: 3.5, bottomRight: 4.5});
|
|
|
|
expect(toTRBLCorners({topLeft: '1', topRight: '2', bottomLeft: '3', bottomRight: '4'})).toEqual(
|
|
|
|
{topLeft: 1, topRight: 2, bottomLeft: 3, bottomRight: 4});
|
|
|
|
});
|
|
|
|
it('should fallback to 0 for invalid values', function() {
|
|
|
|
expect(toTRBLCorners({topLeft: 'foo', topRight: 'foo', bottomLeft: 'foo', bottomRight: 'foo'})).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
expect(toTRBLCorners({topLeft: null, topRight: null, bottomLeft: null, bottomRight: null})).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
expect(toTRBLCorners({})).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
expect(toTRBLCorners('foo')).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
expect(toTRBLCorners(null)).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
expect(toTRBLCorners(undefined)).toEqual(
|
|
|
|
{topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-21 15:03:03 +02:00
|
|
|
describe('toPadding', function() {
|
|
|
|
it ('should support number values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding(4)).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding(4.5)).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
|
|
|
|
});
|
|
|
|
it ('should support string values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding('4')).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding('4.5')).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
|
|
|
|
});
|
|
|
|
it ('should support object values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({top: 1, right: 2, bottom: 3, left: 4})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({top: 1.5, right: 2.5, bottom: 3.5, left: 4.5})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 1.5, right: 2.5, bottom: 3.5, left: 4.5, height: 5, width: 7});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({top: '1', right: '2', bottom: '3', left: '4'})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
|
|
|
|
});
|
|
|
|
it ('should fallback to 0 for invalid values', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({top: 'foo', right: 'foo', bottom: 'foo', left: 'foo'})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({top: null, right: null, bottom: null, left: null})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding({})).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding('foo')).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding(null)).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(toPadding(undefined)).toEqual(
|
2017-07-21 15:03:03 +02:00
|
|
|
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
|
|
|
|
});
|
|
|
|
});
|
2017-08-24 09:34:38 +02:00
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
describe('toFont', function() {
|
|
|
|
it('should return a font with default values', function() {
|
|
|
|
const defaultFont = Object.assign({}, Chart.defaults.font);
|
2020-01-03 20:07:38 +01:00
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
Object.assign(Chart.defaults.font, {
|
|
|
|
family: 'foobar',
|
|
|
|
size: 42,
|
|
|
|
style: 'xxxyyy',
|
2020-01-03 20:07:38 +01:00
|
|
|
lineHeight: 1.5
|
|
|
|
});
|
2018-12-20 09:56:06 +01:00
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
expect(toFont({})).toEqual({
|
2018-12-20 09:56:06 +01:00
|
|
|
family: 'foobar',
|
|
|
|
lineHeight: 63,
|
|
|
|
size: 42,
|
|
|
|
string: 'xxxyyy 42px foobar',
|
|
|
|
style: 'xxxyyy',
|
2020-11-19 19:59:24 +01:00
|
|
|
weight: null
|
2018-12-20 09:56:06 +01:00
|
|
|
});
|
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
Object.assign(Chart.defaults.font, defaultFont);
|
2018-12-20 09:56:06 +01:00
|
|
|
});
|
|
|
|
it ('should return a font with given values', function() {
|
2020-05-21 23:07:06 +02:00
|
|
|
expect(toFont({
|
|
|
|
family: 'bla',
|
2018-12-20 09:56:06 +01:00
|
|
|
lineHeight: 8,
|
2020-05-21 23:07:06 +02:00
|
|
|
size: 21,
|
|
|
|
style: 'zzz'
|
2018-12-20 09:56:06 +01:00
|
|
|
})).toEqual({
|
|
|
|
family: 'bla',
|
|
|
|
lineHeight: 8 * 21,
|
|
|
|
size: 21,
|
|
|
|
string: 'zzz 21px bla',
|
|
|
|
style: 'zzz',
|
2020-11-19 19:59:24 +01:00
|
|
|
weight: null
|
2018-12-20 09:56:06 +01:00
|
|
|
});
|
|
|
|
});
|
2020-01-06 13:40:49 +01:00
|
|
|
it ('should handle a string font size', function() {
|
2020-05-21 23:07:06 +02:00
|
|
|
expect(toFont({
|
|
|
|
family: 'bla',
|
2020-01-06 13:40:49 +01:00
|
|
|
lineHeight: 8,
|
2020-05-21 23:07:06 +02:00
|
|
|
size: '21',
|
|
|
|
style: 'zzz'
|
2020-01-06 13:40:49 +01:00
|
|
|
})).toEqual({
|
|
|
|
family: 'bla',
|
|
|
|
lineHeight: 8 * 21,
|
|
|
|
size: 21,
|
|
|
|
string: 'zzz 21px bla',
|
|
|
|
style: 'zzz',
|
2020-11-19 19:59:24 +01:00
|
|
|
weight: null
|
2020-01-06 13:40:49 +01:00
|
|
|
});
|
|
|
|
});
|
2020-05-21 23:07:06 +02:00
|
|
|
it('should return null as a font string if size or family are missing', function() {
|
|
|
|
const fontFamily = Chart.defaults.font.family;
|
|
|
|
const fontSize = Chart.defaults.font.size;
|
|
|
|
delete Chart.defaults.font.family;
|
|
|
|
delete Chart.defaults.font.size;
|
|
|
|
|
|
|
|
expect(toFont({
|
|
|
|
style: 'italic',
|
|
|
|
size: 12
|
2018-12-20 09:56:06 +01:00
|
|
|
}).string).toBeNull();
|
2020-05-21 23:07:06 +02:00
|
|
|
expect(toFont({
|
|
|
|
style: 'italic',
|
|
|
|
family: 'serif'
|
2018-12-20 09:56:06 +01:00
|
|
|
}).string).toBeNull();
|
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
Chart.defaults.font.family = fontFamily;
|
|
|
|
Chart.defaults.font.size = fontSize;
|
2018-12-20 09:56:06 +01:00
|
|
|
});
|
2020-05-21 23:07:06 +02:00
|
|
|
it('font.style should be optional for font strings', function() {
|
|
|
|
const fontStyle = Chart.defaults.font.style;
|
|
|
|
delete Chart.defaults.font.style;
|
2018-12-20 09:56:06 +01:00
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
expect(toFont({
|
|
|
|
size: 12,
|
|
|
|
family: 'serif'
|
2018-12-20 09:56:06 +01:00
|
|
|
}).string).toBe('12px serif');
|
|
|
|
|
2020-05-21 23:07:06 +02:00
|
|
|
Chart.defaults.font.style = fontStyle;
|
2018-12-20 09:56:06 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-24 09:34:38 +02:00
|
|
|
describe('resolve', function() {
|
|
|
|
it ('should fallback to the first defined input', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([42])).toBe(42);
|
|
|
|
expect(resolve([42, 'foo'])).toBe(42);
|
|
|
|
expect(resolve([undefined, 42, 'foo'])).toBe(42);
|
|
|
|
expect(resolve([42, 'foo', undefined])).toBe(42);
|
|
|
|
expect(resolve([undefined])).toBe(undefined);
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should correctly handle empty values (null, 0, "")', function() {
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([0, 'foo'])).toBe(0);
|
|
|
|
expect(resolve(['', 'foo'])).toBe('');
|
|
|
|
expect(resolve([null, 'foo'])).toBe(null);
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should support indexable options if index is provided', function() {
|
|
|
|
var input = [42, 'foo', 'bar'];
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input], undefined, 0)).toBe(42);
|
|
|
|
expect(resolve([input], undefined, 1)).toBe('foo');
|
|
|
|
expect(resolve([input], undefined, 2)).toBe('bar');
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should fallback if an indexable option value is undefined', function() {
|
|
|
|
var input = [42, undefined, 'bar'];
|
2020-05-29 22:27:10 +02:00
|
|
|
expect(resolve([input], undefined, 1)).toBe(undefined);
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input, 'foo'], undefined, 1)).toBe('foo');
|
2020-05-29 22:27:10 +02:00
|
|
|
});
|
|
|
|
it ('should loop if an indexable option index is out of bounds', function() {
|
|
|
|
var input = [42, undefined, 'bar'];
|
|
|
|
expect(resolve([input], undefined, 3)).toBe(42);
|
|
|
|
expect(resolve([input, 'foo'], undefined, 4)).toBe('foo');
|
|
|
|
expect(resolve([input, 'foo'], undefined, 5)).toBe('bar');
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should not handle indexable options if index is undefined', function() {
|
|
|
|
var array = [42, 'foo', 'bar'];
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([array])).toBe(array);
|
|
|
|
expect(resolve([array], undefined, undefined)).toBe(array);
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should support scriptable options if context is provided', function() {
|
|
|
|
var input = function(context) {
|
|
|
|
return context.v * 2;
|
|
|
|
};
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([42], {v: 42})).toBe(42);
|
|
|
|
expect(resolve([input], {v: 42})).toBe(84);
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should fallback if a scriptable option returns undefined', function() {
|
|
|
|
var input = function() {};
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input], {v: 42})).toBe(undefined);
|
|
|
|
expect(resolve([input, 'foo'], {v: 42})).toBe('foo');
|
|
|
|
expect(resolve([input, undefined, 'foo'], {v: 42})).toBe('foo');
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should not handle scriptable options if context is undefined', function() {
|
|
|
|
var input = function(context) {
|
|
|
|
return context.v * 2;
|
|
|
|
};
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input])).toBe(input);
|
|
|
|
expect(resolve([input], undefined)).toBe(input);
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
it ('should handle scriptable and indexable option', function() {
|
|
|
|
var input = function(context) {
|
|
|
|
return [context.v, undefined, 'bar'];
|
|
|
|
};
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
|
|
|
|
expect(resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
|
2020-05-29 22:27:10 +02:00
|
|
|
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('bar');
|
2018-12-20 09:56:06 +01:00
|
|
|
expect(resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
|
2017-08-24 09:34:38 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-20 19:20:54 +02:00
|
|
|
});
|