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:
Dessen Xu 2021-05-05 18:50:31 +08:00 committed by GitHub
parent e244267b7d
commit c5241aa610
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 5 deletions

View File

@ -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,
});
});
});
});

View File

@ -6,8 +6,8 @@ import {
ALL_VARIABLE_TEXT,
ALL_VARIABLE_VALUE,
getInstanceState,
VariablePayload,
initialVariablesState,
VariablePayload,
VariablesState,
} from '../state/types';
@ -45,12 +45,10 @@ export const dataSourceVariableSlice = createSlice({
continue;
}
if (regex && !regex.exec(source.name)) {
continue;
if (isValid(source, regex)) {
options.push({ text: source.name, value: source.name, selected: false });
}
options.push({ text: source.name, value: source.name, selected: false });
if (source.isDefault) {
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 { createDataSourceOptions } = dataSourceVariableSlice.actions;