Angular cleanup: Don't inject in link_srv (#36849)

* Angular cleanup: Don't inject in link_srv

* link_srv: Fix unit tests

* kick drone
This commit is contained in:
Ashley Harrison
2021-07-19 09:16:36 +01:00
committed by GitHub
parent 37caebc934
commit b8a5f38d2a
4 changed files with 41 additions and 31 deletions

View File

@@ -2,21 +2,23 @@ import { getFieldLinksSupplier } from './linkSuppliers';
import { applyFieldOverrides, createTheme, DataFrameView, dateTime, FieldDisplay, toDataFrame } from '@grafana/data'; import { applyFieldOverrides, createTheme, DataFrameView, dateTime, FieldDisplay, toDataFrame } from '@grafana/data';
import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from './link_srv'; import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from './link_srv';
import { TemplateSrv } from '../../templating/template_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', () => { describe('getFieldLinksSupplier', () => {
let originalLinkSrv: LinkService; let originalLinkSrv: LinkService;
let templateSrv = new TemplateSrv(); let templateSrv = new TemplateSrv();
beforeAll(() => { beforeAll(() => {
// We do not need more here and TimeSrv is hard to setup fully. const linkService = new LinkSrv();
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);
originalLinkSrv = getLinkSrv(); originalLinkSrv = getLinkSrv();
setLinkSrv(linkService); setLinkSrv(linkService);

View File

@@ -1,6 +1,6 @@
import { chain } from 'lodash'; import { chain } from 'lodash';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; import { getTemplateSrv } from '@grafana/runtime';
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import { getConfig } from 'app/core/config'; import { getConfig } from 'app/core/config';
import { import {
@@ -263,15 +263,14 @@ export interface LinkService {
} }
export class LinkSrv implements LinkService { export class LinkSrv implements LinkService {
/** @ngInject */ constructor() {}
constructor(private templateSrv: TemplateSrv, private timeSrv: TimeSrv) {}
getLinkUrl(link: any) { 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 } = {}; let params: { [key: string]: any } = {};
if (link.keepTime) { if (link.keepTime) {
const range = this.timeSrv.timeRangeForUrl(); const range = getTimeSrv().timeRangeForUrl();
params['from'] = range.from; params['from'] = range.from;
params['to'] = range.to; params['to'] = range.to;
} }
@@ -288,10 +287,11 @@ export class LinkSrv implements LinkService {
} }
getAnchorInfo(link: any) { getAnchorInfo(link: any) {
const templateSrv = getTemplateSrv();
const info: any = {}; const info: any = {};
info.href = this.getLinkUrl(link); info.href = this.getLinkUrl(link);
info.title = this.templateSrv.replace(link.title || ''); info.title = templateSrv.replace(link.title || '');
info.tooltip = this.templateSrv.replace(link.tooltip || ''); info.tooltip = templateSrv.replace(link.tooltip || '');
return info; return info;
} }

View File

@@ -1,7 +1,7 @@
import { FieldType, locationUtil, toDataFrame, VariableOrigin } from '@grafana/data'; import { FieldType, locationUtil, toDataFrame, VariableOrigin } from '@grafana/data';
import { setTemplateSrv } from '@grafana/runtime'; import { setTemplateSrv } from '@grafana/runtime';
import { getDataFrameVars, LinkSrv } from '../link_srv'; 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 { TemplateSrv } from 'app/features/templating/template_srv';
import { variableAdapters } from 'app/features/variables/adapters'; import { variableAdapters } from 'app/features/variables/adapters';
import { createQueryVariableAdapter } from 'app/features/variables/query/adapter'; import { createQueryVariableAdapter } from 'app/features/variables/query/adapter';
@@ -17,6 +17,7 @@ jest.mock('app/core/core', () => ({
describe('linkSrv', () => { describe('linkSrv', () => {
let linkSrv: LinkSrv; let linkSrv: LinkSrv;
let templateSrv: TemplateSrv; let templateSrv: TemplateSrv;
let originalTimeService: TimeSrv;
function initLinkSrv() { function initLinkSrv() {
const _dashboard: any = { const _dashboard: any = {
@@ -29,6 +30,7 @@ describe('linkSrv', () => {
timeSrv.init(_dashboard); timeSrv.init(_dashboard);
timeSrv.setTime({ from: 'now-1h', to: 'now' }); timeSrv.setTime({ from: 'now-1h', to: 'now' });
_dashboard.refresh = false; _dashboard.refresh = false;
setTimeSrv(timeSrv);
templateSrv = initTemplateSrv([ templateSrv = initTemplateSrv([
{ type: 'query', name: 'home', current: { value: '127.0.0.1' } }, { type: 'query', name: 'home', current: { value: '127.0.0.1' } },
@@ -37,10 +39,11 @@ describe('linkSrv', () => {
setTemplateSrv(templateSrv); setTemplateSrv(templateSrv);
linkSrv = new LinkSrv(templateSrv, timeSrv); linkSrv = new LinkSrv();
} }
beforeAll(() => { beforeAll(() => {
originalTimeService = getTimeSrv();
variableAdapters.register(createQueryVariableAdapter()); variableAdapters.register(createQueryVariableAdapter());
}); });
@@ -50,6 +53,10 @@ describe('linkSrv', () => {
jest.resetAllMocks(); jest.resetAllMocks();
}); });
afterAll(() => {
setTimeSrv(originalTimeService);
});
describe('getDataLinkUIModel', () => { describe('getDataLinkUIModel', () => {
describe('built in variables', () => { describe('built in variables', () => {
it('should not trim white space from data links', () => { it('should not trim white space from data links', () => {

View File

@@ -2,24 +2,25 @@ import React from 'react';
import { DebugSection } from './DebugSection'; import { DebugSection } from './DebugSection';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from '../../../../features/panel/panellinks/link_srv'; import { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from '../../../../features/panel/panellinks/link_srv';
import { TimeSrv } from '../../../../features/dashboard/services/TimeSrv';
import { dateTime } from '@grafana/data'; 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', () => { describe('DebugSection', () => {
let originalLinkSrv: LinkService; let originalLinkSrv: LinkService;
// This needs to be setup so we can test interpolation in the debugger // This needs to be setup so we can test interpolation in the debugger
beforeAll(() => { beforeAll(() => {
// We do not need more here and TimeSrv is hard to setup fully. const linkService = new LinkSrv();
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);
originalLinkSrv = getLinkSrv(); originalLinkSrv = getLinkSrv();
setLinkSrv(linkService); setLinkSrv(linkService);
}); });