Commit 42ba0dc7 by Tobias Skarhed

Karma to Jest: Cloudwatch datasource

parent d4bd5213
import '../datasource'; import '../datasource';
import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common'; //import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
import helpers from 'test/specs/helpers'; import { TemplateSrvStub, jestTimeSrvStub } from 'test/specs/helpers';
import CloudWatchDatasource from '../datasource'; import CloudWatchDatasource from '../datasource';
import 'app/features/dashboard/time_srv'; import 'app/features/dashboard/time_srv';
describe('CloudWatchDatasource', function() { describe('CloudWatchDatasource', function() {
var ctx = new helpers.ServiceTestContext();
var instanceSettings = { let instanceSettings = {
jsonData: { defaultRegion: 'us-east-1', access: 'proxy' }, jsonData: { defaultRegion: 'us-east-1', access: 'proxy' },
}; };
let templateSrv = new TemplateSrvStub();
let timeSrv = new jestTimeSrvStub();
let backendSrv = {};
let ctx = <any>{
backendSrv,
templateSrv
};
beforeEach(angularMocks.module('grafana.core')); beforeEach(() => {
beforeEach(angularMocks.module('grafana.services')); ctx.ds = new CloudWatchDatasource(instanceSettings, {}, backendSrv, templateSrv, timeSrv);
beforeEach(angularMocks.module('grafana.controllers')); });
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv'])); // beforeEach(angularMocks.module('grafana.core'));
beforeEach(ctx.createService('timeSrv')); // beforeEach(angularMocks.module('grafana.services'));
// beforeEach(angularMocks.module('grafana.controllers'));
beforeEach( // beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { // beforeEach(ctx.createService('timeSrv'));
ctx.$q = $q;
ctx.$httpBackend = $httpBackend; // beforeEach(
ctx.$rootScope = $rootScope; // angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
ctx.ds = $injector.instantiate(CloudWatchDatasource, { // ctx.$q = $q;
instanceSettings: instanceSettings, // ctx.$httpBackend = $httpBackend;
}); // ctx.$rootScope = $rootScope;
$httpBackend.when('GET', /\.html$/).respond(''); // ctx.ds = $injector.instantiate(CloudWatchDatasource, {
}) // instanceSettings: instanceSettings,
); // });
// $httpBackend.when('GET', /\.html$/).respond('');
// })
// );
describe('When performing CloudWatch query', function() { describe('When performing CloudWatch query', function() {
var requestParams; var requestParams;
...@@ -67,24 +77,24 @@ describe('CloudWatchDatasource', function() { ...@@ -67,24 +77,24 @@ describe('CloudWatchDatasource', function() {
}, },
}; };
beforeEach(function() { beforeEach(async () => {
ctx.backendSrv.datasourceRequest = function(params) { ctx.backendSrv.datasourceRequest = await jest.fn((params) => {
requestParams = params.data; requestParams = params.data;
return ctx.$q.when({ data: response }); return Promise.resolve({ data: response });
}; });
}); });
it('should generate the correct query', function(done) { it('should generate the correct query', function(done) {
ctx.ds.query(query).then(function() { ctx.ds.query(query).then(function() {
var params = requestParams.queries[0]; var params = requestParams.queries[0];
expect(params.namespace).to.be(query.targets[0].namespace); expect(params.namespace).toBe(query.targets[0].namespace);
expect(params.metricName).to.be(query.targets[0].metricName); expect(params.metricName).toBe(query.targets[0].metricName);
expect(params.dimensions['InstanceId']).to.be('i-12345678'); expect(params.dimensions['InstanceId']).toBe('i-12345678');
expect(params.statistics).to.eql(query.targets[0].statistics); expect(params.statistics).toEqual(query.targets[0].statistics);
expect(params.period).to.be(query.targets[0].period); expect(params.period).toBe(query.targets[0].period);
done(); done();
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
it('should generate the correct query with interval variable', function(done) { it('should generate the correct query with interval variable', function(done) {
...@@ -111,19 +121,19 @@ describe('CloudWatchDatasource', function() { ...@@ -111,19 +121,19 @@ describe('CloudWatchDatasource', function() {
ctx.ds.query(query).then(function() { ctx.ds.query(query).then(function() {
var params = requestParams.queries[0]; var params = requestParams.queries[0];
expect(params.period).to.be('600'); expect(params.period).toBe('600');
done(); done();
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
it('should return series list', function(done) { it('should return series list', function(done) {
ctx.ds.query(query).then(function(result) { ctx.ds.query(query).then(function(result) {
expect(result.data[0].target).to.be(response.results.A.series[0].name); expect(result.data[0].target).toBe(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]); expect(result.data[0].datapoints[0][0]).toBe(response.results.A.series[0].points[0][0]);
done(); done();
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
it('should generate the correct targets by expanding template variables', function() { it('should generate the correct targets by expanding template variables', function() {
...@@ -173,7 +183,7 @@ describe('CloudWatchDatasource', function() { ...@@ -173,7 +183,7 @@ describe('CloudWatchDatasource', function() {
]; ];
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv); var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-34567890'); expect(result[0].dimensions.InstanceId).toBe('i-34567890');
}); });
it('should generate the correct targets by expanding template variables from url', function() { it('should generate the correct targets by expanding template variables from url', function() {
...@@ -220,7 +230,7 @@ describe('CloudWatchDatasource', function() { ...@@ -220,7 +230,7 @@ describe('CloudWatchDatasource', function() {
]; ];
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv); var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-45678901'); expect(result[0].dimensions.InstanceId).toBe('i-45678901');
}); });
}); });
...@@ -228,21 +238,21 @@ describe('CloudWatchDatasource', function() { ...@@ -228,21 +238,21 @@ describe('CloudWatchDatasource', function() {
it('should return the datasource region if empty or "default"', function() { it('should return the datasource region if empty or "default"', function() {
var defaultRegion = instanceSettings.jsonData.defaultRegion; var defaultRegion = instanceSettings.jsonData.defaultRegion;
expect(ctx.ds.getActualRegion()).to.be(defaultRegion); expect(ctx.ds.getActualRegion()).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('')).to.be(defaultRegion); expect(ctx.ds.getActualRegion('')).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('default')).to.be(defaultRegion); expect(ctx.ds.getActualRegion('default')).toBe(defaultRegion);
}); });
it('should return the specified region if specified', function() { it('should return the specified region if specified', function() {
expect(ctx.ds.getActualRegion('some-fake-region-1')).to.be('some-fake-region-1'); expect(ctx.ds.getActualRegion('some-fake-region-1')).toBe('some-fake-region-1');
}); });
var requestParams; var requestParams;
beforeEach(function() { beforeEach(function() {
ctx.ds.performTimeSeriesQuery = function(request) { ctx.ds.performTimeSeriesQuery = jest.fn((request) => {
requestParams = request; requestParams = request;
return ctx.$q.when({ data: {} }); return Promise.resolve({ data: {} });
}; });
}); });
it('should query for the datasource region if empty or "default"', function(done) { it('should query for the datasource region if empty or "default"', function(done) {
...@@ -264,10 +274,10 @@ describe('CloudWatchDatasource', function() { ...@@ -264,10 +274,10 @@ describe('CloudWatchDatasource', function() {
}; };
ctx.ds.query(query).then(function(result) { ctx.ds.query(query).then(function(result) {
expect(requestParams.queries[0].region).to.be(instanceSettings.jsonData.defaultRegion); expect(requestParams.queries[0].region).toBe(instanceSettings.jsonData.defaultRegion);
done(); done();
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
}); });
...@@ -311,18 +321,18 @@ describe('CloudWatchDatasource', function() { ...@@ -311,18 +321,18 @@ describe('CloudWatchDatasource', function() {
}; };
beforeEach(function() { beforeEach(function() {
ctx.backendSrv.datasourceRequest = function(params) { ctx.backendSrv.datasourceRequest = jest.fn((params) => {
return ctx.$q.when({ data: response }); return Promise.resolve({ data: response });
}; });
}); });
it('should return series list', function(done) { it('should return series list', function(done) {
ctx.ds.query(query).then(function(result) { ctx.ds.query(query).then(function(result) {
expect(result.data[0].target).to.be(response.results.A.series[0].name); expect(result.data[0].target).toBe(response.results.A.series[0].name);
expect(result.data[0].datapoints[0][0]).to.be(response.results.A.series[0].points[0][0]); expect(result.data[0].datapoints[0][0]).toBe(response.results.A.series[0].points[0][0]);
done(); done();
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
}); });
...@@ -332,14 +342,14 @@ describe('CloudWatchDatasource', function() { ...@@ -332,14 +342,14 @@ describe('CloudWatchDatasource', function() {
scenario.setup = setupCallback => { scenario.setup = setupCallback => {
beforeEach(() => { beforeEach(() => {
setupCallback(); setupCallback();
ctx.backendSrv.datasourceRequest = args => { ctx.backendSrv.datasourceRequest = jest.fn((args) => {
scenario.request = args.data; scenario.request = args.data;
return ctx.$q.when({ data: scenario.requestResponse }); return Promise.resolve({ data: scenario.requestResponse });
}; });
ctx.ds.metricFindQuery(query).then(args => { ctx.ds.metricFindQuery(query).then(args => {
scenario.result = args; scenario.result = args;
}); });
ctx.$rootScope.$apply(); //ctx.$rootScope.$apply();
}); });
}; };
...@@ -359,9 +369,9 @@ describe('CloudWatchDatasource', function() { ...@@ -359,9 +369,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __GetRegions and return result', () => { it('should call __GetRegions and return result', () => {
expect(scenario.result[0].text).to.contain('us-east-1'); expect(scenario.result[0].text).toContain('us-east-1');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('regions'); expect(scenario.request.queries[0].subtype).toBe('regions');
}); });
}); });
...@@ -377,9 +387,9 @@ describe('CloudWatchDatasource', function() { ...@@ -377,9 +387,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __GetNamespaces and return result', () => { it('should call __GetNamespaces and return result', () => {
expect(scenario.result[0].text).to.contain('AWS/EC2'); expect(scenario.result[0].text).toContain('AWS/EC2');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('namespaces'); expect(scenario.request.queries[0].subtype).toBe('namespaces');
}); });
}); });
...@@ -395,9 +405,9 @@ describe('CloudWatchDatasource', function() { ...@@ -395,9 +405,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __GetMetrics and return result', () => { it('should call __GetMetrics and return result', () => {
expect(scenario.result[0].text).to.be('CPUUtilization'); expect(scenario.result[0].text).toBe('CPUUtilization');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('metrics'); expect(scenario.request.queries[0].subtype).toBe('metrics');
}); });
}); });
...@@ -413,9 +423,9 @@ describe('CloudWatchDatasource', function() { ...@@ -413,9 +423,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __GetDimensions and return result', () => { it('should call __GetDimensions and return result', () => {
expect(scenario.result[0].text).to.be('InstanceId'); expect(scenario.result[0].text).toBe('InstanceId');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_keys'); expect(scenario.request.queries[0].subtype).toBe('dimension_keys');
}); });
}); });
...@@ -431,9 +441,9 @@ describe('CloudWatchDatasource', function() { ...@@ -431,9 +441,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __ListMetrics and return result', () => { it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).to.contain('i-12345678'); expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_values'); expect(scenario.request.queries[0].subtype).toBe('dimension_values');
}); });
}); });
...@@ -449,9 +459,9 @@ describe('CloudWatchDatasource', function() { ...@@ -449,9 +459,9 @@ describe('CloudWatchDatasource', function() {
}); });
it('should call __ListMetrics and return result', () => { it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).to.contain('i-12345678'); expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).to.be('metricFindQuery'); expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).to.be('dimension_values'); expect(scenario.request.queries[0].subtype).toBe('dimension_values');
}); });
}); });
...@@ -544,7 +554,7 @@ describe('CloudWatchDatasource', function() { ...@@ -544,7 +554,7 @@ describe('CloudWatchDatasource', function() {
let now = new Date(options.range.from.valueOf() + t[2] * 1000); let now = new Date(options.range.from.valueOf() + t[2] * 1000);
let expected = t[3]; let expected = t[3];
let actual = ctx.ds.getPeriod(target, options, now); let actual = ctx.ds.getPeriod(target, options, now);
expect(actual).to.be(expected); expect(actual).toBe(expected);
} }
}); });
}); });
...@@ -195,6 +195,24 @@ export function TemplateSrvStub() { ...@@ -195,6 +195,24 @@ export function TemplateSrvStub() {
}; };
} }
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 = { var allDeps = {
ContextSrvStub, ContextSrvStub,
TemplateSrvStub, TemplateSrvStub,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment