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:
Tobias Skarhed 2021-07-09 12:55:56 +02:00 committed by GitHub
parent 663a8935f7
commit 56903582ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -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();

View File

@ -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);