Przeglądaj źródła

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
Tobias Skarhed 7 lat temu
rodzic
commit
2941dff428

+ 14 - 11
public/app/features/plugins/datasource_srv.ts

@@ -91,10 +91,20 @@ export class DatasourceSrv {
 
     _.each(config.datasources, function(value, key) {
       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) {
-          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 {
     }
 
     metricSources.sort(function(a, b) {
-      // these two should always be at the bottom
-      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()) {
+      if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
         return 1;
       }
-      if (a.name.toLowerCase() < b.name.toLowerCase()) {
+      if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
         return -1;
       }
       return 0;

+ 59 - 0
public/app/features/plugins/specs/datasource_srv.jest.ts

@@ -0,0 +1,59 @@
+import config from 'app/core/config';
+import 'app/features/plugins/datasource_srv';
+import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
+
+describe('datasource_srv', function() {
+  let _datasourceSrv = new DatasourceSrv({}, {}, {}, {});
+  let metricSources;
+
+  describe('when loading metric sources', () => {
+    let 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;
+      metricSources = _datasourceSrv.getMetricSources({ skipVariables: true });
+    });
+
+    it('should return a list of sources sorted case insensitively with builtin sources last', () => {
+      expect(metricSources[0].name).toBe('aaa');
+      expect(metricSources[1].name).toBe('BBB');
+      expect(metricSources[2].name).toBe('mmm');
+      expect(metricSources[3].name).toBe('ZZZ');
+      expect(metricSources[4].name).toBe('--Grafana--');
+      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');
+    });
+  });
+});

+ 0 - 64
public/app/features/plugins/specs/datasource_srv_specs.ts

@@ -1,64 +0,0 @@
-import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
-import config from 'app/core/config';
-import 'app/features/plugins/datasource_srv';
-
-describe('datasource_srv', function() {
-  var _datasourceSrv;
-  var metricSources;
-  var templateSrv = {};
-
-  beforeEach(angularMocks.module('grafana.core'));
-  beforeEach(
-    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: {
-        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(function() {
-      config.datasources = unsortedDatasources;
-      metricSources = _datasourceSrv.getMetricSources({ skipVariables: true });
-    });
-
-    it('should return a list of sources sorted case insensitively with builtin sources last', function() {
-      expect(metricSources[0].name).to.be('aaa');
-      expect(metricSources[1].name).to.be('BBB');
-      expect(metricSources[2].name).to.be('mmm');
-      expect(metricSources[3].name).to.be('ZZZ');
-      expect(metricSources[4].name).to.be('--Grafana--');
-      expect(metricSources[5].name).to.be('--Mixed--');
-    });
-  });
-});