Add Jest stubs

This commit is contained in:
Tobias Skarhed 2018-07-03 11:55:23 +02:00
parent 69c85e0d7e
commit 80b2f5c756
2 changed files with 49 additions and 12 deletions

View File

@ -2,8 +2,8 @@ import _ from 'lodash';
import moment from 'moment';
import angular from 'angular';
import { ElasticDatasource } from '../datasource';
import { TimeSrv } from 'app/features/dashboard/time_srv';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { jestTimeSrvStub } from 'test/specs/helpers';
import { jestTemplateSrvStub } from 'test/specs/helpers';
import $q from 'q';
describe('ElasticDatasource', function() {
@ -16,11 +16,9 @@ describe('ElasticDatasource', function() {
appEvent: jest.fn(),
};
let templateSrv = new TemplateSrv();
let templateSrv = new jestTemplateSrvStub();
templateSrv.variables = [];
let timeSrv = new TimeSrv($rootScope, jest.fn(), {}, {}, {});
let timeSrv = new jestTimeSrvStub();
let ctx = <any>{
$rootScope,

View File

@ -195,13 +195,52 @@ export function TemplateSrvStub() {
};
}
export function jestTemplateSrvStub() {
this.variables = [];
this.templateSettings = { interpolate: /\[\[([\s\S]+?)\]\]/g };
this.data = {};
this.replace = jest.fn(text => _.template(text, this.templateSettings)(this.data));
this.init = jest.fn();
this.getAdhocFilters = jest.fn(() => []);
this.fillVariableValuesForUrl = jest.fn();
this.updateTemplateData = jest.fn();
this.variableExists = jest.fn(() => false);
this.variableInitialized = jest.fn();
this.highlightVariablesAsHtml = jest.fn(str => str);
this.setGrafanaVariable = jest.fn((name, value) => {
this.data[name] = value;
});
}
export function jestTimeSrvStub() {
this.init = jest.fn();
this.time = { from: 'now-1h', to: 'now' };
this.timeRange = jest.fn(parse => {
if (parse === false) {
return this.time;
}
return {
from: dateMath.parse(this.time.from, false),
to: dateMath.parse(this.time.to, true),
};
});
this.replace = jest.fn(target => target);
this.setTime = jest.fn(time => {
this.time = time;
});
}
var allDeps = {
ContextSrvStub: ContextSrvStub,
TemplateSrvStub: TemplateSrvStub,
TimeSrvStub: TimeSrvStub,
ControllerTestContext: ControllerTestContext,
ServiceTestContext: ServiceTestContext,
DashboardViewStateStub: DashboardViewStateStub,
ContextSrvStub,
TemplateSrvStub,
TimeSrvStub,
ControllerTestContext,
ServiceTestContext,
DashboardViewStateStub,
jestTimeSrvStub,
jestTemplateSrvStub,
};
// for legacy