forked from chartjs/Chart.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.ticks.tests.js
108 lines (100 loc) · 3.12 KB
/
core.ticks.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
function getLabels(scale) {
return scale.ticks.map(t => t.label);
}
describe('Test tick generators', function() {
// formatters are used as default config values so users want to be able to reference them
it('Should expose formatters api', function() {
expect(typeof Chart.Ticks).toBeDefined();
expect(typeof Chart.Ticks.formatters).toBeDefined();
expect(typeof Chart.Ticks.formatters.values).toBe('function');
expect(typeof Chart.Ticks.formatters.numeric).toBe('function');
});
it('Should generate linear spaced ticks with correct precision', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
data: []
}],
},
options: {
plugins: {
legend: false
},
scales: {
x: {
type: 'linear',
position: 'bottom',
ticks: {
callback: function(value) {
return value.toString();
}
}
},
y: {
type: 'linear',
ticks: {
callback: function(value) {
return value.toString();
}
}
}
}
}
});
var xLabels = getLabels(chart.scales.x);
var yLabels = getLabels(chart.scales.y);
expect(xLabels).toEqual(['0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
expect(yLabels).toEqual(['0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
});
it('Should generate logarithmic spaced ticks with correct precision', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
data: []
}],
},
options: {
plugins: {
legend: false
},
scales: {
x: {
type: 'logarithmic',
position: 'bottom',
min: 0.1,
max: 1,
ticks: {
callback: function(value) {
return value.toString();
}
}
},
y: {
type: 'logarithmic',
min: 0.1,
max: 1,
ticks: {
callback: function(value) {
return value.toString();
}
}
}
}
}
});
var xLabels = getLabels(chart.scales.x);
var yLabels = getLabels(chart.scales.y);
expect(xLabels).toEqual(['0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
expect(yLabels).toEqual(['0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
});
describe('formatters.numeric', function() {
it('should not fail on empty or 1 item array', function() {
const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}}}};
expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, []])).toEqual('1');
expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}]])).toEqual('1');
expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, [{value: 1}, {value: 1.01}]])).toEqual('1.00');
});
});
});