Skip to content

Commit

Permalink
Templating: Fix error when response has data with empty array (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanahuckova authored Jun 17, 2024
1 parent 1796eff commit 67b63ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/DataSource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { DataQueryResponse, DataSourceInstanceSettings, toDataFrame } from '@grafana/data';
import { GitHubVariableQuery } from 'types';
import { of } from 'rxjs';
import { GithubDataSource } from 'DataSource';

describe('DataSource', () => {
describe('metricFindQuery', () => {
it('should return empty array if data in response is empty array', async () => {
const ds = new GithubDataSource({} as DataSourceInstanceSettings);
const query = {} as GitHubVariableQuery;

jest.spyOn(ds, 'query').mockReturnValue(of({ data: [] }));
const res = await ds.metricFindQuery(query, {});
expect(res).toEqual([]);
});

it('should return empty array if no data in response', async () => {
const ds = new GithubDataSource({} as DataSourceInstanceSettings);
const query = {} as GitHubVariableQuery;

jest.spyOn(ds, 'query').mockReturnValue(of({} as DataQueryResponse));
const res = await ds.metricFindQuery(query, {});
expect(res).toEqual([]);
});

it('should return array with values if response has data', async () => {
const ds = new GithubDataSource({} as DataSourceInstanceSettings);
const query = { key: 'test', field: 'test' } as GitHubVariableQuery;

jest.spyOn(ds, 'query').mockReturnValue(
of({
data: [
toDataFrame({
fields: [{ name: 'test', values: ['value1', 'value2'] }],
}),
],
} as DataQueryResponse)
);
const res = await ds.metricFindQuery(query, {});
expect(res).toEqual([
{ value: 'value1', text: 'value1' },
{ value: 'value2', text: 'value2' },
]);
});
});
});
4 changes: 2 additions & 2 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export class GithubDataSource extends DataSourceWithBackend<GitHubQuery, GithubD
rangeRaw: options.rangeRaw,
} as DataQueryRequest;
try {
let res = await this.query(request).toPromise();
if (!res || !res.data || res.data.length < 0) {
const res = await this.query(request).toPromise();

Check warning on line 116 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Build, lint and unit tests

'toPromise' is deprecated. Replaced with {@link firstValueFrom } and {@link lastValueFrom }. Will be removed in v8. Details: https://rxjs.dev/deprecations/to-promise
if (!res || !res.data || res.data.length <= 0) {
return [];
}
const view = new DataFrameView(res.data[0] as DataFrame);
Expand Down

0 comments on commit 67b63ed

Please sign in to comment.