Explore metrics: Fix default datasource bug, add test coverage (#97008)

* select actual default prom data source

* add tests for the function
This commit is contained in:
Brendan O'Handley 2024-11-25 16:36:58 -06:00 committed by GitHub
parent ae27382be5
commit f5958fe0c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 3 deletions

View File

@ -1,8 +1,19 @@
import { AdHocFiltersVariable } from '@grafana/scenes';
import { AdHocFiltersVariable, SceneObjectRef } from '@grafana/scenes';
import { getDatasourceSrv } from '../plugins/datasource_srv';
import { DataTrail } from './DataTrail';
import { getTrailStore } from './TrailStore/TrailStore';
import { MetricDatasourceHelper } from './helpers/MetricDatasourceHelper';
import { limitAdhocProviders } from './utils';
import { getDatasourceForNewTrail, limitAdhocProviders } from './utils';
jest.mock('./TrailStore/TrailStore', () => ({
getTrailStore: jest.fn(),
}));
jest.mock('../plugins/datasource_srv', () => ({
getDatasourceSrv: jest.fn(),
}));
describe('limitAdhocProviders', () => {
let filtersVariable: AdHocFiltersVariable;
@ -60,3 +71,46 @@ describe('limitAdhocProviders', () => {
}
});
});
describe('getDatasourceForNewTrail', () => {
beforeEach(() => {
(getTrailStore as jest.Mock).mockImplementation(() => ({
bookmarks: [],
recent: [],
}));
(getDatasourceSrv as jest.Mock).mockImplementation(() => ({
getList: jest.fn().mockReturnValue([
{ uid: 'prom1', isDefault: true },
{ uid: 'prom2', isDefault: false },
]),
}));
});
it('should return the most recent exploration data source', () => {
const trail = new DataTrail({ key: '1', metric: 'select me', initialDS: 'prom2' });
const trailWithResolveMethod = new SceneObjectRef(trail);
(getTrailStore as jest.Mock).mockImplementation(() => ({
bookmarks: [],
recent: [trailWithResolveMethod],
}));
const result = getDatasourceForNewTrail();
expect(result).toBe('prom2');
});
it('should return the default Prometheus data source if no previous exploration exists', () => {
const result = getDatasourceForNewTrail();
expect(result).toBe('prom1');
});
it('should return the most recently added Prom data source if no default exists and no recent exploration', () => {
(getDatasourceSrv as jest.Mock).mockImplementation(() => ({
getList: jest.fn().mockReturnValue([
{ uid: 'newProm', isDefault: false },
{ uid: 'prom1', isDefault: false },
{ uid: 'prom2', isDefault: false },
]),
}));
const result = getDatasourceForNewTrail();
expect(result).toBe('newProm');
});
});

View File

@ -103,7 +103,9 @@ export function getDatasourceForNewTrail(): string | undefined {
}
const promDatasources = getDatasourceSrv().getList({ type: 'prometheus' });
if (promDatasources.length > 0) {
return promDatasources.find((mds) => mds.uid === config.defaultDatasource)?.uid ?? promDatasources[0].uid;
const defaultDatasource = promDatasources.find((mds) => mds.isDefault);
return defaultDatasource?.uid ?? promDatasources[0].uid;
}
return undefined;
}