grafana/public/app/features/dimensions/scalar.test.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

97 lines
2.3 KiB
TypeScript

import { ArrayVector, DataFrame, FieldType } from '@grafana/data';
import { getScalarDimension } from './scalar';
import { ScalarDimensionMode } from '.';
describe('scalar dimensions', () => {
it('handles string field', () => {
const values = ['-720', '10', '540', '90', '-210'];
const frame: DataFrame = {
name: 'a',
length: values.length,
fields: [
{
name: 'test',
type: FieldType.number,
values: new ArrayVector(values),
config: {
min: -720,
max: 540,
},
},
],
};
const supplier = getScalarDimension(frame, {
min: -360,
max: 360,
field: 'test',
fixed: 0,
mode: ScalarDimensionMode.Clamped,
});
const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
expect(clamped).toEqual([0, 0, 0, 0, 0]);
});
it('clamps out of range values', () => {
const values = [-720, 10, 540, 90, -210];
const frame: DataFrame = {
name: 'a',
length: values.length,
fields: [
{
name: 'test',
type: FieldType.number,
values: new ArrayVector(values),
config: {
min: -720,
max: 540,
},
},
],
};
const supplier = getScalarDimension(frame, {
min: -360,
max: 360,
field: 'test',
fixed: 0,
mode: ScalarDimensionMode.Clamped,
});
const clamped = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
expect(clamped).toEqual([-360, 10, 360, 90, -210]);
});
it('keeps remainder after divisible by max', () => {
const values = [-721, 10, 540, 390, -210];
const frame: DataFrame = {
name: 'a',
length: values.length,
fields: [
{
name: 'test',
type: FieldType.number,
values: new ArrayVector(values),
config: {
min: -721,
max: 540,
},
},
],
};
const supplier = getScalarDimension(frame, {
min: -360,
max: 360,
field: 'test',
fixed: 0,
mode: ScalarDimensionMode.Mod,
});
const remainder = frame.fields[0].values.toArray().map((k, i) => supplier.get(i));
expect(remainder).toEqual([-1, 10, 180, 30, -210]);
});
});