reducers.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import {
  2. itemReducer,
  3. makeExploreItemState,
  4. exploreReducer,
  5. makeInitialUpdateState,
  6. initialExploreState,
  7. } from './reducers';
  8. import { ExploreId, ExploreItemState, ExploreUrlState, ExploreState } from 'app/types/explore';
  9. import { reducerTester } from 'test/core/redux/reducerTester';
  10. import { scanStartAction, scanStopAction, splitOpenAction, splitCloseAction } from './actionTypes';
  11. import { Reducer } from 'redux';
  12. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  13. import { updateLocation } from 'app/core/actions/location';
  14. import { LogsDedupStrategy } from 'app/core/logs_model';
  15. import { serializeStateToUrlParam } from 'app/core/utils/explore';
  16. describe('Explore item reducer', () => {
  17. describe('scanning', () => {
  18. test('should start scanning', () => {
  19. const scanner = jest.fn();
  20. const initalState = {
  21. ...makeExploreItemState(),
  22. scanning: false,
  23. scanner: undefined,
  24. };
  25. reducerTester()
  26. .givenReducer(itemReducer as Reducer<ExploreItemState, ActionOf<any>>, initalState)
  27. .whenActionIsDispatched(scanStartAction({ exploreId: ExploreId.left, scanner }))
  28. .thenStateShouldEqual({
  29. ...makeExploreItemState(),
  30. scanning: true,
  31. scanner,
  32. });
  33. });
  34. test('should stop scanning', () => {
  35. const scanner = jest.fn();
  36. const initalState = {
  37. ...makeExploreItemState(),
  38. scanning: true,
  39. scanner,
  40. scanRange: {},
  41. };
  42. reducerTester()
  43. .givenReducer(itemReducer as Reducer<ExploreItemState, ActionOf<any>>, initalState)
  44. .whenActionIsDispatched(scanStopAction({ exploreId: ExploreId.left }))
  45. .thenStateShouldEqual({
  46. ...makeExploreItemState(),
  47. scanning: false,
  48. scanner: undefined,
  49. scanRange: undefined,
  50. });
  51. });
  52. });
  53. });
  54. export const setup = (urlStateOverrides?: any) => {
  55. const update = makeInitialUpdateState();
  56. const urlStateDefaults: ExploreUrlState = {
  57. datasource: 'some-datasource',
  58. queries: [],
  59. range: {
  60. from: '',
  61. to: '',
  62. },
  63. ui: {
  64. dedupStrategy: LogsDedupStrategy.none,
  65. showingGraph: false,
  66. showingTable: false,
  67. showingLogs: false,
  68. },
  69. };
  70. const urlState: ExploreUrlState = { ...urlStateDefaults, ...urlStateOverrides };
  71. const serializedUrlState = serializeStateToUrlParam(urlState);
  72. const initalState = { split: false, left: { urlState, update }, right: { urlState, update } };
  73. return {
  74. initalState,
  75. serializedUrlState,
  76. };
  77. };
  78. describe('Explore reducer', () => {
  79. describe('split view', () => {
  80. it("should make right pane a duplicate of the given item's state on split open", () => {
  81. const leftItemMock = {
  82. containerWidth: 100,
  83. } as ExploreItemState;
  84. const initalState = {
  85. split: null,
  86. left: leftItemMock as ExploreItemState,
  87. right: makeExploreItemState(),
  88. } as ExploreState;
  89. reducerTester()
  90. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  91. .whenActionIsDispatched(splitOpenAction({ itemState: leftItemMock }))
  92. .thenStateShouldEqual({
  93. split: true,
  94. left: leftItemMock,
  95. right: leftItemMock,
  96. });
  97. });
  98. describe('split close', () => {
  99. it('should keep right pane as left when left is closed', () => {
  100. const leftItemMock = {
  101. containerWidth: 100,
  102. } as ExploreItemState;
  103. const rightItemMock = {
  104. containerWidth: 200,
  105. } as ExploreItemState;
  106. const initalState = {
  107. split: null,
  108. left: leftItemMock,
  109. right: rightItemMock,
  110. } as ExploreState;
  111. // closing left item
  112. reducerTester()
  113. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  114. .whenActionIsDispatched(splitCloseAction({ itemId: ExploreId.left }))
  115. .thenStateShouldEqual({
  116. split: false,
  117. left: rightItemMock,
  118. right: initialExploreState.right,
  119. });
  120. });
  121. it('should reset right pane when it is closed ', () => {
  122. const leftItemMock = {
  123. containerWidth: 100,
  124. } as ExploreItemState;
  125. const rightItemMock = {
  126. containerWidth: 200,
  127. } as ExploreItemState;
  128. const initalState = {
  129. split: null,
  130. left: leftItemMock,
  131. right: rightItemMock,
  132. } as ExploreState;
  133. // closing left item
  134. reducerTester()
  135. .givenReducer(exploreReducer as Reducer<ExploreState, ActionOf<any>>, initalState)
  136. .whenActionIsDispatched(splitCloseAction({ itemId: ExploreId.right }))
  137. .thenStateShouldEqual({
  138. split: false,
  139. left: leftItemMock,
  140. right: initialExploreState.right,
  141. });
  142. });
  143. });
  144. });
  145. describe('when updateLocation is dispatched', () => {
  146. describe('and payload does not contain a query', () => {
  147. it('then it should just return state', () => {
  148. reducerTester()
  149. .givenReducer(exploreReducer, {})
  150. .whenActionIsDispatched(updateLocation({ query: null }))
  151. .thenStateShouldEqual({});
  152. });
  153. });
  154. describe('and payload contains a query', () => {
  155. describe("but does not contain 'left'", () => {
  156. it('then it should just return state', () => {
  157. reducerTester()
  158. .givenReducer(exploreReducer, {})
  159. .whenActionIsDispatched(updateLocation({ query: {} }))
  160. .thenStateShouldEqual({});
  161. });
  162. });
  163. describe("and query contains a 'right'", () => {
  164. it('then it should add split in state', () => {
  165. const { initalState, serializedUrlState } = setup();
  166. const expectedState = { ...initalState, split: true };
  167. reducerTester()
  168. .givenReducer(exploreReducer, initalState)
  169. .whenActionIsDispatched(
  170. updateLocation({
  171. query: {
  172. left: serializedUrlState,
  173. right: serializedUrlState,
  174. },
  175. })
  176. )
  177. .thenStateShouldEqual(expectedState);
  178. });
  179. });
  180. describe("and query contains a 'left'", () => {
  181. describe('but urlState is not set in state', () => {
  182. it('then it should just add urlState and update in state', () => {
  183. const { initalState, serializedUrlState } = setup();
  184. const stateWithoutUrlState = { ...initalState, left: { urlState: null } };
  185. const expectedState = { ...initalState };
  186. reducerTester()
  187. .givenReducer(exploreReducer, stateWithoutUrlState)
  188. .whenActionIsDispatched(
  189. updateLocation({
  190. query: {
  191. left: serializedUrlState,
  192. },
  193. path: '/explore',
  194. })
  195. )
  196. .thenStateShouldEqual(expectedState);
  197. });
  198. });
  199. describe("but '/explore' is missing in path", () => {
  200. it('then it should just add urlState and update in state', () => {
  201. const { initalState, serializedUrlState } = setup();
  202. const expectedState = { ...initalState };
  203. reducerTester()
  204. .givenReducer(exploreReducer, initalState)
  205. .whenActionIsDispatched(
  206. updateLocation({
  207. query: {
  208. left: serializedUrlState,
  209. },
  210. path: '/dashboard',
  211. })
  212. )
  213. .thenStateShouldEqual(expectedState);
  214. });
  215. });
  216. describe("and '/explore' is in path", () => {
  217. describe('and datasource differs', () => {
  218. it('then it should return update datasource', () => {
  219. const { initalState, serializedUrlState } = setup();
  220. const expectedState = {
  221. ...initalState,
  222. left: {
  223. ...initalState.left,
  224. update: {
  225. ...initalState.left.update,
  226. datasource: true,
  227. },
  228. },
  229. };
  230. const stateWithDifferentDataSource = {
  231. ...initalState,
  232. left: {
  233. ...initalState.left,
  234. urlState: {
  235. ...initalState.left.urlState,
  236. datasource: 'different datasource',
  237. },
  238. },
  239. };
  240. reducerTester()
  241. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  242. .whenActionIsDispatched(
  243. updateLocation({
  244. query: {
  245. left: serializedUrlState,
  246. },
  247. path: '/explore',
  248. })
  249. )
  250. .thenStateShouldEqual(expectedState);
  251. });
  252. });
  253. describe('and range differs', () => {
  254. it('then it should return update range', () => {
  255. const { initalState, serializedUrlState } = setup();
  256. const expectedState = {
  257. ...initalState,
  258. left: {
  259. ...initalState.left,
  260. update: {
  261. ...initalState.left.update,
  262. range: true,
  263. },
  264. },
  265. };
  266. const stateWithDifferentDataSource = {
  267. ...initalState,
  268. left: {
  269. ...initalState.left,
  270. urlState: {
  271. ...initalState.left.urlState,
  272. range: {
  273. from: 'now',
  274. to: 'now-6h',
  275. },
  276. },
  277. },
  278. };
  279. reducerTester()
  280. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  281. .whenActionIsDispatched(
  282. updateLocation({
  283. query: {
  284. left: serializedUrlState,
  285. },
  286. path: '/explore',
  287. })
  288. )
  289. .thenStateShouldEqual(expectedState);
  290. });
  291. });
  292. describe('and queries differs', () => {
  293. it('then it should return update queries', () => {
  294. const { initalState, serializedUrlState } = setup();
  295. const expectedState = {
  296. ...initalState,
  297. left: {
  298. ...initalState.left,
  299. update: {
  300. ...initalState.left.update,
  301. queries: true,
  302. },
  303. },
  304. };
  305. const stateWithDifferentDataSource = {
  306. ...initalState,
  307. left: {
  308. ...initalState.left,
  309. urlState: {
  310. ...initalState.left.urlState,
  311. queries: [{ expr: '{__filename__="some.log"}' }],
  312. },
  313. },
  314. };
  315. reducerTester()
  316. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  317. .whenActionIsDispatched(
  318. updateLocation({
  319. query: {
  320. left: serializedUrlState,
  321. },
  322. path: '/explore',
  323. })
  324. )
  325. .thenStateShouldEqual(expectedState);
  326. });
  327. });
  328. describe('and ui differs', () => {
  329. it('then it should return update ui', () => {
  330. const { initalState, serializedUrlState } = setup();
  331. const expectedState = {
  332. ...initalState,
  333. left: {
  334. ...initalState.left,
  335. update: {
  336. ...initalState.left.update,
  337. ui: true,
  338. },
  339. },
  340. };
  341. const stateWithDifferentDataSource = {
  342. ...initalState,
  343. left: {
  344. ...initalState.left,
  345. urlState: {
  346. ...initalState.left.urlState,
  347. ui: {
  348. ...initalState.left.urlState.ui,
  349. showingGraph: true,
  350. },
  351. },
  352. },
  353. };
  354. reducerTester()
  355. .givenReducer(exploreReducer, stateWithDifferentDataSource)
  356. .whenActionIsDispatched(
  357. updateLocation({
  358. query: {
  359. left: serializedUrlState,
  360. },
  361. path: '/explore',
  362. })
  363. )
  364. .thenStateShouldEqual(expectedState);
  365. });
  366. });
  367. describe('and nothing differs', () => {
  368. fit('then it should return update ui', () => {
  369. const { initalState, serializedUrlState } = setup();
  370. const expectedState = { ...initalState };
  371. reducerTester()
  372. .givenReducer(exploreReducer, initalState)
  373. .whenActionIsDispatched(
  374. updateLocation({
  375. query: {
  376. left: serializedUrlState,
  377. },
  378. path: '/explore',
  379. })
  380. )
  381. .thenStateShouldEqual(expectedState);
  382. });
  383. });
  384. });
  385. });
  386. });
  387. });
  388. });