query_ctrl.jest.ts 6.2 KB

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