瀏覽代碼

Add Jest stubs

Tobias Skarhed 7 年之前
父節點
當前提交
80b2f5c756
共有 2 個文件被更改,包括 49 次插入12 次删除
  1. 4 6
      public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts
  2. 45 6
      public/test/specs/helpers.ts

+ 4 - 6
public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts

@@ -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,

+ 45 - 6
public/test/specs/helpers.ts

@@ -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