helpers.js 5.1 KB

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