helpers.ts 5.5 KB

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