query-ctrl-specs.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.ctrl = $controller(OpenTsQueryCtrl, {$scope: ctx.scope}, {
  18. panelCtrl: ctx.panelCtrl,
  19. datasource: ctx.datasource,
  20. target: ctx.target,
  21. });
  22. ctx.scope.$digest();
  23. }));
  24. describe('init query_ctrl variables', function() {
  25. it('filter types should be initialized', function() {
  26. expect(ctx.ctrl.filterTypes.length).to.be(7);
  27. });
  28. it('aggregators should be initialized', function() {
  29. expect(ctx.ctrl.aggregators.length).to.be(8);
  30. });
  31. it('fill policy options should be initialized', function() {
  32. expect(ctx.ctrl.fillPolicies.length).to.be(4);
  33. });
  34. });
  35. describe('when adding filters and tags', function() {
  36. it('addTagMode should be false when closed', function() {
  37. ctx.ctrl.addTagMode = true;
  38. ctx.ctrl.closeAddTagMode();
  39. expect(ctx.ctrl.addTagMode).to.be(false);
  40. });
  41. it('addFilterMode should be false when closed', function() {
  42. ctx.ctrl.addFilterMode = true;
  43. ctx.ctrl.closeAddFilterMode();
  44. expect(ctx.ctrl.addFilterMode).to.be(false);
  45. });
  46. it('removing a tag from the tags list', function() {
  47. ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"};
  48. ctx.ctrl.removeTag("tagk");
  49. expect(Object.keys(ctx.ctrl.target.tags).length).to.be(1);
  50. });
  51. it('removing a filter from the filters list', function() {
  52. ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}];
  53. ctx.ctrl.removeFilter(0);
  54. expect(ctx.ctrl.target.filters.length).to.be(0);
  55. });
  56. it('adding a filter when tags exist should generate error', function() {
  57. ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"};
  58. ctx.ctrl.addFilter();
  59. expect(ctx.ctrl.errors.filters).to.be('Please remove tags to use filters, tags and filters are mutually exclusive.');
  60. });
  61. it('adding a tag when filters exist should generate error', function() {
  62. ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}];
  63. ctx.ctrl.addTag();
  64. expect(ctx.ctrl.errors.tags).to.be('Please remove filters to use tags, tags and filters are mutually exclusive.');
  65. });
  66. });
  67. });