mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fix tests
This commit is contained in:
parent
df8fc3f7cb
commit
94799f568e
@ -113,8 +113,7 @@ export class Gauge extends PureComponent<Props> {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const numeric = value.numeric !== null ? value.numeric : 0;
|
const plotSeries = { data: [[0, value.numeric]] }; // May be NaN
|
||||||
const plotSeries = { data: [[0, numeric]] };
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$.plot(this.canvasElement, [plotSeries], options);
|
$.plot(this.canvasElement, [plotSeries], options);
|
||||||
|
@ -1,36 +1,62 @@
|
|||||||
import { getValueProcessor, getColorFromThreshold } from './valueProcessor';
|
import { getValueProcessor, getColorFromThreshold, ValueProcessor, DisplayValue } from './valueProcessor';
|
||||||
import { getTheme } from '../themes/index';
|
|
||||||
import { GrafanaThemeType } from '../types/theme';
|
|
||||||
import { MappingType, ValueMapping } from '../types/panel';
|
import { MappingType, ValueMapping } from '../types/panel';
|
||||||
|
|
||||||
describe('Process values', () => {
|
function assertSame(input: any, processor: ValueProcessor, match: DisplayValue) {
|
||||||
const basicConversions = [
|
const value = processor(input);
|
||||||
{ value: null, text: '' },
|
expect(value.text).toEqual(match.text);
|
||||||
{ value: undefined, text: '' },
|
if (match.hasOwnProperty('numeric')) {
|
||||||
{ value: 1.23, text: '1.23' },
|
expect(value.numeric).toEqual(match.numeric);
|
||||||
{ value: 1, text: '1' },
|
}
|
||||||
{ value: 'hello', text: 'hello' },
|
}
|
||||||
{ value: {}, text: '[object Object]' },
|
|
||||||
{ value: [], text: '' },
|
|
||||||
{ value: [1, 2, 3], text: '1,2,3' },
|
|
||||||
{ value: ['a', 'b', 'c'], text: 'a,b,c' },
|
|
||||||
];
|
|
||||||
|
|
||||||
it('should return return a string for any input value', () => {
|
describe('Process simple display values', () => {
|
||||||
const processor = getValueProcessor();
|
const processor = getValueProcessor();
|
||||||
basicConversions.forEach(item => {
|
|
||||||
expect(processor(item.value).text).toBe(item.text);
|
it('support null', () => {
|
||||||
});
|
assertSame(null, processor, { text: '', numeric: NaN });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add a suffix to any value', () => {
|
it('support undefined', () => {
|
||||||
const processor = getValueProcessor({
|
assertSame(undefined, processor, { text: '', numeric: NaN });
|
||||||
prefix: 'xxx',
|
});
|
||||||
theme: getTheme(GrafanaThemeType.Dark),
|
|
||||||
});
|
it('support NaN', () => {
|
||||||
basicConversions.forEach(item => {
|
assertSame(NaN, processor, { text: 'NaN', numeric: NaN });
|
||||||
expect(processor(item.value).text).toBe('xxx' + item.text);
|
});
|
||||||
});
|
it('Simple Float', () => {
|
||||||
|
assertSame(1.23456, processor, { text: '1.23456', numeric: 1.23456 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Integer', () => {
|
||||||
|
assertSame(3, processor, { text: '3', numeric: 3 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Text', () => {
|
||||||
|
assertSame('3', processor, { text: '3', numeric: 3 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Simple String', () => {
|
||||||
|
assertSame('hello', processor, { text: 'hello', numeric: NaN });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('empty array', () => {
|
||||||
|
assertSame([], processor, { text: '', numeric: NaN });
|
||||||
|
});
|
||||||
|
it('array of text', () => {
|
||||||
|
assertSame(['a', 'b', 'c'], processor, { text: 'a,b,c', numeric: NaN });
|
||||||
|
});
|
||||||
|
it('array of numbers', () => {
|
||||||
|
assertSame([1, 2, 3], processor, { text: '1,2,3', numeric: NaN });
|
||||||
|
});
|
||||||
|
it('empty object', () => {
|
||||||
|
assertSame({}, processor, { text: '[object Object]', numeric: NaN });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('boolean true', () => {
|
||||||
|
assertSame(true, processor, { text: 'true', numeric: 1 });
|
||||||
|
});
|
||||||
|
it('boolean false', () => {
|
||||||
|
assertSame(false, processor, { text: 'false', numeric: 0 });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import { getColorFromHexRgbOrName } from './namedColorsPalette';
|
|||||||
|
|
||||||
export interface DisplayValue {
|
export interface DisplayValue {
|
||||||
text: string; // How the value should be displayed
|
text: string; // How the value should be displayed
|
||||||
numeric?: number; // the value as a number
|
numeric: number; // Use isNaN to check if it actually is a number
|
||||||
color?: string; // suggested color
|
color?: string; // suggested color
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,18 +37,25 @@ export function getValueProcessor(options?: DisplayValueOptions): ValueProcessor
|
|||||||
let color = options.color;
|
let color = options.color;
|
||||||
|
|
||||||
let text = _.toString(value);
|
let text = _.toString(value);
|
||||||
const numeric = _.toNumber(value);
|
let numeric = toNumber(value);
|
||||||
|
|
||||||
|
let shouldFormat = true;
|
||||||
if (mappings && mappings.length > 0) {
|
if (mappings && mappings.length > 0) {
|
||||||
const mappedValue = getMappedValue(mappings, value);
|
const mappedValue = getMappedValue(mappings, value);
|
||||||
if (mappedValue) {
|
if (mappedValue) {
|
||||||
text = mappedValue.text;
|
text = mappedValue.text;
|
||||||
// TODO? convert the mapped value back to a number?
|
const v = toNumber(text);
|
||||||
|
if (!isNaN(v)) {
|
||||||
|
numeric = v;
|
||||||
|
}
|
||||||
|
shouldFormat = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.isNumber(numeric)) {
|
if (!isNaN(numeric)) {
|
||||||
text = formatFunc(numeric, options.decimals, options.scaledDecimals, options.isUtc);
|
if (shouldFormat) {
|
||||||
|
text = formatFunc(numeric, options.decimals, options.scaledDecimals, options.isUtc);
|
||||||
|
}
|
||||||
if (thresholds && thresholds.length > 0) {
|
if (thresholds && thresholds.length > 0) {
|
||||||
color = getColorFromThreshold(numeric, thresholds, theme);
|
color = getColorFromThreshold(numeric, thresholds, theme);
|
||||||
}
|
}
|
||||||
@ -69,8 +76,22 @@ export function getValueProcessor(options?: DisplayValueOptions): ValueProcessor
|
|||||||
return toStringProcessor;
|
return toStringProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Will return any value as a number or NaN */
|
||||||
|
function toNumber(value: any): number {
|
||||||
|
if (typeof value === 'number') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (value === null || value === undefined || Array.isArray(value)) {
|
||||||
|
return NaN; // lodash calls them 0
|
||||||
|
}
|
||||||
|
if (typeof value === 'boolean') {
|
||||||
|
return value ? 1 : 0;
|
||||||
|
}
|
||||||
|
return _.toNumber(value);
|
||||||
|
}
|
||||||
|
|
||||||
function toStringProcessor(value: any): DisplayValue {
|
function toStringProcessor(value: any): DisplayValue {
|
||||||
return { text: _.toString(value), numeric: _.toNumber(value) };
|
return { text: _.toString(value), numeric: toNumber(value) };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getColorFromThreshold(value: number, thresholds: Threshold[], theme?: GrafanaTheme): string {
|
export function getColorFromThreshold(value: number, thresholds: Threshold[], theme?: GrafanaTheme): string {
|
||||||
|
Loading…
Reference in New Issue
Block a user