query-ctrl-specs.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {
  2. describe,
  3. beforeEach,
  4. it,
  5. sinon,
  6. expect,
  7. angularMocks
  8. } from "test/lib/common";
  9. import helpers from "test/specs/helpers";
  10. import { OpenTsQueryCtrl } from "../query_ctrl";
  11. describe("OpenTsQueryCtrl", function() {
  12. var ctx = new helpers.ControllerTestContext();
  13. beforeEach(angularMocks.module("grafana.core"));
  14. beforeEach(angularMocks.module("grafana.services"));
  15. beforeEach(
  16. angularMocks.module(function($compileProvider) {
  17. $compileProvider.preAssignBindingsEnabled(true);
  18. })
  19. );
  20. beforeEach(ctx.providePhase(["backendSrv", "templateSrv"]));
  21. beforeEach(ctx.providePhase());
  22. beforeEach(
  23. angularMocks.inject(($rootScope, $controller, $q) => {
  24. ctx.$q = $q;
  25. ctx.scope = $rootScope.$new();
  26. ctx.target = { target: "" };
  27. ctx.panelCtrl = {
  28. panel: {
  29. targets: [ctx.target]
  30. }
  31. };
  32. ctx.panelCtrl.refresh = sinon.spy();
  33. ctx.datasource.getAggregators = sinon.stub().returns(ctx.$q.when([]));
  34. ctx.datasource.getFilterTypes = sinon.stub().returns(ctx.$q.when([]));
  35. ctx.ctrl = $controller(
  36. OpenTsQueryCtrl,
  37. { $scope: ctx.scope },
  38. {
  39. panelCtrl: ctx.panelCtrl,
  40. datasource: ctx.datasource,
  41. target: ctx.target
  42. }
  43. );
  44. ctx.scope.$digest();
  45. })
  46. );
  47. describe("init query_ctrl variables", function() {
  48. it("filter types should be initialized", function() {
  49. expect(ctx.ctrl.filterTypes.length).to.be(7);
  50. });
  51. it("aggregators should be initialized", function() {
  52. expect(ctx.ctrl.aggregators.length).to.be(8);
  53. });
  54. it("fill policy options should be initialized", function() {
  55. expect(ctx.ctrl.fillPolicies.length).to.be(4);
  56. });
  57. });
  58. describe("when adding filters and tags", function() {
  59. it("addTagMode should be false when closed", function() {
  60. ctx.ctrl.addTagMode = true;
  61. ctx.ctrl.closeAddTagMode();
  62. expect(ctx.ctrl.addTagMode).to.be(false);
  63. });
  64. it("addFilterMode should be false when closed", function() {
  65. ctx.ctrl.addFilterMode = true;
  66. ctx.ctrl.closeAddFilterMode();
  67. expect(ctx.ctrl.addFilterMode).to.be(false);
  68. });
  69. it("removing a tag from the tags list", function() {
  70. ctx.ctrl.target.tags = { tagk: "tag_key", tagk2: "tag_value2" };
  71. ctx.ctrl.removeTag("tagk");
  72. expect(Object.keys(ctx.ctrl.target.tags).length).to.be(1);
  73. });
  74. it("removing a filter from the filters list", function() {
  75. ctx.ctrl.target.filters = [
  76. {
  77. tagk: "tag_key",
  78. filter: "tag_value2",
  79. type: "wildcard",
  80. groupBy: true
  81. }
  82. ];
  83. ctx.ctrl.removeFilter(0);
  84. expect(ctx.ctrl.target.filters.length).to.be(0);
  85. });
  86. it("adding a filter when tags exist should generate error", function() {
  87. ctx.ctrl.target.tags = { tagk: "tag_key", tagk2: "tag_value2" };
  88. ctx.ctrl.addFilter();
  89. expect(ctx.ctrl.errors.filters).to.be(
  90. "Please remove tags to use filters, tags and filters are mutually exclusive."
  91. );
  92. });
  93. it("adding a tag when filters exist should generate error", function() {
  94. ctx.ctrl.target.filters = [
  95. {
  96. tagk: "tag_key",
  97. filter: "tag_value2",
  98. type: "wildcard",
  99. groupBy: true
  100. }
  101. ];
  102. ctx.ctrl.addTag();
  103. expect(ctx.ctrl.errors.tags).to.be(
  104. "Please remove filters to use tags, tags and filters are mutually exclusive."
  105. );
  106. });
  107. });
  108. });