helpers.ts 5.6 KB

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