helpers.ts 5.2 KB

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