actions.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { refreshExplore } from './actions';
  2. import { ExploreId, ExploreUrlState, ExploreUpdateState } from 'app/types';
  3. import { thunkTester } from 'test/core/thunk/thunkTester';
  4. import { LogsDedupStrategy } from 'app/core/logs_model';
  5. import {
  6. initializeExploreAction,
  7. InitializeExplorePayload,
  8. changeTimeAction,
  9. updateUIStateAction,
  10. setQueriesAction,
  11. } from './actionTypes';
  12. import { Emitter } from 'app/core/core';
  13. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  14. import { makeInitialUpdateState } from './reducers';
  15. jest.mock('app/features/plugins/datasource_srv', () => ({
  16. getDatasourceSrv: () => ({
  17. getExternal: jest.fn().mockReturnValue([]),
  18. get: jest.fn().mockReturnValue({
  19. testDatasource: jest.fn(),
  20. init: jest.fn(),
  21. }),
  22. }),
  23. }));
  24. const setup = (updateOverides?: Partial<ExploreUpdateState>) => {
  25. const exploreId = ExploreId.left;
  26. const containerWidth = 1920;
  27. const eventBridge = {} as Emitter;
  28. const ui = { dedupStrategy: LogsDedupStrategy.none, showingGraph: false, showingLogs: false, showingTable: false };
  29. const range = { from: 'now', to: 'now' };
  30. const urlState: ExploreUrlState = { datasource: 'some-datasource', queries: [], range, ui };
  31. const updateDefaults = makeInitialUpdateState();
  32. const update = { ...updateDefaults, ...updateOverides };
  33. const initialState = {
  34. explore: {
  35. [exploreId]: {
  36. initialized: true,
  37. urlState,
  38. containerWidth,
  39. eventBridge,
  40. update,
  41. datasourceInstance: { name: 'some-datasource' },
  42. queries: [],
  43. range,
  44. ui,
  45. },
  46. },
  47. };
  48. return {
  49. initialState,
  50. exploreId,
  51. range,
  52. ui,
  53. containerWidth,
  54. eventBridge,
  55. };
  56. };
  57. describe('refreshExplore', () => {
  58. describe('when explore is initialized', () => {
  59. describe('and update datasource is set', () => {
  60. it('then it should dispatch initializeExplore', () => {
  61. const { exploreId, ui, range, initialState, containerWidth, eventBridge } = setup({ datasource: true });
  62. thunkTester(initialState)
  63. .givenThunk(refreshExplore)
  64. .whenThunkIsDispatched(exploreId)
  65. .thenDispatchedActionsAreEqual(dispatchedActions => {
  66. const initializeExplore = dispatchedActions[0] as ActionOf<InitializeExplorePayload>;
  67. const { type, payload } = initializeExplore;
  68. expect(type).toEqual(initializeExploreAction.type);
  69. expect(payload.containerWidth).toEqual(containerWidth);
  70. expect(payload.eventBridge).toEqual(eventBridge);
  71. expect(payload.exploreDatasources).toEqual([]);
  72. expect(payload.queries.length).toBe(1); // Queries have generated keys hard to expect on
  73. expect(payload.range).toEqual(range);
  74. expect(payload.ui).toEqual(ui);
  75. return true;
  76. });
  77. });
  78. });
  79. describe('and update range is set', () => {
  80. it('then it should dispatch changeTimeAction', () => {
  81. const { exploreId, range, initialState } = setup({ range: true });
  82. thunkTester(initialState)
  83. .givenThunk(refreshExplore)
  84. .whenThunkIsDispatched(exploreId)
  85. .thenDispatchedActionsAreEqual(dispatchedActions => {
  86. expect(dispatchedActions[0].type).toEqual(changeTimeAction.type);
  87. expect(dispatchedActions[0].payload).toEqual({ exploreId, range });
  88. return true;
  89. });
  90. });
  91. });
  92. describe('and update ui is set', () => {
  93. it('then it should dispatch updateUIStateAction', () => {
  94. const { exploreId, initialState, ui } = setup({ ui: true });
  95. thunkTester(initialState)
  96. .givenThunk(refreshExplore)
  97. .whenThunkIsDispatched(exploreId)
  98. .thenDispatchedActionsAreEqual(dispatchedActions => {
  99. expect(dispatchedActions[0].type).toEqual(updateUIStateAction.type);
  100. expect(dispatchedActions[0].payload).toEqual({ ...ui, exploreId });
  101. return true;
  102. });
  103. });
  104. });
  105. describe('and update queries is set', () => {
  106. it('then it should dispatch setQueriesAction', () => {
  107. const { exploreId, initialState } = setup({ queries: true });
  108. thunkTester(initialState)
  109. .givenThunk(refreshExplore)
  110. .whenThunkIsDispatched(exploreId)
  111. .thenDispatchedActionsAreEqual(dispatchedActions => {
  112. expect(dispatchedActions[0].type).toEqual(setQueriesAction.type);
  113. expect(dispatchedActions[0].payload).toEqual({ exploreId, queries: [] });
  114. return true;
  115. });
  116. });
  117. });
  118. });
  119. describe('when update is not initialized', () => {
  120. it('then it should not dispatch any actions', () => {
  121. const exploreId = ExploreId.left;
  122. const initialState = { explore: { [exploreId]: { initialized: false } } };
  123. thunkTester(initialState)
  124. .givenThunk(refreshExplore)
  125. .whenThunkIsDispatched(exploreId)
  126. .thenThereAreNoDispatchedActions();
  127. });
  128. });
  129. });