helpers.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 { PanelPlugin } from 'app/types';
  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 = new TemplateSrvStub();
  16. this.datasourceSrv = {
  17. getMetricSources: () => {},
  18. get: () => {
  19. return {
  20. then: callback => {
  21. callback(self.datasource);
  22. },
  23. };
  24. },
  25. };
  26. this.isUtc = false;
  27. this.providePhase = mocks => {
  28. return angularMocks.module($provide => {
  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, key) => {
  37. $provide.value(key, value);
  38. });
  39. });
  40. };
  41. this.createPanelController = Ctrl => {
  42. return angularMocks.inject(($controller, $rootScope, $q, $location, $browser) => {
  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 PanelPlugin;
  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 => {
  71. return angularMocks.inject(($controller, $rootScope, $q, $location, $browser) => {
  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 = new 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 = false) => {
  95. self.isUtc = isUtc;
  96. };
  97. }
  98. export function ServiceTestContext(this: any) {
  99. const self = this;
  100. self.templateSrv = new TemplateSrvStub();
  101. self.timeSrv = new TimeSrvStub();
  102. self.datasourceSrv = {};
  103. self.backendSrv = {};
  104. self.$routeParams = {};
  105. this.providePhase = mocks => {
  106. return angularMocks.module($provide => {
  107. _.each(mocks, key => {
  108. $provide.value(key, self[key]);
  109. });
  110. });
  111. };
  112. this.createService = name => {
  113. // @ts-ignore
  114. return angularMocks.inject(($q, $rootScope, $httpBackend, $injector, $location, $timeout) => {
  115. self.$q = $q;
  116. self.$rootScope = $rootScope;
  117. self.$httpBackend = $httpBackend;
  118. self.$location = $location;
  119. self.$rootScope.onAppEvent = () => {};
  120. self.$rootScope.appEvent = () => {};
  121. self.$timeout = $timeout;
  122. self.service = $injector.get(name);
  123. });
  124. };
  125. }
  126. export function DashboardViewStateStub(this: any) {
  127. this.registerPanel = () => {};
  128. }
  129. export function TimeSrvStub(this: any) {
  130. this.init = () => {};
  131. this.time = { from: 'now-1h', to: 'now' };
  132. this.timeRange = function(parse: boolean) {
  133. if (parse === false) {
  134. return this.time;
  135. }
  136. return {
  137. from: dateMath.parse(this.time.from, false),
  138. to: dateMath.parse(this.time.to, true),
  139. };
  140. };
  141. this.setTime = function(time: any) {
  142. this.time = time;
  143. };
  144. }
  145. export function ContextSrvStub(this: any) {
  146. this.hasRole = () => {
  147. return true;
  148. };
  149. }
  150. export function TemplateSrvStub(this: any) {
  151. this.variables = [];
  152. this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g };
  153. this.data = {};
  154. this.replace = (text: string) => {
  155. return _.template(text, this.templateSettings)(this.data);
  156. };
  157. this.init = () => {};
  158. this.getAdhocFilters = (): any => {
  159. return [];
  160. };
  161. this.fillVariableValuesForUrl = () => {};
  162. this.updateIndex = () => {};
  163. this.variableExists = () => {
  164. return false;
  165. };
  166. this.variableInitialized = () => {};
  167. this.highlightVariablesAsHtml = (str: string) => {
  168. return str;
  169. };
  170. this.setGrafanaVariable = function(name: string, value: string) {
  171. this.data[name] = value;
  172. };
  173. }
  174. const allDeps = {
  175. ContextSrvStub,
  176. TemplateSrvStub,
  177. TimeSrvStub,
  178. ControllerTestContext,
  179. ServiceTestContext,
  180. DashboardViewStateStub,
  181. };
  182. // for legacy
  183. export default allDeps;