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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 { getLinkSrv, LinkService, LinkSrv, setLinkSrv } from './link_srv';
import { TemplateSrv } from '../../templating/template_srv';
import { TimeSrv } from '../../dashboard/services/TimeSrv';
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 = {
jest.mock('app/features/dashboard/services/TimeSrv', () => ({
getTimeSrv: () => ({
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);
}),
}));
describe('getFieldLinksSupplier', () => {
let originalLinkSrv: LinkService;
let templateSrv = new TemplateSrv();
beforeAll(() => {
const linkService = new LinkSrv();
originalLinkSrv = getLinkSrv();
setLinkSrv(linkService);

View File

@ -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;
}

View File

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

View File

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