helpers.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import _ from 'lodash';
  2. import config from 'app/core/config';
  3. import * as dateMath from '@grafana/ui/src/utils/datemath';
  4. import { angularMocks, sinon } from '../lib/common';
  5. import { PanelModel } from 'app/features/dashboard/state/PanelModel';
  6. import { PanelPluginMeta, RawTimeRange } from '@grafana/ui';
  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: any, $q: any, $location: any, $browser: any) => {
  43. self.scope = $rootScope.$new();
  44. self.$location = $location;
  45. self.$browser = $browser;
  46. self.$q = $q;
  47. self.panel = new PanelModel({ type: 'test' });
  48. self.dashboard = { meta: {} };
  49. self.isUtc = false;
  50. self.dashboard.isTimezoneUtc = () => {
  51. return self.isUtc;
  52. };
  53. $rootScope.appEvent = sinon.spy();
  54. $rootScope.onAppEvent = sinon.spy();
  55. $rootScope.colors = [];
  56. for (let i = 0; i < 50; i++) {
  57. $rootScope.colors.push('#' + i);
  58. }
  59. config.panels['test'] = { info: {} } as PanelPluginMeta;
  60. self.ctrl = $controller(
  61. Ctrl,
  62. { $scope: self.scope },
  63. {
  64. panel: self.panel,
  65. dashboard: self.dashboard,
  66. }
  67. );
  68. });
  69. };
  70. this.createControllerPhase = (controllerName: string) => {
  71. return angularMocks.inject(($controller: any, $rootScope: any, $q: any, $location: any, $browser: any) => {
  72. self.scope = $rootScope.$new();
  73. self.$location = $location;
  74. self.$browser = $browser;
  75. self.scope.contextSrv = {};
  76. self.scope.panel = {};
  77. self.scope.dashboard = { meta: {} };
  78. self.scope.dashboardMeta = {};
  79. self.scope.dashboardViewState = DashboardViewStateStub();
  80. self.scope.appEvent = sinon.spy();
  81. self.scope.onAppEvent = sinon.spy();
  82. $rootScope.colors = [];
  83. for (let i = 0; i < 50; i++) {
  84. $rootScope.colors.push('#' + i);
  85. }
  86. self.$q = $q;
  87. self.scope.skipDataOnInit = true;
  88. self.scope.skipAutoInit = true;
  89. self.controller = $controller(controllerName, {
  90. $scope: self.scope,
  91. });
  92. });
  93. };
  94. this.setIsUtc = (isUtc: any = false) => {
  95. self.isUtc = isUtc;
  96. };
  97. }
  98. export function ServiceTestContext(this: any) {
  99. const self = this;
  100. self.templateSrv = TemplateSrvStub();
  101. self.timeSrv = new TimeSrvStub();
  102. self.datasourceSrv = {};
  103. self.backendSrv = {};
  104. self.$routeParams = {};
  105. this.providePhase = (mocks: any) => {
  106. return angularMocks.module(($provide: any) => {
  107. _.each(mocks, (key: string) => {
  108. $provide.value(key, self[key]);
  109. });
  110. });
  111. };
  112. this.createService = (name: string) => {
  113. // @ts-ignore
  114. return angularMocks.inject(
  115. ($q: any, $rootScope: any, $httpBackend: any, $injector: any, $location: any, $timeout: any) => {
  116. self.$q = $q;
  117. self.$rootScope = $rootScope;
  118. self.$httpBackend = $httpBackend;
  119. self.$location = $location;
  120. self.$rootScope.onAppEvent = () => {};
  121. self.$rootScope.appEvent = () => {};
  122. self.$timeout = $timeout;
  123. self.service = $injector.get(name);
  124. }
  125. );
  126. };
  127. }
  128. export function DashboardViewStateStub(this: any) {
  129. this.registerPanel = () => {};
  130. }
  131. export class TimeSrvStub {
  132. time: RawTimeRange;
  133. constructor() {
  134. this.time = { from: 'now-1h', to: 'now' };
  135. }
  136. init() {}
  137. timeRange(parse: boolean) {
  138. if (parse === false) {
  139. return this.time;
  140. }
  141. return {
  142. from: dateMath.parse(this.time.from, false),
  143. to: dateMath.parse(this.time.to, true),
  144. };
  145. }
  146. setTime(time: any) {
  147. this.time = time;
  148. }
  149. }
  150. export class ContextSrvStub {
  151. isGrafanaVisibile = jest.fn();
  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;