TimeSrv: Do not update URL multiple times for same, consecutive range updates (#39083)

This commit is contained in:
Dominik Prokop 2021-09-10 13:05:34 +02:00 committed by GitHub
parent e78d880db3
commit 92d736665a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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', () => {

View File

@ -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());