diff --git a/public/app/features/templating/query_variable.ts b/public/app/features/templating/query_variable.ts index da3b71c0254..a40aade24ef 100644 --- a/public/app/features/templating/query_variable.ts +++ b/public/app/features/templating/query_variable.ts @@ -140,8 +140,13 @@ export class QueryVariable implements Variable { } for (i = 0; i < metricNames.length; i++) { var item = metricNames[i]; - var value = item.value || item.text; - var text = item.text || item.value; + var text = item.text === undefined || item.text === null + ? item.value + : item.text; + + var value = item.value === undefined || item.value === null + ? item.text + : item.value; if (_.isNumber(value)) { value = value.toString(); @@ -178,7 +183,7 @@ export class QueryVariable implements Variable { if (sortType === 1) { options = _.sortBy(options, 'text'); } else if (sortType === 2) { - options = _.sortBy(options, function(opt) { + options = _.sortBy(options, (opt) => { var matches = opt.text.match(/.*?(\d+).*/); if (!matches || matches.length < 2) { return -1; diff --git a/public/app/features/templating/specs/query_variable_specs.ts b/public/app/features/templating/specs/query_variable_specs.ts index 5d59ec53d64..4d3539fb7c6 100644 --- a/public/app/features/templating/specs/query_variable_specs.ts +++ b/public/app/features/templating/specs/query_variable_specs.ts @@ -51,20 +51,23 @@ describe('QueryVariable', () => { 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}, + {text: null, value: '7'}, + {text: undefined, value: '8'}, + {text: 9, value: null}, + {text: 10, value: undefined}, + {text: '', value: undefined}, + {text: undefined, value: ''}, ]; var result = variable.metricNamesToVariableValues(input); it('should return in same order', () => { var i = 0; + + expect(result.length).to.be(11); expect(result[i++].text).to.be(''); expect(result[i++].text).to.be('0'); expect(result[i++].text).to.be('1');