GraphNG: Handle infinite numbers as nulls when converting to plot array (#35638)

This commit is contained in:
Dominik Prokop
2021-06-15 22:00:09 +02:00
committed by GitHub
parent 368637c35a
commit 60f5865ee2
5 changed files with 152 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
import { preparePlotConfigBuilder, preparePlotFrame } from './utils';
import { prepareGraphableFrames, preparePlotConfigBuilder, preparePlotFrame } from './utils';
import {
createTheme,
DefaultTimeZone,
@@ -140,4 +140,56 @@ describe('BarChart utils', () => {
).toMatchSnapshot();
});
});
describe('prepareGraphableFrames', () => {
it('will warn when there is no data in the response', () => {
const result = prepareGraphableFrames([]);
expect(result.warn).toEqual('No data in response');
});
it('will warn when there is no string field in the response', () => {
const df = new MutableDataFrame({
fields: [
{ name: 'a', type: FieldType.time, values: [1, 2, 3, 4, 5] },
{ name: 'value', values: [1, 2, 3, 4, 5] },
],
});
const result = prepareGraphableFrames([df]);
expect(result.warn).toEqual('Bar charts requires a string field');
expect(result.frames).toBeUndefined();
});
it('will warn when there are no numeric fields in the response', () => {
const df = new MutableDataFrame({
fields: [
{ name: 'a', type: FieldType.string, values: ['a', 'b', 'c', 'd', 'e'] },
{ name: 'value', type: FieldType.boolean, values: [true, true, true, true, true] },
],
});
const result = prepareGraphableFrames([df]);
expect(result.warn).toEqual('No numeric fields found');
expect(result.frames).toBeUndefined();
});
it('will convert NaN and Infinty to nulls', () => {
const df = new MutableDataFrame({
fields: [
{ name: 'a', type: FieldType.string, values: ['a', 'b', 'c', 'd', 'e'] },
{ name: 'value', values: [-10, NaN, 10, -Infinity, +Infinity] },
],
});
const result = prepareGraphableFrames([df]);
const field = result.frames![0].fields[1];
expect(field!.values.toArray()).toMatchInlineSnapshot(`
Array [
-10,
null,
10,
null,
null,
]
`);
});
});
});