helpers.js 4.0 KB

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