functions.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { AGGREGATIONS, SYSTEM_LABELS } from './constants';
  2. import {
  3. extractServicesFromMetricDescriptors,
  4. getAggregationOptionsByMetric,
  5. getAlignmentOptionsByMetric,
  6. getAlignmentPickerData,
  7. getLabelKeys,
  8. getMetricTypes,
  9. getMetricTypesByService,
  10. labelsToGroupedOptions,
  11. stringArrayToFilters,
  12. } from './functions';
  13. import { newMockDatasource } from './specs/testData';
  14. import { AlignmentTypes, MetricDescriptor, MetricKind, ValueTypes } from './types';
  15. jest.mock('@grafana/runtime', () => ({
  16. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  17. getTemplateSrv: () => ({
  18. replace: jest.fn().mockImplementation((s: string) => s),
  19. }),
  20. }));
  21. describe('functions', () => {
  22. describe('extractServicesFromMetricDescriptors', () => {
  23. it('should return unique metric descriptors', () => {
  24. const desc: MetricDescriptor = {
  25. valueType: '',
  26. metricKind: MetricKind.CUMULATIVE,
  27. type: '',
  28. unit: '',
  29. service: '1',
  30. serviceShortName: '',
  31. displayName: '',
  32. description: '',
  33. };
  34. expect(extractServicesFromMetricDescriptors([desc, desc])).toEqual([desc]);
  35. });
  36. });
  37. describe('getMetricTypesByService', () => {
  38. it('filters by metric descriptiors', () => {
  39. const desc1: MetricDescriptor = {
  40. valueType: '',
  41. metricKind: MetricKind.CUMULATIVE,
  42. type: '',
  43. unit: '',
  44. service: '1',
  45. serviceShortName: '',
  46. displayName: '',
  47. description: '',
  48. };
  49. const desc2: MetricDescriptor = {
  50. valueType: '',
  51. metricKind: MetricKind.CUMULATIVE,
  52. type: '',
  53. unit: '',
  54. service: '2',
  55. serviceShortName: '',
  56. displayName: '',
  57. description: '',
  58. };
  59. expect(getMetricTypesByService([desc1, desc2], '1')).toEqual([desc1]);
  60. });
  61. });
  62. describe('getMetricTypes', () => {
  63. it('gets metric type that exists in the array', () => {
  64. const desc1: MetricDescriptor = {
  65. valueType: '',
  66. metricKind: MetricKind.CUMULATIVE,
  67. type: '1',
  68. unit: '',
  69. service: 'svc1',
  70. serviceShortName: '',
  71. displayName: 'uno',
  72. description: '',
  73. };
  74. const desc2: MetricDescriptor = {
  75. valueType: '',
  76. metricKind: MetricKind.CUMULATIVE,
  77. type: '2',
  78. unit: '',
  79. service: 'svc2',
  80. serviceShortName: '',
  81. displayName: 'dos',
  82. description: '',
  83. };
  84. expect(getMetricTypes([desc1, desc2], '1', '1', 'svc1')).toEqual({
  85. metricTypes: [{ name: 'uno', value: '1' }],
  86. selectedMetricType: '1',
  87. });
  88. });
  89. it('gets metric type that does not exist in the array', () => {
  90. const desc1: MetricDescriptor = {
  91. valueType: '',
  92. metricKind: MetricKind.CUMULATIVE,
  93. type: '1',
  94. unit: '',
  95. service: 'svc1',
  96. serviceShortName: '',
  97. displayName: 'uno',
  98. description: '',
  99. };
  100. const desc2: MetricDescriptor = {
  101. valueType: '',
  102. metricKind: MetricKind.CUMULATIVE,
  103. type: '2',
  104. unit: '',
  105. service: 'svc2',
  106. serviceShortName: '',
  107. displayName: 'dos',
  108. description: '',
  109. };
  110. expect(getMetricTypes([desc1, desc2], '3', '4', 'svc1')).toEqual({
  111. metricTypes: [{ name: 'uno', value: '1' }],
  112. selectedMetricType: '1',
  113. });
  114. });
  115. });
  116. describe('getAlignmentOptionsByMetric', () => {
  117. let result: any;
  118. describe('when double and gauge is passed', () => {
  119. beforeEach(() => {
  120. result = getAlignmentOptionsByMetric(ValueTypes.DOUBLE, MetricKind.GAUGE);
  121. });
  122. it('should return all alignment options except two', () => {
  123. expect(result.length).toBe(10);
  124. expect(result.map((o: any) => o.value)).toEqual(
  125. expect.not.arrayContaining(['REDUCE_COUNT_TRUE', 'REDUCE_COUNT_FALSE'])
  126. );
  127. });
  128. });
  129. describe('when double and delta is passed', () => {
  130. beforeEach(() => {
  131. result = getAlignmentOptionsByMetric(ValueTypes.DOUBLE, MetricKind.DELTA);
  132. });
  133. it('should return all alignment options except four', () => {
  134. expect(result.length).toBe(9);
  135. expect(result.map((o: any) => o.value)).toEqual(
  136. expect.not.arrayContaining([
  137. 'ALIGN_COUNT_TRUE',
  138. 'ALIGN_COUNT_FALSE',
  139. 'ALIGN_FRACTION_TRUE',
  140. 'ALIGN_INTERPOLATE',
  141. ])
  142. );
  143. });
  144. });
  145. });
  146. describe('getAggregationOptionsByMetric', () => {
  147. it('gets a result for a type and a metric kind', () => {
  148. expect(getAggregationOptionsByMetric(ValueTypes.BOOL, MetricKind.CUMULATIVE)).toEqual([
  149. AGGREGATIONS[0],
  150. AGGREGATIONS[6],
  151. ]);
  152. });
  153. });
  154. describe('getLabelKeys', () => {
  155. it('should return labels', async () => {
  156. const ds = newMockDatasource();
  157. ds.getLabels = jest.fn().mockResolvedValue({ l1: true, l2: true });
  158. expect(await getLabelKeys(ds, 'type', 'project')).toEqual(['l1', 'l2', ...SYSTEM_LABELS]);
  159. });
  160. });
  161. describe('getAlignmentPickerData', () => {
  162. it('should return default data', () => {
  163. const res = getAlignmentPickerData();
  164. expect(res.alignOptions).toHaveLength(10);
  165. expect(res.perSeriesAligner).toEqual(AlignmentTypes.ALIGN_MEAN);
  166. });
  167. it('should use provided data', () => {
  168. const res = getAlignmentPickerData(ValueTypes.BOOL, MetricKind.CUMULATIVE);
  169. expect(res.alignOptions).toHaveLength(0);
  170. expect(res.perSeriesAligner).toEqual(AlignmentTypes.ALIGN_MEAN);
  171. });
  172. });
  173. describe('labelsToGroupedOptions', () => {
  174. it('should group in the same label', () => {
  175. expect(labelsToGroupedOptions(['foo', 'bar'])).toEqual([
  176. {
  177. expanded: true,
  178. label: '',
  179. options: [
  180. { label: 'foo', value: 'foo' },
  181. { label: 'bar', value: 'bar' },
  182. ],
  183. },
  184. ]);
  185. });
  186. it('should group in different labels', () => {
  187. expect(labelsToGroupedOptions(['foo.bar', 'foobar'])).toEqual([
  188. {
  189. expanded: true,
  190. label: 'Foo Bar',
  191. options: [{ label: 'foo.bar', value: 'foo.bar' }],
  192. },
  193. {
  194. expanded: true,
  195. label: '',
  196. options: [{ label: 'foobar', value: 'foobar' }],
  197. },
  198. ]);
  199. });
  200. });
  201. describe('stringArrayToFilters', () => {
  202. it('chunks an array', () => {
  203. expect(stringArrayToFilters(['key', 'operator', 'value', 'condition'])).toEqual([
  204. {
  205. condition: 'condition',
  206. key: 'key',
  207. operator: 'operator',
  208. value: 'value',
  209. },
  210. ]);
  211. });
  212. });
  213. });