1
import { formatValue, parseValue, getOptions } from './GraphHelpers';
2
import moment from 'moment';
3
require('../../vendor/flot/jquery.flot'); // need for $.colors
5
describe('GraphHelpers', () => {
6
describe('formatValue', () => {
7
it('formats tick values correctly', () => {
9
{ input: null, output: 'null' },
10
{ input: 0, output: '0.00' },
11
{ input: 2e24, output: '2.00Y' },
12
{ input: 2e23, output: '200.00Z' },
13
{ input: 2e22, output: '20.00Z' },
14
{ input: 2e21, output: '2.00Z' },
15
{ input: 2e19, output: '20.00E' },
16
{ input: 2e18, output: '2.00E' },
17
{ input: 2e17, output: '200.00P' },
18
{ input: 2e16, output: '20.00P' },
19
{ input: 2e15, output: '2.00P' },
20
{ input: 1e15, output: '1.00P' },
21
{ input: 2e14, output: '200.00T' },
22
{ input: 2e13, output: '20.00T' },
23
{ input: 2e12, output: '2.00T' },
24
{ input: 2e11, output: '200.00G' },
25
{ input: 2e10, output: '20.00G' },
26
{ input: 2e9, output: '2.00G' },
27
{ input: 2e8, output: '200.00M' },
28
{ input: 2e7, output: '20.00M' },
29
{ input: 2e6, output: '2.00M' },
30
{ input: 2e5, output: '200.00k' },
31
{ input: 2e4, output: '20.00k' },
32
{ input: 2e3, output: '2.00k' },
33
{ input: 2e2, output: '200.00' },
34
{ input: 2e1, output: '20.00' },
35
{ input: 2, output: '2.00' },
36
{ input: 2e-1, output: '0.20' },
37
{ input: 2e-2, output: '0.02' },
38
{ input: 2e-3, output: '2.00m' },
39
{ input: 2e-4, output: '0.20m' },
40
{ input: 2e-5, output: '0.02m' },
41
{ input: 2e-6, output: '2.00µ' },
42
{ input: 2e-7, output: '0.20µ' },
43
{ input: 2e-8, output: '0.02µ' },
44
{ input: 2e-9, output: '2.00n' },
45
{ input: 2e-10, output: '0.20n' },
46
{ input: 2e-11, output: '0.02n' },
47
{ input: 2e-12, output: '2.00p' },
48
{ input: 2e-13, output: '0.20p' },
49
{ input: 2e-14, output: '0.02p' },
50
{ input: 2e-15, output: '2.00f' },
51
{ input: 2e-16, output: '0.20f' },
52
{ input: 2e-17, output: '0.02f' },
53
{ input: 2e-18, output: '2.00a' },
54
{ input: 2e-19, output: '0.20a' },
55
{ input: 2e-20, output: '0.02a' },
56
{ input: 1e-21, output: '1.00z' },
57
{ input: 2e-21, output: '2.00z' },
58
{ input: 2e-22, output: '0.20z' },
59
{ input: 2e-23, output: '0.02z' },
60
{ input: 2e-24, output: '2.00y' },
61
{ input: 2e-25, output: '0.20y' },
62
{ input: 2e-26, output: '0.02y' },
64
expect(formatValue(t.input)).toBe(t.output);
67
it('should throw error if no match', () => {
68
expect(() => formatValue(undefined as any)).toThrowError("couldn't format a value, this is a bug");
71
describe('parseValue', () => {
72
it('should parse number properly', () => {
73
expect(parseValue('12.3e')).toEqual(12.3);
75
it('should return 0 if value is NaN and stacked prop is true', () => {
76
expect(parseValue('asd')).toEqual(null);
78
it('should return null if value is NaN and stacked prop is false', () => {
79
expect(parseValue('asd')).toBeNull();
82
describe('Plot options', () => {
83
it('should configure options properly if stacked prop is true', () => {
84
expect(getOptions(true, false)).toMatchObject({
87
lines: { lineWidth: 1, steps: false, fill: true },
92
it('should configure options properly if stacked prop is false', () => {
93
expect(getOptions(false, false)).toMatchObject({
96
lines: { lineWidth: 2, steps: false, fill: false },
101
it('should configure options properly if useLocalTime prop is true', () => {
102
expect(getOptions(true, true)).toMatchObject({
106
showMinorTicks: true,
107
timeBase: 'milliseconds',
112
it('should configure options properly if useLocalTime prop is false', () => {
113
expect(getOptions(false, false)).toMatchObject({
117
showMinorTicks: true,
118
timeBase: 'milliseconds',
122
it('should return proper tooltip html from options', () => {
124
getOptions(true, false).tooltip.content('', 1572128592, 1572128592, {
125
series: { labels: { foo: '1', bar: '2' }, color: '' },
129
<div class="date">1970-01-19 04:42:08 +00:00</div>
131
<span class="detail-swatch" style="background-color: "></span>
132
<span>value: <strong>1572128592</strong></span>
134
<div class="mt-2 mb-1 font-weight-bold">Series:</div>
139
<div class="mb-1"><strong>foo</strong>: 1</div><div class="mb-1"><strong>bar</strong>: 2</div>
143
it('should return proper tooltip html from options with local time', () => {
144
moment.tz.setDefault('America/New_York');
146
getOptions(true, true).tooltip.content('', 1572128592, 1572128592, {
147
series: { labels: { foo: '1', bar: '2' }, color: '' },
150
<div class="date">1970-01-18 23:42:08 -05:00</div>
152
<span class="detail-swatch" style="background-color: "></span>
153
<span>value: <strong>1572128592</strong></span>
155
<div class="mt-2 mb-1 font-weight-bold">Series:</div>
160
<div class="mb-1"><strong>foo</strong>: 1</div><div class="mb-1"><strong>bar</strong>: 2</div>
163
it('should return proper tooltip for exemplar', () => {
165
getOptions(true, false).tooltip.content('', 1572128592, 1572128592, {
166
series: { labels: { foo: '1', bar: '2' }, seriesLabels: { foo: '2', bar: '3' }, color: '' },
169
<div class="date">1970-01-19 04:42:08 +00:00</div>
171
<span class="detail-swatch" style="background-color: "></span>
172
<span>value: <strong>1572128592</strong></span>
174
<div class="mt-2 mb-1 font-weight-bold">Trace exemplar:</div>
179
<div class="mb-1"><strong>foo</strong>: 1</div><div class="mb-1"><strong>bar</strong>: 2</div>
182
<div class="mt-2 mb-1 font-weight-bold">Associated series:</div>
186
<div class="mb-1"><strong>foo</strong>: 2</div><div class="mb-1"><strong>bar</strong>: 3</div>
189
it('should render Plot with proper options', () => {
190
expect(getOptions(true, false)).toEqual({
195
mouseActiveRadius: 100,
197
legend: { show: false },
201
showMinorTicks: true,
202
timeBase: 'milliseconds',
204
yaxis: { tickFormatter: expect.anything() },
205
crosshair: { mode: 'xy', color: '#bbb' },
208
cssClass: 'graph-tooltip',
209
content: expect.anything(),
216
lines: { lineWidth: 1, steps: false, fill: true },