mirror of https://github.com/grafana/grafana
Typed variables pt2: Use type-accurate mock variables in tests (#52987)
* wip * make diff easier to read * Update template_srv getVariables to return new TypedVariableModel * update VariableType to use the type from TypedVariableModel * tidy things up * Chore: Use type-accurate mock variables in tests * make createBaseVariableModel take the variable typepull/52983/head
parent
718620c197
commit
1a683e53c5
@ -0,0 +1,83 @@ |
||||
import { |
||||
BaseVariableModel, |
||||
ConstantVariableModel, |
||||
CustomVariableModel, |
||||
LoadingState, |
||||
QueryVariableModel, |
||||
VariableHide, |
||||
VariableOption, |
||||
VariableRefresh, |
||||
VariableSort, |
||||
VariableType, |
||||
} from '@grafana/data'; |
||||
|
||||
function createBaseVariableModel<T extends VariableType>(type: T): BaseVariableModel & { type: T } { |
||||
return { |
||||
name: 'myVariableName', |
||||
id: '0', |
||||
rootStateKey: 'key', |
||||
global: false, |
||||
hide: VariableHide.dontHide, |
||||
skipUrlSync: false, |
||||
index: 0, |
||||
state: LoadingState.NotStarted, |
||||
error: null, |
||||
description: null, |
||||
type, |
||||
}; |
||||
} |
||||
|
||||
export function createVariableOption(value: string, rest: Partial<Omit<VariableOption, 'value'>> = {}): VariableOption { |
||||
return { |
||||
value, |
||||
text: rest.text ?? value, |
||||
selected: rest.selected ?? false, |
||||
}; |
||||
} |
||||
|
||||
export function createQueryVariable(input: Partial<QueryVariableModel> = {}): QueryVariableModel { |
||||
return { |
||||
...createBaseVariableModel('query'), |
||||
label: 'DefaultLabel', |
||||
datasource: { |
||||
uid: 'abc-123', |
||||
type: 'prometheus', |
||||
}, |
||||
definition: 'def', |
||||
sort: VariableSort.alphabeticalAsc, |
||||
query: 'label_values(job)', |
||||
regex: '', |
||||
refresh: VariableRefresh.onDashboardLoad, |
||||
multi: false, |
||||
includeAll: false, |
||||
current: createVariableOption('prom-prod', { text: 'Prometheus (main)', selected: true }), |
||||
options: [ |
||||
createVariableOption('prom-prod', { text: 'Prometheus (main)', selected: true }), |
||||
createVariableOption('prom-dev'), |
||||
], |
||||
...input, |
||||
}; |
||||
} |
||||
|
||||
export function createConstantVariable(input: Partial<ConstantVariableModel>): ConstantVariableModel { |
||||
return { |
||||
...createBaseVariableModel('constant'), |
||||
query: '', |
||||
current: createVariableOption('database'), |
||||
options: [], |
||||
hide: VariableHide.hideVariable, |
||||
...input, |
||||
}; |
||||
} |
||||
|
||||
export function createCustomVariable(input: Partial<CustomVariableModel>): CustomVariableModel { |
||||
return { |
||||
...createBaseVariableModel('custom'), |
||||
multi: false, |
||||
includeAll: false, |
||||
current: createVariableOption('prom-prod', { text: 'Prometheus (main)', selected: true }), |
||||
options: [], |
||||
query: '', |
||||
...input, |
||||
}; |
||||
} |
Loading…
Reference in new issue