grafana/public/app/features/plugins/specs/datasource_srv.test.ts
Torkel Ödegaard 47e51cb6b3
Refactor: Plugin exports & data source / panel types (#16364)
* wip: began work off removing meta and pluginExports from DataSourceApi interface

* WIP: changing how plugins are exports and loaded

* Down the refactoring rabit hole that keeps expanding

* TestData now returns DataSourcePlugin

* Refactoring: fixed app config page loading, type renamings and more typings

* Refactor: Correct casing on DatasourceStatus => DataSourceStatus
2019-04-04 18:30:15 +02:00

108 lines
3.0 KiB
TypeScript

import config from 'app/core/config';
import 'app/features/plugins/datasource_srv';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
import { PluginMeta } from '@grafana/ui/src/types';
// Datasource variable $datasource with current value 'BBB'
const templateSrv = {
variables: [
{
type: 'datasource',
name: 'datasource',
current: {
value: 'BBB',
},
},
],
};
describe('datasource_srv', () => {
const _datasourceSrv = new DatasourceSrv({}, {}, {}, templateSrv);
describe('when loading external datasources', () => {
beforeEach(() => {
config.datasources = {
buildInDs: {
type: 'b',
name: 'buildIn',
meta: { builtIn: true } as PluginMeta,
jsonData: {},
},
nonBuildIn: {
type: 'e',
name: 'external1',
meta: { builtIn: false } as PluginMeta,
jsonData: {},
},
nonExplore: {
type: 'e2',
name: 'external2',
meta: {} as PluginMeta,
jsonData: {},
},
};
});
it('should return list of explore sources', () => {
const externalSources = _datasourceSrv.getExternal();
expect(externalSources.length).toBe(2);
expect(externalSources[0].name).toBe('external1');
expect(externalSources[1].name).toBe('external2');
});
});
describe('when loading metric sources', () => {
let metricSources;
const unsortedDatasources = {
mmm: {
type: 'test-db',
meta: { metrics: { m: 1 } },
},
'--Grafana--': {
type: 'grafana',
meta: { builtIn: true, metrics: { m: 1 }, id: 'grafana' },
},
'--Mixed--': {
type: 'test-db',
meta: { builtIn: true, metrics: { m: 1 }, id: 'mixed' },
},
ZZZ: {
type: 'test-db',
meta: { metrics: { m: 1 } },
},
aaa: {
type: 'test-db',
meta: { metrics: { m: 1 } },
},
BBB: {
type: 'test-db',
meta: { metrics: { m: 1 } },
},
};
beforeEach(() => {
config.datasources = unsortedDatasources as any;
metricSources = _datasourceSrv.getMetricSources({});
config.defaultDatasource = 'BBB';
});
it('should return a list of sources sorted case insensitively with builtin sources last', () => {
expect(metricSources[1].name).toBe('aaa');
expect(metricSources[2].name).toBe('BBB');
expect(metricSources[3].name).toBe('mmm');
expect(metricSources[4].name).toBe('ZZZ');
expect(metricSources[5].name).toBe('--Grafana--');
expect(metricSources[6].name).toBe('--Mixed--');
});
it('should set default data source', () => {
expect(metricSources[3].name).toBe('default');
expect(metricSources[3].sort).toBe('BBB');
});
it('should set default inject the variable datasources', () => {
expect(metricSources[0].name).toBe('$datasource');
expect(metricSources[0].sort).toBe('$datasource');
});
});
});