From c0f9c06f2163dc57424257b204e6c6c449aa0212 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 29 Jun 2018 13:37:21 +0200 Subject: [PATCH 1/6] Karma to Jest: completer --- .../{completer_specs.ts => completer.jest.ts} | 70 +++++++++---------- 1 file changed, 34 insertions(+), 36 deletions(-) rename public/app/plugins/datasource/prometheus/specs/{completer_specs.ts => completer.jest.ts} (79%) diff --git a/public/app/plugins/datasource/prometheus/specs/completer_specs.ts b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts similarity index 79% rename from public/app/plugins/datasource/prometheus/specs/completer_specs.ts rename to public/app/plugins/datasource/prometheus/specs/completer.jest.ts index 84694834089..cb8dd8e5bd6 100644 --- a/public/app/plugins/datasource/prometheus/specs/completer_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts @@ -1,47 +1,45 @@ -import { describe, it, sinon, expect } from 'test/lib/common'; -import helpers from 'test/specs/helpers'; +//import { describe, it, sinon, expect } from 'test/lib/common'; +//import helpers from 'test/specs/helpers'; import { PromCompleter } from '../completer'; import { PrometheusDatasource } from '../datasource'; +import { BackendSrv } from 'app/core/services/backend_srv'; +jest.mock('../datasource'); +jest.mock('app/core/services/backend_srv'); describe('Prometheus editor completer', function() { - var ctx = new helpers.ServiceTestContext(); - beforeEach(ctx.providePhase(['templateSrv'])); + //beforeEach(ctx.providePhase(['templateSrv'])); function getSessionStub(data) { return { - getTokenAt: sinon.stub().returns(data.currentToken), - getTokens: sinon.stub().returns(data.tokens), - getLine: sinon.stub().returns(data.line), + getTokenAt:jest.fn(()=> (data.currentToken)), + getTokens:jest.fn(()=> (data.tokens)), + getLine:jest.fn(()=> (data.line)), }; } let editor = {}; - let datasourceStub = { - performInstantQuery: sinon - .stub() - .withArgs({ expr: '{__name__="node_cpu"' }) - .returns( - Promise.resolve({ - data: { + + let backendSrv = {} + let datasourceStub = new PrometheusDatasource({},{},backendSrv,{},{}); + + datasourceStub.performInstantQuery = jest.fn(() => Promise.resolve({ data: { - result: [ - { - metric: { - job: 'node', - instance: 'localhost:9100', + data: { + result: [ + { + metric: { + job: 'node', + instance: 'localhost:9100', + }, }, - }, - ], + ], + }, }, - }, - }) - ), - performSuggestQuery: sinon - .stub() - .withArgs('node', true) - .returns(Promise.resolve(['node_cpu'])), - }; + }) + ); + datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu'])); + let templateSrv = { variables: [ @@ -62,9 +60,9 @@ describe('Prometheus editor completer', function() { }); return completer.getCompletions(editor, session, { row: 0, column: 10 }, '[', (s, res) => { - expect(res[0].caption).to.eql('$__interval'); - expect(res[0].value).to.eql('[$__interval'); - expect(res[0].meta).to.eql('range vector'); + expect(res[0].caption).toEqual('$__interval'); + expect(res[0].value).toEqual('[$__interval'); + expect(res[0].meta).toEqual('range vector'); }); }); }); @@ -93,7 +91,7 @@ describe('Prometheus editor completer', function() { }); return completer.getCompletions(editor, session, { row: 0, column: 10 }, 'j', (s, res) => { - expect(res[0].meta).to.eql('label name'); + expect(res[0].meta).toEqual('label name'); }); }); }); @@ -125,7 +123,7 @@ describe('Prometheus editor completer', function() { }); return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'j', (s, res) => { - expect(res[0].meta).to.eql('label name'); + expect(res[0].meta).toEqual('label name'); }); }); }); @@ -156,7 +154,7 @@ describe('Prometheus editor completer', function() { }); return completer.getCompletions(editor, session, { row: 0, column: 15 }, 'n', (s, res) => { - expect(res[0].meta).to.eql('label value'); + expect(res[0].meta).toEqual('label value'); }); }); }); @@ -192,7 +190,7 @@ describe('Prometheus editor completer', function() { }); return completer.getCompletions(editor, session, { row: 0, column: 23 }, 'm', (s, res) => { - expect(res[0].meta).to.eql('label name'); + expect(res[0].meta).toEqual('label name'); }); }); }); From 49a8c2e0c138118f4e1bc3bfa37446eba596b98c Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 29 Jun 2018 13:44:11 +0200 Subject: [PATCH 2/6] Make beautiful --- .../prometheus/specs/completer.jest.ts | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/specs/completer.jest.ts b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts index cb8dd8e5bd6..b401cb9bf65 100644 --- a/public/app/plugins/datasource/prometheus/specs/completer.jest.ts +++ b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts @@ -12,35 +12,35 @@ describe('Prometheus editor completer', function() { function getSessionStub(data) { return { - getTokenAt:jest.fn(()=> (data.currentToken)), - getTokens:jest.fn(()=> (data.tokens)), - getLine:jest.fn(()=> (data.line)), + getTokenAt: jest.fn(() => data.currentToken), + getTokens: jest.fn(() => data.tokens), + getLine: jest.fn(() => data.line), }; } let editor = {}; - let backendSrv = {} - let datasourceStub = new PrometheusDatasource({},{},backendSrv,{},{}); + let backendSrv = {}; + let datasourceStub = new PrometheusDatasource({}, {}, backendSrv, {}, {}); - datasourceStub.performInstantQuery = jest.fn(() => Promise.resolve({ - data: { - data: { - result: [ - { - metric: { - job: 'node', - instance: 'localhost:9100', - }, - }, - ], + datasourceStub.performInstantQuery = jest.fn(() => + Promise.resolve({ + data: { + data: { + result: [ + { + metric: { + job: 'node', + instance: 'localhost:9100', }, }, - }) - ); + ], + }, + }, + }) + ); datasourceStub.performSuggestQuery = jest.fn(() => Promise.resolve(['node_cpu'])); - let templateSrv = { variables: [ { From d2f81d52d4b121cbc0bc6c39527900a4c5cf2042 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Mon, 2 Jul 2018 09:43:34 +0200 Subject: [PATCH 3/6] Karma to Jest: begin influx query_ctrl --- .../influxdb/specs/query_ctrl.jest.ts | 222 ++++++++++++++++++ .../influxdb/specs/query_ctrl_specs.ts | 193 --------------- 2 files changed, 222 insertions(+), 193 deletions(-) create mode 100644 public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts delete mode 100644 public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts new file mode 100644 index 00000000000..dd6c9b4fa18 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts @@ -0,0 +1,222 @@ +import '../query_ctrl'; +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 helpers from 'test/specs/helpers'; +import { InfluxQueryCtrl } from '../query_ctrl'; + +describe('InfluxDBQueryCtrl', () => { + //var ctx = new helpers.ControllerTestContext(); + + // beforeEach(angularMocks.module('grafana.core')); + // beforeEach(angularMocks.module('grafana.controllers')); + // beforeEach(angularMocks.module('grafana.services')); + // beforeEach( + // angularMocks.module(($ =>compileProvider) { + // $compileProvider.preAssignBindingsEnabled(true); + // }) + // ); + // beforeEach(ctx.providePhase()); + + // beforeEach( + // angularMocks.inject(($rootScope, $controller, $q) => { + // ctx.$q = $q; + // ctx.scope = $rootScope.$new(); + // ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); + // ctx.target = { target: {} }; + // ctx.panelCtrl = { + // panel: { + // targets: [ctx.target], + // }, + // }; + // ctx.panelCtrl.refresh = sinon.spy(); + // influxQueryCtrl = $controller( + // InfluxQueryCtrl, + // { $scope: ctx.scope }, + // { + // panelCtrl: ctx.panelCtrl, + // target: ctx.target, + // datasource: ctx.datasource, + // } + // ); + // }) + // ); + + InfluxQueryCtrl.prototype.target = { target: {} }; + InfluxQueryCtrl.prototype.panelCtrl = { + refresh: jest.fn(), + panel: { + targets: InfluxQueryCtrl.prototype.target, + }, + }; + InfluxQueryCtrl.prototype.datasource = { + metricFindQuery: jest.fn(() => Promise.resolve([])), + }; + + // let uiSegmentSrv = { + // newPlusButton: jest.fn(), + // newSegment: jest.fn(), + // newSelectMeasurement: jest.fn() + // }; + let influxQueryCtrl; + + beforeEach(() => { + influxQueryCtrl = new InfluxQueryCtrl( + {}, + {}, + {}, + {}, + new uiSegmentSrv({ trustAsHtml: jest.fn() }, { highlightVariablesAsHtml: jest.fn() }) + ); + }); + describe('init', () => { + it('should init tagSegments', () => { + expect(influxQueryCtrl.tagSegments.length).toBe(1); + }); + + it('should init measurementSegment', () => { + expect(influxQueryCtrl.measurementSegment.value).toBe('select measurement'); + }); + }); + + describe('when first tag segment is updated', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + }); + + it('should update tag key', () => { + expect(influxQueryCtrl.target.tags[0].key).toBe('asd'); + expect(influxQueryCtrl.tagSegments[0].type).toBe('key'); + }); + + it('should add tagSegments', () => { + console.log(influxQueryCtrl.tagSegments); + expect(influxQueryCtrl.tagSegments.length).toBe(3); + }); + }); + + describe('when last tag value segment is updated', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + }); + + it('should update tag value', () => { + expect(influxQueryCtrl.target.tags[0].value).toBe('server1'); + }); + + it('should set tag operator', () => { + expect(influxQueryCtrl.target.tags[0].operator).toBe('='); + }); + + it('should add plus button for another filter', () => { + expect(influxQueryCtrl.tagSegments[3].fake).toBe(true); + }); + }); + + describe('when last tag value segment is updated to regex', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2); + }); + + it('should update operator', () => { + expect(influxQueryCtrl.tagSegments[1].value).toBe('=~'); + expect(influxQueryCtrl.target.tags[0].operator).toBe('=~'); + }); + }); + + describe('when second tag key is added', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + }); + + it('should update tag key', () => { + expect(influxQueryCtrl.target.tags[1].key).toBe('key2'); + }); + + it('should add AND segment', () => { + expect(influxQueryCtrl.tagSegments[3].value).toBe('AND'); + }); + }); + + describe('when condition is changed', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + influxQueryCtrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3); + }); + + it('should update tag condition', () => { + expect(influxQueryCtrl.target.tags[1].condition).toBe('OR'); + }); + + it('should update AND segment', () => { + expect(influxQueryCtrl.tagSegments[3].value).toBe('OR'); + expect(influxQueryCtrl.tagSegments.length).toBe(7); + }); + }); + + describe('when deleting first tag filter after value is selected', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 0); + }); + + it('should remove tags', () => { + expect(influxQueryCtrl.target.tags.length).toBe(0); + }); + + it('should remove all segment after 2 and replace with plus button', () => { + expect(influxQueryCtrl.tagSegments.length).toBe(1); + expect(influxQueryCtrl.tagSegments[0].type).toBe('plus-button'); + }); + }); + + describe('when deleting second tag value before second tag value is complete', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', () => { + expect(influxQueryCtrl.tagSegments.length).toBe(4); + expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); + }); + }); + + describe('when deleting second tag value before second tag value is complete', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', () => { + expect(influxQueryCtrl.tagSegments.length).toBe(4); + expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); + }); + }); + + describe('when deleting second tag value after second tag filter is complete', () => { + beforeEach(() => { + influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + influxQueryCtrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6); + influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', () => { + expect(influxQueryCtrl.tagSegments.length).toBe(4); + expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); + }); + }); +}); diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts deleted file mode 100644 index 4daa48d6b9d..00000000000 --- a/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts +++ /dev/null @@ -1,193 +0,0 @@ -import '../query_ctrl'; -import 'app/core/services/segment_srv'; -import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common'; -import helpers from 'test/specs/helpers'; -import { InfluxQueryCtrl } from '../query_ctrl'; - -describe('InfluxDBQueryCtrl', function() { - var ctx = new helpers.ControllerTestContext(); - - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.controllers')); - beforeEach(angularMocks.module('grafana.services')); - beforeEach( - angularMocks.module(function($compileProvider) { - $compileProvider.preAssignBindingsEnabled(true); - }) - ); - beforeEach(ctx.providePhase()); - - beforeEach( - angularMocks.inject(($rootScope, $controller, $q) => { - ctx.$q = $q; - ctx.scope = $rootScope.$new(); - ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); - ctx.target = { target: {} }; - ctx.panelCtrl = { - panel: { - targets: [ctx.target], - }, - }; - ctx.panelCtrl.refresh = sinon.spy(); - ctx.ctrl = $controller( - InfluxQueryCtrl, - { $scope: ctx.scope }, - { - panelCtrl: ctx.panelCtrl, - target: ctx.target, - datasource: ctx.datasource, - } - ); - }) - ); - - describe('init', function() { - it('should init tagSegments', function() { - expect(ctx.ctrl.tagSegments.length).to.be(1); - }); - - it('should init measurementSegment', function() { - expect(ctx.ctrl.measurementSegment.value).to.be('select measurement'); - }); - }); - - describe('when first tag segment is updated', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - }); - - it('should update tag key', function() { - expect(ctx.ctrl.target.tags[0].key).to.be('asd'); - expect(ctx.ctrl.tagSegments[0].type).to.be('key'); - }); - - it('should add tagSegments', function() { - expect(ctx.ctrl.tagSegments.length).to.be(3); - }); - }); - - describe('when last tag value segment is updated', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - }); - - it('should update tag value', function() { - expect(ctx.ctrl.target.tags[0].value).to.be('server1'); - }); - - it('should set tag operator', function() { - expect(ctx.ctrl.target.tags[0].operator).to.be('='); - }); - - it('should add plus button for another filter', function() { - expect(ctx.ctrl.tagSegments[3].fake).to.be(true); - }); - }); - - describe('when last tag value segment is updated to regex', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2); - }); - - it('should update operator', function() { - expect(ctx.ctrl.tagSegments[1].value).to.be('=~'); - expect(ctx.ctrl.target.tags[0].operator).to.be('=~'); - }); - }); - - describe('when second tag key is added', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - }); - - it('should update tag key', function() { - expect(ctx.ctrl.target.tags[1].key).to.be('key2'); - }); - - it('should add AND segment', function() { - expect(ctx.ctrl.tagSegments[3].value).to.be('AND'); - }); - }); - - describe('when condition is changed', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - ctx.ctrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3); - }); - - it('should update tag condition', function() { - expect(ctx.ctrl.target.tags[1].condition).to.be('OR'); - }); - - it('should update AND segment', function() { - expect(ctx.ctrl.tagSegments[3].value).to.be('OR'); - expect(ctx.ctrl.tagSegments.length).to.be(7); - }); - }); - - describe('when deleting first tag filter after value is selected', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 0); - }); - - it('should remove tags', function() { - expect(ctx.ctrl.target.tags.length).to.be(0); - }); - - it('should remove all segment after 2 and replace with plus button', function() { - expect(ctx.ctrl.tagSegments.length).to.be(1); - expect(ctx.ctrl.tagSegments[0].type).to.be('plus-button'); - }); - }); - - describe('when deleting second tag value before second tag value is complete', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', function() { - expect(ctx.ctrl.tagSegments.length).to.be(4); - expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); - }); - }); - - describe('when deleting second tag value before second tag value is complete', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', function() { - expect(ctx.ctrl.tagSegments.length).to.be(4); - expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); - }); - }); - - describe('when deleting second tag value after second tag filter is complete', function() { - beforeEach(function() { - ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - ctx.ctrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6); - ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', function() { - expect(ctx.ctrl.tagSegments.length).to.be(4); - expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); - }); - }); -}); From d6381bed7cebe7c0270bf0ddacc8333e17fb9658 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Mon, 2 Jul 2018 14:34:58 +0200 Subject: [PATCH 4/6] Test fail depending on test order --- .../plugins/datasource/influxdb/query_ctrl.ts | 2 +- .../influxdb/specs/query_ctrl.jest.ts | 4 +- .../influxdb/specs/query_ctrl_specs.ts | 195 ++++++++++++++++++ 3 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts diff --git a/public/app/plugins/datasource/influxdb/query_ctrl.ts b/public/app/plugins/datasource/influxdb/query_ctrl.ts index ce669c9f458..17449711143 100644 --- a/public/app/plugins/datasource/influxdb/query_ctrl.ts +++ b/public/app/plugins/datasource/influxdb/query_ctrl.ts @@ -338,7 +338,7 @@ export class InfluxQueryCtrl extends QueryCtrl { this.tagSegments.push(this.uiSegmentSrv.newPlusButton()); } } - + console.log(this.tagSegments); this.rebuildTargetTagConditions(); } diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts index dd6c9b4fa18..0c1ed3ed6b2 100644 --- a/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts @@ -46,7 +46,7 @@ describe('InfluxDBQueryCtrl', () => { InfluxQueryCtrl.prototype.panelCtrl = { refresh: jest.fn(), panel: { - targets: InfluxQueryCtrl.prototype.target, + targets: [InfluxQueryCtrl.prototype.target], }, }; InfluxQueryCtrl.prototype.datasource = { @@ -69,6 +69,7 @@ describe('InfluxDBQueryCtrl', () => { new uiSegmentSrv({ trustAsHtml: jest.fn() }, { highlightVariablesAsHtml: jest.fn() }) ); }); + describe('init', () => { it('should init tagSegments', () => { expect(influxQueryCtrl.tagSegments.length).toBe(1); @@ -90,7 +91,6 @@ describe('InfluxDBQueryCtrl', () => { }); it('should add tagSegments', () => { - console.log(influxQueryCtrl.tagSegments); expect(influxQueryCtrl.tagSegments.length).toBe(3); }); }); diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts new file mode 100644 index 00000000000..151dd7ab0c6 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts @@ -0,0 +1,195 @@ +import '../query_ctrl'; +import 'app/core/services/segment_srv'; +import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common'; +import helpers from 'test/specs/helpers'; +import { InfluxQueryCtrl } from '../query_ctrl'; + +describe('InfluxDBQueryCtrl', function() { + var ctx = new helpers.ControllerTestContext(); + + beforeEach(angularMocks.module('grafana.core')); + beforeEach(angularMocks.module('grafana.controllers')); + beforeEach(angularMocks.module('grafana.services')); + beforeEach( + angularMocks.module(function($compileProvider) { + $compileProvider.preAssignBindingsEnabled(true); + }) + ); + beforeEach(ctx.providePhase()); + + beforeEach( + angularMocks.inject(($rootScope, $controller, $q) => { + ctx.$q = $q; + ctx.scope = $rootScope.$new(); + ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); + ctx.target = { target: {} }; + ctx.panelCtrl = { + panel: { + targets: [ctx.target], + }, + }; + ctx.panelCtrl.refresh = sinon.spy(); + ctx.ctrl = $controller( + InfluxQueryCtrl, + { $scope: ctx.scope }, + { + panelCtrl: ctx.panelCtrl, + target: ctx.target, + datasource: ctx.datasource, + } + ); + }) + ); + + describe('init', function() { + it('should init tagSegments', function() { + expect(ctx.ctrl.tagSegments.length).to.be(1); + }); + + it('should init measurementSegment', function() { + expect(ctx.ctrl.measurementSegment.value).to.be('select measurement'); + }); + }); + + describe('when first tag segment is updated', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + }); + + it('should update tag key', function() { + console.log(ctx.ctrl.target.tags); + expect(ctx.ctrl.target.tags[0].key).to.be('asd'); + expect(ctx.ctrl.tagSegments[0].type).to.be('key'); + }); + + it('should add tagSegments', function() { + console.log(ctx.ctrl.tagSegments); + expect(ctx.ctrl.tagSegments.length).to.be(3); + }); + }); + + describe('when last tag value segment is updated', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + }); + + it('should update tag value', function() { + expect(ctx.ctrl.target.tags[0].value).to.be('server1'); + }); + + it('should set tag operator', function() { + expect(ctx.ctrl.target.tags[0].operator).to.be('='); + }); + + it('should add plus button for another filter', function() { + expect(ctx.ctrl.tagSegments[3].fake).to.be(true); + }); + }); + + describe('when last tag value segment is updated to regex', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2); + }); + + it('should update operator', function() { + expect(ctx.ctrl.tagSegments[1].value).to.be('=~'); + expect(ctx.ctrl.target.tags[0].operator).to.be('=~'); + }); + }); + + describe('when second tag key is added', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + }); + + it('should update tag key', function() { + expect(ctx.ctrl.target.tags[1].key).to.be('key2'); + }); + + it('should add AND segment', function() { + expect(ctx.ctrl.tagSegments[3].value).to.be('AND'); + }); + }); + + describe('when condition is changed', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + ctx.ctrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3); + }); + + it('should update tag condition', function() { + expect(ctx.ctrl.target.tags[1].condition).to.be('OR'); + }); + + it('should update AND segment', function() { + expect(ctx.ctrl.tagSegments[3].value).to.be('OR'); + expect(ctx.ctrl.tagSegments.length).to.be(7); + }); + }); + + describe('when deleting first tag filter after value is selected', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 0); + }); + + it('should remove tags', function() { + expect(ctx.ctrl.target.tags.length).to.be(0); + }); + + it('should remove all segment after 2 and replace with plus button', function() { + expect(ctx.ctrl.tagSegments.length).to.be(1); + expect(ctx.ctrl.tagSegments[0].type).to.be('plus-button'); + }); + }); + + describe('when deleting second tag value before second tag value is complete', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', function() { + expect(ctx.ctrl.tagSegments.length).to.be(4); + expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); + }); + }); + + describe('when deleting second tag value before second tag value is complete', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', function() { + expect(ctx.ctrl.tagSegments.length).to.be(4); + expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); + }); + }); + + describe('when deleting second tag value after second tag filter is complete', function() { + beforeEach(function() { + ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); + ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); + ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); + ctx.ctrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6); + ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4); + }); + + it('should remove all segment after 2 and replace with plus button', function() { + expect(ctx.ctrl.tagSegments.length).to.be(4); + expect(ctx.ctrl.tagSegments[3].type).to.be('plus-button'); + }); + }); +}); From 51caf470f50c07fdb7f6d47d7fe022f2ebfc1ac5 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 24 Jul 2018 14:55:54 +0200 Subject: [PATCH 5/6] Remove influx qeury_ctrl jest, as it is already completed --- .../influxdb/specs/query_ctrl.jest.ts | 222 ------------------ .../prometheus/specs/completer.jest.ts | 3 - 2 files changed, 225 deletions(-) delete mode 100644 public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts deleted file mode 100644 index 0c1ed3ed6b2..00000000000 --- a/public/app/plugins/datasource/influxdb/specs/query_ctrl.jest.ts +++ /dev/null @@ -1,222 +0,0 @@ -import '../query_ctrl'; -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 helpers from 'test/specs/helpers'; -import { InfluxQueryCtrl } from '../query_ctrl'; - -describe('InfluxDBQueryCtrl', () => { - //var ctx = new helpers.ControllerTestContext(); - - // beforeEach(angularMocks.module('grafana.core')); - // beforeEach(angularMocks.module('grafana.controllers')); - // beforeEach(angularMocks.module('grafana.services')); - // beforeEach( - // angularMocks.module(($ =>compileProvider) { - // $compileProvider.preAssignBindingsEnabled(true); - // }) - // ); - // beforeEach(ctx.providePhase()); - - // beforeEach( - // angularMocks.inject(($rootScope, $controller, $q) => { - // ctx.$q = $q; - // ctx.scope = $rootScope.$new(); - // ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); - // ctx.target = { target: {} }; - // ctx.panelCtrl = { - // panel: { - // targets: [ctx.target], - // }, - // }; - // ctx.panelCtrl.refresh = sinon.spy(); - // influxQueryCtrl = $controller( - // InfluxQueryCtrl, - // { $scope: ctx.scope }, - // { - // panelCtrl: ctx.panelCtrl, - // target: ctx.target, - // datasource: ctx.datasource, - // } - // ); - // }) - // ); - - InfluxQueryCtrl.prototype.target = { target: {} }; - InfluxQueryCtrl.prototype.panelCtrl = { - refresh: jest.fn(), - panel: { - targets: [InfluxQueryCtrl.prototype.target], - }, - }; - InfluxQueryCtrl.prototype.datasource = { - metricFindQuery: jest.fn(() => Promise.resolve([])), - }; - - // let uiSegmentSrv = { - // newPlusButton: jest.fn(), - // newSegment: jest.fn(), - // newSelectMeasurement: jest.fn() - // }; - let influxQueryCtrl; - - beforeEach(() => { - influxQueryCtrl = new InfluxQueryCtrl( - {}, - {}, - {}, - {}, - new uiSegmentSrv({ trustAsHtml: jest.fn() }, { highlightVariablesAsHtml: jest.fn() }) - ); - }); - - describe('init', () => { - it('should init tagSegments', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(1); - }); - - it('should init measurementSegment', () => { - expect(influxQueryCtrl.measurementSegment.value).toBe('select measurement'); - }); - }); - - describe('when first tag segment is updated', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - }); - - it('should update tag key', () => { - expect(influxQueryCtrl.target.tags[0].key).toBe('asd'); - expect(influxQueryCtrl.tagSegments[0].type).toBe('key'); - }); - - it('should add tagSegments', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(3); - }); - }); - - describe('when last tag value segment is updated', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - }); - - it('should update tag value', () => { - expect(influxQueryCtrl.target.tags[0].value).toBe('server1'); - }); - - it('should set tag operator', () => { - expect(influxQueryCtrl.target.tags[0].operator).toBe('='); - }); - - it('should add plus button for another filter', () => { - expect(influxQueryCtrl.tagSegments[3].fake).toBe(true); - }); - }); - - describe('when last tag value segment is updated to regex', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2); - }); - - it('should update operator', () => { - expect(influxQueryCtrl.tagSegments[1].value).toBe('=~'); - expect(influxQueryCtrl.target.tags[0].operator).toBe('=~'); - }); - }); - - describe('when second tag key is added', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - }); - - it('should update tag key', () => { - expect(influxQueryCtrl.target.tags[1].key).toBe('key2'); - }); - - it('should add AND segment', () => { - expect(influxQueryCtrl.tagSegments[3].value).toBe('AND'); - }); - }); - - describe('when condition is changed', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - influxQueryCtrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3); - }); - - it('should update tag condition', () => { - expect(influxQueryCtrl.target.tags[1].condition).toBe('OR'); - }); - - it('should update AND segment', () => { - expect(influxQueryCtrl.tagSegments[3].value).toBe('OR'); - expect(influxQueryCtrl.tagSegments.length).toBe(7); - }); - }); - - describe('when deleting first tag filter after value is selected', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 0); - }); - - it('should remove tags', () => { - expect(influxQueryCtrl.target.tags.length).toBe(0); - }); - - it('should remove all segment after 2 and replace with plus button', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(1); - expect(influxQueryCtrl.tagSegments[0].type).toBe('plus-button'); - }); - }); - - describe('when deleting second tag value before second tag value is complete', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(4); - expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); - }); - }); - - describe('when deleting second tag value before second tag value is complete', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(4); - expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); - }); - }); - - describe('when deleting second tag value after second tag filter is complete', () => { - beforeEach(() => { - influxQueryCtrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0); - influxQueryCtrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2); - influxQueryCtrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3); - influxQueryCtrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6); - influxQueryCtrl.tagSegmentUpdated(influxQueryCtrl.removeTagFilterSegment, 4); - }); - - it('should remove all segment after 2 and replace with plus button', () => { - expect(influxQueryCtrl.tagSegments.length).toBe(4); - expect(influxQueryCtrl.tagSegments[3].type).toBe('plus-button'); - }); - }); -}); diff --git a/public/app/plugins/datasource/prometheus/specs/completer.jest.ts b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts index b401cb9bf65..fbe2dce0ce5 100644 --- a/public/app/plugins/datasource/prometheus/specs/completer.jest.ts +++ b/public/app/plugins/datasource/prometheus/specs/completer.jest.ts @@ -1,6 +1,3 @@ -//import { describe, it, sinon, expect } from 'test/lib/common'; -//import helpers from 'test/specs/helpers'; - import { PromCompleter } from '../completer'; import { PrometheusDatasource } from '../datasource'; import { BackendSrv } from 'app/core/services/backend_srv'; From 27c081349fb11f1ad8d304873aa9cc92a45a2027 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Tue, 24 Jul 2018 17:03:58 +0200 Subject: [PATCH 6/6] Remove old influx stuff --- public/app/plugins/datasource/influxdb/query_ctrl.ts | 2 +- .../app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/query_ctrl.ts b/public/app/plugins/datasource/influxdb/query_ctrl.ts index 17449711143..ce669c9f458 100644 --- a/public/app/plugins/datasource/influxdb/query_ctrl.ts +++ b/public/app/plugins/datasource/influxdb/query_ctrl.ts @@ -338,7 +338,7 @@ export class InfluxQueryCtrl extends QueryCtrl { this.tagSegments.push(this.uiSegmentSrv.newPlusButton()); } } - console.log(this.tagSegments); + this.rebuildTargetTagConditions(); } diff --git a/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts b/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts index 151dd7ab0c6..4daa48d6b9d 100644 --- a/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/query_ctrl_specs.ts @@ -57,13 +57,11 @@ describe('InfluxDBQueryCtrl', function() { }); it('should update tag key', function() { - console.log(ctx.ctrl.target.tags); expect(ctx.ctrl.target.tags[0].key).to.be('asd'); expect(ctx.ctrl.tagSegments[0].type).to.be('key'); }); it('should add tagSegments', function() { - console.log(ctx.ctrl.tagSegments); expect(ctx.ctrl.tagSegments.length).to.be(3); }); });