mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 04:04:00 -06:00
QueryVariable: Default to first option when none is set (#36521)
* TemplateVariables: Default to first option when none is set * Add test * Add tests and rewrite logic
This commit is contained in:
parent
663a8935f7
commit
56903582ce
@ -343,6 +343,58 @@ describe('sharedReducer', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setCurrentVariableValue is dispatched and current.value has no value', () => {
|
||||
it('then the first available option should be selected', () => {
|
||||
const adapter = createQueryVariableAdapter();
|
||||
const { initialState } = getVariableTestContext(adapter, {
|
||||
options: [
|
||||
{ text: 'A', value: 'A', selected: false },
|
||||
{ text: 'B', value: 'B', selected: false },
|
||||
{ text: 'C', value: 'C', selected: false },
|
||||
],
|
||||
});
|
||||
const current = { text: '', value: '', selected: false };
|
||||
const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
|
||||
reducerTester<VariablesState>()
|
||||
.givenReducer(sharedReducer, cloneDeep(initialState))
|
||||
.whenActionIsDispatched(setCurrentVariableValue(payload))
|
||||
.thenStateShouldEqual({
|
||||
...initialState,
|
||||
'0': ({
|
||||
...initialState[0],
|
||||
options: [
|
||||
{ selected: true, text: 'A', value: 'A' },
|
||||
{ selected: false, text: 'B', value: 'B' },
|
||||
{ selected: false, text: 'C', value: 'C' },
|
||||
],
|
||||
current: { selected: true, text: 'A', value: 'A' },
|
||||
} as unknown) as QueryVariableModel,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setCurrentVariableValue is dispatched and current.value has no value and there are no options', () => {
|
||||
it('then no option should be selected', () => {
|
||||
const adapter = createQueryVariableAdapter();
|
||||
const { initialState } = getVariableTestContext(adapter, {
|
||||
options: [],
|
||||
});
|
||||
const current = { text: '', value: '', selected: false };
|
||||
const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
|
||||
reducerTester<VariablesState>()
|
||||
.givenReducer(sharedReducer, cloneDeep(initialState))
|
||||
.whenActionIsDispatched(setCurrentVariableValue(payload))
|
||||
.thenStateShouldEqual({
|
||||
...initialState,
|
||||
'0': ({
|
||||
...initialState[0],
|
||||
options: [],
|
||||
current: { selected: false, text: '', value: '' },
|
||||
} as unknown) as QueryVariableModel,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setCurrentVariableValue is dispatched and current.value is an Array with values containing All value', () => {
|
||||
it('then state should be correct', () => {
|
||||
const adapter = createQueryVariableAdapter();
|
||||
|
@ -121,6 +121,15 @@ const sharedReducerSlice = createSlice({
|
||||
const { option } = action.payload.data;
|
||||
const current = { ...option, text: ensureStringValues(option?.text), value: ensureStringValues(option?.value) };
|
||||
|
||||
// If no value is set, default to the first avilable
|
||||
if (!current.value && instanceState.options.length) {
|
||||
instanceState.options.forEach((option, index) => {
|
||||
option.selected = !Boolean(index);
|
||||
});
|
||||
instanceState.current = instanceState.options[0];
|
||||
return;
|
||||
}
|
||||
|
||||
instanceState.current = current;
|
||||
instanceState.options = instanceState.options.map((option) => {
|
||||
option.value = ensureStringValues(option.value);
|
||||
|
Loading…
Reference in New Issue
Block a user