1import { ArrayVector, DataFrame, FieldType } from '@grafana/data';
2import { ScalarDimensionMode } from '.';
3import { getScalarDimension } from './scalar';
4
5describe('scalar dimensions', () => {
6  it('handles string field', () => {
7    const values = ['-720', '10', '540', '90', '-210'];
8    const frame: DataFrame = {
9      name: 'a',
10      length: values.length,
11      fields: [
12        {
13          name: 'test',
14          type: FieldType.number,
15          values: new ArrayVector(values),
16          config: {
17            min: -720,
18            max: 540,
19          },
20        },
21      ],
22    };
23
24    const supplier = getScalarDimension(frame, {
25      min: -360,
26      max: 360,
27      field: 'test',
28      fixed: 0,
29      mode: ScalarDimensionMode.Clamped,
30    });
31
32    const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
33    expect(clamped).toEqual([0, 0, 0, 0, 0]);
34  });
35  it('clamps out of range values', () => {
36    const values = [-720, 10, 540, 90, -210];
37    const frame: DataFrame = {
38      name: 'a',
39      length: values.length,
40      fields: [
41        {
42          name: 'test',
43          type: FieldType.number,
44          values: new ArrayVector(values),
45          config: {
46            min: -720,
47            max: 540,
48          },
49        },
50      ],
51    };
52
53    const supplier = getScalarDimension(frame, {
54      min: -360,
55      max: 360,
56      field: 'test',
57      fixed: 0,
58      mode: ScalarDimensionMode.Clamped,
59    });
60
61    const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
62    expect(clamped).toEqual([-360, 10, 360, 90, -210]);
63  });
64
65  it('keeps remainder after divisible by max', () => {
66    const values = [-721, 10, 540, 390, -210];
67    const frame: DataFrame = {
68      name: 'a',
69      length: values.length,
70      fields: [
71        {
72          name: 'test',
73          type: FieldType.number,
74          values: new ArrayVector(values),
75          config: {
76            min: -721,
77            max: 540,
78          },
79        },
80      ],
81    };
82
83    const supplier = getScalarDimension(frame, {
84      min: -360,
85      max: 360,
86      field: 'test',
87      fixed: 0,
88      mode: ScalarDimensionMode.Mod,
89    });
90
91    const remainder = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
92    expect(remainder).toEqual([-1, 10, 180, 30, -210]);
93  });
94});
95