From bd417bedb238650b10a7bd62e9abc2e74c0d1a90 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:12:07 +0200 Subject: [PATCH 1/7] Start elastic ds test conversion --- ...datasource_specs.ts => datasource.jest.ts} | 116 ++++++++++-------- 1 file changed, 65 insertions(+), 51 deletions(-) rename public/app/plugins/datasource/elasticsearch/specs/{datasource_specs.ts => datasource.jest.ts} (75%) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts similarity index 75% rename from public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts rename to public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 558bccf3d0f..9a3d57f1d4f 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,31 +2,45 @@ import _ from 'lodash'; import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; import moment from 'moment'; import angular from 'angular'; -import helpers from 'test/specs/helpers'; import { ElasticDatasource } from '../datasource'; +import { TimeSrv } from 'app/features/dashboard/time_srv'; +import $q from 'q'; describe('ElasticDatasource', function() { - var ctx = new helpers.ServiceTestContext(); + //var ctx = new helpers.ServiceTestContext(); + let backendSrv = { + datasourceRequest: jest.fn() + }; - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); + let $rootScope = { + $on: jest.fn(), + appEvent: jest.fn(), + }; - beforeEach( - angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { - ctx.$q = $q; - ctx.$httpBackend = $httpBackend; - ctx.$rootScope = $rootScope; - ctx.$injector = $injector; - $httpBackend.when('GET', /\.html$/).respond(''); - }) - ); + let timeSrv = new TimeSrv($rootScope,jest.fn(),{},{},{}); + + let ctx = { + $rootScope, + backendSrv, + $q + }; + // beforeEach(angularMocks.module('grafana.core')); + // beforeEach(angularMocks.module('grafana.services')); + // beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); + + // beforeEach( + // angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { + // ctx.$q = $q; + // ctx.$httpBackend = $httpBackend; + // ctx.$rootScope = $rootScope; + // ctx.$injector = $injector; + // $httpBackend.when('GET', /\.html$/).respond(''); + // }) + // ); function createDatasource(instanceSettings) { instanceSettings.jsonData = instanceSettings.jsonData || {}; - ctx.ds = ctx.$injector.instantiate(ElasticDatasource, { - instanceSettings: instanceSettings, - }); + ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, { replace: jest.fn() }, timeSrv); } describe('When testing datasource with index pattern', function() { @@ -40,13 +54,13 @@ describe('ElasticDatasource', function() { it('should translate index pattern to current day', function() { var requestOptions; - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: {} }); - }; + return Promise.resolve({ data: {} }); + }); ctx.ds.testDatasource(); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); var today = moment.utc().format('YYYY.MM.DD'); expect(requestOptions.url).to.be('http://es.com/asd-' + today + '/_mapping'); @@ -63,10 +77,10 @@ describe('ElasticDatasource', function() { jsonData: { interval: 'Daily', esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -82,19 +96,19 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should translate index pattern to current day', function() { - expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']); + expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']); }); it('should json escape lucene query', function() { var body = angular.fromJson(parts[1]); - expect(body.query.bool.filter[1].query_string.query).to.be('escape\\:test'); + expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test'); }); }); @@ -108,10 +122,10 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn((options) => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -127,18 +141,18 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should set search type to query_then_fetch', function() { - expect(header.search_type).to.eql('query_then_fetch'); + expect(header.search_type).toEqual('query_then_fetch'); }); it('should set size', function() { var body = angular.fromJson(parts[1]); - expect(body.size).to.be(500); + expect(body.size).toBe(500); }); }); @@ -146,8 +160,8 @@ describe('ElasticDatasource', function() { beforeEach(function() { createDatasource({ url: 'http://es.com', index: 'metricbeat' }); - ctx.backendSrv.datasourceRequest = function(options) { - return ctx.$q.when({ + ctx.backendSrv.datasourceRequest = jest.fn((options) => { + return Promise.resolve({ data: { metricbeat: { mappings: { @@ -190,7 +204,7 @@ describe('ElasticDatasource', function() { }, }, }); - }; + }) }); it('should return nested fields', function() { @@ -201,7 +215,7 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql([ + expect(fields).toEqual([ '@timestamp', 'beat.name.raw', 'beat.name', @@ -212,7 +226,7 @@ describe('ElasticDatasource', function() { 'system.process.name', ]); }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); }); it('should return fields related to query type', function() { @@ -224,7 +238,7 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']); + expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']); }); ctx.ds @@ -235,10 +249,10 @@ describe('ElasticDatasource', function() { }) .then(fieldObjects => { var fields = _.map(fieldObjects, 'text'); - expect(fields).to.eql(['@timestamp']); + expect(fields).toEqual(['@timestamp']); }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); }); }); @@ -271,18 +285,18 @@ describe('ElasticDatasource', function() { ], }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); it('should not set search type to count', function() { - expect(header.search_type).to.not.eql('count'); + expect(header.search_type).not.toEqual('count'); }); it('should set size to 0', function() { var body = angular.fromJson(parts[1]); - expect(body.size).to.be(0); + expect(body.size).toBe(0); }); }); @@ -324,7 +338,7 @@ describe('ElasticDatasource', function() { results = res; }); - ctx.$rootScope.$apply(); + // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); @@ -332,24 +346,24 @@ describe('ElasticDatasource', function() { }); it('should get results', function() { - expect(results.length).to.eql(2); + expect(results.length).toEqual(2); }); it('should use key or key_as_string', function() { - expect(results[0].text).to.eql('test'); - expect(results[1].text).to.eql('test2_as_string'); + expect(results[0].text).toEqual('test'); + expect(results[1].text).toEqual('test2_as_string'); }); it('should not set search type to count', function() { - expect(header.search_type).to.not.eql('count'); + expect(header.search_type).not.toEqual('count'); }); it('should set size to 0', function() { - expect(body.size).to.be(0); + expect(body.size).toBe(0); }); it('should not set terms aggregation size to 0', function() { - expect(body['aggs']['1']['terms'].size).to.not.be(0); + expect(body['aggs']['1']['terms'].size).not.toBe(0); }); }); }); From 12e76ad1972fe04fb97e870bab50524a3dcf014b Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:47:50 +0200 Subject: [PATCH 2/7] Remove logs and comments --- .../elasticsearch/specs/datasource.jest.ts | 80 +++++++------------ 1 file changed, 30 insertions(+), 50 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index 9a3d57f1d4f..def60ecdcb3 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -1,15 +1,14 @@ import _ from 'lodash'; -import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; 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 $q from 'q'; describe('ElasticDatasource', function() { - //var ctx = new helpers.ServiceTestContext(); let backendSrv = { - datasourceRequest: jest.fn() + datasourceRequest: jest.fn(), }; let $rootScope = { @@ -17,30 +16,21 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let timeSrv = new TimeSrv($rootScope,jest.fn(),{},{},{}); + let templateSrv = new TemplateSrv(); + + templateSrv.variables = []; + + let timeSrv = new TimeSrv($rootScope, jest.fn(), {}, {}, {}); let ctx = { $rootScope, backendSrv, - $q + $q, }; - // beforeEach(angularMocks.module('grafana.core')); - // beforeEach(angularMocks.module('grafana.services')); - // beforeEach(ctx.providePhase(['templateSrv', 'backendSrv', 'timeSrv'])); - - // beforeEach( - // angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { - // ctx.$q = $q; - // ctx.$httpBackend = $httpBackend; - // ctx.$rootScope = $rootScope; - // ctx.$injector = $injector; - // $httpBackend.when('GET', /\.html$/).respond(''); - // }) - // ); function createDatasource(instanceSettings) { instanceSettings.jsonData = instanceSettings.jsonData || {}; - ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, { replace: jest.fn() }, timeSrv); + ctx.ds = new ElasticDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv); } describe('When testing datasource with index pattern', function() { @@ -54,35 +44,34 @@ describe('ElasticDatasource', function() { it('should translate index pattern to current day', function() { var requestOptions; - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: {} }); }); ctx.ds.testDatasource(); - // ctx.$rootScope.$apply(); var today = moment.utc().format('YYYY.MM.DD'); - expect(requestOptions.url).to.be('http://es.com/asd-' + today + '/_mapping'); + expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping'); }); }); describe('When issuing metric query with interval pattern', function() { var requestOptions, parts, header; - beforeEach(function() { + beforeEach(async () => { createDatasource({ url: 'http://es.com', index: '[asd-]YYYY.MM.DD', jsonData: { interval: 'Daily', esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: { responses: [] } }); }); - ctx.ds.query({ + await ctx.ds.query({ range: { from: moment.utc([2015, 4, 30, 10]), to: moment.utc([2015, 5, 1, 10]), @@ -96,8 +85,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); - parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -122,7 +109,7 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '2' }, }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; return Promise.resolve({ data: { responses: [] } }); }); @@ -141,7 +128,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -157,10 +143,10 @@ describe('ElasticDatasource', function() { }); describe('When getting fields', function() { - beforeEach(function() { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: 'metricbeat' }); - ctx.backendSrv.datasourceRequest = jest.fn((options) => { + ctx.backendSrv.datasourceRequest = jest.fn(options => { return Promise.resolve({ data: { metricbeat: { @@ -204,7 +190,7 @@ describe('ElasticDatasource', function() { }, }, }); - }) + }); }); it('should return nested fields', function() { @@ -226,7 +212,6 @@ describe('ElasticDatasource', function() { 'system.process.name', ]); }); - // ctx.$rootScope.$apply(); }); it('should return fields related to query type', function() { @@ -251,8 +236,6 @@ describe('ElasticDatasource', function() { var fields = _.map(fieldObjects, 'text'); expect(fields).toEqual(['@timestamp']); }); - - // ctx.$rootScope.$apply(); }); }); @@ -266,10 +249,10 @@ describe('ElasticDatasource', function() { jsonData: { esVersion: '5' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; - return ctx.$q.when({ data: { responses: [] } }); - }; + return Promise.resolve({ data: { responses: [] } }); + }); ctx.ds.query({ range: { @@ -285,7 +268,6 @@ describe('ElasticDatasource', function() { ], }); - // ctx.$rootScope.$apply(); parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); }); @@ -303,16 +285,16 @@ describe('ElasticDatasource', function() { describe('When issuing metricFind query on es5.x', function() { var requestOptions, parts, header, body, results; - beforeEach(function() { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: 'test', jsonData: { esVersion: '5' }, }); - ctx.backendSrv.datasourceRequest = function(options) { + ctx.backendSrv.datasourceRequest = jest.fn(options => { requestOptions = options; - return ctx.$q.when({ + return Promise.resolve({ data: { responses: [ { @@ -332,37 +314,35 @@ describe('ElasticDatasource', function() { ], }, }); - }; + }); ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then(res => { results = res; }); - // ctx.$rootScope.$apply(); - parts = requestOptions.data.split('\n'); header = angular.fromJson(parts[0]); body = angular.fromJson(parts[1]); }); - it('should get results', function() { + it('should get results', () => { expect(results.length).toEqual(2); }); - it('should use key or key_as_string', function() { + it('should use key or key_as_string', () => { expect(results[0].text).toEqual('test'); expect(results[1].text).toEqual('test2_as_string'); }); - it('should not set search type to count', function() { + it('should not set search type to count', () => { expect(header.search_type).not.toEqual('count'); }); - it('should set size to 0', function() { + it('should set size to 0', () => { expect(body.size).toBe(0); }); - it('should not set terms aggregation size to 0', function() { + it('should not set terms aggregation size to 0', () => { expect(body['aggs']['1']['terms'].size).not.toBe(0); }); }); From 69c85e0d7efc4bb2096b9855c75274240087947a Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 10:55:48 +0200 Subject: [PATCH 3/7] Remove async --- .../plugins/datasource/elasticsearch/specs/datasource.jest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index def60ecdcb3..32b5a4dbbd5 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -59,7 +59,7 @@ describe('ElasticDatasource', function() { describe('When issuing metric query with interval pattern', function() { var requestOptions, parts, header; - beforeEach(async () => { + beforeEach(() => { createDatasource({ url: 'http://es.com', index: '[asd-]YYYY.MM.DD', @@ -71,7 +71,7 @@ describe('ElasticDatasource', function() { return Promise.resolve({ data: { responses: [] } }); }); - await ctx.ds.query({ + ctx.ds.query({ range: { from: moment.utc([2015, 4, 30, 10]), to: moment.utc([2015, 5, 1, 10]), From 80b2f5c7567cdef4c6cb288001ceb44f164d48be Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 11:55:23 +0200 Subject: [PATCH 4/7] 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 From 8f1bcd91178375c007688e022677611cd5d4c9f7 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 3 Jul 2018 13:33:30 +0200 Subject: [PATCH 5/7] Remove q and stub --- .../datasource/elasticsearch/specs/datasource.jest.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index bfd55544d7b..b6d19d003da 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,9 +2,7 @@ import _ from 'lodash'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; -import { jestTimeSrvStub } from 'test/specs/helpers'; -import { jestTemplateSrvStub } from 'test/specs/helpers'; -import $q from 'q'; +import { jestTimeSrvStub, TemplateSrvStub } from 'test/specs/helpers'; describe('ElasticDatasource', function() { let backendSrv = { @@ -16,14 +14,13 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let templateSrv = new jestTemplateSrvStub(); + let templateSrv = new TemplateSrvStub(); let timeSrv = new jestTimeSrvStub(); let ctx = { $rootScope, backendSrv, - $q, }; function createDatasource(instanceSettings) { From 12d158f391d0256802ff5be4bb0fb1f9335f4b66 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Wed, 4 Jul 2018 10:43:36 +0200 Subject: [PATCH 6/7] Add mocks in test file --- .../elasticsearch/specs/datasource.jest.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index b6d19d003da..e9f7a61a572 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -2,7 +2,8 @@ import _ from 'lodash'; import moment from 'moment'; import angular from 'angular'; import { ElasticDatasource } from '../datasource'; -import { jestTimeSrvStub, TemplateSrvStub } from 'test/specs/helpers'; + +import * as dateMath from 'app/core/utils/datemath'; describe('ElasticDatasource', function() { let backendSrv = { @@ -14,9 +15,23 @@ describe('ElasticDatasource', function() { appEvent: jest.fn(), }; - let templateSrv = new TemplateSrvStub(); + let templateSrv = { + replace: jest.fn(text => text), + getAdhocFilters: jest.fn(() => []), + }; - let timeSrv = new jestTimeSrvStub(); + let timeSrv = { + time: { from: 'now-1h', to: 'now' }, + timeRange: jest.fn(parse => { + return { + from: dateMath.parse(this.time.from, false), + to: dateMath.parse(this.time.to, true), + }; + }), + setTime: jest.fn(time => { + this.time = time; + }), + }; let ctx = { $rootScope, From d3c213973c7d6eeb0eb2affbf2178bad2713c641 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Wed, 4 Jul 2018 11:23:12 +0200 Subject: [PATCH 7/7] Basic cleanup --- .../elasticsearch/specs/datasource.jest.ts | 2 +- public/test/specs/helpers.ts | 39 ------------------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts index e9f7a61a572..36e7a63a005 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/datasource.jest.ts @@ -22,7 +22,7 @@ describe('ElasticDatasource', function() { let timeSrv = { time: { from: 'now-1h', to: 'now' }, - timeRange: jest.fn(parse => { + timeRange: jest.fn(() => { return { from: dateMath.parse(this.time.from, false), to: dateMath.parse(this.time.to, true), diff --git a/public/test/specs/helpers.ts b/public/test/specs/helpers.ts index d98e79ff06b..677419f3f75 100644 --- a/public/test/specs/helpers.ts +++ b/public/test/specs/helpers.ts @@ -195,43 +195,6 @@ 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, TemplateSrvStub, @@ -239,8 +202,6 @@ var allDeps = { ControllerTestContext, ServiceTestContext, DashboardViewStateStub, - jestTimeSrvStub, - jestTemplateSrvStub, }; // for legacy