Browse Source

templating: adds test for sorting template values

bergquist 8 years ago
parent
commit
927b39fa63

+ 5 - 6
public/app/features/templating/query_variable.ts

@@ -138,7 +138,6 @@ export class QueryVariable implements Variable {
     if (this.regex) {
       regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
     }
-
     for (i = 0; i < metricNames.length; i++) {
       var item = metricNames[i];
       var value = item.value || item.text;
@@ -181,11 +180,11 @@ export class QueryVariable implements Variable {
     } else if (sortType === 2) {
       options = _.sortBy(options, function(opt) {
         var matches = opt.text.match(/.*?(\d+).*/);
-  if (!matches) {
-    return 0;
-  } else {
-    return parseInt(matches[1], 10);
-  }
+        if (!matches || matches.length < 2) {
+          return 0;
+        } else {
+          return parseInt(matches[1], 10);
+        }
       });
     }
 

+ 36 - 3
public/app/features/templating/specs/query_variable_specs.ts

@@ -2,11 +2,11 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/co
 
 import {QueryVariable} from '../query_variable';
 
-describe('QueryVariable', function() {
+describe('QueryVariable', () => {
 
-  describe('when creating from model', function() {
+  describe('when creating from model', () => {
 
-    it('should set defaults', function() {
+    it('should set defaults', () => {
       var variable = new QueryVariable({}, null, null, null, null);
       expect(variable.datasource).to.be(null);
       expect(variable.refresh).to.be(0);
@@ -42,5 +42,38 @@ describe('QueryVariable', function() {
       expect(model.options.length).to.be(0);
     });
   });
+
+  describe('can convert and sort metric names',() => {
+    var variable = new QueryVariable({}, null, null, null, null);
+    variable.sort = 51;
+
+    describe('can sort a mixed array of metric variables', () => {
+      var input = [
+        {text: '0', value: '0'},
+        {text: '1', value: '1'},
+        {text: '', value: ''},
+        {text: null, value: 3},
+        {text: undefined, value: 4},
+        {text: '5', value: null},
+        {text: '6', value: undefined},
+        {text: null, value: '3'},
+        {text: undefined, value: '4'},
+        {text: 5, value: null},
+        {text: 6, value: undefined},
+      ];
+
+      var result = variable.metricNamesToVariableValues(input);
+
+      it('should return in same order', () => {
+        expect(result[0].text).to.be('0');
+        expect(result[1].text).to.be('1');
+        expect(result[2].text).to.be('');
+        expect(result[3].text).to.be('3');
+        expect(result[4].text).to.be('4');
+        expect(result[5].text).to.be('5');
+        expect(result[6].text).to.be('6');
+      });
+    });
+  });
 });