From d94817146ef7d9dfaf5204934a5dd7f8fd2e0060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Mon, 14 Jun 2021 06:06:34 +0200 Subject: [PATCH] Variables: Hides default data source if missing from regex (#35561) --- .../variables/datasource/reducer.test.ts | 27 ++++++++++++++++++- .../features/variables/datasource/reducer.ts | 16 +++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/public/app/features/variables/datasource/reducer.test.ts b/public/app/features/variables/datasource/reducer.test.ts index 316e354ac18..d0a684fd062 100644 --- a/public/app/features/variables/datasource/reducer.test.ts +++ b/public/app/features/variables/datasource/reducer.test.ts @@ -100,7 +100,7 @@ describe('dataSourceVariableReducer', () => { }); describe('when createDataSourceOptions is dispatched without default in the regex and item is default data source', () => { - it('then the state should include an extra default option', () => { + it('then the state not should include an extra default option', () => { const plugins = getMockPlugins(3); const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p)); sources[1].isDefault = true; @@ -111,6 +111,31 @@ describe('dataSourceVariableReducer', () => { }); const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: /pretty/ }); + reducerTester() + .givenReducer(dataSourceVariableReducer, cloneDeep(initialState)) + .whenActionIsDispatched(createDataSourceOptions(payload)) + .thenStateShouldEqual({ + ...initialState, + ['0']: ({ + ...initialState['0'], + options: [{ text: 'pretty cool plugin-1', value: 'pretty cool plugin-1', selected: false }], + } as unknown) as DataSourceVariableModel, + }); + }); + }); + + describe('when createDataSourceOptions is dispatched without the regex and item is default data source', () => { + it('then the state should include an extra default option', () => { + const plugins = getMockPlugins(3); + const sources: DataSourceInstanceSettings[] = plugins.map((p) => getDataSourceInstanceSetting(p.name, p)); + sources[1].isDefault = true; + + const { initialState } = getVariableTestContext(adapter, { + query: sources[1].meta.id, + includeAll: false, + }); + const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: undefined }); + reducerTester() .givenReducer(dataSourceVariableReducer, cloneDeep(initialState)) .whenActionIsDispatched(createDataSourceOptions(payload)) diff --git a/public/app/features/variables/datasource/reducer.ts b/public/app/features/variables/datasource/reducer.ts index fe26d29ffbf..260b693e239 100644 --- a/public/app/features/variables/datasource/reducer.ts +++ b/public/app/features/variables/datasource/reducer.ts @@ -49,7 +49,7 @@ export const dataSourceVariableSlice = createSlice({ options.push({ text: source.name, value: source.name, selected: false }); } - if (source.isDefault) { + if (isDefault(source, regex)) { options.push({ text: 'default', value: 'default', selected: false }); } } @@ -72,7 +72,19 @@ function isValid(source: DataSourceInstanceSettings, regex?: RegExp) { return true; } - return regex && regex.exec(source.name); + return regex.exec(source.name); +} + +function isDefault(source: DataSourceInstanceSettings, regex?: RegExp) { + if (!source.isDefault) { + return false; + } + + if (!regex) { + return true; + } + + return regex.exec('default'); } export const dataSourceVariableReducer = dataSourceVariableSlice.reducer;