helpers.ts 5.3 KB

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