helpers.js 5.0 KB

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