From 927b39fa63c0ba8f4132d8f205da2cf52c24db8b Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 16 Feb 2017 10:41:47 +0100 Subject: [PATCH] templating: adds test for sorting template values --- .../app/features/templating/query_variable.ts | 11 +++--- .../templating/specs/query_variable_specs.ts | 39 +++++++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/public/app/features/templating/query_variable.ts b/public/app/features/templating/query_variable.ts index 7dfcb854194..0a5eeae3629 100644 --- a/public/app/features/templating/query_variable.ts +++ b/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); + } }); } diff --git a/public/app/features/templating/specs/query_variable_specs.ts b/public/app/features/templating/specs/query_variable_specs.ts index 591362e0d84..5b886eb442a 100644 --- a/public/app/features/templating/specs/query_variable_specs.ts +++ b/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'); + }); + }); + }); });