actions.test.ts 10 KB

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