forked from vega/vega-lite
/
format.test.ts
146 lines (131 loc) · 5.01 KB
/
format.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
import {vgField} from '../../src/channeldef';
import {formatSignalRef, guideFormatType, numberFormat, timeFormatExpression} from '../../src/compile/format';
import {defaultConfig} from '../../src/config';
import {NOMINAL, ORDINAL, QUANTITATIVE, TEMPORAL} from '../../src/type';
describe('Format', () => {
describe('timeFormatExpression()', () => {
it('should get the right time expression for month', () => {
const fieldDef = {timeUnit: 'month', field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression(
vgField(fieldDef, {expr: 'datum'}),
'month',
undefined,
defaultConfig.timeFormat,
false
);
expect(expression).toBe(
'timeFormat(datum["month_a"], timeUnitSpecifier(["month"], {"year-month":"%b %Y ","year-month-date":"%b %d, %Y "}))'
);
});
it('should get the right time expression for yearmonth with custom format', () => {
const fieldDef = {timeUnit: 'yearmonth', field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression(
vgField(fieldDef, {expr: 'datum'}),
'month',
'%Y',
defaultConfig.timeFormat,
false
);
expect(expression).toBe(`timeFormat(datum["yearmonth_a"], '%Y')`);
});
it('should get the right time expression for quarter', () => {
const fieldDef = {timeUnit: 'quarter', field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression(
vgField(fieldDef, {expr: 'datum'}),
'quarter',
undefined,
defaultConfig.timeFormat,
false
);
expect(expression).toBe(
'timeFormat(datum["quarter_a"], timeUnitSpecifier(["quarter"], {"year-month":"%b %Y ","year-month-date":"%b %d, %Y "}))'
);
});
it('should get the right time expression for yearquarter', () => {
const expression = timeFormatExpression(
'datum["data"]',
'yearquarter',
undefined,
defaultConfig.timeFormat,
false
);
expect(expression).toBe(
'timeFormat(datum["data"], timeUnitSpecifier(["year","quarter"], {"year-month":"%b %Y ","year-month-date":"%b %d, %Y "}))'
);
});
it('should get the right time expression for yearmonth with custom format and utc scale type', () => {
const fieldDef = {timeUnit: 'yearmonth', field: 'a', type: TEMPORAL} as const;
const expression = timeFormatExpression(
vgField(fieldDef, {expr: 'datum'}),
'month',
'%Y',
defaultConfig.timeFormat,
true
);
expect(expression).toBe(`utcFormat(datum["yearmonth_a"], '%Y')`);
});
});
describe('numberFormat()', () => {
it('should use number format for quantitative scale', () => {
expect(numberFormat(QUANTITATIVE, undefined, {numberFormat: 'd'})).toBe('d');
});
it('should use number format for ordinal and nominal data but don not use config', () => {
for (const type of [ORDINAL, NOMINAL]) {
expect(numberFormat(type, undefined, {numberFormat: 'd'})).toBeUndefined();
expect(numberFormat(type, 'd', {numberFormat: 'd'})).toBe('d');
}
});
it('should support empty number format', () => {
expect(numberFormat(QUANTITATIVE, undefined, {numberFormat: ''})).toBe('');
});
it('should use format if provided', () => {
expect(numberFormat(QUANTITATIVE, 'a', {})).toBe('a');
});
it('should not use number format for binned quantitative scale', () => {
expect(numberFormat(QUANTITATIVE, undefined, {})).toBeUndefined();
});
it('should not use number format for temporal scale', () => {
expect(numberFormat(TEMPORAL, undefined, {})).toBeUndefined();
expect(numberFormat(ORDINAL, undefined, {})).toBeUndefined();
});
});
describe('formatSignalRef()', () => {
it('should format ordinal field defs if format is present', () => {
expect(
formatSignalRef({
fieldOrDatumDef: {field: 'foo', type: 'ordinal'},
format: '.2f',
formatType: undefined,
expr: 'parent',
config: {}
})
).toEqual({
signal: 'format(parent["foo"], ".2f")'
});
});
it('should formats datumDef if format is present', () => {
expect(
formatSignalRef({
fieldOrDatumDef: {datum: 200, type: 'quantitative'},
format: '.2f',
formatType: undefined,
expr: 'parent',
config: {}
})
).toEqual({
signal: 'format(200, ".2f")'
});
});
});
describe('guideFormatType()', () => {
it('should return existing format type', () => {
expect(guideFormatType('number', {field: ' foo', type: 'quantitative'}, 'ordinal')).toBe('number');
expect(guideFormatType('time', {field: ' foo', type: 'quantitative'}, 'ordinal')).toBe('time');
});
it('should return utc for utc time units', () => {
expect(
guideFormatType('', {field: ' foo', type: 'ordinal', timeUnit: {utc: true, unit: 'year'}}, 'ordinal')
).toBe('utc');
});
});
});