/
properties.test.ts
365 lines (324 loc) · 12.2 KB
/
properties.test.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import {NONPOSITION_SCALE_CHANNELS, OFFSET_SCALE_CHANNELS} from '../../../src/channel';
import * as rules from '../../../src/compile/scale/properties';
import {AREA, BAR, LINE} from '../../../src/mark';
import {ScaleType} from '../../../src/scale';
describe('compile/scale', () => {
describe('nice', () => {
it('should return nice for x and y', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.nice('linear', c, undefined, undefined, undefined, {type: 'quantitative'})).toBe(true);
}
});
it('should not return nice for binned x and y', () => {
for (const c of ['x', 'y'] as const) {
expect(
rules.nice('linear', c, undefined, undefined, undefined, {type: 'quantitative', field: 'a', bin: true})
).toBeUndefined();
}
});
it('should not return nice for temporal x and y', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.nice('time', c, undefined, undefined, undefined, {type: 'temporal'})).toBeUndefined();
}
});
it('should not return nice when domain is set explicitly', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.nice('time', c, [0, 42], undefined, undefined, {type: 'quantitative'})).toBeUndefined();
}
});
it('should not return nice when domainMin is set explicitly', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.nice('time', c, undefined, 0, undefined, {type: 'quantitative'})).toBeUndefined();
}
});
it('should not return nice when domainMax is set explicitly', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.nice('time', c, [0, 42], undefined, 0, {type: 'quantitative'})).toBeUndefined();
}
});
});
describe('padding', () => {
it('should be pointPadding for point scale if channel is x or y and padding is not specified', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.padding(c, 'point', {pointPadding: 13}, undefined, undefined, undefined)).toBe(13);
}
});
it('should be continuousBandSize for linear x-scale of vertical bar', () => {
expect(
rules.padding(
'x',
'linear',
{},
{field: 'date', type: 'temporal'},
{type: 'bar', orient: 'vertical'},
{continuousBandSize: 13}
)
).toBe(13);
});
it('should be undefined for linear x-scale for binned field of vertical bar', () => {
expect(
rules.padding(
'x',
'linear',
{},
{bin: true, field: 'date', type: 'temporal'},
{type: 'bar', orient: 'vertical'},
{continuousBandSize: 13}
)
).toBeUndefined();
});
it('should be continuousBandSize for linear y-scale of horizontal bar', () => {
expect(
rules.padding(
'y',
'linear',
{},
{field: 'date', type: 'temporal'},
{type: 'bar', orient: 'horizontal'},
{continuousBandSize: 13}
)
).toBe(13);
});
});
describe('paddingInner', () => {
it('should be undefined if padding is specified', () => {
expect(rules.paddingInner(10, 'x', 'bar', 'band', {})).toBeUndefined();
});
it('should be bandPaddingInner if channel is x or y and padding is not specified', () => {
expect(rules.paddingInner(undefined, 'x', 'bar', 'band', {bandPaddingInner: 15})).toBe(15);
expect(rules.paddingInner(undefined, 'y', 'bar', 'band', {bandPaddingInner: 15})).toBe(15);
});
it('should be config.scale.nestedOffsetPaddingInner if channel is x or y and padding is not specified and there is a nested offset encoding', () => {
expect(rules.paddingInner(undefined, 'x', 'bar', 'band', {bandWithNestedOffsetPaddingInner: 15}, true)).toBe(15);
expect(rules.paddingInner(undefined, 'y', 'bar', 'band', {bandWithNestedOffsetPaddingInner: 15}, true)).toBe(15);
});
it('should be undefined for non-xy channels', () => {
for (const c of NONPOSITION_SCALE_CHANNELS) {
expect(rules.paddingInner(undefined, c, 'bar', 'band', {bandPaddingInner: 15})).toBeUndefined();
}
});
it('should be offsetBandPaddingInnher for x/y-offset channels', () => {
for (const c of OFFSET_SCALE_CHANNELS) {
expect(rules.paddingInner(undefined, c, 'bar', 'band', {offsetBandPaddingInner: 0.11})).toBe(0.11);
}
});
});
describe('paddingOuter', () => {
it('should be undefined if padding is specified', () => {
for (const scaleType of ['point', 'band'] as ScaleType[]) {
expect(rules.paddingOuter(10, 'x', scaleType, 0, {})).toBeUndefined();
}
});
it('should be config.scale.bandPaddingOuter for band scale if channel is x or y and padding is not specified and config.scale.bandPaddingOuter', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.paddingOuter(undefined, c, 'band', 0, {bandPaddingOuter: 16})).toBe(16);
}
});
it('should be config.scale.nestedOffsetPaddingOuter for band scale if channel is x or y and padding is not specified and there is a nested offset encoding', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.paddingOuter(undefined, c, 'band', 0, {bandWithNestedOffsetPaddingOuter: 16}, true)).toBe(16);
}
});
it('should be paddingInner/2 for band scale if channel is x or y and padding is not specified and config.scale.bandPaddingOuter', () => {
for (const c of ['x', 'y'] as const) {
expect(rules.paddingOuter(undefined, c, 'band', 10, {})).toBe(5);
}
});
it('should be undefined for non-xy channels', () => {
for (const c of NONPOSITION_SCALE_CHANNELS) {
for (const scaleType of ['point', 'band'] as ScaleType[]) {
expect(rules.paddingOuter(undefined, c, scaleType, 0, {})).toBeUndefined();
}
}
});
it('should be 0.5 for x/yOffset channels with point scales', () => {
for (const c of ['xOffset', 'yOffset'] as const) {
expect(rules.paddingOuter(undefined, c, 'point', 0, {})).toBe(0.5);
}
});
it('should be offsetBandPaddingOuter for x/yOffset channels with band scales', () => {
for (const c of ['xOffset', 'yOffset'] as const) {
expect(rules.paddingOuter(undefined, c, 'band', 0, {offsetBandPaddingOuter: 0.23})).toBe(0.23);
}
});
});
describe('reverse', () => {
it('should return true for a continuous scale with sort = "descending"', () => {
expect(rules.reverse('linear', 'descending', 'x', {})).toBe(true);
});
it('should return false for a discrete scale with sort = "descending"', () => {
expect(rules.reverse('point', 'descending', 'x', {})).toBeUndefined();
});
it('should return xReverse for continuous x scale', () => {
expect(rules.reverse('linear', 'ascending', 'x', {xReverse: {signal: 'rtl'}})).toEqual({signal: 'rtl'});
});
it('should return flip xReverse for continuous x scale with descending sort', () => {
expect(rules.reverse('linear', 'descending', 'x', {xReverse: {signal: 'rtl'}})).toEqual({signal: '!rtl'});
expect(rules.reverse('linear', 'descending', 'x', {xReverse: true})).toBe(false);
});
});
describe('interpolate', () => {
it('should return hcl for colored quantitative field', () => {
expect(rules.interpolate('color', 'quantitative')).toBe('hcl');
});
it('should return undefined for colored nominal field', () => {
expect(rules.interpolate('color', 'nominal')).toBeUndefined();
});
it('should return undefined for size', () => {
expect(rules.interpolate('size', 'quantitative')).toBeUndefined();
});
});
describe('zero', () => {
it('should return default (undefined) when mapping a quantitative field to x with scale.domain = "unaggregated"', () => {
expect(
rules.zero('x', {field: 'a', type: 'quantitative'}, 'unaggregated', {type: 'point'}, 'linear', undefined, false)
).toBeUndefined();
});
it('should return true when mapping a quantitative field to size', () => {
expect(
rules.zero('size', {field: 'a', type: 'quantitative'}, undefined, {type: 'point'}, 'linear', undefined, false)
).toBeTruthy();
});
it('should return false when mapping a ordinal field to size', () => {
expect(
!rules.zero('size', {field: 'a', type: 'ordinal'}, undefined, {type: 'point'}, 'linear', undefined, false)
).toBeTruthy();
});
it('should return default (undefined) when mapping a non-binned quantitative field to x/y of point', () => {
for (const channel of ['x', 'y'] as const) {
expect(
rules.zero(
channel,
{field: 'a', type: 'quantitative'},
undefined,
{type: 'point'},
'linear',
undefined,
false
)
).toBeUndefined();
}
});
it('should return false when mapping a quantitative field to dimension axis of bar, line, and area', () => {
for (const mark of [BAR, AREA, LINE]) {
expect(
rules.zero(
'x',
{field: 'a', type: 'quantitative'},
undefined,
{type: mark, orient: 'vertical'},
'linear',
undefined,
false
)
).toBe(false);
expect(
rules.zero(
'y',
{field: 'a', type: 'quantitative'},
undefined,
{type: mark, orient: 'horizontal'},
'linear',
undefined,
false
)
).toBe(false);
}
});
it('should return false when mapping a binned quantitative field to x/y', () => {
for (const channel of ['x', 'y'] as const) {
expect(
!rules.zero(
channel,
{bin: true, field: 'a', type: 'quantitative'},
undefined,
{type: 'point'},
'linear',
undefined,
false
)
).toBeTruthy();
}
});
it('should return false when mapping a non-binned quantitative field with custom domain to x/y', () => {
for (const channel of ['x', 'y'] as const) {
expect(
!rules.zero(
channel,
{
bin: true,
field: 'a',
type: 'quantitative'
},
[3, 5],
{type: 'point'},
'linear',
undefined,
false
)
).toBeTruthy();
}
});
it(`should return config.scale.zero instead of true if it is specified`, () => {
const configZero = false;
for (const channel of ['x', 'y'] as const) {
expect(
rules.zero(
channel,
{field: 'a', type: 'quantitative'},
undefined,
{type: 'point'},
'linear',
{zero: configZero},
false
)
).toBe(configZero);
}
expect(
rules.zero(
'size',
{field: 'a', type: 'ordinal'},
undefined,
{type: 'point'},
'linear',
{zero: configZero},
false
)
).toBe(configZero);
// ranged bar/area should take default configZero
expect(
rules.zero('x', {field: 'a', type: 'quantitative'}, undefined, {type: BAR}, 'linear', {zero: configZero}, true)
).toBe(configZero);
});
it(`should return true for x/y scales of the non-ranged are/bar charts regardless to config`, () => {
for (const mark of [BAR, AREA]) {
for (const channel of ['x', 'y'] as const) {
expect(
rules.zero(
channel,
{field: 'a', type: 'quantitative'},
undefined,
{type: mark},
'linear',
{zero: false},
false
)
).toBe(true);
}
}
});
it(`should return true for the continuous & quantitative size scale regardless to config`, () => {
expect(
rules.zero(
'size',
{field: 'a', type: 'quantitative'},
undefined,
{type: 'point'},
'linear',
{zero: false},
false
)
).toBe(true);
});
});
});