query_ctrl.jest.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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', () => {
  7. let uiSegmentSrv = {
  8. newPlusButton: () => {},
  9. newKey: key => key,
  10. newKeyValue: key => key,
  11. newSegment: seg => seg,
  12. newSelectMeasurement: () => {
  13. return { value: 'select measurement' };
  14. },
  15. newOperator: op => op,
  16. newFake: () => {},
  17. };
  18. let ctx = <any>{
  19. dataSource: {},
  20. };
  21. // beforeEach(angularMocks.module('grafana.core'));
  22. // beforeEach(angularMocks.module('grafana.controllers'));
  23. // beforeEach(angularMocks.module('grafana.services'));
  24. // beforeEach(
  25. // angularMocks.module(($ =>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(async () => {
  54. InfluxQueryCtrl.prototype.datasource = {
  55. metricFindQuery: jest.fn(() => Promise.resolve([])),
  56. };
  57. InfluxQueryCtrl.prototype.panelCtrl = {
  58. panel: {
  59. targets: [InfluxQueryCtrl.target],
  60. },
  61. };
  62. InfluxQueryCtrl.prototype.target = { target: {} };
  63. console.log('creating new instance');
  64. ctx.ctrl = await new InfluxQueryCtrl({}, {}, {}, {}, uiSegmentSrv);
  65. });
  66. describe('init', () => {
  67. it('should init tagSegments', () => {
  68. expect(ctx.ctrl.tagSegments.length).toBe(1);
  69. });
  70. it('should init measurementSegment', () => {
  71. expect(ctx.ctrl.measurementSegment.value).toBe('select measurement');
  72. });
  73. });
  74. describe('when first tag segment is updated', () => {
  75. beforeEach(() => {
  76. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  77. });
  78. it('should update tag key', () => {
  79. expect(ctx.ctrl.target.tags[0].key).toBe('asd');
  80. expect(ctx.ctrl.tagSegments[0].type).toBe('key');
  81. });
  82. it('should add tagSegments', () => {
  83. expect(ctx.ctrl.tagSegments.length).toBe(3);
  84. });
  85. });
  86. describe('when last tag value segment is updated', () => {
  87. beforeEach(() => {
  88. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  89. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  90. });
  91. it('should update tag value', () => {
  92. expect(ctx.ctrl.target.tags[0].value).toBe('server1');
  93. });
  94. it('should set tag operator', () => {
  95. expect(ctx.ctrl.target.tags[0].operator).toBe('=');
  96. });
  97. it('should add plus button for another filter', () => {
  98. expect(ctx.ctrl.tagSegments[3].fake).toBe(true);
  99. });
  100. });
  101. describe('when last tag value segment is updated to regex', () => {
  102. beforeEach(() => {
  103. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  104. ctx.ctrl.tagSegmentUpdated({ value: '/server.*/', type: 'value' }, 2);
  105. });
  106. it('should update operator', () => {
  107. expect(ctx.ctrl.tagSegments[1].value).toBe('=~');
  108. expect(ctx.ctrl.target.tags[0].operator).toBe('=~');
  109. });
  110. });
  111. describe('when second tag key is added', () => {
  112. beforeEach(() => {
  113. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  114. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  115. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  116. });
  117. it('should update tag key', () => {
  118. expect(ctx.ctrl.target.tags[1].key).toBe('key2');
  119. });
  120. it('should add AND segment', () => {
  121. expect(ctx.ctrl.tagSegments[3].value).toBe('AND');
  122. });
  123. });
  124. describe('when condition is changed', () => {
  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({ value: 'OR', type: 'condition' }, 3);
  130. });
  131. it('should update tag condition', () => {
  132. expect(ctx.ctrl.target.tags[1].condition).toBe('OR');
  133. });
  134. it('should update AND segment', () => {
  135. expect(ctx.ctrl.tagSegments[3].value).toBe('OR');
  136. expect(ctx.ctrl.tagSegments.length).toBe(7);
  137. });
  138. });
  139. describe('when deleting first tag filter after value is selected', () => {
  140. beforeEach(() => {
  141. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  142. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  143. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 0);
  144. });
  145. it('should remove tags', () => {
  146. expect(ctx.ctrl.target.tags.length).toBe(0);
  147. });
  148. it('should remove all segment after 2 and replace with plus button', () => {
  149. expect(ctx.ctrl.tagSegments.length).toBe(1);
  150. expect(ctx.ctrl.tagSegments[0].type).toBe('plus-button');
  151. });
  152. });
  153. describe('when deleting second tag value before second tag value is complete', () => {
  154. beforeEach(() => {
  155. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  156. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  157. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  158. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  159. });
  160. it('should remove all segment after 2 and replace with plus button', () => {
  161. expect(ctx.ctrl.tagSegments.length).toBe(4);
  162. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  163. });
  164. });
  165. describe('when deleting second tag value before second tag value is complete', () => {
  166. beforeEach(() => {
  167. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  168. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  169. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  170. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  171. });
  172. it('should remove all segment after 2 and replace with plus button', () => {
  173. expect(ctx.ctrl.tagSegments.length).toBe(4);
  174. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  175. });
  176. });
  177. describe('when deleting second tag value after second tag filter is complete', () => {
  178. beforeEach(() => {
  179. ctx.ctrl.tagSegmentUpdated({ value: 'asd', type: 'plus-button' }, 0);
  180. ctx.ctrl.tagSegmentUpdated({ value: 'server1', type: 'value' }, 2);
  181. ctx.ctrl.tagSegmentUpdated({ value: 'key2', type: 'plus-button' }, 3);
  182. ctx.ctrl.tagSegmentUpdated({ value: 'value', type: 'value' }, 6);
  183. ctx.ctrl.tagSegmentUpdated(ctx.ctrl.removeTagFilterSegment, 4);
  184. });
  185. it('should remove all segment after 2 and replace with plus button', () => {
  186. expect(ctx.ctrl.tagSegments.length).toBe(4);
  187. expect(ctx.ctrl.tagSegments[3].type).toBe('plus-button');
  188. });
  189. });
  190. });