Commit ccd964e1 by Tobias Skarhed

Convert query control

parent 45eadae6
import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
import helpers from 'test/specs/helpers';
import { OpenTsQueryCtrl } from '../query_ctrl'; import { OpenTsQueryCtrl } from '../query_ctrl';
describe('OpenTsQueryCtrl', function() { describe('OpenTsQueryCtrl', () => {
var ctx = new helpers.ControllerTestContext(); var ctx = <any>{
target: { target: '' },
datasource: {
tsdbVersion: '',
getAggregators: () => Promise.resolve([]),
getFilterTypes: () => Promise.resolve([]),
},
};
beforeEach(angularMocks.module('grafana.core')); ctx.panelCtrl = {
beforeEach(angularMocks.module('grafana.services')); panel: {
beforeEach( targets: [ctx.target],
angularMocks.module(function($compileProvider) { },
$compileProvider.preAssignBindingsEnabled(true); refresh: () => {},
}) };
);
beforeEach(ctx.providePhase(['backendSrv', 'templateSrv'])); OpenTsQueryCtrl.prototype = Object.assign(OpenTsQueryCtrl.prototype, ctx);
beforeEach(ctx.providePhase()); beforeEach(() => {
beforeEach( ctx.ctrl = new OpenTsQueryCtrl({}, {});
angularMocks.inject(($rootScope, $controller, $q) => { });
ctx.$q = $q;
ctx.scope = $rootScope.$new();
ctx.target = { target: '' };
ctx.panelCtrl = {
panel: {
targets: [ctx.target],
},
};
ctx.panelCtrl.refresh = sinon.spy();
ctx.datasource.getAggregators = sinon.stub().returns(ctx.$q.when([]));
ctx.datasource.getFilterTypes = sinon.stub().returns(ctx.$q.when([]));
ctx.ctrl = $controller(
OpenTsQueryCtrl,
{ $scope: ctx.scope },
{
panelCtrl: ctx.panelCtrl,
datasource: ctx.datasource,
target: ctx.target,
}
);
ctx.scope.$digest();
})
);
describe('init query_ctrl variables', function() { describe('init query_ctrl variables', () => {
it('filter types should be initialized', function() { it('filter types should be initialized', () => {
expect(ctx.ctrl.filterTypes.length).to.be(7); expect(ctx.ctrl.filterTypes.length).toBe(7);
}); });
it('aggregators should be initialized', function() { it('aggregators should be initialized', () => {
expect(ctx.ctrl.aggregators.length).to.be(8); expect(ctx.ctrl.aggregators.length).toBe(8);
}); });
it('fill policy options should be initialized', function() { it('fill policy options should be initialized', () => {
expect(ctx.ctrl.fillPolicies.length).to.be(4); expect(ctx.ctrl.fillPolicies.length).toBe(4);
}); });
}); });
describe('when adding filters and tags', function() { describe('when adding filters and tags', () => {
it('addTagMode should be false when closed', function() { it('addTagMode should be false when closed', () => {
ctx.ctrl.addTagMode = true; ctx.ctrl.addTagMode = true;
ctx.ctrl.closeAddTagMode(); ctx.ctrl.closeAddTagMode();
expect(ctx.ctrl.addTagMode).to.be(false); expect(ctx.ctrl.addTagMode).toBe(false);
}); });
it('addFilterMode should be false when closed', function() { it('addFilterMode should be false when closed', () => {
ctx.ctrl.addFilterMode = true; ctx.ctrl.addFilterMode = true;
ctx.ctrl.closeAddFilterMode(); ctx.ctrl.closeAddFilterMode();
expect(ctx.ctrl.addFilterMode).to.be(false); expect(ctx.ctrl.addFilterMode).toBe(false);
}); });
it('removing a tag from the tags list', function() { it('removing a tag from the tags list', () => {
ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' }; ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' };
ctx.ctrl.removeTag('tagk'); ctx.ctrl.removeTag('tagk');
expect(Object.keys(ctx.ctrl.target.tags).length).to.be(1); expect(Object.keys(ctx.ctrl.target.tags).length).toBe(1);
}); });
it('removing a filter from the filters list', function() { it('removing a filter from the filters list', () => {
ctx.ctrl.target.filters = [ ctx.ctrl.target.filters = [
{ {
tagk: 'tag_key', tagk: 'tag_key',
...@@ -86,18 +66,18 @@ describe('OpenTsQueryCtrl', function() { ...@@ -86,18 +66,18 @@ describe('OpenTsQueryCtrl', function() {
}, },
]; ];
ctx.ctrl.removeFilter(0); ctx.ctrl.removeFilter(0);
expect(ctx.ctrl.target.filters.length).to.be(0); expect(ctx.ctrl.target.filters.length).toBe(0);
}); });
it('adding a filter when tags exist should generate error', function() { it('adding a filter when tags exist should generate error', () => {
ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' }; ctx.ctrl.target.tags = { tagk: 'tag_key', tagk2: 'tag_value2' };
ctx.ctrl.addFilter(); ctx.ctrl.addFilter();
expect(ctx.ctrl.errors.filters).to.be( expect(ctx.ctrl.errors.filters).toBe(
'Please remove tags to use filters, tags and filters are mutually exclusive.' 'Please remove tags to use filters, tags and filters are mutually exclusive.'
); );
}); });
it('adding a tag when filters exist should generate error', function() { it('adding a tag when filters exist should generate error', () => {
ctx.ctrl.target.filters = [ ctx.ctrl.target.filters = [
{ {
tagk: 'tag_key', tagk: 'tag_key',
...@@ -107,7 +87,7 @@ describe('OpenTsQueryCtrl', function() { ...@@ -107,7 +87,7 @@ describe('OpenTsQueryCtrl', function() {
}, },
]; ];
ctx.ctrl.addTag(); ctx.ctrl.addTag();
expect(ctx.ctrl.errors.tags).to.be('Please remove filters to use tags, tags and filters are mutually exclusive.'); expect(ctx.ctrl.errors.tags).toBe('Please remove filters to use tags, tags and filters are mutually exclusive.');
}); });
}); });
}); });
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