mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
TimeSrv: Do not update URL multiple times for same, consecutive range updates (#39083)
This commit is contained in:
parent
e78d880db3
commit
92d736665a
@ -1,7 +1,7 @@
|
||||
import { TimeSrv } from './TimeSrv';
|
||||
import { ContextSrvStub } from 'test/specs/helpers';
|
||||
import { isDateTime, dateTime } from '@grafana/data';
|
||||
import { locationService } from '@grafana/runtime';
|
||||
import { HistoryWrapper, locationService, setLocationService } from '@grafana/runtime';
|
||||
|
||||
jest.mock('app/core/core', () => ({
|
||||
appEvents: {
|
||||
@ -12,6 +12,7 @@ jest.mock('app/core/core', () => ({
|
||||
describe('timeSrv', () => {
|
||||
let timeSrv: TimeSrv;
|
||||
let _dashboard: any;
|
||||
const pushSpy = jest.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
_dashboard = {
|
||||
@ -22,6 +23,17 @@ describe('timeSrv', () => {
|
||||
};
|
||||
timeSrv = new TimeSrv(new ContextSrvStub() as any);
|
||||
timeSrv.init(_dashboard);
|
||||
|
||||
beforeEach(() => {
|
||||
pushSpy.mockClear();
|
||||
|
||||
setLocationService(new HistoryWrapper());
|
||||
const origPush = locationService.push;
|
||||
locationService.push = (args: any) => {
|
||||
pushSpy();
|
||||
origPush(args);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
describe('timeRange', () => {
|
||||
@ -219,6 +231,13 @@ describe('timeSrv', () => {
|
||||
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
|
||||
expect(_dashboard.refresh).toBe('10s');
|
||||
});
|
||||
|
||||
it('should update location only once for consecutive calls with the same range', () => {
|
||||
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
|
||||
timeSrv.setTime({ from: 'now-1h', to: 'now-10s' });
|
||||
|
||||
expect(pushSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pauseAutoRefresh', () => {
|
||||
|
@ -285,6 +285,13 @@ export class TimeSrv {
|
||||
const urlRange = this.timeRangeForUrl();
|
||||
const urlParams = locationService.getSearch();
|
||||
|
||||
const from = urlParams.get('from');
|
||||
const to = urlParams.get('to');
|
||||
|
||||
if (from && to && from === urlRange.from.toString() && to === urlRange.to.toString()) {
|
||||
return;
|
||||
}
|
||||
|
||||
urlParams.set('from', urlRange.from.toString());
|
||||
urlParams.set('to', urlRange.to.toString());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user