helpers.js 4.0 KB

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