mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
toDataFrame: detect field properties using in rather than hasOwnProperty (#23673)
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
import { DataFrameDTO, FieldType, TableData, TimeSeries } from '../types/index';
|
||||
import { dateTime } from '../datetime/moment_wrapper';
|
||||
import { MutableDataFrame } from './MutableDataFrame';
|
||||
import { ArrayDataFrame } from './ArrayDataFrame';
|
||||
|
||||
describe('toDataFrame', () => {
|
||||
it('converts timeseries to series', () => {
|
||||
@@ -73,6 +74,19 @@ describe('toDataFrame', () => {
|
||||
expect(again).toBe(input);
|
||||
});
|
||||
|
||||
it('Make sure ArrayDataFrame is used as a DataFrame without modification', () => {
|
||||
const orig = [
|
||||
{ a: 1, b: 2 },
|
||||
{ a: 3, b: 4 },
|
||||
];
|
||||
const array = new ArrayDataFrame(orig);
|
||||
const frame = toDataFrame(array);
|
||||
expect(frame).toEqual(array);
|
||||
expect(frame instanceof ArrayDataFrame).toEqual(true);
|
||||
expect(frame.length).toEqual(orig.length);
|
||||
expect(frame.fields.map(f => f.name)).toEqual(['a', 'b']);
|
||||
});
|
||||
|
||||
it('throws when table rows is not array', () => {
|
||||
expect(() =>
|
||||
toDataFrame({
|
||||
|
||||
@@ -261,9 +261,9 @@ export const isTableData = (data: any): data is DataFrame => data && data.hasOwn
|
||||
export const isDataFrame = (data: any): data is DataFrame => data && data.hasOwnProperty('fields');
|
||||
|
||||
export const toDataFrame = (data: any): DataFrame => {
|
||||
if (data.hasOwnProperty('fields')) {
|
||||
if ('fields' in data) {
|
||||
// DataFrameDTO does not have length
|
||||
if (data.hasOwnProperty('length')) {
|
||||
if ('length' in data) {
|
||||
return data as DataFrame;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user