processQueryResultsEpic.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { mockExploreState } from 'test/mocks/mockExploreState';
  2. import { epicTester } from 'test/core/redux/epicTester';
  3. import {
  4. processQueryResultsAction,
  5. resetQueryErrorAction,
  6. querySuccessAction,
  7. scanStopAction,
  8. scanRangeAction,
  9. } from '../actionTypes';
  10. import { SeriesData, LoadingState } from '@grafana/ui';
  11. import { processQueryResultsEpic } from './processQueryResultsEpic';
  12. import TableModel from 'app/core/table_model';
  13. const testContext = () => {
  14. const serieA: SeriesData = {
  15. fields: [],
  16. refId: 'A',
  17. rows: [],
  18. };
  19. const serieB: SeriesData = {
  20. fields: [],
  21. refId: 'B',
  22. rows: [],
  23. };
  24. const series = [serieA, serieB];
  25. const latency = 0;
  26. const loadingState = LoadingState.Done;
  27. return {
  28. latency,
  29. series,
  30. loadingState,
  31. };
  32. };
  33. describe('processQueryResultsEpic', () => {
  34. describe('when processQueryResultsAction is dispatched', () => {
  35. describe('and datasourceInstance is the same', () => {
  36. describe('and explore is not scanning', () => {
  37. it('then resetQueryErrorAction and querySuccessAction are dispatched and eventBridge emits correct message', () => {
  38. const { datasourceId, exploreId, state, eventBridge } = mockExploreState();
  39. const { latency, series, loadingState } = testContext();
  40. const graphResult = [];
  41. const tableResult = new TableModel();
  42. const logsResult = null;
  43. epicTester(processQueryResultsEpic, state)
  44. .whenActionIsDispatched(
  45. processQueryResultsAction({ exploreId, datasourceId, loadingState, series, latency })
  46. )
  47. .thenResultingActionsEqual(
  48. resetQueryErrorAction({ exploreId, refIds: ['A', 'B'] }),
  49. querySuccessAction({ exploreId, loadingState, graphResult, tableResult, logsResult, latency })
  50. );
  51. expect(eventBridge.emit).toBeCalledTimes(1);
  52. expect(eventBridge.emit).toBeCalledWith('data-received', series);
  53. });
  54. });
  55. describe('and explore is scanning', () => {
  56. describe('and we have a result', () => {
  57. it('then correct actions are dispatched', () => {
  58. const { datasourceId, exploreId, state } = mockExploreState({ scanning: true });
  59. const { latency, series, loadingState } = testContext();
  60. const graphResult = [];
  61. const tableResult = new TableModel();
  62. const logsResult = null;
  63. epicTester(processQueryResultsEpic, state)
  64. .whenActionIsDispatched(
  65. processQueryResultsAction({ exploreId, datasourceId, loadingState, series, latency })
  66. )
  67. .thenResultingActionsEqual(
  68. resetQueryErrorAction({ exploreId, refIds: ['A', 'B'] }),
  69. querySuccessAction({ exploreId, loadingState, graphResult, tableResult, logsResult, latency }),
  70. scanStopAction({ exploreId })
  71. );
  72. });
  73. });
  74. describe('and we do not have a result', () => {
  75. it('then correct actions are dispatched', () => {
  76. const { datasourceId, exploreId, state, scanner } = mockExploreState({ scanning: true });
  77. const { latency, loadingState } = testContext();
  78. const graphResult = [];
  79. const tableResult = new TableModel();
  80. const logsResult = null;
  81. epicTester(processQueryResultsEpic, state)
  82. .whenActionIsDispatched(
  83. processQueryResultsAction({ exploreId, datasourceId, loadingState, series: [], latency })
  84. )
  85. .thenResultingActionsEqual(
  86. resetQueryErrorAction({ exploreId, refIds: [] }),
  87. querySuccessAction({ exploreId, loadingState, graphResult, tableResult, logsResult, latency }),
  88. scanRangeAction({ exploreId, range: scanner() })
  89. );
  90. });
  91. });
  92. });
  93. });
  94. describe('and datasourceInstance is not the same', () => {
  95. it('then no actions are dispatched and eventBridge does not emit message', () => {
  96. const { exploreId, state, eventBridge } = mockExploreState();
  97. const { series, loadingState } = testContext();
  98. epicTester(processQueryResultsEpic, state)
  99. .whenActionIsDispatched(
  100. processQueryResultsAction({ exploreId, datasourceId: 'other id', loadingState, series, latency: 0 })
  101. )
  102. .thenNoActionsWhereDispatched();
  103. expect(eventBridge.emit).not.toBeCalled();
  104. });
  105. });
  106. });
  107. });