query-ctrl-specs.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import helpers from 'test/specs/helpers';
  3. import {OpenTsQueryCtrl} from "../query_ctrl";
  4. describe('OpenTsQueryCtrl', function() {
  5. var ctx = new helpers.ControllerTestContext();
  6. beforeEach(angularMocks.module('grafana.core'));
  7. beforeEach(angularMocks.module('grafana.services'));
  8. beforeEach(ctx.providePhase(['backendSrv','templateSrv']));
  9. beforeEach(ctx.providePhase());
  10. beforeEach(angularMocks.inject(($rootScope, $controller, $q) => {
  11. ctx.$q = $q;
  12. ctx.scope = $rootScope.$new();
  13. ctx.target = {target: ''};
  14. ctx.panelCtrl = {panel: {}};
  15. ctx.panelCtrl.refresh = sinon.spy();
  16. ctx.datasource.getAggregators = sinon.stub().returns(ctx.$q.when([]));
  17. ctx.datasource.getFilterTypes = sinon.stub().returns(ctx.$q.when([]));
  18. ctx.ctrl = $controller(OpenTsQueryCtrl, {$scope: ctx.scope}, {
  19. panelCtrl: ctx.panelCtrl,
  20. datasource: ctx.datasource,
  21. target: ctx.target,
  22. });
  23. ctx.scope.$digest();
  24. }));
  25. describe('init query_ctrl variables', function() {
  26. it('filter types should be initialized', function() {
  27. expect(ctx.ctrl.filterTypes.length).to.be(7);
  28. });
  29. it('aggregators should be initialized', function() {
  30. expect(ctx.ctrl.aggregators.length).to.be(8);
  31. });
  32. it('fill policy options should be initialized', function() {
  33. expect(ctx.ctrl.fillPolicies.length).to.be(4);
  34. });
  35. });
  36. describe('when adding filters and tags', function() {
  37. it('addTagMode should be false when closed', function() {
  38. ctx.ctrl.addTagMode = true;
  39. ctx.ctrl.closeAddTagMode();
  40. expect(ctx.ctrl.addTagMode).to.be(false);
  41. });
  42. it('addFilterMode should be false when closed', function() {
  43. ctx.ctrl.addFilterMode = true;
  44. ctx.ctrl.closeAddFilterMode();
  45. expect(ctx.ctrl.addFilterMode).to.be(false);
  46. });
  47. it('removing a tag from the tags list', function() {
  48. ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"};
  49. ctx.ctrl.removeTag("tagk");
  50. expect(Object.keys(ctx.ctrl.target.tags).length).to.be(1);
  51. });
  52. it('removing a filter from the filters list', function() {
  53. ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}];
  54. ctx.ctrl.removeFilter(0);
  55. expect(ctx.ctrl.target.filters.length).to.be(0);
  56. });
  57. it('adding a filter when tags exist should generate error', function() {
  58. ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"};
  59. ctx.ctrl.addFilter();
  60. expect(ctx.ctrl.errors.filters).to.be('Please remove tags to use filters, tags and filters are mutually exclusive.');
  61. });
  62. it('adding a tag when filters exist should generate error', function() {
  63. ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}];
  64. ctx.ctrl.addTag();
  65. expect(ctx.ctrl.errors.tags).to.be('Please remove filters to use tags, tags and filters are mutually exclusive.');
  66. });
  67. });
  68. });