ResultProcessor.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. jest.mock('@grafana/data/src/utils/moment_wrapper', () => ({
  2. dateTime: (ts: any) => {
  3. return {
  4. valueOf: () => ts,
  5. fromNow: () => 'fromNow() jest mocked',
  6. format: (fmt: string) => 'format() jest mocked',
  7. };
  8. },
  9. toUtc: (ts: any) => {
  10. return {
  11. format: (fmt: string) => 'format() jest mocked',
  12. };
  13. },
  14. }));
  15. import { ResultProcessor } from './ResultProcessor';
  16. import { ExploreItemState, ExploreMode } from 'app/types/explore';
  17. import TableModel from 'app/core/table_model';
  18. import { TimeSeries, LogRowModel, toDataFrame, FieldType } from '@grafana/data';
  19. const testContext = (options: any = {}) => {
  20. const timeSeries = toDataFrame({
  21. name: 'A-series',
  22. refId: 'A',
  23. fields: [
  24. { name: 'A-series', type: FieldType.number, values: [4, 5, 6] },
  25. { name: 'time', type: FieldType.time, values: [100, 200, 300] },
  26. ],
  27. });
  28. const table = toDataFrame({
  29. name: 'table-res',
  30. refId: 'A',
  31. fields: [
  32. { name: 'value', type: FieldType.number, values: [4, 5, 6] },
  33. { name: 'time', type: FieldType.time, values: [100, 200, 300] },
  34. { name: 'message', type: FieldType.string, values: ['this is a message', 'second message', 'third'] },
  35. ],
  36. });
  37. const emptyTable = toDataFrame({ name: 'empty-table', refId: 'A', fields: [] });
  38. const defaultOptions = {
  39. mode: ExploreMode.Metrics,
  40. dataFrames: [timeSeries, table, emptyTable],
  41. graphResult: [] as TimeSeries[],
  42. tableResult: new TableModel(),
  43. logsResult: { hasUniqueLabels: false, rows: [] as LogRowModel[] },
  44. };
  45. const combinedOptions = { ...defaultOptions, ...options };
  46. const state = ({
  47. mode: combinedOptions.mode,
  48. graphResult: combinedOptions.graphResult,
  49. tableResult: combinedOptions.tableResult,
  50. logsResult: combinedOptions.logsResult,
  51. queryIntervals: { intervalMs: 10 },
  52. } as any) as ExploreItemState;
  53. const resultProcessor = new ResultProcessor(state, combinedOptions.dataFrames, 60000);
  54. return {
  55. dataFrames: combinedOptions.dataFrames,
  56. resultProcessor,
  57. };
  58. };
  59. describe('ResultProcessor', () => {
  60. describe('constructed without result', () => {
  61. describe('when calling getGraphResult', () => {
  62. it('then it should return null', () => {
  63. const { resultProcessor } = testContext({ dataFrames: [] });
  64. const theResult = resultProcessor.getGraphResult();
  65. expect(theResult).toEqual(null);
  66. });
  67. });
  68. describe('when calling getTableResult', () => {
  69. it('then it should return null', () => {
  70. const { resultProcessor } = testContext({ dataFrames: [] });
  71. const theResult = resultProcessor.getTableResult();
  72. expect(theResult).toEqual(null);
  73. });
  74. });
  75. describe('when calling getLogsResult', () => {
  76. it('then it should return null', () => {
  77. const { resultProcessor } = testContext({ dataFrames: [] });
  78. const theResult = resultProcessor.getLogsResult();
  79. expect(theResult).toBeNull();
  80. });
  81. });
  82. });
  83. describe('constructed with a result that is a DataQueryResponse', () => {
  84. describe('when calling getGraphResult', () => {
  85. it('then it should return correct graph result', () => {
  86. const { resultProcessor } = testContext();
  87. const theResult = resultProcessor.getGraphResult();
  88. expect(theResult).toEqual([
  89. {
  90. label: 'A-series',
  91. color: '#7EB26D',
  92. data: [[100, 4], [200, 5], [300, 6]],
  93. info: undefined,
  94. isVisible: true,
  95. yAxis: {
  96. index: 1,
  97. },
  98. },
  99. ]);
  100. });
  101. });
  102. describe('when calling getTableResult', () => {
  103. it('then it should return correct table result', () => {
  104. const { resultProcessor } = testContext();
  105. const theResult = resultProcessor.getTableResult();
  106. expect(theResult).toEqual({
  107. columnMap: {},
  108. columns: [
  109. { text: 'value', type: 'number', filterable: undefined },
  110. { text: 'time', type: 'time', filterable: undefined },
  111. { text: 'message', type: 'string', filterable: undefined },
  112. ],
  113. rows: [[4, 100, 'this is a message'], [5, 200, 'second message'], [6, 300, 'third']],
  114. type: 'table',
  115. });
  116. });
  117. });
  118. describe('when calling getLogsResult', () => {
  119. it('then it should return correct logs result', () => {
  120. const { resultProcessor } = testContext({ mode: ExploreMode.Logs });
  121. const theResult = resultProcessor.getLogsResult();
  122. expect(theResult).toEqual({
  123. hasUniqueLabels: false,
  124. meta: [],
  125. rows: [
  126. {
  127. entry: 'third',
  128. hasAnsi: false,
  129. labels: undefined,
  130. logLevel: 'unknown',
  131. raw: 'third',
  132. searchWords: [] as string[],
  133. timeEpochMs: 300,
  134. timeFromNow: 'fromNow() jest mocked',
  135. timeLocal: 'format() jest mocked',
  136. timeUtc: 'format() jest mocked',
  137. timestamp: 300,
  138. uniqueLabels: {},
  139. },
  140. {
  141. entry: 'second message',
  142. hasAnsi: false,
  143. labels: undefined,
  144. logLevel: 'unknown',
  145. raw: 'second message',
  146. searchWords: [] as string[],
  147. timeEpochMs: 200,
  148. timeFromNow: 'fromNow() jest mocked',
  149. timeLocal: 'format() jest mocked',
  150. timeUtc: 'format() jest mocked',
  151. timestamp: 200,
  152. uniqueLabels: {},
  153. },
  154. {
  155. entry: 'this is a message',
  156. hasAnsi: false,
  157. labels: undefined,
  158. logLevel: 'unknown',
  159. raw: 'this is a message',
  160. searchWords: [] as string[],
  161. timeEpochMs: 100,
  162. timeFromNow: 'fromNow() jest mocked',
  163. timeLocal: 'format() jest mocked',
  164. timeUtc: 'format() jest mocked',
  165. timestamp: 100,
  166. uniqueLabels: {},
  167. },
  168. ],
  169. series: [
  170. {
  171. label: 'A-series',
  172. color: '#7EB26D',
  173. data: [[100, 4], [200, 5], [300, 6]],
  174. info: undefined,
  175. isVisible: true,
  176. yAxis: {
  177. index: 1,
  178. },
  179. },
  180. ],
  181. });
  182. });
  183. });
  184. });
  185. });