Fix array display from url

This commit is contained in:
Simon Podlipsky 2018-07-21 23:31:47 +02:00
parent abbb6f933c
commit e67d3df14c
No known key found for this signature in database
GPG Key ID: 725C2BD962B42663
2 changed files with 34 additions and 8 deletions

View File

@ -76,8 +76,8 @@ describe('VariableSrv init', function(this: any) {
{
name: 'apps',
type: type,
current: { text: 'test', value: 'test' },
options: [{ text: 'test', value: 'test' }],
current: { text: 'Test', value: 'test' },
options: [{ text: 'Test', value: 'test' }],
},
];
scenario.urlParams['var-apps'] = 'new';
@ -160,11 +160,11 @@ describe('VariableSrv init', function(this: any) {
name: 'apps',
type: 'query',
multi: true,
current: { text: 'val1', value: 'val1' },
current: { text: 'Val1', value: 'val1' },
options: [
{ text: 'val1', value: 'val1' },
{ text: 'val2', value: 'val2' },
{ text: 'val3', value: 'val3', selected: true },
{ text: 'Val1', value: 'val1' },
{ text: 'Val2', value: 'val2' },
{ text: 'Val3', value: 'val3', selected: true },
],
},
];
@ -176,7 +176,7 @@ describe('VariableSrv init', function(this: any) {
expect(variable.current.value.length).toBe(2);
expect(variable.current.value[0]).toBe('val2');
expect(variable.current.value[1]).toBe('val1');
expect(variable.current.text).toBe('val2 + val1');
expect(variable.current.text).toBe('Val2 + Val1');
expect(variable.options[0].selected).toBe(true);
expect(variable.options[1].selected).toBe(true);
});
@ -187,6 +187,30 @@ describe('VariableSrv init', function(this: any) {
});
});
describeInitScenario(
'when template variable is present in url multiple times and variables have no text',
scenario => {
scenario.setup(() => {
scenario.variables = [
{
name: 'apps',
type: 'query',
multi: true,
},
];
scenario.urlParams['var-apps'] = ['val1', 'val2'];
});
it('should display concatenated values in text', () => {
const variable = ctx.variableSrv.variables[0];
expect(variable.current.value.length).toBe(2);
expect(variable.current.value[0]).toBe('val1');
expect(variable.current.value[1]).toBe('val2');
expect(variable.current.text).toBe('val1 + val2');
});
}
);
describeInitScenario('when template variable is present in url multiple times using key/values', scenario => {
scenario.setup(() => {
scenario.variables = [

View File

@ -236,8 +236,10 @@ export class VariableSrv {
setOptionAsCurrent(variable, option) {
variable.current = _.cloneDeep(option);
if (_.isArray(variable.current.text)) {
if (_.isArray(variable.current.text) && variable.current.text.length > 0) {
variable.current.text = variable.current.text.join(' + ');
} else if (_.isArray(variable.current.value) && variable.current.value[0] !== '$__all') {
variable.current.text = variable.current.value.join(' + ');
}
this.selectOptionsForCurrentValue(variable);