grafana/src/test/specs/helpers.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-08-07 03:42:05 -05:00
define([
2014-10-01 06:49:36 -05:00
'kbn',
'lodash'
], function(kbn, _) {
2014-08-07 03:42:05 -05:00
'use strict';
function ControllerTestContext() {
var self = this;
this.datasource = {};
this.$element = {};
this.annotationsSrv = {};
this.timeSrv = new TimeSrvStub();
this.templateSrv = new TemplateSrvStub();
2014-08-07 03:42:05 -05:00
this.datasourceSrv = {
getMetricSources: function() {},
get: function() { return self.datasource; }
};
this.providePhase = function(mocks) {
2014-08-07 03:42:05 -05:00
return module(function($provide) {
$provide.value('datasourceSrv', self.datasourceSrv);
$provide.value('annotationsSrv', self.annotationsSrv);
$provide.value('timeSrv', self.timeSrv);
$provide.value('templateSrv', self.templateSrv);
$provide.value('$element', self.$element);
2014-10-01 06:49:36 -05:00
_.each(mocks, function(value, key) {
$provide.value(key, value);
});
2014-08-07 03:42:05 -05:00
});
};
this.createControllerPhase = function(controllerName) {
return inject(function($controller, $rootScope, $q, $location) {
2014-08-07 03:42:05 -05:00
self.scope = $rootScope.$new();
self.$location = $location;
self.scope.contextSrv = {};
2014-08-07 03:42:05 -05:00
self.scope.panel = {};
2014-08-11 08:59:03 -05:00
self.scope.row = { panels:[] };
self.scope.dashboard = {};
self.scope.dashboardViewState = new DashboardViewStateStub();
2014-10-01 06:49:36 -05:00
self.scope.appEvent = sinon.spy();
self.scope.onAppEvent = sinon.spy();
2014-08-11 08:59:03 -05:00
$rootScope.colors = [];
for (var i = 0; i < 50; i++) { $rootScope.colors.push('#' + i); }
2014-08-07 03:42:05 -05:00
self.$q = $q;
self.scope.skipDataOnInit = true;
2014-10-01 06:49:36 -05:00
self.scope.skipAutoInit = true;
2014-08-07 03:42:05 -05:00
self.controller = $controller(controllerName, {
$scope: self.scope
});
});
};
}
2014-08-08 10:33:00 -05:00
function ServiceTestContext() {
var self = this;
self.templateSrv = new TemplateSrvStub();
self.timeSrv = new TimeSrvStub();
self.datasourceSrv = {};
self.$routeParams = {};
this.providePhase = function(mocks) {
return module(function($provide) {
_.each(mocks, function(key) {
$provide.value(key, self[key]);
});
});
};
2014-08-08 10:33:00 -05:00
this.createService = function(name) {
return inject(function($q, $rootScope, $httpBackend, $injector) {
2014-08-08 10:33:00 -05:00
self.$q = $q;
self.$rootScope = $rootScope;
self.$httpBackend = $httpBackend;
self.$rootScope.onAppEvent = function() {};
self.$rootScope.appEvent = function() {};
self.service = $injector.get(name);
});
2014-08-08 10:33:00 -05:00
};
}
function DashboardViewStateStub() {
this.registerPanel = function() {
};
}
function TimeSrvStub() {
2014-10-01 06:49:36 -05:00
this.init = sinon.spy();
2014-08-08 10:33:00 -05:00
this.time = { from:'now-1h', to: 'now'};
this.timeRange = function(parse) {
if (parse === false) {
2014-08-08 10:33:00 -05:00
return this.time;
}
return {
from : kbn.parseDate(this.time.from),
to : kbn.parseDate(this.time.to)
2014-08-08 10:33:00 -05:00
};
};
this.replace = function(target) {
2014-08-08 10:33:00 -05:00
return target;
};
}
function TemplateSrvStub() {
this.variables = [];
this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
this.data = {};
this.replace = function(text) {
return _.template(text, this.data, this.templateSettings);
};
this.init = function() {};
this.updateTemplateData = function() { };
this.variableExists = function() { return false; };
this.highlightVariablesAsHtml = function(str) { return str; };
this.setGrafanaVariable = function(name, value) {
this.data[name] = value;
};
}
2014-08-07 03:42:05 -05:00
return {
2014-08-08 10:33:00 -05:00
ControllerTestContext: ControllerTestContext,
TimeSrvStub: TimeSrvStub,
2014-08-08 10:33:00 -05:00
ServiceTestContext: ServiceTestContext
2014-08-07 03:42:05 -05:00
};
});