From 10e74fe9945f1977d2f6a4e5e5b37a28947aa303 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 1 Dec 2021 07:34:27 -0500 Subject: [PATCH] Kiosk: Fixes graph zoom issue clearing kiosk mode (#42501) (#42569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 28b619a40e3c76bb47be23818be61169671d6a92) Co-authored-by: Torkel Ödegaard --- .../dashboard/services/TimeSrv.test.ts | 27 ++++++++++++------- .../features/dashboard/services/TimeSrv.ts | 16 ++++------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index e00e63cc059..24a10364e10 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -1,6 +1,7 @@ import { TimeSrv } from './TimeSrv'; import { ContextSrvStub } from 'test/specs/helpers'; import { dateTime, isDateTime } from '@grafana/data'; +import * as H from 'history'; import { HistoryWrapper, locationService, setLocationService } from '@grafana/runtime'; import { beforeEach } from '../../../../test/lib/common'; @@ -13,7 +14,7 @@ jest.mock('app/core/core', () => ({ describe('timeSrv', () => { let timeSrv: TimeSrv; let _dashboard: any; - const pushSpy = jest.fn(); + let locationUpdates: H.Location[] = []; beforeEach(() => { _dashboard = { @@ -22,18 +23,15 @@ describe('timeSrv', () => { refresh: false, timeRangeUpdated: jest.fn(() => {}), }; + 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); - }; + locationUpdates = []; + const history = new HistoryWrapper(); + history.getHistory().listen((x) => locationUpdates.push(x)); + setLocationService(history); }); }); @@ -237,7 +235,16 @@ describe('timeSrv', () => { timeSrv.setTime({ from: 'now-1h', to: 'now-10s' }); timeSrv.setTime({ from: 'now-1h', to: 'now-10s' }); - expect(pushSpy).toHaveBeenCalledTimes(1); + expect(locationUpdates.length).toBe(1); + }); + + it('should update location so that bool params are preserved', () => { + locationService.partial({ kiosk: true }); + + timeSrv.setTime({ from: 'now-1h', to: 'now-10s' }); + timeSrv.setTime({ from: 'now-1h', to: 'now-10s' }); + + expect(locationUpdates[1].search).toEqual('?kiosk&from=now-1h&to=now-10s'); }); }); diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index 68576ec0a45..db23af30d3c 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -287,22 +287,16 @@ export class TimeSrv { // update url if (fromRouteUpdate !== true) { const urlRange = this.timeRangeForUrl(); - const urlParams = locationService.getSearch(); + const urlParams = locationService.getSearchObject(); - const from = urlParams.get('from'); - const to = urlParams.get('to'); - - if (from && to && from === urlRange.from.toString() && to === urlRange.to.toString()) { + if (urlParams.from === urlRange.from.toString() && urlParams.to === urlRange.to.toString()) { return; } - urlParams.set('from', urlRange.from.toString()); - urlParams.set('to', urlRange.to.toString()); + urlParams.from = urlRange.from.toString(); + urlParams.to = urlRange.to.toString(); - locationService.push({ - ...locationService.getLocation(), - search: urlParams.toString(), - }); + locationService.partial(urlParams); } this.refreshDashboard();