Variables: Do not reset description on variable type change (#31933)

This commit is contained in:
Alex Khomenko
2021-03-12 13:49:19 +02:00
committed by GitHub
parent 1716de88b5
commit 8404d54277
3 changed files with 37 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import {
addVariable, addVariable,
changeVariableOrder, changeVariableOrder,
changeVariableProp, changeVariableProp,
changeVariableType,
duplicateVariable, duplicateVariable,
removeVariable, removeVariable,
setCurrentVariableValue, setCurrentVariableValue,
@@ -16,7 +17,7 @@ import {
variableStateNotStarted, variableStateNotStarted,
} from './sharedReducer'; } from './sharedReducer';
import { ConstantVariableModel, QueryVariableModel, VariableHide, VariableOption } from '../types'; 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 { variableAdapters } from '../adapters';
import { createQueryVariableAdapter } from '../query/adapter'; import { createQueryVariableAdapter } from '../query/adapter';
import { initialQueryVariableModelState } from '../query/reducer'; 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<VariablesState>()
.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',
},
});
});
});
}); });

View File

@@ -101,7 +101,7 @@ const sharedReducerSlice = createSlice({
}, },
changeVariableType: (state: VariablesState, action: PayloadAction<VariablePayload<{ newType: VariableType }>>) => { changeVariableType: (state: VariablesState, action: PayloadAction<VariablePayload<{ newType: VariableType }>>) => {
const { id } = action.payload; const { id } = action.payload;
const { label, name, index } = state[id]; const { label, name, index, description } = state[id];
state[id] = { state[id] = {
...cloneDeep(variableAdapters.get(action.payload.data.newType).initialState), ...cloneDeep(variableAdapters.get(action.payload.data.newType).initialState),
@@ -109,6 +109,7 @@ const sharedReducerSlice = createSlice({
label, label,
name, name,
index, index,
description,
}; };
}, },
setCurrentVariableValue: ( setCurrentVariableValue: (

View File

@@ -18,6 +18,7 @@ export interface When<State> {
export interface Then<State> { export interface Then<State> {
thenStateShouldEqual: (state: State) => When<State>; thenStateShouldEqual: (state: State) => When<State>;
thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When<State>; thenStatePredicateShouldEqual: (predicate: (resultingState: State) => boolean) => When<State>;
whenActionIsDispatched: (action: PayloadAction<any> | Action<any>) => Then<State>;
} }
interface ObjectType extends Object { interface ObjectType extends Object {