From 60f79a35483675e7e65daff6824744b72400fd06 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Fri, 11 Jun 2021 16:20:18 +0100 Subject: [PATCH] Dashboard: handle the case where `refresh_intervals` could be null (#35511) * handle the case where refresh_intervals === null + add unit test * have a clean _dashboard for each test * modify check to see if refresh_intervals is an array --- .../dashboard/services/TimeSrv.test.ts | 25 +++++++++++++------ .../features/dashboard/services/TimeSrv.ts | 4 ++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index df8c2e296e8..cde22696f41 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -11,17 +11,17 @@ jest.mock('app/core/core', () => ({ describe('timeSrv', () => { let timeSrv: TimeSrv; - - const _dashboard: any = { - time: { from: 'now-6h', to: 'now' }, - getTimezone: jest.fn(() => 'browser'), - timeRangeUpdated: jest.fn(() => {}), - }; + let _dashboard: any; beforeEach(() => { + _dashboard = { + time: { from: 'now-6h', to: 'now' }, + getTimezone: jest.fn(() => 'browser'), + refresh: false, + timeRangeUpdated: jest.fn(() => {}), + }; timeSrv = new TimeSrv(new ContextSrvStub() as any); timeSrv.init(_dashboard); - _dashboard.refresh = false; }); describe('timeRange', () => { @@ -130,6 +130,17 @@ describe('timeSrv', () => { expect(timeSrv.time.to).toEqual('now'); }); + it('should handle refresh_intervals=null when refresh is enabled', () => { + locationService.push('/d/id?refresh=30s'); + + timeSrv = new TimeSrv(new ContextSrvStub() as any); + + _dashboard.timepicker = { + refresh_intervals: null, + }; + expect(() => timeSrv.init(_dashboard)).not.toThrow(); + }); + describe('data point windowing', () => { it('handles time window specfied as interval string', () => { locationService.push('/d/id?time=1410337645000&time.window=10s'); diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index 2bb6389f313..2bb7346138d 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -156,7 +156,9 @@ export class TimeSrv { this.refresh = getRefreshFromUrl({ params: paramsJSON, currentRefresh: this.refresh, - refreshIntervals: this.dashboard?.timepicker?.refresh_intervals, + refreshIntervals: Array.isArray(this.dashboard?.timepicker?.refresh_intervals) + ? this.dashboard?.timepicker?.refresh_intervals + : undefined, isAllowedIntervalFn: this.contextSrv.isAllowedInterval, minRefreshInterval: config.minRefreshInterval, });