/
helpers.options.tests.js
256 lines (244 loc) · 10.4 KB
/
helpers.options.tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const {toLineHeight, toPadding, toFont, resolve, toTRBLCorners} = Chart.helpers;
describe('Chart.helpers.options', function() {
describe('toLineHeight', function() {
it ('should support keyword values', function() {
expect(toLineHeight('normal', 16)).toBe(16 * 1.2);
});
it ('should support unitless values', function() {
expect(toLineHeight(1.4, 16)).toBe(16 * 1.4);
expect(toLineHeight('1.4', 16)).toBe(16 * 1.4);
});
it ('should support length values', function() {
expect(toLineHeight('42px', 16)).toBe(42);
expect(toLineHeight('1.4em', 16)).toBe(16 * 1.4);
});
it ('should support percentage values', function() {
expect(toLineHeight('140%', 16)).toBe(16 * 1.4);
});
it ('should fallback to default (1.2) for invalid values', function() {
expect(toLineHeight(null, 16)).toBe(16 * 1.2);
expect(toLineHeight(undefined, 16)).toBe(16 * 1.2);
expect(toLineHeight('foobar', 16)).toBe(16 * 1.2);
});
});
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});
});
});
describe('toPadding', function() {
it ('should support number values', function() {
expect(toPadding(4)).toEqual(
{top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
expect(toPadding(4.5)).toEqual(
{top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
});
it ('should support string values', function() {
expect(toPadding('4')).toEqual(
{top: 4, right: 4, bottom: 4, left: 4, height: 8, width: 8});
expect(toPadding('4.5')).toEqual(
{top: 4.5, right: 4.5, bottom: 4.5, left: 4.5, height: 9, width: 9});
});
it ('should support object values', function() {
expect(toPadding({top: 1, right: 2, bottom: 3, left: 4})).toEqual(
{top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
expect(toPadding({top: 1.5, right: 2.5, bottom: 3.5, left: 4.5})).toEqual(
{top: 1.5, right: 2.5, bottom: 3.5, left: 4.5, height: 5, width: 7});
expect(toPadding({top: '1', right: '2', bottom: '3', left: '4'})).toEqual(
{top: 1, right: 2, bottom: 3, left: 4, height: 4, width: 6});
});
it ('should fallback to 0 for invalid values', function() {
expect(toPadding({top: 'foo', right: 'foo', bottom: 'foo', left: 'foo'})).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
expect(toPadding({top: null, right: null, bottom: null, left: null})).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
expect(toPadding({})).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
expect(toPadding('foo')).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
expect(toPadding(null)).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
expect(toPadding(undefined)).toEqual(
{top: 0, right: 0, bottom: 0, left: 0, height: 0, width: 0});
});
it('should support x / y shorthands', function() {
expect(toPadding({x: 1, y: 2})).toEqual(
{top: 2, right: 1, bottom: 2, left: 1, height: 4, width: 2});
expect(toPadding({x: 1, left: 0})).toEqual(
{top: 0, right: 1, bottom: 0, left: 0, height: 0, width: 1});
expect(toPadding({y: 5, bottom: 0})).toEqual(
{top: 5, right: 0, bottom: 0, left: 0, height: 5, width: 0});
});
});
describe('toFont', function() {
it('should return a font with default values', function() {
const defaultFont = Object.assign({}, Chart.defaults.font);
Object.assign(Chart.defaults.font, {
family: 'foobar',
size: 42,
style: 'oblique 9deg',
lineHeight: 1.5
});
expect(toFont({})).toEqual({
family: 'foobar',
lineHeight: 63,
size: 42,
string: 'oblique 9deg 42px foobar',
style: 'oblique 9deg',
weight: null
});
Object.assign(Chart.defaults.font, defaultFont);
});
it ('should return a font with given values', function() {
expect(toFont({
family: 'bla',
lineHeight: 8,
size: 21,
style: 'oblique -90deg'
})).toEqual({
family: 'bla',
lineHeight: 8 * 21,
size: 21,
string: 'oblique -90deg 21px bla',
style: 'oblique -90deg',
weight: null
});
});
it ('should handle a string font size', function() {
expect(toFont({
family: 'bla',
lineHeight: 8,
size: '21',
style: 'italic'
})).toEqual({
family: 'bla',
lineHeight: 8 * 21,
size: 21,
string: 'italic 21px bla',
style: 'italic',
weight: null
});
});
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
}).string).toBeNull();
expect(toFont({
style: 'italic',
family: 'serif'
}).string).toBeNull();
Chart.defaults.font.family = fontFamily;
Chart.defaults.font.size = fontSize;
});
it('font.style should be optional for font strings', function() {
const fontStyle = Chart.defaults.font.style;
delete Chart.defaults.font.style;
expect(toFont({
size: 12,
family: 'serif'
}).string).toBe('12px serif');
Chart.defaults.font.style = fontStyle;
});
});
describe('resolve', function() {
it ('should fallback to the first defined input', function() {
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);
});
it ('should correctly handle empty values (null, 0, "")', function() {
expect(resolve([0, 'foo'])).toBe(0);
expect(resolve(['', 'foo'])).toBe('');
expect(resolve([null, 'foo'])).toBe(null);
});
it ('should support indexable options if index is provided', function() {
var input = [42, 'foo', 'bar'];
expect(resolve([input], undefined, 0)).toBe(42);
expect(resolve([input], undefined, 1)).toBe('foo');
expect(resolve([input], undefined, 2)).toBe('bar');
});
it ('should fallback if an indexable option value is undefined', function() {
var input = [42, undefined, 'bar'];
expect(resolve([input], undefined, 1)).toBe(undefined);
expect(resolve([input, 'foo'], undefined, 1)).toBe('foo');
});
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');
});
it ('should not handle indexable options if index is undefined', function() {
var array = [42, 'foo', 'bar'];
expect(resolve([array])).toBe(array);
expect(resolve([array], undefined, undefined)).toBe(array);
});
it ('should support scriptable options if context is provided', function() {
var input = function(context) {
return context.v * 2;
};
expect(resolve([42], {v: 42})).toBe(42);
expect(resolve([input], {v: 42})).toBe(84);
});
it ('should fallback if a scriptable option returns undefined', function() {
var input = function() {};
expect(resolve([input], {v: 42})).toBe(undefined);
expect(resolve([input, 'foo'], {v: 42})).toBe('foo');
expect(resolve([input, undefined, 'foo'], {v: 42})).toBe('foo');
});
it ('should not handle scriptable options if context is undefined', function() {
var input = function(context) {
return context.v * 2;
};
expect(resolve([input])).toBe(input);
expect(resolve([input], undefined)).toBe(input);
});
it ('should handle scriptable and indexable option', function() {
var input = function(context) {
return [context.v, undefined, 'bar'];
};
expect(resolve([input, 'foo'], {v: 42}, 0)).toBe(42);
expect(resolve([input, 'foo'], {v: 42}, 1)).toBe('foo');
expect(resolve([input, 'foo'], {v: 42}, 5)).toBe('bar');
expect(resolve([input, ['foo', 'bar']], {v: 42}, 1)).toBe('bar');
});
});
});