actions.test.ts 9.6 KB

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