query_ctrl.test.ts 6.1 KB

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