1import { each, template } from 'lodash'; 2 3import config from 'app/core/config'; 4import { angularMocks, sinon } from '../lib/common'; 5import { PanelModel } from 'app/features/dashboard/state/PanelModel'; 6import { RawTimeRange, PanelPluginMeta, dateMath } from '@grafana/data'; 7import { GrafanaRootScope } from 'app/angular/GrafanaCtrl'; 8 9export function ControllerTestContext(this: any) { 10 const self = this; 11 12 this.datasource = {}; 13 this.$element = {}; 14 this.$sanitize = {}; 15 this.annotationsSrv = {}; 16 this.contextSrv = {}; 17 this.timeSrv = new TimeSrvStub(); 18 this.templateSrv = TemplateSrvStub(); 19 this.datasourceSrv = { 20 getMetricSources: () => {}, 21 get: () => { 22 return { 23 then: (callback: (ds: any) => void) => { 24 callback(self.datasource); 25 }, 26 }; 27 }, 28 }; 29 this.isUtc = false; 30 31 this.providePhase = (mocks: any) => { 32 return angularMocks.module(($provide: any) => { 33 $provide.value('contextSrv', self.contextSrv); 34 $provide.value('datasourceSrv', self.datasourceSrv); 35 $provide.value('annotationsSrv', self.annotationsSrv); 36 $provide.value('timeSrv', self.timeSrv); 37 $provide.value('templateSrv', self.templateSrv); 38 $provide.value('$element', self.$element); 39 $provide.value('$sanitize', self.$sanitize); 40 each(mocks, (value: any, key: any) => { 41 $provide.value(key, value); 42 }); 43 }); 44 }; 45 46 this.createPanelController = (Ctrl: any) => { 47 return angularMocks.inject(($controller: any, $rootScope: GrafanaRootScope, $browser: any) => { 48 self.scope = $rootScope.$new(); 49 self.$browser = $browser; 50 self.panel = new PanelModel({ type: 'test' }); 51 self.dashboard = { meta: {} }; 52 self.isUtc = false; 53 self.dashboard.getTimezone = () => { 54 return self.isUtc ? 'utc' : 'browser'; 55 }; 56 57 $rootScope.appEvent = sinon.spy(); 58 $rootScope.onAppEvent = sinon.spy(); 59 $rootScope.colors = []; 60 61 for (let i = 0; i < 50; i++) { 62 $rootScope.colors.push('#' + i); 63 } 64 65 config.panels['test'] = { info: {} } as PanelPluginMeta; 66 self.ctrl = $controller( 67 Ctrl, 68 { $scope: self.scope }, 69 { 70 panel: self.panel, 71 dashboard: self.dashboard, 72 } 73 ); 74 }); 75 }; 76 77 this.createControllerPhase = (controllerName: string) => { 78 return angularMocks.inject(($controller: any, $rootScope: GrafanaRootScope, $browser: any) => { 79 self.scope = $rootScope.$new(); 80 self.$browser = $browser; 81 self.scope.contextSrv = {}; 82 self.scope.panel = {}; 83 self.scope.dashboard = { meta: {} }; 84 self.scope.dashboardMeta = {}; 85 self.scope.dashboardViewState = DashboardViewStateStub(); 86 self.scope.appEvent = sinon.spy(); 87 self.scope.onAppEvent = sinon.spy(); 88 89 $rootScope.colors = []; 90 for (let i = 0; i < 50; i++) { 91 $rootScope.colors.push('#' + i); 92 } 93 94 self.scope.skipDataOnInit = true; 95 self.scope.skipAutoInit = true; 96 self.controller = $controller(controllerName, { 97 $scope: self.scope, 98 }); 99 }); 100 }; 101 102 this.setIsUtc = (isUtc: any = false) => { 103 self.isUtc = isUtc; 104 }; 105} 106 107export function DashboardViewStateStub(this: any) { 108 this.registerPanel = () => {}; 109} 110 111export class TimeSrvStub { 112 time: RawTimeRange; 113 114 constructor() { 115 this.time = { from: 'now-1h', to: 'now' }; 116 } 117 118 init() {} 119 120 timeRange(parse: boolean) { 121 if (parse === false) { 122 return this.time; 123 } 124 return { 125 from: dateMath.parse(this.time.from, false), 126 to: dateMath.parse(this.time.to, true), 127 }; 128 } 129 130 setTime(time: any) { 131 this.time = time; 132 } 133} 134 135export class ContextSrvStub { 136 isGrafanaVisible = jest.fn(); 137 138 getValidInterval() { 139 return '10s'; 140 } 141 142 hasRole() { 143 return true; 144 } 145 146 isAllowedInterval() { 147 return true; 148 } 149} 150 151export function TemplateSrvStub(this: any) { 152 this.variables = []; 153 this.getVariables = function () { 154 return this.variables; 155 }; 156 this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g }; 157 this.data = {}; 158 this.replace = (text: string) => { 159 return template(text, this.templateSettings)(this.data); 160 }; 161 this.init = () => {}; 162 this.getAdhocFilters = (): any => { 163 return []; 164 }; 165 this.fillVariableValuesForUrl = () => {}; 166 this.updateIndex = () => {}; 167 this.variableExists = () => { 168 return false; 169 }; 170 this.variableInitialized = () => {}; 171 this.highlightVariablesAsHtml = (str: string) => { 172 return str; 173 }; 174 this.setGrafanaVariable = function (name: string, value: string) { 175 this.data[name] = value; 176 }; 177} 178 179const allDeps = { 180 ContextSrvStub, 181 TemplateSrvStub, 182 TimeSrvStub, 183 ControllerTestContext, 184 DashboardViewStateStub, 185}; 186 187// for legacy 188export default allDeps; 189