Prometheus: Fixes crash in the new options component when range was undefined (#45921)

* Prometheus: Fixes crash in the new options component when range was undefined

* Simplified conditions
pull/45964/head
Torkel Ödegaard 4 years ago committed by GitHub
parent 2c90dcf3c0
commit 9b6552c7b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderOptions.test.tsx
  2. 12
      public/app/plugins/datasource/prometheus/querybuilder/types.ts

@ -59,13 +59,19 @@ describe('PromQueryBuilderOptions', () => {
legendFormat: '{{label_name}}',
});
});
it('Handle defaults with undefined range', async () => {
setup(getQueryWithDefaults({ refId: 'A', expr: '', range: undefined, instant: true }, CoreApp.Dashboard));
expect(screen.getByText('Type: Instant')).toBeInTheDocument();
});
});
function setup(queryOverrides: Partial<PromQuery> = {}) {
const props = {
query: {
...getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.PanelEditor),
queryOverrides,
...queryOverrides,
},
onRunQuery: jest.fn(),
onChange: jest.fn(),

@ -131,14 +131,14 @@ export function getQueryWithDefaults(query: PromQuery, app: CoreApp | undefined)
result = { ...result, expr: '', legendFormat: LegendFormatMode.Auto };
}
// Default to range query
if (query.range == null) {
if (query.range == null && query.instant == null) {
// Default to range query
result = { ...result, range: true };
}
// In explore we default to both instant & range
if (query.instant == null && app === CoreApp.Explore) {
result = { ...result, instant: true };
// In explore we default to both instant & range
if (app === CoreApp.Explore) {
result.instant = true;
}
}
return result;

Loading…
Cancel
Save