From b7d26b12dd681f2eb69abf36eaca0a6b7a5e9c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 24 Jun 2020 08:36:03 +0200 Subject: [PATCH] Templating: Fix searchFilter issue in templating system (#25770) --- .../features/variables/query/actions.test.ts | 26 +++++++++++++++++++ .../app/features/variables/query/actions.ts | 8 +++++- public/test/core/redux/reduxTester.ts | 8 +++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/public/app/features/variables/query/actions.test.ts b/public/app/features/variables/query/actions.test.ts index a83d4d45ccf..2ccb6dc3c38 100644 --- a/public/app/features/variables/query/actions.test.ts +++ b/public/app/features/variables/query/actions.test.ts @@ -188,6 +188,32 @@ describe('query actions', () => { }); }); + describe('when updateQueryVariableOptions is dispatched for variable with searchFilter', () => { + it('then correct actions are dispatched', async () => { + const variable = createVariable({ includeAll: true, useTags: false }); + const optionsMetrics = [createMetric('A'), createMetric('B')]; + + mockDatasourceMetrics(variable, optionsMetrics, []); + + const tester = await reduxTester<{ templating: TemplatingState }>() + .givenRootReducer(getRootReducer()) + .whenActionIsDispatched(addVariable(toVariablePayload(variable, { global: false, index: 0, model: variable }))) + .whenActionIsDispatched(setIdInEditor({ id: variable.id })) + .whenAsyncActionIsDispatched(updateQueryVariableOptions(toVariablePayload(variable), 'search'), true); + + const update = { results: optionsMetrics, templatedRegex: '' }; + + tester.thenDispatchedActionsPredicateShouldEqual(actions => { + const [clearErrors, updateOptions] = actions; + const expectedNumberOfActions = 2; + + expect(clearErrors).toEqual(removeVariableEditorError({ errorProp: 'update' })); + expect(updateOptions).toEqual(updateVariableOptions(toVariablePayload(variable, update))); + return actions.length === expectedNumberOfActions; + }); + }); + }); + describe('when updateQueryVariableOptions is dispatched and fails for variable open in editor', () => { it('then correct actions are dispatched', async () => { const variable = createVariable({ includeAll: true, useTags: false }); diff --git a/public/app/features/variables/query/actions.ts b/public/app/features/variables/query/actions.ts index 48cdf35bb7e..660bb96dee2 100644 --- a/public/app/features/variables/query/actions.ts +++ b/public/app/features/variables/query/actions.ts @@ -52,7 +52,13 @@ export const updateQueryVariableOptions = ( await dispatch(updateVariableTags(toVariablePayload(variableInState, tagResults))); } - await dispatch(validateVariableSelectionState(toVariableIdentifier(variableInState))); + // If we are searching options there is no need to validate selection state + // This condition was added to as validateVariableSelectionState will update the current value of the variable + // So after search and selection the current value is already update so no setValue, refresh & url update is performed + // The if statement below fixes https://github.com/grafana/grafana/issues/25671 + if (!searchFilter) { + await dispatch(validateVariableSelectionState(toVariableIdentifier(variableInState))); + } } catch (err) { console.error(err); if (err.data && err.data.message) { diff --git a/public/test/core/redux/reduxTester.ts b/public/test/core/redux/reduxTester.ts index b70fcf574f1..338ec29e888 100644 --- a/public/test/core/redux/reduxTester.ts +++ b/public/test/core/redux/reduxTester.ts @@ -50,10 +50,16 @@ export const reduxTester = (args?: ReduxTesterArguments): ReduxTes const debug = args?.debug ?? false; let store: EnhancedStore | null = null; + const defaultMiddleware = getDefaultMiddleware({ + thunk: false, + serializableCheck: false, + immutableCheck: false, + } as any); + const givenRootReducer = (rootReducer: Reducer): ReduxTesterWhen => { store = configureStore({ reducer: rootReducer, - middleware: [...getDefaultMiddleware(), logActionsMiddleWare, thunk] as [ThunkMiddleware], + middleware: [...defaultMiddleware, logActionsMiddleWare, thunk] as [ThunkMiddleware], preloadedState, });