Commit 2941dff4 by Tobias Skarhed Committed by Torkel Ödegaard

Karma to Jest: datasource_srv (#12456)

* Karma to Jest: datasource_srv

* Sort function differs between Karma and Jest

* Fix error based on .sort() implementation

* Remove Karma test

* alerting: only log when screenshot been uploaded

* Remove comments

* Karma to Jest: datasource_srv

* Sort function differs between Karma and Jest

* Fix error based on .sort() implementation

* Remove Karma test

* Remove comments

* Remove console.log

* Remove console.log

* Change sorting and add test for default data source
parent 10e86eda
...@@ -91,10 +91,20 @@ export class DatasourceSrv { ...@@ -91,10 +91,20 @@ export class DatasourceSrv {
_.each(config.datasources, function(value, key) { _.each(config.datasources, function(value, key) {
if (value.meta && value.meta.metrics) { if (value.meta && value.meta.metrics) {
metricSources.push({ value: key, name: key, meta: value.meta }); let metricSource = { value: key, name: key, meta: value.meta, sort: key };
//Make sure grafana and mixed are sorted at the bottom
if (value.meta.id === 'grafana') {
metricSource.sort = String.fromCharCode(253);
} else if (value.meta.id === 'mixed') {
metricSource.sort = String.fromCharCode(254);
}
metricSources.push(metricSource);
if (key === config.defaultDatasource) { if (key === config.defaultDatasource) {
metricSources.push({ value: null, name: 'default', meta: value.meta }); metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
metricSources.push(metricSource);
} }
} }
}); });
...@@ -104,17 +114,10 @@ export class DatasourceSrv { ...@@ -104,17 +114,10 @@ export class DatasourceSrv {
} }
metricSources.sort(function(a, b) { metricSources.sort(function(a, b) {
// these two should always be at the bottom if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
if (a.meta.id === 'mixed' || a.meta.id === 'grafana') {
return 1;
}
if (b.meta.id === 'mixed' || b.meta.id === 'grafana') {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1; return 1;
} }
if (a.name.toLowerCase() < b.name.toLowerCase()) { if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
return -1; return -1;
} }
return 0; return 0;
......
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import config from 'app/core/config'; import config from 'app/core/config';
import 'app/features/plugins/datasource_srv'; import 'app/features/plugins/datasource_srv';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
describe('datasource_srv', function() { describe('datasource_srv', function() {
var _datasourceSrv; let _datasourceSrv = new DatasourceSrv({}, {}, {}, {});
var metricSources; let metricSources;
var templateSrv = {};
beforeEach(angularMocks.module('grafana.core')); describe('when loading metric sources', () => {
beforeEach( let unsortedDatasources = {
angularMocks.module(function($provide) {
$provide.value('templateSrv', templateSrv);
})
);
beforeEach(angularMocks.module('grafana.services'));
beforeEach(
angularMocks.inject(function(datasourceSrv) {
_datasourceSrv = datasourceSrv;
})
);
describe('when loading metric sources', function() {
var unsortedDatasources = {
mmm: { mmm: {
type: 'test-db', type: 'test-db',
meta: { metrics: { m: 1 } }, meta: { metrics: { m: 1 } },
...@@ -47,18 +33,27 @@ describe('datasource_srv', function() { ...@@ -47,18 +33,27 @@ describe('datasource_srv', function() {
meta: { metrics: { m: 1 } }, meta: { metrics: { m: 1 } },
}, },
}; };
beforeEach(function() { beforeEach(() => {
config.datasources = unsortedDatasources; config.datasources = unsortedDatasources;
metricSources = _datasourceSrv.getMetricSources({ skipVariables: true }); metricSources = _datasourceSrv.getMetricSources({ skipVariables: true });
}); });
it('should return a list of sources sorted case insensitively with builtin sources last', function() { it('should return a list of sources sorted case insensitively with builtin sources last', () => {
expect(metricSources[0].name).to.be('aaa'); expect(metricSources[0].name).toBe('aaa');
expect(metricSources[1].name).to.be('BBB'); expect(metricSources[1].name).toBe('BBB');
expect(metricSources[2].name).to.be('mmm'); expect(metricSources[2].name).toBe('mmm');
expect(metricSources[3].name).to.be('ZZZ'); expect(metricSources[3].name).toBe('ZZZ');
expect(metricSources[4].name).to.be('--Grafana--'); expect(metricSources[4].name).toBe('--Grafana--');
expect(metricSources[5].name).to.be('--Mixed--'); expect(metricSources[5].name).toBe('--Mixed--');
});
beforeEach(() => {
config.defaultDatasource = 'BBB';
});
it('should set default data source', () => {
expect(metricSources[2].name).toBe('default');
expect(metricSources[2].sort).toBe('BBB');
}); });
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment