mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DashboardScenes: TemplateSrv should return timeRange from scenes if context found (#90735)
* templateSrv should return timeRange from scenes if context found * add test * prettier * test
This commit is contained in:
parent
d75475ae03
commit
01e161c5b1
@ -30,12 +30,16 @@ variableAdapters.setInit(() => [
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const interpolateMock = jest.fn();
|
const interpolateMock = jest.fn();
|
||||||
|
const timeRangeMock = jest.fn().mockReturnValue({
|
||||||
|
state: { value: { from: dateTime(1594671549254), to: dateTime(1594671549254), raw: { from: '12', to: '14' } } },
|
||||||
|
});
|
||||||
|
|
||||||
jest.mock('@grafana/scenes', () => ({
|
jest.mock('@grafana/scenes', () => ({
|
||||||
...jest.requireActual('@grafana/scenes'),
|
...jest.requireActual('@grafana/scenes'),
|
||||||
sceneGraph: {
|
sceneGraph: {
|
||||||
...jest.requireActual('@grafana/scenes').sceneGraph,
|
...jest.requireActual('@grafana/scenes').sceneGraph,
|
||||||
interpolate: (...args: unknown[]) => interpolateMock(...args),
|
interpolate: (...args: unknown[]) => interpolateMock(...args),
|
||||||
|
getTimeRange: (...args: unknown[]) => timeRangeMock(...args),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -889,5 +893,27 @@ describe('templateSrv', () => {
|
|||||||
expect(podVar.current.value).toEqual(['pA', 'pB']);
|
expect(podVar.current.value).toEqual(['pA', 'pB']);
|
||||||
expect(podVar.current.text).toEqual(['podA', 'podB']);
|
expect(podVar.current.text).toEqual(['podA', 'podB']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should return timeRange from scenes context', () => {
|
||||||
|
window.__grafanaSceneContext = new EmbeddedScene({
|
||||||
|
body: new SceneCanvasText({ text: 'hello' }),
|
||||||
|
});
|
||||||
|
_templateSrv.updateTimeRange({
|
||||||
|
from: dateTime(1594671549254),
|
||||||
|
to: dateTime(1594671549254),
|
||||||
|
raw: { from: '10', to: '10' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const deactivate = window.__grafanaSceneContext.activate();
|
||||||
|
|
||||||
|
expect(_templateSrv.timeRange).not.toBeNull();
|
||||||
|
expect(_templateSrv.timeRange).not.toBeUndefined();
|
||||||
|
expect(_templateSrv.timeRange!.raw).toEqual({ from: '12', to: '14' });
|
||||||
|
expect(timeRangeMock).toHaveBeenCalled();
|
||||||
|
|
||||||
|
deactivate();
|
||||||
|
|
||||||
|
expect(_templateSrv.timeRange!.raw).toEqual({ from: '10', to: '10' });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -51,7 +51,7 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
private regex = variableRegex;
|
private regex = variableRegex;
|
||||||
private index: any = {};
|
private index: any = {};
|
||||||
private grafanaVariables = new Map<string, any>();
|
private grafanaVariables = new Map<string, any>();
|
||||||
private timeRange?: TimeRange | null = null;
|
private _timeRange?: TimeRange | null = null;
|
||||||
private _adhocFiltersDeprecationWarningLogged = new Map<string, boolean>();
|
private _adhocFiltersDeprecationWarningLogged = new Map<string, boolean>();
|
||||||
|
|
||||||
constructor(private dependencies: TemplateSrvDependencies = runtimeDependencies) {
|
constructor(private dependencies: TemplateSrvDependencies = runtimeDependencies) {
|
||||||
@ -60,7 +60,7 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
|
|
||||||
init(variables: any, timeRange?: TimeRange) {
|
init(variables: any, timeRange?: TimeRange) {
|
||||||
this._variables = variables;
|
this._variables = variables;
|
||||||
this.timeRange = timeRange;
|
this._timeRange = timeRange;
|
||||||
this.updateIndex();
|
this.updateIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,6 +83,16 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
return this.dependencies.getVariables();
|
return this.dependencies.getVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get timeRange(): TimeRange | null | undefined {
|
||||||
|
if (window.__grafanaSceneContext && window.__grafanaSceneContext.isActive) {
|
||||||
|
const sceneTimeRange = sceneGraph.getTimeRange(window.__grafanaSceneContext);
|
||||||
|
|
||||||
|
return sceneTimeRange.state.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._timeRange;
|
||||||
|
}
|
||||||
|
|
||||||
updateIndex() {
|
updateIndex() {
|
||||||
const existsOrEmpty = (value: unknown) => value || value === '';
|
const existsOrEmpty = (value: unknown) => value || value === '';
|
||||||
|
|
||||||
@ -110,7 +120,7 @@ export class TemplateSrv implements BaseTemplateSrv {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateTimeRange(timeRange: TimeRange) {
|
updateTimeRange(timeRange: TimeRange) {
|
||||||
this.timeRange = timeRange;
|
this._timeRange = timeRange;
|
||||||
this.updateIndex();
|
this.updateIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user