diff --git a/public/app/features/variables/editor/actions.ts b/public/app/features/variables/editor/actions.ts index 1a7164a1881..150d1e83f54 100644 --- a/public/app/features/variables/editor/actions.ts +++ b/public/app/features/variables/editor/actions.ts @@ -57,11 +57,6 @@ export const onEditorAdd = (identifier: VariableIdentifier): ThunkResult = export const changeVariableName = (identifier: VariableIdentifier, newName: string): ThunkResult => { return (dispatch, getState) => { - const variableInState = getVariable(identifier.id, getState()); - if (newName === variableInState.name) { - return; - } - let errorText = null; if (!newName.match(/^(?!__).*$/)) { errorText = "Template names cannot begin with '__', that's reserved for Grafana's global variables"; @@ -100,6 +95,10 @@ export const completeChangeVariableName = (identifier: VariableIdentifier, newNa getState ) => { const originalVariable = getVariable(identifier.id, getState()); + if (originalVariable.name === newName) { + dispatch(changeVariableNameSucceeded(toVariablePayload(identifier, { newName }))); + return; + } const model = { ...cloneDeep(originalVariable), name: newName, id: newName }; const global = originalVariable.global; const index = originalVariable.index; diff --git a/public/app/features/variables/state/actions.test.ts b/public/app/features/variables/state/actions.test.ts index 17088532ba3..5ca68d71dd4 100644 --- a/public/app/features/variables/state/actions.test.ts +++ b/public/app/features/variables/state/actions.test.ts @@ -316,7 +316,7 @@ describe('shared actions', () => { describe('changeVariableName', () => { describe('when changeVariableName is dispatched with the same name', () => { - it('then no actions are dispatched', () => { + it('then the correct actions are dispatched', () => { const textbox = textboxBuilder() .withId('textbox') .withName('textbox') @@ -333,10 +333,11 @@ describe('shared actions', () => { addVariable(toVariablePayload(constant, { global: false, index: 1, model: constant })) ) .whenActionIsDispatched(changeVariableName(toVariableIdentifier(constant), constant.name), true) - .thenNoActionsWhereDispatched(); + .thenDispatchedActionsShouldEqual( + changeVariableNameSucceeded({ type: 'constant', id: 'constant', data: { newName: 'constant' } }) + ); }); }); - describe('when changeVariableName is dispatched with an unique name', () => { it('then the correct actions are dispatched', () => { const textbox = textboxBuilder()