mirror of https://github.com/grafana/grafana
TemplateVariables: make sure we handle multi/single value with correct data type (#23208)
* Fixed issue with multi value. * Made some refactorings after feedback from Torkel and Hugo. * minor refactorings. * changed so we don't make the current value to array if multi is false. * added snapshot to contain v23. * Fixed so we always use the correct type when setting value for multi/non-multi. * added some more tests. * added tests. * some small adjustments after feedbackpull/23263/head
parent
6402dde646
commit
ac7af7d4c3
@ -0,0 +1,76 @@ |
||||
import { VariableOption } from 'app/features/templating/types'; |
||||
import { alignCurrentWithMulti } from './multiOptions'; |
||||
|
||||
describe('alignCurrentWithMulti', () => { |
||||
describe('when current has string array values and multi is false', () => { |
||||
it('should return current without string arrays', () => { |
||||
const current: VariableOption = { |
||||
value: ['A'], |
||||
text: ['A'], |
||||
selected: false, |
||||
}; |
||||
|
||||
const next = alignCurrentWithMulti(current, false); |
||||
|
||||
expect(next).toEqual({ |
||||
value: 'A', |
||||
text: 'A', |
||||
selected: false, |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when current has string values and multi is true', () => { |
||||
it('should return current with string arrays', () => { |
||||
const current: VariableOption = { |
||||
value: 'A', |
||||
text: 'A', |
||||
selected: false, |
||||
}; |
||||
|
||||
const next = alignCurrentWithMulti(current, true); |
||||
|
||||
expect(next).toEqual({ |
||||
value: ['A'], |
||||
text: ['A'], |
||||
selected: false, |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when current has string values and multi is false', () => { |
||||
it('should return current without string arrays', () => { |
||||
const current: VariableOption = { |
||||
value: 'A', |
||||
text: 'A', |
||||
selected: false, |
||||
}; |
||||
|
||||
const next = alignCurrentWithMulti(current, false); |
||||
|
||||
expect(next).toEqual({ |
||||
value: 'A', |
||||
text: 'A', |
||||
selected: false, |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('when current has string array values and multi is true', () => { |
||||
it('should return current with string arrays', () => { |
||||
const current: VariableOption = { |
||||
value: ['A'], |
||||
text: ['A'], |
||||
selected: false, |
||||
}; |
||||
|
||||
const next = alignCurrentWithMulti(current, true); |
||||
|
||||
expect(next).toEqual({ |
||||
value: ['A'], |
||||
text: ['A'], |
||||
selected: false, |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,44 @@ |
||||
import { VariableOption } from 'app/features/templating/types'; |
||||
|
||||
export const alignCurrentWithMulti = (current: VariableOption, value: boolean): VariableOption => { |
||||
if (!current) { |
||||
return current; |
||||
} |
||||
|
||||
if (value && !Array.isArray(current.value)) { |
||||
return { |
||||
...current, |
||||
value: convertToMulti(current.value), |
||||
text: convertToMulti(current.text), |
||||
}; |
||||
} |
||||
|
||||
if (!value && Array.isArray(current.value)) { |
||||
return { |
||||
...current, |
||||
value: convertToSingle(current.value), |
||||
text: convertToSingle(current.text), |
||||
}; |
||||
} |
||||
|
||||
return current; |
||||
}; |
||||
|
||||
const convertToSingle = (value: string | string[]): string => { |
||||
if (!Array.isArray(value)) { |
||||
return value; |
||||
} |
||||
|
||||
if (value.length > 0) { |
||||
return value[0]; |
||||
} |
||||
|
||||
return ''; |
||||
}; |
||||
|
||||
const convertToMulti = (value: string | string[]): string[] => { |
||||
if (Array.isArray(value)) { |
||||
return value; |
||||
} |
||||
return [value]; |
||||
}; |
||||
Loading…
Reference in new issue