From 8404d54277a20ebc0de2a13ca620d5bfa74fb5ec Mon Sep 17 00:00:00 2001 From: Alex Khomenko Date: Fri, 12 Mar 2021 13:49:19 +0200 Subject: [PATCH] Variables: Do not reset description on variable type change (#31933) --- .../variables/state/sharedReducer.test.ts | 35 ++++++++++++++++++- .../features/variables/state/sharedReducer.ts | 3 +- public/test/core/redux/reducerTester.ts | 1 + 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/public/app/features/variables/state/sharedReducer.test.ts b/public/app/features/variables/state/sharedReducer.test.ts index 23f2b77cfec..2fabef18488 100644 --- a/public/app/features/variables/state/sharedReducer.test.ts +++ b/public/app/features/variables/state/sharedReducer.test.ts @@ -6,6 +6,7 @@ import { addVariable, changeVariableOrder, changeVariableProp, + changeVariableType, duplicateVariable, removeVariable, setCurrentVariableValue, @@ -16,7 +17,7 @@ import { variableStateNotStarted, } from './sharedReducer'; import { ConstantVariableModel, QueryVariableModel, VariableHide, VariableOption } from '../types'; -import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, toVariablePayload } from './types'; +import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, toVariablePayload, VariableIdentifier } from './types'; import { variableAdapters } from '../adapters'; import { createQueryVariableAdapter } from '../query/adapter'; import { initialQueryVariableModelState } from '../query/reducer'; @@ -489,4 +490,36 @@ describe('sharedReducer', () => { }); }); }); + + describe('when changeVariableType is dispatched', () => { + it('then state should be correct', () => { + const queryAdapter = createQueryVariableAdapter(); + const { initialState: queryAdapterState } = getVariableTestContext(queryAdapter); + const constantAdapter = createConstantVariableAdapter(); + const { initialState: constantAdapterState } = getVariableTestContext(constantAdapter); + const newType = 'constant' as VariableType; + const identifier: VariableIdentifier = { id: '0', type: 'query' }; + const payload = toVariablePayload(identifier, { newType }); + reducerTester() + .givenReducer(sharedReducer, cloneDeep(queryAdapterState)) + .whenActionIsDispatched(changeVariableNameSucceeded(toVariablePayload(identifier, { newName: 'test' }))) + .whenActionIsDispatched( + changeVariableProp(toVariablePayload(identifier, { propName: 'description', propValue: 'new description' })) + ) + .whenActionIsDispatched( + changeVariableProp(toVariablePayload(identifier, { propName: 'label', propValue: 'new label' })) + ) + .whenActionIsDispatched(changeVariableType(payload)) + .thenStateShouldEqual({ + ...constantAdapterState, + '0': { + ...constantAdapterState[0], + name: 'test', + description: 'new description', + label: 'new label', + type: 'constant', + }, + }); + }); + }); }); diff --git a/public/app/features/variables/state/sharedReducer.ts b/public/app/features/variables/state/sharedReducer.ts index e2928f71e5d..a31eafe2a36 100644 --- a/public/app/features/variables/state/sharedReducer.ts +++ b/public/app/features/variables/state/sharedReducer.ts @@ -101,7 +101,7 @@ const sharedReducerSlice = createSlice({ }, changeVariableType: (state: VariablesState, action: PayloadAction>) => { const { id } = action.payload; - const { label, name, index } = state[id]; + const { label, name, index, description } = state[id]; state[id] = { ...cloneDeep(variableAdapters.get(action.payload.data.newType).initialState), @@ -109,6 +109,7 @@ const sharedReducerSlice = createSlice({ label, name, index, + description, }; }, setCurrentVariableValue: ( diff --git a/public/test/core/redux/reducerTester.ts b/public/test/core/redux/reducerTester.ts index c61788c1c34..a64b6099565 100644 --- a/public/test/core/redux/reducerTester.ts +++ b/public/test/core/redux/reducerTester.ts @@ -18,6 +18,7 @@ export interface When { export interface Then { thenStateShouldEqual: (state: State) => When; thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When; + whenActionIsDispatched: (action: PayloadAction | Action) => Then; } interface ObjectType extends Object {