reducers.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. import {
  2. itemReducer,
  3. makeExploreItemState,
  4. exploreReducer,
  5. makeInitialUpdateState,
  6. initialExploreState,
  7. } from './reducers';
  8. import {
  9. ExploreId,
  10. ExploreItemState,
  11. ExploreUrlState,
  12. ExploreState,
  13. RangeScanner,
  14. ExploreMode,
  15. } from 'app/types/explore';
  16. import { reducerTester } from 'test/core/redux/reducerTester';
  17. import {
  18. scanStartAction,
  19. testDataSourcePendingAction,
  20. testDataSourceSuccessAction,
  21. testDataSourceFailureAction,
  22. updateDatasourceInstanceAction,
  23. splitOpenAction,
  24. splitCloseAction,
  25. changeModeAction,
  26. scanStopAction,
  27. runQueriesAction,
  28. } from './actionTypes';
  29. import { Reducer } from 'redux';
  30. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  31. import { updateLocation } from 'app/core/actions/location';
  32. import { serializeStateToUrlParam } from 'app/core/utils/explore';
  33. import TableModel from 'app/core/table_model';
  34. import { DataSourceApi, DataQuery, LogsModel, LogsDedupStrategy, LoadingState } from '@grafana/ui';
  35. describe('Explore item reducer', () => {
  36. describe('scanning', () => {
  37. it('should start scanning', () => {
  38. const scanner = jest.fn();
  39. const initalState = {
  40. ...makeExploreItemState(),
  41. scanning: false,
  42. scanner: undefined as RangeScanner,
  43. };
  44. reducerTester()
  45. .givenReducer(itemReducer as Reducer<ExploreItemState, ActionOf<any>>, initalState)
  46. .whenActionIsDispatched(scanStartAction({ exploreId: ExploreId.left, scanner }))
  47. .thenStateShouldEqual({
  48. ...makeExploreItemState(),
  49. scanning: true,
  50. scanner,
  51. });
  52. });
  53. it('should stop scanning', () => {
  54. const scanner = jest.fn();
  55. const initalState = {
  56. ...makeExploreItemState(),
  57. scanning: true,
  58. scanner,
  59. scanRange: {},
  60. };
  61. reducerTester()
  62. .givenReducer(itemReducer as Reducer<ExploreItemState, ActionOf<any>>, initalState)
  63. .whenActionIsDispatched(scanStopAction({ exploreId: ExploreId.left }))
  64. .thenStateShouldEqual({
  65. ...makeExploreItemState(),
  66. scanning: false,
  67. scanner: undefined,
  68. scanRange: undefined,
  69. });
  70. });
  71. });
  72. describe('testing datasource', () => {
  73. describe('when testDataSourcePendingAction is dispatched', () => {
  74. it('then it should set datasourceError', () => {
  75. reducerTester()
  76. .givenReducer(itemReducer, { datasourceError: {} })
  77. .whenActionIsDispatched(testDataSourcePendingAction({ exploreId: ExploreId.left }))
  78. .thenStateShouldEqual({ datasourceError: null });
  79. });
  80. });
  81. describe('when testDataSourceSuccessAction is dispatched', () => {
  82. it('then it should set datasourceError', () => {
  83. reducerTester()
  84. .givenReducer(itemReducer, { datasourceError: {} })
  85. .whenActionIsDispatched(testDataSourceSuccessAction({ exploreId: ExploreId.left }))
  86. .thenStateShouldEqual({ datasourceError: null });
  87. });
  88. });
  89. describe('when testDataSourceFailureAction is dispatched', () => {
  90. it('then it should set correct state', () => {
  91. const error = 'some error';
  92. const initalState: Partial<ExploreItemState> = {
  93. datasourceError: null,
  94. graphResult: [],
  95. tableResult: {} as TableModel,
  96. logsResult: {} as LogsModel,
  97. update: {
  98. datasource: true,
  99. queries: true,
  100. range: true,
  101. ui: true,
  102. },
  103. };
  104. const expectedState = {
  105. datasourceError: error,
  106. graphResult: undefined as any[],
  107. tableResult: undefined as TableModel,
  108. logsResult: undefined as LogsModel,
  109. update: makeInitialUpdateState(),
  110. };
  111. reducerTester()
  112. .givenReducer(itemReducer, initalState)
  113. .whenActionIsDispatched(testDataSourceFailureAction({ exploreId: ExploreId.left, error }))
  114. .thenStateShouldEqual(expectedState);
  115. });
  116. });
  117. describe('when changeDataType is dispatched', () => {
  118. it('then it should set correct state', () => {
  119. reducerTester()
  120. .givenReducer(itemReducer, {})
  121. .whenActionIsDispatched(changeModeAction({ exploreId: ExploreId.left, mode: ExploreMode.Logs }))
  122. .thenStateShouldEqual({
  123. mode: ExploreMode.Logs,
  124. });
  125. });
  126. });
  127. });
  128. describe('changing datasource', () => {
  129. describe('when updateDatasourceInstanceAction is dispatched', () => {
  130. describe('and datasourceInstance supports graph, logs, table and has a startpage', () => {
  131. it('then it should set correct state', () => {
  132. const StartPage = {};
  133. const datasourceInstance = {
  134. meta: {
  135. metrics: true,
  136. logs: true,
  137. },
  138. components: {
  139. ExploreStartPage: StartPage,
  140. },
  141. } as DataSourceApi;
  142. const queries: DataQuery[] = [];
  143. const queryKeys: string[] = [];
  144. const initalState: Partial<ExploreItemState> = {
  145. datasourceInstance: null,
  146. StartPage: null,
  147. showingStartPage: false,
  148. queries,
  149. queryKeys,
  150. };
  151. const expectedState = {
  152. datasourceInstance,
  153. StartPage,
  154. showingStartPage: true,
  155. queries,
  156. queryKeys,
  157. supportedModes: [ExploreMode.Metrics, ExploreMode.Logs],
  158. mode: ExploreMode.Metrics,
  159. loadingState: LoadingState.NotStarted,
  160. latency: 0,
  161. queryErrors: [],
  162. };
  163. reducerTester()
  164. .givenReducer(itemReducer, initalState)
  165. .whenActionIsDispatched(updateDatasourceInstanceAction({ exploreId: ExploreId.left, datasourceInstance }))
  166. .thenStateShouldEqual(expectedState);
  167. });
  168. });
  169. });
  170. });
  171. describe('run queries', () => {
  172. describe('when runQueriesAction is dispatched', () => {
  173. it('then it should set correct state', () => {
  174. const initalState: Partial<ExploreItemState> = {
  175. showingStartPage: true,
  176. range: null,
  177. };
  178. const expectedState = {
  179. queryIntervals: {
  180. interval: '1s',
  181. intervalMs: 1000,
  182. },
  183. showingStartPage: false,
  184. range: null,
  185. };
  186. reducerTester()
  187. .givenReducer(itemReducer, initalState)
  188. .whenActionIsDispatched(runQueriesAction({ exploreId: ExploreId.left }))
  189. .thenStateShouldEqual(expectedState);
  190. });
  191. });
  192. });
  193. });
  194. export const setup = (urlStateOverrides?: any) => {
  195. const update = makeInitialUpdateState();
  196. const urlStateDefaults: ExploreUrlState = {
  197. datasource: 'some-datasource',
  198. queries: [],
  199. range: {
  200. from: '',
  201. to: '',
  202. },
  203. ui: {
  204. dedupStrategy: LogsDedupStrategy.none,
  205. showingGraph: false,
  206. showingTable: false,
  207. showingLogs: false,
  208. },
  209. };
  210. const urlState: ExploreUrlState = { ...urlStateDefaults, ...urlStateOverrides };
  211. const serializedUrlState = serializeStateToUrlParam(urlState);
  212. const initalState = { split: false, left: { urlState, update }, right: { urlState, update } };
  213. return {
  214. initalState,
  215. serializedUrlState,
  216. };
  217. };
  218. describe('Explore reducer', () => {
  219. describe('split view', () => {
  220. it("should make right pane a duplicate of the given item's state on split open", () => {
  221. const leftItemMock = {
  222. containerWidth: 100,
  223. } as ExploreItemState;
  224. const initalState = {
  225. split: null,
  226. left: leftItemMock as ExploreItemState,
  227. right: makeExploreItemState(),
  228. } as ExploreState;
  229. reducerTester()
  230. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  231. .whenActionIsDispatched(splitOpenAction({ itemState: leftItemMock }))
  232. .thenStateShouldEqual({
  233. split: true,
  234. left: leftItemMock,
  235. right: leftItemMock,
  236. });
  237. });
  238. describe('split close', () => {
  239. it('should keep right pane as left when left is closed', () => {
  240. const leftItemMock = {
  241. containerWidth: 100,
  242. } as ExploreItemState;
  243. const rightItemMock = {
  244. containerWidth: 200,
  245. } as ExploreItemState;
  246. const initalState = {
  247. split: null,
  248. left: leftItemMock,
  249. right: rightItemMock,
  250. } as ExploreState;
  251. // closing left item
  252. reducerTester()
  253. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  254. .whenActionIsDispatched(splitCloseAction({ itemId: ExploreId.left }))
  255. .thenStateShouldEqual({
  256. split: false,
  257. left: rightItemMock,
  258. right: initialExploreState.right,
  259. });
  260. });
  261. it('should reset right pane when it is closed ', () => {
  262. const leftItemMock = {
  263. containerWidth: 100,
  264. } as ExploreItemState;
  265. const rightItemMock = {
  266. containerWidth: 200,
  267. } as ExploreItemState;
  268. const initalState = {
  269. split: null,
  270. left: leftItemMock,
  271. right: rightItemMock,
  272. } as ExploreState;
  273. // closing left item
  274. reducerTester()
  275. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  276. .whenActionIsDispatched(splitCloseAction({ itemId: ExploreId.right }))
  277. .thenStateShouldEqual({
  278. split: false,
  279. left: leftItemMock,
  280. right: initialExploreState.right,
  281. });
  282. });
  283. });
  284. });
  285. describe('when updateLocation is dispatched', () => {
  286. describe('and payload does not contain a query', () => {
  287. it('then it should just return state', () => {
  288. reducerTester()
  289. .givenReducer(exploreReducer, {})
  290. .whenActionIsDispatched(updateLocation({ query: null }))
  291. .thenStateShouldEqual({});
  292. });
  293. });
  294. describe('and payload contains a query', () => {
  295. describe("but does not contain 'left'", () => {
  296. it('then it should just return state', () => {
  297. reducerTester()
  298. .givenReducer(exploreReducer, {})
  299. .whenActionIsDispatched(updateLocation({ query: {} }))
  300. .thenStateShouldEqual({});
  301. });
  302. });
  303. describe("and query contains a 'right'", () => {
  304. it('then it should add split in state', () => {
  305. const { initalState, serializedUrlState } = setup();
  306. const expectedState = { ...initalState, split: true };
  307. reducerTester()
  308. .givenReducer(exploreReducer, initalState)
  309. .whenActionIsDispatched(
  310. updateLocation({
  311. query: {
  312. left: serializedUrlState,
  313. right: serializedUrlState,
  314. },
  315. })
  316. )
  317. .thenStateShouldEqual(expectedState);
  318. });
  319. });
  320. describe("and query contains a 'left'", () => {
  321. describe('but urlState is not set in state', () => {
  322. it('then it should just add urlState and update in state', () => {
  323. const { initalState, serializedUrlState } = setup();
  324. const urlState: ExploreUrlState = null;
  325. const stateWithoutUrlState = { ...initalState, left: { urlState } };
  326. const expectedState = { ...initalState };
  327. reducerTester()
  328. .givenReducer(exploreReducer, stateWithoutUrlState)
  329. .whenActionIsDispatched(
  330. updateLocation({
  331. query: {
  332. left: serializedUrlState,
  333. },
  334. path: '/explore',
  335. })
  336. )
  337. .thenStateShouldEqual(expectedState);
  338. });
  339. });
  340. describe("but '/explore' is missing in path", () => {
  341. it('then it should just add urlState and update in state', () => {
  342. const { initalState, serializedUrlState } = setup();
  343. const expectedState = { ...initalState };
  344. reducerTester()
  345. .givenReducer(exploreReducer, initalState)
  346. .whenActionIsDispatched(
  347. updateLocation({
  348. query: {
  349. left: serializedUrlState,
  350. },
  351. path: '/dashboard',
  352. })
  353. )
  354. .thenStateShouldEqual(expectedState);
  355. });
  356. });
  357. describe("and '/explore' is in path", () => {
  358. describe('and datasource differs', () => {
  359. it('then it should return update datasource', () => {
  360. const { initalState, serializedUrlState } = setup();
  361. const expectedState = {
  362. ...initalState,
  363. left: {
  364. ...initalState.left,
  365. update: {
  366. ...initalState.left.update,
  367. datasource: true,
  368. },
  369. },
  370. };
  371. const stateWithDifferentDataSource = {
  372. ...initalState,
  373. left: {
  374. ...initalState.left,
  375. urlState: {
  376. ...initalState.left.urlState,
  377. datasource: 'different datasource',
  378. },
  379. },
  380. };
  381. reducerTester()
  382. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  383. .whenActionIsDispatched(
  384. updateLocation({
  385. query: {
  386. left: serializedUrlState,
  387. },
  388. path: '/explore',
  389. })
  390. )
  391. .thenStateShouldEqual(expectedState);
  392. });
  393. });
  394. describe('and range differs', () => {
  395. it('then it should return update range', () => {
  396. const { initalState, serializedUrlState } = setup();
  397. const expectedState = {
  398. ...initalState,
  399. left: {
  400. ...initalState.left,
  401. update: {
  402. ...initalState.left.update,
  403. range: true,
  404. },
  405. },
  406. };
  407. const stateWithDifferentDataSource = {
  408. ...initalState,
  409. left: {
  410. ...initalState.left,
  411. urlState: {
  412. ...initalState.left.urlState,
  413. range: {
  414. from: 'now',
  415. to: 'now-6h',
  416. },
  417. },
  418. },
  419. };
  420. reducerTester()
  421. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  422. .whenActionIsDispatched(
  423. updateLocation({
  424. query: {
  425. left: serializedUrlState,
  426. },
  427. path: '/explore',
  428. })
  429. )
  430. .thenStateShouldEqual(expectedState);
  431. });
  432. });
  433. describe('and queries differs', () => {
  434. it('then it should return update queries', () => {
  435. const { initalState, serializedUrlState } = setup();
  436. const expectedState = {
  437. ...initalState,
  438. left: {
  439. ...initalState.left,
  440. update: {
  441. ...initalState.left.update,
  442. queries: true,
  443. },
  444. },
  445. };
  446. const stateWithDifferentDataSource = {
  447. ...initalState,
  448. left: {
  449. ...initalState.left,
  450. urlState: {
  451. ...initalState.left.urlState,
  452. queries: [{ expr: '{__filename__="some.log"}' }],
  453. },
  454. },
  455. };
  456. reducerTester()
  457. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  458. .whenActionIsDispatched(
  459. updateLocation({
  460. query: {
  461. left: serializedUrlState,
  462. },
  463. path: '/explore',
  464. })
  465. )
  466. .thenStateShouldEqual(expectedState);
  467. });
  468. });
  469. describe('and ui differs', () => {
  470. it('then it should return update ui', () => {
  471. const { initalState, serializedUrlState } = setup();
  472. const expectedState = {
  473. ...initalState,
  474. left: {
  475. ...initalState.left,
  476. update: {
  477. ...initalState.left.update,
  478. ui: true,
  479. },
  480. },
  481. };
  482. const stateWithDifferentDataSource = {
  483. ...initalState,
  484. left: {
  485. ...initalState.left,
  486. urlState: {
  487. ...initalState.left.urlState,
  488. ui: {
  489. ...initalState.left.urlState.ui,
  490. showingGraph: true,
  491. },
  492. },
  493. },
  494. };
  495. reducerTester()
  496. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  497. .whenActionIsDispatched(
  498. updateLocation({
  499. query: {
  500. left: serializedUrlState,
  501. },
  502. path: '/explore',
  503. })
  504. )
  505. .thenStateShouldEqual(expectedState);
  506. });
  507. });
  508. describe('and nothing differs', () => {
  509. it('then it should return update ui', () => {
  510. const { initalState, serializedUrlState } = setup();
  511. const expectedState = { ...initalState };
  512. reducerTester()
  513. .givenReducer(exploreReducer, initalState)
  514. .whenActionIsDispatched(
  515. updateLocation({
  516. query: {
  517. left: serializedUrlState,
  518. },
  519. path: '/explore',
  520. })
  521. )
  522. .thenStateShouldEqual(expectedState);
  523. });
  524. });
  525. });
  526. });
  527. });
  528. });
  529. });