helpers.ts 5.4 KB

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