From b8a5f38d2a9c8b84551fed2959ee200d08ef420b Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Mon, 19 Jul 2021 09:16:36 +0100 Subject: [PATCH] Angular cleanup: Don't inject in link_srv (#36849) * Angular cleanup: Don't inject in link_srv * link_srv: Fix unit tests * kick drone --- .../panel/panellinks/linkSuppliers.test.ts | 22 ++++++++++-------- .../app/features/panel/panellinks/link_srv.ts | 16 ++++++------- .../panel/panellinks/specs/link_srv.test.ts | 11 +++++++-- .../loki/configuration/DebugSection.test.tsx | 23 ++++++++++--------- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/public/app/features/panel/panellinks/linkSuppliers.test.ts b/public/app/features/panel/panellinks/linkSuppliers.test.ts index e5d0611fde7..69f44e4af14 100644 --- a/public/app/features/panel/panellinks/linkSuppliers.test.ts +++ b/public/app/features/panel/panellinks/linkSuppliers.test.ts @@ -2,21 +2,23 @@ import { getFieldLinksSupplier } from './linkSuppliers'; import { applyFieldOverrides, createTheme, DataFrameView, dateTime, FieldDisplay, toDataFrame } from '@grafana/data'; import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from './link_srv'; import { TemplateSrv } from '../../templating/template_srv'; -import { TimeSrv } from '../../dashboard/services/TimeSrv'; + +// We do not need more here and TimeSrv is hard to setup fully. +jest.mock('app/features/dashboard/services/TimeSrv', () => ({ + getTimeSrv: () => ({ + timeRangeForUrl() { + const from = dateTime().subtract(1, 'h'); + const to = dateTime(); + return { from, to, raw: { from, to } }; + }, + }), +})); describe('getFieldLinksSupplier', () => { let originalLinkSrv: LinkService; let templateSrv = new TemplateSrv(); beforeAll(() => { - // We do not need more here and TimeSrv is hard to setup fully. - const timeSrvMock: TimeSrv = { - timeRangeForUrl() { - const from = dateTime().subtract(1, 'h'); - const to = dateTime(); - return { from, to, raw: { from, to } }; - }, - } as any; - const linkService = new LinkSrv(new TemplateSrv(), timeSrvMock); + const linkService = new LinkSrv(); originalLinkSrv = getLinkSrv(); setLinkSrv(linkService); diff --git a/public/app/features/panel/panellinks/link_srv.ts b/public/app/features/panel/panellinks/link_srv.ts index 82e9045df55..0858d78e2c9 100644 --- a/public/app/features/panel/panellinks/link_srv.ts +++ b/public/app/features/panel/panellinks/link_srv.ts @@ -1,6 +1,6 @@ import { chain } from 'lodash'; -import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; -import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; +import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; +import { getTemplateSrv } from '@grafana/runtime'; import coreModule from 'app/core/core_module'; import { getConfig } from 'app/core/config'; import { @@ -263,15 +263,14 @@ export interface LinkService { } export class LinkSrv implements LinkService { - /** @ngInject */ - constructor(private templateSrv: TemplateSrv, private timeSrv: TimeSrv) {} + constructor() {} getLinkUrl(link: any) { - let url = locationUtil.assureBaseUrl(this.templateSrv.replace(link.url || '')); + let url = locationUtil.assureBaseUrl(getTemplateSrv().replace(link.url || '')); let params: { [key: string]: any } = {}; if (link.keepTime) { - const range = this.timeSrv.timeRangeForUrl(); + const range = getTimeSrv().timeRangeForUrl(); params['from'] = range.from; params['to'] = range.to; } @@ -288,10 +287,11 @@ export class LinkSrv implements LinkService { } getAnchorInfo(link: any) { + const templateSrv = getTemplateSrv(); const info: any = {}; info.href = this.getLinkUrl(link); - info.title = this.templateSrv.replace(link.title || ''); - info.tooltip = this.templateSrv.replace(link.tooltip || ''); + info.title = templateSrv.replace(link.title || ''); + info.tooltip = templateSrv.replace(link.tooltip || ''); return info; } diff --git a/public/app/features/panel/panellinks/specs/link_srv.test.ts b/public/app/features/panel/panellinks/specs/link_srv.test.ts index ba970c9bfd6..b32b5b62b15 100644 --- a/public/app/features/panel/panellinks/specs/link_srv.test.ts +++ b/public/app/features/panel/panellinks/specs/link_srv.test.ts @@ -1,7 +1,7 @@ import { FieldType, locationUtil, toDataFrame, VariableOrigin } from '@grafana/data'; import { setTemplateSrv } from '@grafana/runtime'; import { getDataFrameVars, LinkSrv } from '../link_srv'; -import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; +import { getTimeSrv, setTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { TemplateSrv } from 'app/features/templating/template_srv'; import { variableAdapters } from 'app/features/variables/adapters'; import { createQueryVariableAdapter } from 'app/features/variables/query/adapter'; @@ -17,6 +17,7 @@ jest.mock('app/core/core', () => ({ describe('linkSrv', () => { let linkSrv: LinkSrv; let templateSrv: TemplateSrv; + let originalTimeService: TimeSrv; function initLinkSrv() { const _dashboard: any = { @@ -29,6 +30,7 @@ describe('linkSrv', () => { timeSrv.init(_dashboard); timeSrv.setTime({ from: 'now-1h', to: 'now' }); _dashboard.refresh = false; + setTimeSrv(timeSrv); templateSrv = initTemplateSrv([ { type: 'query', name: 'home', current: { value: '127.0.0.1' } }, @@ -37,10 +39,11 @@ describe('linkSrv', () => { setTemplateSrv(templateSrv); - linkSrv = new LinkSrv(templateSrv, timeSrv); + linkSrv = new LinkSrv(); } beforeAll(() => { + originalTimeService = getTimeSrv(); variableAdapters.register(createQueryVariableAdapter()); }); @@ -50,6 +53,10 @@ describe('linkSrv', () => { jest.resetAllMocks(); }); + afterAll(() => { + setTimeSrv(originalTimeService); + }); + describe('getDataLinkUIModel', () => { describe('built in variables', () => { it('should not trim white space from data links', () => { diff --git a/public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx b/public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx index e6c54a590d0..177eb167481 100644 --- a/public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx +++ b/public/app/plugins/datasource/loki/configuration/DebugSection.test.tsx @@ -2,24 +2,25 @@ import React from 'react'; import { DebugSection } from './DebugSection'; import { mount } from 'enzyme'; import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from '../../../../features/panel/panellinks/link_srv'; -import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv'; import { dateTime } from '@grafana/data'; -import { TemplateSrv } from '../../../../features/templating/template_srv'; + +// We do not need more here and TimeSrv is hard to setup fully. +jest.mock('app/features/dashboard/services/TimeSrv', () => ({ + getTimeSrv: () => ({ + timeRangeForUrl() { + const from = dateTime().subtract(1, 'h'); + const to = dateTime(); + return { from, to, raw: { from, to } }; + }, + }), +})); describe('DebugSection', () => { let originalLinkSrv: LinkService; // This needs to be setup so we can test interpolation in the debugger beforeAll(() => { - // We do not need more here and TimeSrv is hard to setup fully. - const timeSrvMock: TimeSrv = { - timeRangeForUrl() { - const from = dateTime().subtract(1, 'h'); - const to = dateTime(); - return { from, to, raw: { from, to } }; - }, - } as any; - const linkService = new LinkSrv(new TemplateSrv(), timeSrvMock); + const linkService = new LinkSrv(); originalLinkSrv = getLinkSrv(); setLinkSrv(linkService); });