Variables: Hides default data source if missing from regex (#35561)

This commit is contained in:
Hugo Häggmark
2021-06-14 06:06:34 +02:00
committed by GitHub
parent 056e17216e
commit d94817146e
2 changed files with 40 additions and 3 deletions

View File

@@ -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<VariablesState>()
.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<DataSourceVariableModel>(adapter, {
query: sources[1].meta.id,
includeAll: false,
});
const payload = toVariablePayload({ id: '0', type: 'datasource' }, { sources, regex: undefined });
reducerTester<VariablesState>()
.givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
.whenActionIsDispatched(createDataSourceOptions(payload))

View File

@@ -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;