mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Add default support (#32625)
* support default * fix * fix * fix better * Refactor: simplifies the logic a bit and changes test description Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>
This commit is contained in:
parent
e244267b7d
commit
c5241aa610
@ -73,4 +73,57 @@ describe('dataSourceVariableReducer', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when createDataSourceOptions is dispatched with default in 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: /default/ });
|
||||||
|
|
||||||
|
reducerTester<VariablesState>()
|
||||||
|
.givenReducer(dataSourceVariableReducer, cloneDeep(initialState))
|
||||||
|
.whenActionIsDispatched(createDataSourceOptions(payload))
|
||||||
|
.thenStateShouldEqual({
|
||||||
|
...initialState,
|
||||||
|
['0']: ({
|
||||||
|
...initialState['0'],
|
||||||
|
options: [{ text: 'default', value: 'default', selected: false }],
|
||||||
|
} as unknown) as DataSourceVariableModel,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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', () => {
|
||||||
|
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: /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 },
|
||||||
|
{ text: 'default', value: 'default', selected: false },
|
||||||
|
],
|
||||||
|
} as unknown) as DataSourceVariableModel,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -6,8 +6,8 @@ import {
|
|||||||
ALL_VARIABLE_TEXT,
|
ALL_VARIABLE_TEXT,
|
||||||
ALL_VARIABLE_VALUE,
|
ALL_VARIABLE_VALUE,
|
||||||
getInstanceState,
|
getInstanceState,
|
||||||
VariablePayload,
|
|
||||||
initialVariablesState,
|
initialVariablesState,
|
||||||
|
VariablePayload,
|
||||||
VariablesState,
|
VariablesState,
|
||||||
} from '../state/types';
|
} from '../state/types';
|
||||||
|
|
||||||
@ -45,12 +45,10 @@ export const dataSourceVariableSlice = createSlice({
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (regex && !regex.exec(source.name)) {
|
if (isValid(source, regex)) {
|
||||||
continue;
|
options.push({ text: source.name, value: source.name, selected: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
options.push({ text: source.name, value: source.name, selected: false });
|
|
||||||
|
|
||||||
if (source.isDefault) {
|
if (source.isDefault) {
|
||||||
options.push({ text: 'default', value: 'default', selected: false });
|
options.push({ text: 'default', value: 'default', selected: false });
|
||||||
}
|
}
|
||||||
@ -69,5 +67,13 @@ export const dataSourceVariableSlice = createSlice({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function isValid(source: DataSourceInstanceSettings, regex?: RegExp) {
|
||||||
|
if (!regex) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return regex && regex.exec(source.name);
|
||||||
|
}
|
||||||
|
|
||||||
export const dataSourceVariableReducer = dataSourceVariableSlice.reducer;
|
export const dataSourceVariableReducer = dataSourceVariableSlice.reducer;
|
||||||
export const { createDataSourceOptions } = dataSourceVariableSlice.actions;
|
export const { createDataSourceOptions } = dataSourceVariableSlice.actions;
|
||||||
|
Loading…
Reference in New Issue
Block a user