From 6670acd0825893d937006e81cb378cbeee6da9c2 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Mon, 9 Jul 2018 12:52:56 +0200 Subject: [PATCH] Remove irrelevant tests and templateSrv stub --- .../datasource/cloudwatch/datasource.ts | 1 + .../cloudwatch/specs/datasource.jest.ts | 110 ++---------------- 2 files changed, 10 insertions(+), 101 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/datasource.ts b/public/app/plugins/datasource/cloudwatch/datasource.ts index 4101759ec1d..391f65bd7ae 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.ts +++ b/public/app/plugins/datasource/cloudwatch/datasource.ts @@ -404,6 +404,7 @@ export default class CloudWatchDatasource { } expandTemplateVariable(targets, scopedVars, templateSrv) { + // Datasource and template srv logic uber-complected. This should be cleaned up. return _.chain(targets) .map(target => { var dimensionKey = _.findKey(target.dimensions, v => { diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts index fbeaaad8e67..b06d00b3ee4 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource.jest.ts @@ -1,14 +1,19 @@ import '../datasource'; -import { TemplateSrvStub } from 'test/specs/helpers'; import CloudWatchDatasource from '../datasource'; import 'app/features/dashboard/time_srv'; import * as dateMath from 'app/core/utils/datemath'; +import _ from 'lodash'; describe('CloudWatchDatasource', function() { let instanceSettings = { jsonData: { defaultRegion: 'us-east-1', access: 'proxy' }, }; - let templateSrv = new TemplateSrvStub(); + + let templateSrv = { + templateSettings: { interpolate: /\[\[([\s\S]+?)\]\]/g }, + replace: jest.fn(text => _.template(text, templateSrv.templateSettings)(templateSrv.data)), + variableExists: jest.fn(() => false), + }; let timeSrv = { time: { from: 'now-1h', to: 'now' }, @@ -68,8 +73,8 @@ describe('CloudWatchDatasource', function() { }, }; - beforeEach(async () => { - ctx.backendSrv.datasourceRequest = await jest.fn(params => { + beforeEach(() => { + ctx.backendSrv.datasourceRequest = jest.fn(params => { requestParams = params.data; return Promise.resolve({ data: response }); }); @@ -123,103 +128,6 @@ describe('CloudWatchDatasource', function() { done(); }); }); - - it('should generate the correct targets by expanding template variables', function() { - var templateSrv = { - variables: [ - { - name: 'instance_id', - options: [ - { text: 'i-23456789', value: 'i-23456789', selected: false }, - { text: 'i-34567890', value: 'i-34567890', selected: true }, - ], - current: { - text: 'i-34567890', - value: 'i-34567890', - }, - }, - ], - replace: function(target, scopedVars) { - if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') { - return 'i-34567890'; - } else { - return ''; - } - }, - getVariableName: function(e) { - return 'instance_id'; - }, - variableExists: function(e) { - return true; - }, - containsVariable: function(str, variableName) { - return str.indexOf('$' + variableName) !== -1; - }, - }; - - var targets = [ - { - region: 'us-east-1', - namespace: 'AWS/EC2', - metricName: 'CPUUtilization', - dimensions: { - InstanceId: '$instance_id', - }, - statistics: ['Average'], - period: 300, - }, - ]; - - var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv); - expect(result[0].dimensions.InstanceId).toBe('i-34567890'); - }); - - it('should generate the correct targets by expanding template variables from url', function() { - var templateSrv = { - variables: [ - { - name: 'instance_id', - options: [ - { text: 'i-23456789', value: 'i-23456789', selected: false }, - { text: 'i-34567890', value: 'i-34567890', selected: false }, - ], - current: 'i-45678901', - }, - ], - replace: function(target, scopedVars) { - if (target === '$instance_id') { - return 'i-45678901'; - } else { - return ''; - } - }, - getVariableName: function(e) { - return 'instance_id'; - }, - variableExists: function(e) { - return true; - }, - containsVariable: function(str, variableName) { - return str.indexOf('$' + variableName) !== -1; - }, - }; - - var targets = [ - { - region: 'us-east-1', - namespace: 'AWS/EC2', - metricName: 'CPUUtilization', - dimensions: { - InstanceId: '$instance_id', - }, - statistics: ['Average'], - period: 300, - }, - ]; - - var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv); - expect(result[0].dimensions.InstanceId).toBe('i-45678901'); - }); }); describe('When query region is "default"', function() {