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