helpers.ts 4.7 KB

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