Commit b58a7642 by Tobias Skarhed

Karma to Jest

parent 25c82335
import 'app/core/services/segment_srv'; import { uiSegmentSrv } from 'app/core/services/segment_srv';
import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common'; // import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
import gfunc from '../gfunc'; import gfunc from '../gfunc';
import helpers from 'test/specs/helpers'; // import helpers from 'test/specs/helpers';
import { GraphiteQueryCtrl } from '../query_ctrl'; import { GraphiteQueryCtrl } from '../query_ctrl';
describe('GraphiteQueryCtrl', function() { describe('GraphiteQueryCtrl', () => {
let ctx = <any>{
let datasource = { datasource: {
metricFindQuery: jest.fn(() => Promise.resolve([])), metricFindQuery: jest.fn(() => Promise.resolve([])),
getFuncDefs: jest.fn(() => Promise.resolve(gfunc.getFuncDefs('1.0'))), getFuncDefs: jest.fn(() => Promise.resolve(gfunc.getFuncDefs('1.0'))),
getFuncDef: gfunc.getFuncDef, getFuncDef: gfunc.getFuncDef,
waitForFuncDefsLoaded: jest.fn(() => Promise.resolve(null)), waitForFuncDefsLoaded: jest.fn(() => Promise.resolve(null)),
createFuncInstance: gfunc.createFuncInstance, createFuncInstance: gfunc.createFuncInstance,
},
target: { target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' },
panelCtrl: {
refresh: jest.fn(),
},
}; };
let ctx = {
ctx.panelCtrl.panel = {
targets: [ctx.target],
}; };
// beforeEach(angularMocks.module('grafana.core')); // beforeEach(angularMocks.module('grafana.core'));
...@@ -60,156 +65,170 @@ describe('GraphiteQueryCtrl', function() { ...@@ -60,156 +65,170 @@ describe('GraphiteQueryCtrl', function() {
// }) // })
// ); // );
describe('init', function() { beforeEach(() => {
it('should validate metric key exists', function() { GraphiteQueryCtrl.prototype.target = ctx.target;
expect(ctx.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*'); GraphiteQueryCtrl.prototype.datasource = ctx.datasource;
GraphiteQueryCtrl.prototype.panelCtrl = ctx.panelCtrl;
ctx.ctrl = new GraphiteQueryCtrl(
{},
{},
new uiSegmentSrv({ trustAsHtml: html => html }, { highlightVariablesAsHtml: () => {} }),
{},
{}
);
});
describe('init', () => {
it('should validate metric key exists', () => {
expect(ctx.datasource.metricFindQuery.mock.calls[0][0]).toBe('test.prod.*');
}); });
it('should delete last segment if no metrics are found', function() { it('should delete last segment if no metrics are found', () => {
expect(ctx.ctrl.segments[2].value).to.be('select metric'); expect(ctx.ctrl.segments[2].value).toBe('select metric');
}); });
it('should parse expression and build function model', function() { it('should parse expression and build function model', () => {
expect(ctx.ctrl.queryModel.functions.length).to.be(2); expect(ctx.ctrl.queryModel.functions.length).toBe(2);
}); });
}); });
describe('when adding function', function() { describe('when adding function', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'test.prod.*.count'; ctx.ctrl.target.target = 'test.prod.*.count';
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode')); ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode'));
}); });
it('should add function with correct node number', function() { it('should add function with correct node number', () => {
expect(ctx.ctrl.queryModel.functions[0].params[0]).to.be(2); expect(ctx.ctrl.queryModel.functions[0].params[0]).toBe(2);
}); });
it('should update target', function() { it('should update target', () => {
expect(ctx.ctrl.target.target).to.be('aliasByNode(test.prod.*.count, 2)'); expect(ctx.ctrl.target.target).toBe('aliasByNode(test.prod.*.count, 2)');
}); });
it('should call refresh', function() { it('should call refresh', () => {
expect(ctx.panelCtrl.refresh.called).to.be(true); expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
}); });
}); });
describe('when adding function before any metric segment', function() { describe('when adding function before any metric segment', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = ''; ctx.ctrl.target.target = '';
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([{ expandable: true }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: true }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent')); ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent'));
}); });
it('should add function and remove select metric link', function() { it('should add function and remove select metric link', () => {
expect(ctx.ctrl.segments.length).to.be(0); expect(ctx.ctrl.segments.length).toBe(0);
}); });
}); });
describe('when initializing target without metric expression and only function', function() { describe('when initializing target without metric expression and only function', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'asPercent(#A, #B)'; ctx.ctrl.target.target = 'asPercent(#A, #B)';
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.scope.$digest();
}); });
it('should not add select metric segment', function() { it('should not add select metric segment', () => {
expect(ctx.ctrl.segments.length).to.be(1); expect(ctx.ctrl.segments.length).toBe(1);
}); });
it('should add second series ref as param', function() { it('should add second series ref as param', () => {
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1); expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
}); });
}); });
describe('when initializing a target with single param func using variable', function() { describe('when initializing a target with single param func using variable', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'movingAverage(prod.count, $var)'; ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
}); });
it('should add 2 segments', function() { it('should add 2 segments', () => {
expect(ctx.ctrl.segments.length).to.be(2); expect(ctx.ctrl.segments.length).toBe(2);
}); });
it('should add function param', function() { it('should add function param', () => {
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1); expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
}); });
}); });
describe('when initializing target without metric expression and function with series-ref', function() { describe('when initializing target without metric expression and function with series-ref', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)'; ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
}); });
it('should add segments', function() { it('should add segments', () => {
expect(ctx.ctrl.segments.length).to.be(3); expect(ctx.ctrl.segments.length).toBe(3);
}); });
it('should have correct func params', function() { it('should have correct func params', () => {
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1); expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
}); });
}); });
describe('when getting altSegments and metricFindQuery returns empty array', function() { describe('when getting altSegments and metricFindQuery returns empty array', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'test.count'; ctx.ctrl.target.target = 'test.count';
ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.getAltSegments(1).then(function(results) { ctx.ctrl.getAltSegments(1).then(function(results) {
ctx.altSegments = results; ctx.altSegments = results;
}); });
ctx.scope.$digest();
}); });
it('should have no segments', function() { it('should have no segments', () => {
expect(ctx.altSegments.length).to.be(0); expect(ctx.altSegments.length).toBe(0);
}); });
}); });
describe('targetChanged', function() { describe('targetChanged', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.target.target = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.target.target = ''; ctx.ctrl.target.target = '';
ctx.ctrl.targetChanged(); ctx.ctrl.targetChanged();
}); });
it('should rebuld target after expression model', function() { it('should rebuild target after expression model', () => {
expect(ctx.ctrl.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)'); expect(ctx.ctrl.target.target).toBe('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
}); });
it('should call panelCtrl.refresh', function() { it('should call panelCtrl.refresh', () => {
expect(ctx.panelCtrl.refresh.called).to.be(true); expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
}); });
}); });
describe('when updating targets with nested query', function() { describe('when updating targets with nested query', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'scaleToSeconds(#A, 60)'; ctx.ctrl.target.target = 'scaleToSeconds(#A, 60)';
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
}); });
it('should add function params', function() { it('should add function params', () => {
expect(ctx.ctrl.queryModel.segments.length).to.be(1); expect(ctx.ctrl.queryModel.segments.length).toBe(1);
expect(ctx.ctrl.queryModel.segments[0].value).to.be('#A'); expect(ctx.ctrl.queryModel.segments[0].value).toBe('#A');
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1); expect(ctx.ctrl.queryModel.functions[0].params.length).toBe(1);
expect(ctx.ctrl.queryModel.functions[0].params[0]).to.be(60); expect(ctx.ctrl.queryModel.functions[0].params[0]).toBe(60);
}); });
it('target should remain the same', function() { it('target should remain the same', () => {
expect(ctx.ctrl.target.target).to.be('scaleToSeconds(#A, 60)'); expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
}); });
it('targetFull should include nested queries', function() { it('targetFull should include nested queries', () => {
ctx.ctrl.panelCtrl.panel.targets = [ ctx.ctrl.panelCtrl.panel.targets = [
{ {
target: 'nested.query.count', target: 'nested.query.count',
...@@ -219,17 +238,17 @@ describe('GraphiteQueryCtrl', function() { ...@@ -219,17 +238,17 @@ describe('GraphiteQueryCtrl', function() {
ctx.ctrl.updateModelTarget(); ctx.ctrl.updateModelTarget();
expect(ctx.ctrl.target.target).to.be('scaleToSeconds(#A, 60)'); expect(ctx.ctrl.target.target).toBe('scaleToSeconds(#A, 60)');
expect(ctx.ctrl.target.targetFull).to.be('scaleToSeconds(nested.query.count, 60)'); expect(ctx.ctrl.target.targetFull).toBe('scaleToSeconds(nested.query.count, 60)');
}); });
}); });
describe('when updating target used in other query', function() { describe('when updating target used in other query', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'metrics.a.count'; ctx.ctrl.target.target = 'metrics.a.count';
ctx.ctrl.target.refId = 'A'; ctx.ctrl.target.refId = 'A';
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }]; ctx.ctrl.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }];
...@@ -237,113 +256,113 @@ describe('GraphiteQueryCtrl', function() { ...@@ -237,113 +256,113 @@ describe('GraphiteQueryCtrl', function() {
ctx.ctrl.updateModelTarget(); ctx.ctrl.updateModelTarget();
}); });
it('targetFull of other query should update', function() { it('targetFull of other query should update', () => {
expect(ctx.ctrl.panel.targets[1].targetFull).to.be('sumSeries(metrics.a.count)'); expect(ctx.ctrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.a.count)');
}); });
}); });
describe('when adding seriesByTag function', function() { describe('when adding seriesByTag function', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = ''; ctx.ctrl.target.target = '';
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.addFunction(gfunc.getFuncDef('seriesByTag')); ctx.ctrl.addFunction(gfunc.getFuncDef('seriesByTag'));
}); });
it('should update functions', function() { it('should update functions', () => {
expect(ctx.ctrl.queryModel.getSeriesByTagFuncIndex()).to.be(0); expect(ctx.ctrl.queryModel.getSeriesByTagFuncIndex()).toBe(0);
}); });
it('should update seriesByTagUsed flag', function() { it('should update seriesByTagUsed flag', () => {
expect(ctx.ctrl.queryModel.seriesByTagUsed).to.be(true); expect(ctx.ctrl.queryModel.seriesByTagUsed).toBe(true);
}); });
it('should update target', function() { it('should update target', () => {
expect(ctx.ctrl.target.target).to.be('seriesByTag()'); expect(ctx.ctrl.target.target).toBe('seriesByTag()');
}); });
it('should call refresh', function() { it('should call refresh', () => {
expect(ctx.panelCtrl.refresh.called).to.be(true); expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
}); });
}); });
describe('when parsing seriesByTag function', function() { describe('when parsing seriesByTag function', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')"; ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
}); });
it('should add tags', function() { it('should add tags', () => {
const expected = [ const expected = [
{ key: 'tag1', operator: '=', value: 'value1' }, { key: 'tag1', operator: '=', value: 'value1' },
{ key: 'tag2', operator: '!=~', value: 'value2' }, { key: 'tag2', operator: '!=~', value: 'value2' },
]; ];
expect(ctx.ctrl.queryModel.tags).to.eql(expected); expect(ctx.ctrl.queryModel.tags).toEqual(expected);
}); });
it('should add plus button', function() { it('should add plus button', () => {
expect(ctx.ctrl.addTagSegments.length).to.be(1); expect(ctx.ctrl.addTagSegments.length).toBe(1);
}); });
}); });
describe('when tag added', function() { describe('when tag added', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = 'seriesByTag()'; ctx.ctrl.target.target = 'seriesByTag()';
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.addNewTag({ value: 'tag1' }); ctx.ctrl.addNewTag({ value: 'tag1' });
}); });
it('should update tags with default value', function() { it('should update tags with default value', () => {
const expected = [{ key: 'tag1', operator: '=', value: '' }]; const expected = [{ key: 'tag1', operator: '=', value: '' }];
expect(ctx.ctrl.queryModel.tags).to.eql(expected); expect(ctx.ctrl.queryModel.tags).toEqual(expected);
}); });
it('should update target', function() { it('should update target', () => {
const expected = "seriesByTag('tag1=')"; const expected = "seriesByTag('tag1=')";
expect(ctx.ctrl.target.target).to.eql(expected); expect(ctx.ctrl.target.target).toEqual(expected);
}); });
}); });
describe('when tag changed', function() { describe('when tag changed', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')"; ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.tagChanged({ key: 'tag1', operator: '=', value: 'new_value' }, 0); ctx.ctrl.tagChanged({ key: 'tag1', operator: '=', value: 'new_value' }, 0);
}); });
it('should update tags', function() { it('should update tags', () => {
const expected = [ const expected = [
{ key: 'tag1', operator: '=', value: 'new_value' }, { key: 'tag1', operator: '=', value: 'new_value' },
{ key: 'tag2', operator: '!=~', value: 'value2' }, { key: 'tag2', operator: '!=~', value: 'value2' },
]; ];
expect(ctx.ctrl.queryModel.tags).to.eql(expected); expect(ctx.ctrl.queryModel.tags).toEqual(expected);
}); });
it('should update target', function() { it('should update target', () => {
const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')"; const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')";
expect(ctx.ctrl.target.target).to.eql(expected); expect(ctx.ctrl.target.target).toEqual(expected);
}); });
}); });
describe('when tag removed', function() { describe('when tag removed', () => {
beforeEach(function() { beforeEach(() => {
ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')"; ctx.ctrl.target.target = "seriesByTag('tag1=value1', 'tag2!=~value2')";
ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{ expandable: false }])); ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
ctx.ctrl.parseTarget(); ctx.ctrl.parseTarget();
ctx.ctrl.removeTag(0); ctx.ctrl.removeTag(0);
}); });
it('should update tags', function() { it('should update tags', () => {
const expected = [{ key: 'tag2', operator: '!=~', value: 'value2' }]; const expected = [{ key: 'tag2', operator: '!=~', value: 'value2' }];
expect(ctx.ctrl.queryModel.tags).to.eql(expected); expect(ctx.ctrl.queryModel.tags).toEqual(expected);
}); });
it('should update target', function() { it('should update target', () => {
const expected = "seriesByTag('tag2!=~value2')"; const expected = "seriesByTag('tag2!=~value2')";
expect(ctx.ctrl.target.target).to.eql(expected); expect(ctx.ctrl.target.target).toEqual(expected);
}); });
}); });
}); });
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