ResultProcessor.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 defaultOptions = {
  38. mode: ExploreMode.Metrics,
  39. dataFrames: [timeSeries, table],
  40. graphResult: [] as TimeSeries[],
  41. tableResult: new TableModel(),
  42. logsResult: { hasUniqueLabels: false, rows: [] as LogRowModel[] },
  43. };
  44. const combinedOptions = { ...defaultOptions, ...options };
  45. const state = ({
  46. mode: combinedOptions.mode,
  47. graphResult: combinedOptions.graphResult,
  48. tableResult: combinedOptions.tableResult,
  49. logsResult: combinedOptions.logsResult,
  50. queryIntervals: { intervalMs: 10 },
  51. } as any) as ExploreItemState;
  52. const resultProcessor = new ResultProcessor(state, combinedOptions.dataFrames);
  53. return {
  54. dataFrames: combinedOptions.dataFrames,
  55. resultProcessor,
  56. };
  57. };
  58. describe('ResultProcessor', () => {
  59. describe('constructed without result', () => {
  60. describe('when calling getGraphResult', () => {
  61. it('then it should return an empty array', () => {
  62. const { resultProcessor } = testContext({ dataFrames: [] });
  63. const theResult = resultProcessor.getGraphResult();
  64. expect(theResult).toEqual([]);
  65. });
  66. });
  67. describe('when calling getTableResult', () => {
  68. it('then it should return an empty TableModel', () => {
  69. const { resultProcessor } = testContext({ dataFrames: [] });
  70. const theResult = resultProcessor.getTableResult();
  71. expect(theResult).toEqual(new TableModel());
  72. });
  73. });
  74. describe('when calling getLogsResult', () => {
  75. it('then it should return null', () => {
  76. const { resultProcessor } = testContext({ dataFrames: [] });
  77. const theResult = resultProcessor.getLogsResult();
  78. expect(theResult).toBeNull();
  79. });
  80. });
  81. });
  82. describe('constructed with a result that is a DataQueryResponse', () => {
  83. describe('when calling getGraphResult', () => {
  84. it('then it should return correct graph result', () => {
  85. const { resultProcessor } = testContext();
  86. const theResult = resultProcessor.getGraphResult();
  87. expect(theResult).toEqual([
  88. {
  89. label: 'A-series',
  90. color: '#7EB26D',
  91. data: [[100, 4], [200, 5], [300, 6]],
  92. info: undefined,
  93. isVisible: true,
  94. yAxis: {
  95. index: 1,
  96. },
  97. },
  98. ]);
  99. });
  100. });
  101. describe('when calling getTableResult', () => {
  102. it('then it should return correct table result', () => {
  103. const { resultProcessor } = testContext();
  104. const theResult = resultProcessor.getTableResult();
  105. expect(theResult).toEqual({
  106. columnMap: {},
  107. columns: [
  108. { text: 'value', type: 'number', filterable: undefined },
  109. { text: 'time', type: 'time', filterable: undefined },
  110. { text: 'message', type: 'string', filterable: undefined },
  111. ],
  112. rows: [[4, 100, 'this is a message'], [5, 200, 'second message'], [6, 300, 'third']],
  113. type: 'table',
  114. });
  115. });
  116. });
  117. describe('when calling getLogsResult', () => {
  118. it('then it should return correct logs result', () => {
  119. const { resultProcessor } = testContext({ mode: ExploreMode.Logs });
  120. const theResult = resultProcessor.getLogsResult();
  121. expect(theResult).toEqual({
  122. hasUniqueLabels: false,
  123. meta: [],
  124. rows: [
  125. {
  126. entry: 'third',
  127. hasAnsi: false,
  128. labels: undefined,
  129. logLevel: 'unknown',
  130. raw: 'third',
  131. searchWords: [] as string[],
  132. timeEpochMs: 300,
  133. timeFromNow: 'fromNow() jest mocked',
  134. timeLocal: 'format() jest mocked',
  135. timeUtc: 'format() jest mocked',
  136. timestamp: 300,
  137. uniqueLabels: {},
  138. },
  139. {
  140. entry: 'second message',
  141. hasAnsi: false,
  142. labels: undefined,
  143. logLevel: 'unknown',
  144. raw: 'second message',
  145. searchWords: [] as string[],
  146. timeEpochMs: 200,
  147. timeFromNow: 'fromNow() jest mocked',
  148. timeLocal: 'format() jest mocked',
  149. timeUtc: 'format() jest mocked',
  150. timestamp: 200,
  151. uniqueLabels: {},
  152. },
  153. {
  154. entry: 'this is a message',
  155. hasAnsi: false,
  156. labels: undefined,
  157. logLevel: 'unknown',
  158. raw: 'this is a message',
  159. searchWords: [] as string[],
  160. timeEpochMs: 100,
  161. timeFromNow: 'fromNow() jest mocked',
  162. timeLocal: 'format() jest mocked',
  163. timeUtc: 'format() jest mocked',
  164. timestamp: 100,
  165. uniqueLabels: {},
  166. },
  167. ],
  168. series: [
  169. {
  170. label: 'A-series',
  171. color: '#7EB26D',
  172. data: [[100, 4], [200, 5], [300, 6]],
  173. info: undefined,
  174. isVisible: true,
  175. yAxis: {
  176. index: 1,
  177. },
  178. },
  179. ],
  180. });
  181. });
  182. });
  183. });
  184. });