From 80b2f5c7567cdef4c6cb288001ceb44f164d48be Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 11:55:23 +0200 Subject: [PATCH] Add Jest stubs --- .../elasticsearch/specs/datasource.jest.ts | 10 ++-- public/test/specs/helpers.ts | 51 ++++++++++++++++--- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 32b5a4dbbd5..bfd55544d7b 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/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 = { $rootScope, diff --git a/public/test/specs/helpers.ts b/public/test/specs/helpers.ts index dd8bd39846e..d98e79ff06b 100644 --- a/public/test/specs/helpers.ts +++ b/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