query_ctrl.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import '../query_ctrl';
  2. import { uiSegmentSrv } from 'app/core/services/segment_srv';
  3. import { InfluxQueryCtrl } from '../query_ctrl';
  4. describe('InfluxDBQueryCtrl', () => {
  5. const ctx = {} as any;
  6. beforeEach(() => {
  7. InfluxQueryCtrl.prototype.datasource = {
  8. metricFindQuery: () => Promise.resolve([]),
  9. };
  10. InfluxQueryCtrl.prototype.target = { target: {} };
  11. InfluxQueryCtrl.prototype.panelCtrl = {
  12. panel: {
  13. targets: [InfluxQueryCtrl.prototype.target],
  14. },
  15. refresh: () => {},
  16. };
  17. ctx.ctrl = new InfluxQueryCtrl(
  18. {},
  19. {},
  20. {},
  21. {},
  22. new uiSegmentSrv({ trustAsHtml: html => html }, { highlightVariablesAsHtml: () => {} })
  23. );
  24. });
  25. describe('init', () => {
  26. it('should init tagSegments', () => {
  27. expect(ctx.ctrl.tagSegments.length).toBe(1);
  28. });
  29. it('should init measurementSegment', () => {
  30. expect(ctx.ctrl.measurementSegment.value).toBe('select measurement');
  31. });
  32. });
  33. describe('when first tag segment is updated', () => {
  34. beforeEach(() => {
  35. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  36. });
  37. it('should update tag key', () => {
  38. expect(ctx.ctrl.target.tags[0].key).toBe('asd');
  39. expect(ctx.ctrl.tagSegments[0].type).toBe('key');
  40. });
  41. it('should add tagSegments', () => {
  42. expect(ctx.ctrl.tagSegments.length).toBe(3);
  43. });
  44. });
  45. describe('when last tag value segment is updated', () => {
  46. beforeEach(() => {
  47. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  48. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  49. });
  50. it('should update tag value', () => {
  51. expect(ctx.ctrl.target.tags[0].value).toBe('server1');
  52. });
  53. it('should set tag operator', () => {
  54. expect(ctx.ctrl.target.tags[0].operator).toBe('=');
  55. });
  56. it('should add plus button for another filter', () => {
  57. expect(ctx.ctrl.tagSegments[3].fake).toBe(true);
  58. });
  59. });
  60. describe('when last tag value segment is updated to regex', () => {
  61. beforeEach(() => {
  62. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  63. ctx.ctrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2);
  64. });
  65. it('should update operator', () => {
  66. expect(ctx.ctrl.tagSegments[1].value).toBe('=~');
  67. expect(ctx.ctrl.target.tags[0].operator).toBe('=~');
  68. });
  69. });
  70. describe('when second tag key is added', () => {
  71. beforeEach(() => {
  72. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  73. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  74. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  75. });
  76. it('should update tag key', () => {
  77. expect(ctx.ctrl.target.tags[1].key).toBe('key2');
  78. });
  79. it('should add AND segment', () => {
  80. expect(ctx.ctrl.tagSegments[3].value).toBe('AND');
  81. });
  82. });
  83. describe('when condition is changed', () => {
  84. beforeEach(() => {
  85. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  86. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  87. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  88. ctx.ctrl.tagSegmentUpdated({ value: 'OR', type: 'condition' }, 3);
  89. });
  90. it('should update tag condition', () => {
  91. expect(ctx.ctrl.target.tags[1].condition).toBe('OR');
  92. });
  93. it('should update AND segment', () => {
  94. expect(ctx.ctrl.tagSegments[3].value).toBe('OR');
  95. expect(ctx.ctrl.tagSegments.length).toBe(7);
  96. });
  97. });
  98. describe('when deleting first tag filter after value is selected', () => {
  99. beforeEach(() => {
  100. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  101. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  102. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 0);
  103. });
  104. it('should remove tags', () => {
  105. expect(ctx.ctrl.target.tags.length).toBe(0);
  106. });
  107. it('should remove all segment after 2 and replace with plus button', () => {
  108. expect(ctx.ctrl.tagSegments.length).toBe(1);
  109. expect(ctx.ctrl.tagSegments[0].type).toBe('plus-button');
  110. });
  111. });
  112. describe('when deleting second tag value before second tag value is complete', () => {
  113. beforeEach(() => {
  114. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  115. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  116. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  117. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  118. });
  119. it('should remove all segment after 2 and replace with plus button', () => {
  120. expect(ctx.ctrl.tagSegments.length).toBe(4);
  121. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  122. });
  123. });
  124. describe('when deleting second tag value before second tag value is complete', () => {
  125. beforeEach(() => {
  126. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  127. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  128. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  129. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  130. });
  131. it('should remove all segment after 2 and replace with plus button', () => {
  132. expect(ctx.ctrl.tagSegments.length).toBe(4);
  133. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  134. });
  135. });
  136. describe('when deleting second tag value after second tag filter is complete', () => {
  137. beforeEach(() => {
  138. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  139. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  140. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  141. ctx.ctrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6);
  142. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  143. });
  144. it('should remove all segment after 2 and replace with plus button', () => {
  145. expect(ctx.ctrl.tagSegments.length).toBe(4);
  146. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  147. });
  148. });
  149. });