helpers.ts 5.1 KB

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