helpers.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. define([
  2. 'kbn',
  3. 'lodash'
  4. ], function(kbn, _) {
  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() { return self.datasource; }
  16. };
  17. this.providePhase = function(mocks) {
  18. return module(function($provide) {
  19. $provide.value('datasourceSrv', self.datasourceSrv);
  20. $provide.value('annotationsSrv', self.annotationsSrv);
  21. $provide.value('timeSrv', self.timeSrv);
  22. $provide.value('templateSrv', self.templateSrv);
  23. $provide.value('$element', self.$element);
  24. _.each(mocks, function(key, value) {
  25. $provide.value(key, value);
  26. });
  27. });
  28. };
  29. this.createControllerPhase = function(controllerName) {
  30. return inject(function($controller, $rootScope, $q, $location) {
  31. self.scope = $rootScope.$new();
  32. self.$location = $location;
  33. self.scope.panel = {};
  34. self.scope.row = { panels:[] };
  35. self.scope.dashboard = {};
  36. self.scope.dashboardViewState = new DashboardViewStateStub();
  37. $rootScope.colors = [];
  38. for (var i = 0; i < 50; i++) { $rootScope.colors.push('#' + i); }
  39. self.$q = $q;
  40. self.scope.skipDataOnInit = true;
  41. self.controller = $controller(controllerName, {
  42. $scope: self.scope
  43. });
  44. });
  45. };
  46. }
  47. function ServiceTestContext() {
  48. var self = this;
  49. self.templateSrv = new TemplateSrvStub();
  50. self.timeSrv = new TimeSrvStub();
  51. self.datasourceSrv = {};
  52. self.$routeParams = {};
  53. this.providePhase = function(mocks) {
  54. return module(function($provide) {
  55. _.each(mocks, function(key) {
  56. $provide.value(key, self[key]);
  57. });
  58. });
  59. };
  60. this.createService = function(name) {
  61. return inject(function($q, $rootScope, $httpBackend, $injector) {
  62. self.$q = $q;
  63. self.$rootScope = $rootScope;
  64. self.$httpBackend = $httpBackend;
  65. self.$rootScope.onAppEvent = function() {};
  66. self.$rootScope.appEvent = function() {};
  67. self.service = $injector.get(name);
  68. });
  69. };
  70. }
  71. function DashboardViewStateStub() {
  72. this.registerPanel = function() {
  73. };
  74. }
  75. function TimeSrvStub() {
  76. this.time = { from:'now-1h', to: 'now'};
  77. this.timeRange = function(parse) {
  78. if (parse === false) {
  79. return this.time;
  80. }
  81. return {
  82. from : kbn.parseDate(this.time.from),
  83. to : kbn.parseDate(this.time.to)
  84. };
  85. };
  86. this.replace = function(target) {
  87. return target;
  88. };
  89. }
  90. function TemplateSrvStub() {
  91. this.variables = [];
  92. this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
  93. this.data = {};
  94. this.replace = function(text) {
  95. return _.template(text, this.data, this.templateSettings);
  96. };
  97. this.init = function() {};
  98. this.updateTemplateData = function() { };
  99. this.variableExists = function() { return false; };
  100. this.highlightVariablesAsHtml = function(str) { return str; };
  101. this.setGrafanaVariable = function(name, value) {
  102. this.data[name] = value;
  103. };
  104. }
  105. return {
  106. ControllerTestContext: ControllerTestContext,
  107. TimeSrvStub: TimeSrvStub,
  108. ServiceTestContext: ServiceTestContext
  109. };
  110. });