query-ctrl-specs.ts 3.2 KB

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