query_ctrl.jest.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import '../query_ctrl';
  2. import 'app/core/services/segment_srv';
  3. // import { describe, beforeEach, it, sinon, expect, angularMocks } from 'test/lib/common';
  4. // import helpers from 'test/specs/helpers';
  5. import { InfluxQueryCtrl } from '../query_ctrl';
  6. describe('InfluxDBQueryCtrl', function() {
  7. let uiSegmentSrv = {
  8. newPlusButton: () => {},
  9. };
  10. let ctx = <any>{
  11. dataSource: {
  12. metricFindQuery: jest.fn(() => Promise.resolve([])),
  13. },
  14. };
  15. InfluxQueryCtrl.prototype.panelCtrl = {
  16. target: { target: {} },
  17. panel: {
  18. targets: [this.target],
  19. },
  20. };
  21. // beforeEach(angularMocks.module('grafana.core'));
  22. // beforeEach(angularMocks.module('grafana.controllers'));
  23. // beforeEach(angularMocks.module('grafana.services'));
  24. // beforeEach(
  25. // angularMocks.module(function($compileProvider) {
  26. // $compileProvider.preAssignBindingsEnabled(true);
  27. // })
  28. // );
  29. // beforeEach(ctx.providePhase());
  30. // beforeEach(
  31. // angularMocks.inject(($rootScope, $controller, $q) => {
  32. // ctx.$q = $q;
  33. // ctx.scope = $rootScope.$new();
  34. // ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  35. // ctx.target = { target: {} };
  36. // ctx.panelCtrl = {
  37. // panel: {
  38. // targets: [ctx.target],
  39. // },
  40. // };
  41. // ctx.panelCtrl.refresh = sinon.spy();
  42. // ctx.ctrl = $controller(
  43. // InfluxQueryCtrl,
  44. // { $scope: ctx.scope },
  45. // {
  46. // panelCtrl: ctx.panelCtrl,
  47. // target: ctx.target,
  48. // datasource: ctx.datasource,
  49. // }
  50. // );
  51. // })
  52. // );
  53. beforeEach(() => {
  54. ctx.ctrl = new InfluxQueryCtrl({}, {}, {}, {}, uiSegmentSrv);
  55. });
  56. describe('init', function() {
  57. it('should init tagSegments', function() {
  58. expect(ctx.ctrl.tagSegments.length).toBe(1);
  59. });
  60. it('should init measurementSegment', function() {
  61. expect(ctx.ctrl.measurementSegment.value).toBe('select measurement');
  62. });
  63. });
  64. describe('when first tag segment is updated', function() {
  65. beforeEach(function() {
  66. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  67. });
  68. it('should update tag key', function() {
  69. expect(ctx.ctrl.target.tags[0].key).toBe('asd');
  70. expect(ctx.ctrl.tagSegments[0].type).toBe('key');
  71. });
  72. it('should add tagSegments', function() {
  73. expect(ctx.ctrl.tagSegments.length).toBe(3);
  74. });
  75. });
  76. describe('when last tag value segment is updated', function() {
  77. beforeEach(function() {
  78. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  79. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  80. });
  81. it('should update tag value', function() {
  82. expect(ctx.ctrl.target.tags[0].value).toBe('server1');
  83. });
  84. it('should set tag operator', function() {
  85. expect(ctx.ctrl.target.tags[0].operator).toBe('=');
  86. });
  87. it('should add plus button for another filter', function() {
  88. expect(ctx.ctrl.tagSegments[3].fake).toBe(true);
  89. });
  90. });
  91. describe('when last tag value segment is updated to regex', function() {
  92. beforeEach(function() {
  93. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  94. ctx.ctrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2);
  95. });
  96. it('should update operator', function() {
  97. expect(ctx.ctrl.tagSegments[1].value).toBe('=~');
  98. expect(ctx.ctrl.target.tags[0].operator).toBe('=~');
  99. });
  100. });
  101. describe('when second tag key is added', function() {
  102. beforeEach(function() {
  103. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  104. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  105. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  106. });
  107. it('should update tag key', function() {
  108. expect(ctx.ctrl.target.tags[1].key).toBe('key2');
  109. });
  110. it('should add AND segment', function() {
  111. expect(ctx.ctrl.tagSegments[3].value).toBe('AND');
  112. });
  113. });
  114. describe('when condition is changed', function() {
  115. beforeEach(function() {
  116. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  117. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  118. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  119. ctx.ctrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3);
  120. });
  121. it('should update tag condition', function() {
  122. expect(ctx.ctrl.target.tags[1].condition).toBe('OR');
  123. });
  124. it('should update AND segment', function() {
  125. expect(ctx.ctrl.tagSegments[3].value).toBe('OR');
  126. expect(ctx.ctrl.tagSegments.length).toBe(7);
  127. });
  128. });
  129. describe('when deleting first tag filter after value is selected', function() {
  130. beforeEach(function() {
  131. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  132. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  133. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 0);
  134. });
  135. it('should remove tags', function() {
  136. expect(ctx.ctrl.target.tags.length).toBe(0);
  137. });
  138. it('should remove all segment after 2 and replace with plus button', function() {
  139. expect(ctx.ctrl.tagSegments.length).toBe(1);
  140. expect(ctx.ctrl.tagSegments[0].type).toBe('plus-button');
  141. });
  142. });
  143. describe('when deleting second tag value before second tag value is complete', function() {
  144. beforeEach(function() {
  145. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  146. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  147. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  148. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  149. });
  150. it('should remove all segment after 2 and replace with plus button', function() {
  151. expect(ctx.ctrl.tagSegments.length).toBe(4);
  152. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  153. });
  154. });
  155. describe('when deleting second tag value before second tag value is complete', function() {
  156. beforeEach(function() {
  157. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  158. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  159. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  160. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  161. });
  162. it('should remove all segment after 2 and replace with plus button', function() {
  163. expect(ctx.ctrl.tagSegments.length).toBe(4);
  164. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  165. });
  166. });
  167. describe('when deleting second tag value after second tag filter is complete', function() {
  168. beforeEach(function() {
  169. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  170. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  171. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  172. ctx.ctrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6);
  173. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  174. });
  175. it('should remove all segment after 2 and replace with plus button', function() {
  176. expect(ctx.ctrl.tagSegments.length).toBe(4);
  177. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  178. });
  179. });
  180. });