actions.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { refreshExplore, testDatasource, loadDatasource } 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. testDataSourcePendingAction,
  12. testDataSourceSuccessAction,
  13. testDataSourceFailureAction,
  14. loadDatasourcePendingAction,
  15. loadDatasourceReadyAction,
  16. } from './actionTypes';
  17. import { Emitter } from 'app/core/core';
  18. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  19. import { makeInitialUpdateState } from './reducers';
  20. import { DataQuery } from '@grafana/ui/src/types/datasource';
  21. import { DefaultTimeZone, RawTimeRange } from '@grafana/ui';
  22. import { toUtc } from '@grafana/ui/src/utils/moment_wrapper';
  23. jest.mock('app/features/plugins/datasource_srv', () => ({
  24. getDatasourceSrv: () => ({
  25. getExternal: jest.fn().mockReturnValue([]),
  26. get: jest.fn().mockReturnValue({
  27. testDatasource: jest.fn(),
  28. init: jest.fn(),
  29. }),
  30. }),
  31. }));
  32. const t = toUtc();
  33. const testRange = {
  34. from: t,
  35. to: t,
  36. raw: {
  37. from: t,
  38. to: t,
  39. },
  40. };
  41. jest.mock('app/core/utils/explore', () => ({
  42. ...jest.requireActual('app/core/utils/explore'),
  43. getTimeRangeFromUrl: (range: RawTimeRange) => testRange,
  44. }));
  45. const setup = (updateOverides?: Partial<ExploreUpdateState>) => {
  46. const exploreId = ExploreId.left;
  47. const containerWidth = 1920;
  48. const eventBridge = {} as Emitter;
  49. const ui = { dedupStrategy: LogsDedupStrategy.none, showingGraph: false, showingLogs: false, showingTable: false };
  50. const timeZone = DefaultTimeZone;
  51. const range = testRange;
  52. const urlState: ExploreUrlState = {
  53. datasource: 'some-datasource',
  54. queries: [],
  55. range: range.raw,
  56. ui,
  57. };
  58. const updateDefaults = makeInitialUpdateState();
  59. const update = { ...updateDefaults, ...updateOverides };
  60. const initialState = {
  61. user: {
  62. timeZone,
  63. },
  64. explore: {
  65. [exploreId]: {
  66. initialized: true,
  67. urlState,
  68. containerWidth,
  69. eventBridge,
  70. update,
  71. datasourceInstance: { name: 'some-datasource' },
  72. queries: [] as DataQuery[],
  73. range,
  74. ui,
  75. refreshInterval: {
  76. label: 'Off',
  77. value: 0,
  78. },
  79. },
  80. },
  81. };
  82. return {
  83. initialState,
  84. exploreId,
  85. range,
  86. ui,
  87. containerWidth,
  88. eventBridge,
  89. };
  90. };
  91. describe('refreshExplore', () => {
  92. describe('when explore is initialized', () => {
  93. describe('and update datasource is set', () => {
  94. it('then it should dispatch initializeExplore', async () => {
  95. const { exploreId, ui, initialState, containerWidth, eventBridge } = setup({ datasource: true });
  96. const dispatchedActions = await thunkTester(initialState)
  97. .givenThunk(refreshExplore)
  98. .whenThunkIsDispatched(exploreId);
  99. const initializeExplore = dispatchedActions[2] as ActionOf<InitializeExplorePayload>;
  100. const { type, payload } = initializeExplore;
  101. expect(type).toEqual(initializeExploreAction.type);
  102. expect(payload.containerWidth).toEqual(containerWidth);
  103. expect(payload.eventBridge).toEqual(eventBridge);
  104. expect(payload.queries.length).toBe(1); // Queries have generated keys hard to expect on
  105. expect(payload.range.from).toEqual(testRange.from);
  106. expect(payload.range.to).toEqual(testRange.to);
  107. expect(payload.range.raw.from).toEqual(testRange.raw.from);
  108. expect(payload.range.raw.to).toEqual(testRange.raw.to);
  109. expect(payload.ui).toEqual(ui);
  110. });
  111. });
  112. describe('and update range is set', () => {
  113. it('then it should dispatch changeTimeAction', async () => {
  114. const { exploreId, range, initialState } = setup({ range: true });
  115. const dispatchedActions = await thunkTester(initialState)
  116. .givenThunk(refreshExplore)
  117. .whenThunkIsDispatched(exploreId);
  118. expect(dispatchedActions[0].type).toEqual(changeTimeAction.type);
  119. expect(dispatchedActions[0].payload).toEqual({ exploreId, range });
  120. });
  121. });
  122. describe('and update ui is set', () => {
  123. it('then it should dispatch updateUIStateAction', async () => {
  124. const { exploreId, initialState, ui } = setup({ ui: true });
  125. const dispatchedActions = await thunkTester(initialState)
  126. .givenThunk(refreshExplore)
  127. .whenThunkIsDispatched(exploreId);
  128. expect(dispatchedActions[0].type).toEqual(updateUIStateAction.type);
  129. expect(dispatchedActions[0].payload).toEqual({ ...ui, exploreId });
  130. });
  131. });
  132. describe('and update queries is set', () => {
  133. it('then it should dispatch setQueriesAction', async () => {
  134. const { exploreId, initialState } = setup({ queries: true });
  135. const dispatchedActions = await thunkTester(initialState)
  136. .givenThunk(refreshExplore)
  137. .whenThunkIsDispatched(exploreId);
  138. expect(dispatchedActions[0].type).toEqual(setQueriesAction.type);
  139. expect(dispatchedActions[0].payload).toEqual({ exploreId, queries: [] });
  140. });
  141. });
  142. });
  143. describe('when update is not initialized', () => {
  144. it('then it should not dispatch any actions', async () => {
  145. const exploreId = ExploreId.left;
  146. const initialState = { explore: { [exploreId]: { initialized: false } } };
  147. const dispatchedActions = await thunkTester(initialState)
  148. .givenThunk(refreshExplore)
  149. .whenThunkIsDispatched(exploreId);
  150. expect(dispatchedActions).toEqual([]);
  151. });
  152. });
  153. });
  154. describe('test datasource', () => {
  155. describe('when testDatasource thunk is dispatched', () => {
  156. describe('and testDatasource call on instance is successful', () => {
  157. it('then it should dispatch testDataSourceSuccessAction', async () => {
  158. const exploreId = ExploreId.left;
  159. const mockDatasourceInstance = {
  160. testDatasource: () => {
  161. return Promise.resolve({ status: 'success' });
  162. },
  163. };
  164. const dispatchedActions = await thunkTester({})
  165. .givenThunk(testDatasource)
  166. .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  167. expect(dispatchedActions).toEqual([
  168. testDataSourcePendingAction({ exploreId }),
  169. testDataSourceSuccessAction({ exploreId }),
  170. ]);
  171. });
  172. });
  173. describe('and testDatasource call on instance is not successful', () => {
  174. it('then it should dispatch testDataSourceFailureAction', async () => {
  175. const exploreId = ExploreId.left;
  176. const error = 'something went wrong';
  177. const mockDatasourceInstance = {
  178. testDatasource: () => {
  179. return Promise.resolve({ status: 'fail', message: error });
  180. },
  181. };
  182. const dispatchedActions = await thunkTester({})
  183. .givenThunk(testDatasource)
  184. .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  185. expect(dispatchedActions).toEqual([
  186. testDataSourcePendingAction({ exploreId }),
  187. testDataSourceFailureAction({ exploreId, error }),
  188. ]);
  189. });
  190. });
  191. describe('and testDatasource call on instance throws', () => {
  192. it('then it should dispatch testDataSourceFailureAction', async () => {
  193. const exploreId = ExploreId.left;
  194. const error = 'something went wrong';
  195. const mockDatasourceInstance = {
  196. testDatasource: () => {
  197. throw { statusText: error };
  198. },
  199. };
  200. const dispatchedActions = await thunkTester({})
  201. .givenThunk(testDatasource)
  202. .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  203. expect(dispatchedActions).toEqual([
  204. testDataSourcePendingAction({ exploreId }),
  205. testDataSourceFailureAction({ exploreId, error }),
  206. ]);
  207. });
  208. });
  209. });
  210. });
  211. describe('loading datasource', () => {
  212. describe('when loadDatasource thunk is dispatched', () => {
  213. describe('and all goes fine', () => {
  214. it('then it should dispatch correct actions', async () => {
  215. const exploreId = ExploreId.left;
  216. const name = 'some-datasource';
  217. const initialState = { explore: { [exploreId]: { requestedDatasourceName: name } } };
  218. const mockDatasourceInstance = {
  219. testDatasource: () => {
  220. return Promise.resolve({ status: 'success' });
  221. },
  222. name,
  223. init: jest.fn(),
  224. meta: { id: 'some id' },
  225. };
  226. const dispatchedActions = await thunkTester(initialState)
  227. .givenThunk(loadDatasource)
  228. .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  229. expect(dispatchedActions).toEqual([
  230. loadDatasourcePendingAction({
  231. exploreId,
  232. requestedDatasourceName: mockDatasourceInstance.name,
  233. }),
  234. testDataSourcePendingAction({ exploreId }),
  235. testDataSourceSuccessAction({ exploreId }),
  236. loadDatasourceReadyAction({ exploreId, history: [] }),
  237. ]);
  238. });
  239. });
  240. describe('and user changes datasource during load', () => {
  241. it('then it should dispatch correct actions', async () => {
  242. const exploreId = ExploreId.left;
  243. const name = 'some-datasource';
  244. const initialState = { explore: { [exploreId]: { requestedDatasourceName: 'some-other-datasource' } } };
  245. const mockDatasourceInstance = {
  246. testDatasource: () => {
  247. return Promise.resolve({ status: 'success' });
  248. },
  249. name,
  250. init: jest.fn(),
  251. meta: { id: 'some id' },
  252. };
  253. const dispatchedActions = await thunkTester(initialState)
  254. .givenThunk(loadDatasource)
  255. .whenThunkIsDispatched(exploreId, mockDatasourceInstance);
  256. expect(dispatchedActions).toEqual([
  257. loadDatasourcePendingAction({
  258. exploreId,
  259. requestedDatasourceName: mockDatasourceInstance.name,
  260. }),
  261. testDataSourcePendingAction({ exploreId }),
  262. testDataSourceSuccessAction({ exploreId }),
  263. ]);
  264. });
  265. });
  266. });
  267. });