sharedReducer.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. import { cloneDeep } from 'lodash';
  2. import { LoadingState, VariableType } from '@grafana/data';
  3. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  4. import { variableAdapters } from '../adapters';
  5. import { createConstantVariableAdapter } from '../constant/adapter';
  6. import { initialConstantVariableModelState } from '../constant/reducer';
  7. import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants';
  8. import { changeVariableNameSucceeded } from '../editor/reducer';
  9. import { createQueryVariableAdapter } from '../query/adapter';
  10. import { initialQueryVariableModelState } from '../query/reducer';
  11. import { ConstantVariableModel, QueryVariableModel, VariableHide, VariableOption } from '../types';
  12. import { toVariablePayload } from '../utils';
  13. import { getVariableState, getVariableTestContext } from './helpers';
  14. import {
  15. addVariable,
  16. changeVariableOrder,
  17. changeVariableProp,
  18. changeVariableType,
  19. duplicateVariable,
  20. removeVariable,
  21. setCurrentVariableValue,
  22. sharedReducer,
  23. variableStateCompleted,
  24. variableStateFailed,
  25. variableStateFetching,
  26. variableStateNotStarted,
  27. } from './sharedReducer';
  28. import { initialVariablesState, KeyedVariableIdentifier, VariablesState } from './types';
  29. variableAdapters.setInit(() => [createQueryVariableAdapter(), createConstantVariableAdapter()]);
  30. describe('sharedReducer', () => {
  31. describe('when addVariable is dispatched', () => {
  32. it('then state should be correct', () => {
  33. const model: any = {
  34. name: 'name from model',
  35. type: 'type from model',
  36. current: undefined,
  37. };
  38. const expected: QueryVariableModel = {
  39. ...initialQueryVariableModelState,
  40. id: 'name from model',
  41. global: true,
  42. index: 0,
  43. name: 'name from model',
  44. type: 'type from model' as unknown as VariableType,
  45. current: {} as unknown as VariableOption,
  46. };
  47. const payload = toVariablePayload({ id: 'name from model', type: 'query' }, { global: true, index: 0, model });
  48. reducerTester<VariablesState>()
  49. .givenReducer(sharedReducer, { ...initialVariablesState })
  50. .whenActionIsDispatched(addVariable(payload))
  51. .thenStateShouldEqual({
  52. ['name from model']: expected,
  53. });
  54. });
  55. });
  56. describe('when addVariable is dispatched for a constant model', () => {
  57. it('then state should be correct', () => {
  58. const model: any = {
  59. name: 'constant',
  60. type: 'constant',
  61. query: 'a constant',
  62. current: { selected: true, text: 'A', value: 'A' },
  63. options: [{ selected: true, text: 'A', value: 'A' }],
  64. };
  65. const expected: ConstantVariableModel = {
  66. ...initialConstantVariableModelState,
  67. id: 'constant',
  68. global: true,
  69. index: 0,
  70. name: 'constant',
  71. type: 'constant',
  72. query: 'a constant',
  73. current: { selected: true, text: 'a constant', value: 'a constant' },
  74. options: [{ selected: true, text: 'a constant', value: 'a constant' }],
  75. };
  76. const payload = toVariablePayload({ id: 'constant', type: 'constant' }, { global: true, index: 0, model });
  77. reducerTester<VariablesState>()
  78. .givenReducer(sharedReducer, { ...initialVariablesState })
  79. .whenActionIsDispatched(addVariable(payload))
  80. .thenStateShouldEqual({
  81. ['constant']: expected,
  82. });
  83. });
  84. });
  85. describe('when removeVariable is dispatched and reIndex is true', () => {
  86. it('then state should be correct', () => {
  87. const initialState: VariablesState = getVariableState(3);
  88. const payload = toVariablePayload({ id: '1', type: 'query' }, { reIndex: true });
  89. reducerTester<VariablesState>()
  90. .givenReducer(sharedReducer, initialState)
  91. .whenActionIsDispatched(removeVariable(payload))
  92. .thenStateShouldEqual({
  93. '0': {
  94. id: '0',
  95. rootStateKey: 'key',
  96. type: 'query',
  97. name: 'Name-0',
  98. hide: VariableHide.dontHide,
  99. index: 0,
  100. label: 'Label-0',
  101. skipUrlSync: false,
  102. global: false,
  103. state: LoadingState.NotStarted,
  104. error: null,
  105. description: null,
  106. },
  107. '2': {
  108. id: '2',
  109. rootStateKey: 'key',
  110. type: 'query',
  111. name: 'Name-2',
  112. hide: VariableHide.dontHide,
  113. index: 1,
  114. label: 'Label-2',
  115. skipUrlSync: false,
  116. global: false,
  117. state: LoadingState.NotStarted,
  118. error: null,
  119. description: null,
  120. },
  121. });
  122. });
  123. });
  124. describe('when removeVariable is dispatched and reIndex is false', () => {
  125. it('then state should be correct', () => {
  126. const initialState: VariablesState = getVariableState(3);
  127. const payload = toVariablePayload({ id: '1', type: 'query' }, { reIndex: false });
  128. reducerTester<VariablesState>()
  129. .givenReducer(sharedReducer, initialState)
  130. .whenActionIsDispatched(removeVariable(payload))
  131. .thenStateShouldEqual({
  132. '0': {
  133. id: '0',
  134. rootStateKey: 'key',
  135. type: 'query',
  136. name: 'Name-0',
  137. hide: VariableHide.dontHide,
  138. index: 0,
  139. label: 'Label-0',
  140. skipUrlSync: false,
  141. global: false,
  142. state: LoadingState.NotStarted,
  143. error: null,
  144. description: null,
  145. },
  146. '2': {
  147. id: '2',
  148. rootStateKey: 'key',
  149. type: 'query',
  150. name: 'Name-2',
  151. hide: VariableHide.dontHide,
  152. index: 2,
  153. label: 'Label-2',
  154. skipUrlSync: false,
  155. global: false,
  156. state: LoadingState.NotStarted,
  157. error: null,
  158. description: null,
  159. },
  160. });
  161. });
  162. });
  163. describe('when duplicateVariable is dispatched', () => {
  164. it('then state should be correct', () => {
  165. const initialState: VariablesState = getVariableState(3, -1, false, true);
  166. const payload = toVariablePayload({ id: '1', type: 'query' }, { newId: '11' });
  167. reducerTester<VariablesState>()
  168. .givenReducer(sharedReducer, initialState)
  169. .whenActionIsDispatched(duplicateVariable(payload))
  170. .thenStateShouldEqual({
  171. ...initialState,
  172. '0': {
  173. id: '0',
  174. rootStateKey: 'key',
  175. type: 'query',
  176. name: 'Name-0',
  177. hide: VariableHide.dontHide,
  178. index: 0,
  179. label: 'Label-0',
  180. skipUrlSync: false,
  181. global: false,
  182. state: LoadingState.NotStarted,
  183. error: null,
  184. description: null,
  185. },
  186. '1': {
  187. id: '1',
  188. rootStateKey: 'key',
  189. type: 'query',
  190. name: 'Name-1',
  191. hide: VariableHide.dontHide,
  192. index: 1,
  193. label: 'Label-1',
  194. skipUrlSync: false,
  195. global: false,
  196. state: LoadingState.NotStarted,
  197. error: null,
  198. description: null,
  199. },
  200. '2': {
  201. id: '2',
  202. rootStateKey: 'key',
  203. type: 'query',
  204. name: 'Name-2',
  205. hide: VariableHide.dontHide,
  206. index: 2,
  207. label: 'Label-2',
  208. skipUrlSync: false,
  209. global: false,
  210. state: LoadingState.NotStarted,
  211. error: null,
  212. description: null,
  213. },
  214. '11': {
  215. ...initialQueryVariableModelState,
  216. id: '11',
  217. rootStateKey: 'key',
  218. name: 'copy_of_Name-1',
  219. index: 3,
  220. label: 'Label-1',
  221. },
  222. });
  223. });
  224. });
  225. describe('when changeVariableOrder is dispatched', () => {
  226. it('then state should be correct', () => {
  227. const initialState: VariablesState = getVariableState(3);
  228. const payload = toVariablePayload({ id: '2', type: 'query' }, { fromIndex: 2, toIndex: 0 });
  229. reducerTester<VariablesState>()
  230. .givenReducer(sharedReducer, initialState)
  231. .whenActionIsDispatched(changeVariableOrder(payload))
  232. .thenStateShouldEqual({
  233. '0': {
  234. id: '0',
  235. rootStateKey: 'key',
  236. type: 'query',
  237. name: 'Name-0',
  238. hide: VariableHide.dontHide,
  239. index: 1,
  240. label: 'Label-0',
  241. skipUrlSync: false,
  242. global: false,
  243. state: LoadingState.NotStarted,
  244. error: null,
  245. description: null,
  246. },
  247. '1': {
  248. id: '1',
  249. rootStateKey: 'key',
  250. type: 'query',
  251. name: 'Name-1',
  252. hide: VariableHide.dontHide,
  253. index: 2,
  254. label: 'Label-1',
  255. skipUrlSync: false,
  256. global: false,
  257. state: LoadingState.NotStarted,
  258. error: null,
  259. description: null,
  260. },
  261. '2': {
  262. id: '2',
  263. rootStateKey: 'key',
  264. type: 'query',
  265. name: 'Name-2',
  266. hide: VariableHide.dontHide,
  267. index: 0,
  268. label: 'Label-2',
  269. skipUrlSync: false,
  270. global: false,
  271. state: LoadingState.NotStarted,
  272. error: null,
  273. description: null,
  274. },
  275. });
  276. });
  277. it('then state should be correct', () => {
  278. const initialState: VariablesState = getVariableState(3);
  279. const payload = toVariablePayload({ id: '0', type: 'query' }, { fromIndex: 0, toIndex: 2 });
  280. reducerTester<VariablesState>()
  281. .givenReducer(sharedReducer, initialState)
  282. .whenActionIsDispatched(changeVariableOrder(payload))
  283. .thenStateShouldEqual({
  284. '0': {
  285. id: '0',
  286. rootStateKey: 'key',
  287. type: 'query',
  288. name: 'Name-0',
  289. hide: VariableHide.dontHide,
  290. index: 2,
  291. label: 'Label-0',
  292. skipUrlSync: false,
  293. global: false,
  294. state: LoadingState.NotStarted,
  295. error: null,
  296. description: null,
  297. },
  298. '1': {
  299. id: '1',
  300. rootStateKey: 'key',
  301. type: 'query',
  302. name: 'Name-1',
  303. hide: VariableHide.dontHide,
  304. index: 0,
  305. label: 'Label-1',
  306. skipUrlSync: false,
  307. global: false,
  308. state: LoadingState.NotStarted,
  309. error: null,
  310. description: null,
  311. },
  312. '2': {
  313. id: '2',
  314. rootStateKey: 'key',
  315. type: 'query',
  316. name: 'Name-2',
  317. hide: VariableHide.dontHide,
  318. index: 1,
  319. label: 'Label-2',
  320. skipUrlSync: false,
  321. global: false,
  322. state: LoadingState.NotStarted,
  323. error: null,
  324. description: null,
  325. },
  326. });
  327. });
  328. });
  329. describe('when setCurrentVariableValue is dispatched and current.text is an Array with values', () => {
  330. it('then state should be correct', () => {
  331. const adapter = createQueryVariableAdapter();
  332. const { initialState } = getVariableTestContext(adapter, {
  333. options: [
  334. { text: 'All', value: '$__all', selected: false },
  335. { text: 'A', value: 'A', selected: false },
  336. { text: 'B', value: 'B', selected: false },
  337. ],
  338. });
  339. const current = { text: ['A', 'B'], selected: true, value: ['A', 'B'] };
  340. const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
  341. reducerTester<VariablesState>()
  342. .givenReducer(sharedReducer, cloneDeep(initialState))
  343. .whenActionIsDispatched(setCurrentVariableValue(payload))
  344. .thenStateShouldEqual({
  345. ...initialState,
  346. '0': {
  347. ...initialState[0],
  348. options: [
  349. { selected: false, text: 'All', value: '$__all' },
  350. { selected: true, text: 'A', value: 'A' },
  351. { selected: true, text: 'B', value: 'B' },
  352. ],
  353. current: { selected: true, text: ['A', 'B'], value: ['A', 'B'] },
  354. } as unknown as QueryVariableModel,
  355. });
  356. });
  357. });
  358. describe('when setCurrentVariableValue is dispatched and current.value is an Array with values except All value', () => {
  359. it('then state should be correct', () => {
  360. const adapter = createQueryVariableAdapter();
  361. const { initialState } = getVariableTestContext(adapter, {
  362. options: [
  363. { text: 'All', value: '$__all', selected: false },
  364. { text: 'A', value: 'A', selected: false },
  365. { text: 'B', value: 'B', selected: false },
  366. ],
  367. });
  368. const current = { text: 'A + B', selected: true, value: ['A', 'B'] };
  369. const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
  370. reducerTester<VariablesState>()
  371. .givenReducer(sharedReducer, cloneDeep(initialState))
  372. .whenActionIsDispatched(setCurrentVariableValue(payload))
  373. .thenStateShouldEqual({
  374. ...initialState,
  375. '0': {
  376. ...initialState[0],
  377. options: [
  378. { selected: false, text: 'All', value: '$__all' },
  379. { selected: true, text: 'A', value: 'A' },
  380. { selected: true, text: 'B', value: 'B' },
  381. ],
  382. current: { selected: true, text: 'A + B', value: ['A', 'B'] },
  383. } as unknown as QueryVariableModel,
  384. });
  385. });
  386. });
  387. describe('when setCurrentVariableValue is dispatched and current.value is an Array with values containing All value', () => {
  388. it('then state should be correct', () => {
  389. const adapter = createQueryVariableAdapter();
  390. const { initialState } = getVariableTestContext(adapter, {
  391. options: [
  392. { text: 'All', value: '$__all', selected: false },
  393. { text: 'A', value: 'A', selected: false },
  394. { text: 'B', value: 'B', selected: false },
  395. ],
  396. });
  397. const current = { text: ALL_VARIABLE_TEXT, selected: true, value: [ALL_VARIABLE_VALUE] };
  398. const payload = toVariablePayload({ id: '0', type: 'query' }, { option: current });
  399. reducerTester<VariablesState>()
  400. .givenReducer(sharedReducer, cloneDeep(initialState))
  401. .whenActionIsDispatched(setCurrentVariableValue(payload))
  402. .thenStateShouldEqual({
  403. ...initialState,
  404. '0': {
  405. ...initialState[0],
  406. options: [
  407. { selected: true, text: 'All', value: '$__all' },
  408. { selected: false, text: 'A', value: 'A' },
  409. { selected: false, text: 'B', value: 'B' },
  410. ],
  411. current: { selected: true, text: 'All', value: ['$__all'] },
  412. } as unknown as QueryVariableModel,
  413. });
  414. });
  415. });
  416. describe('when variableStateNotStarted is dispatched', () => {
  417. it('then state should be correct', () => {
  418. const adapter = createQueryVariableAdapter();
  419. const { initialState } = getVariableTestContext(adapter, {
  420. state: LoadingState.Done,
  421. error: 'Some error',
  422. });
  423. const payload = toVariablePayload({ id: '0', type: 'query' });
  424. reducerTester<VariablesState>()
  425. .givenReducer(sharedReducer, cloneDeep(initialState))
  426. .whenActionIsDispatched(variableStateNotStarted(payload))
  427. .thenStateShouldEqual({
  428. ...initialState,
  429. '0': {
  430. ...initialState[0],
  431. state: LoadingState.NotStarted,
  432. error: null,
  433. } as unknown as QueryVariableModel,
  434. });
  435. });
  436. });
  437. describe('when variableStateFetching is dispatched', () => {
  438. it('then state should be correct', () => {
  439. const adapter = createQueryVariableAdapter();
  440. const { initialState } = getVariableTestContext(adapter, {
  441. state: LoadingState.Done,
  442. error: 'Some error',
  443. });
  444. const payload = toVariablePayload({ id: '0', type: 'query' });
  445. reducerTester<VariablesState>()
  446. .givenReducer(sharedReducer, cloneDeep(initialState))
  447. .whenActionIsDispatched(variableStateFetching(payload))
  448. .thenStateShouldEqual({
  449. ...initialState,
  450. '0': {
  451. ...initialState[0],
  452. state: LoadingState.Loading,
  453. error: null,
  454. } as unknown as QueryVariableModel,
  455. });
  456. });
  457. });
  458. describe('when variableStateCompleted is dispatched', () => {
  459. it('then state should be correct', () => {
  460. const adapter = createQueryVariableAdapter();
  461. const { initialState } = getVariableTestContext(adapter, {
  462. state: LoadingState.Loading,
  463. error: 'Some error',
  464. });
  465. const payload = toVariablePayload({ id: '0', type: 'query' });
  466. reducerTester<VariablesState>()
  467. .givenReducer(sharedReducer, cloneDeep(initialState))
  468. .whenActionIsDispatched(variableStateCompleted(payload))
  469. .thenStateShouldEqual({
  470. ...initialState,
  471. '0': {
  472. ...initialState[0],
  473. state: LoadingState.Done,
  474. error: null,
  475. } as unknown as QueryVariableModel,
  476. });
  477. });
  478. });
  479. describe('when variableStateFailed is dispatched', () => {
  480. it('then state should be correct', () => {
  481. const adapter = createQueryVariableAdapter();
  482. const { initialState } = getVariableTestContext(adapter, { state: LoadingState.Loading });
  483. const payload = toVariablePayload({ id: '0', type: 'query' }, { error: 'Some error' });
  484. reducerTester<VariablesState>()
  485. .givenReducer(sharedReducer, cloneDeep(initialState))
  486. .whenActionIsDispatched(variableStateFailed(payload))
  487. .thenStateShouldEqual({
  488. ...initialState,
  489. '0': {
  490. ...initialState[0],
  491. state: LoadingState.Error,
  492. error: 'Some error',
  493. } as unknown as QueryVariableModel,
  494. });
  495. });
  496. });
  497. describe('when changeVariableProp is dispatched', () => {
  498. it('then state should be correct', () => {
  499. const adapter = createQueryVariableAdapter();
  500. const { initialState } = getVariableTestContext(adapter);
  501. const propName = 'label';
  502. const propValue = 'Updated label';
  503. const payload = toVariablePayload({ id: '0', type: 'query' }, { propName, propValue });
  504. reducerTester<VariablesState>()
  505. .givenReducer(sharedReducer, cloneDeep(initialState))
  506. .whenActionIsDispatched(changeVariableProp(payload))
  507. .thenStateShouldEqual({
  508. ...initialState,
  509. '0': {
  510. ...initialState[0],
  511. label: 'Updated label',
  512. },
  513. });
  514. });
  515. });
  516. describe('when changeVariableNameSucceeded is dispatched', () => {
  517. it('then state should be correct', () => {
  518. const adapter = createQueryVariableAdapter();
  519. const { initialState } = getVariableTestContext(adapter);
  520. const newName = 'A new name';
  521. const payload = toVariablePayload({ id: '0', type: 'query' }, { newName });
  522. reducerTester<VariablesState>()
  523. .givenReducer(sharedReducer, cloneDeep(initialState))
  524. .whenActionIsDispatched(changeVariableNameSucceeded(payload))
  525. .thenStateShouldEqual({
  526. ...initialState,
  527. '0': {
  528. ...initialState[0],
  529. name: 'A new name',
  530. },
  531. });
  532. });
  533. });
  534. describe('when changeVariableType is dispatched', () => {
  535. it('then state should be correct', () => {
  536. const queryAdapter = createQueryVariableAdapter();
  537. const { initialState: queryAdapterState } = getVariableTestContext(queryAdapter);
  538. const constantAdapter = createConstantVariableAdapter();
  539. const { initialState: constantAdapterState } = getVariableTestContext(constantAdapter);
  540. const newType = 'constant' as VariableType;
  541. const identifier: KeyedVariableIdentifier = { id: '0', type: 'query', rootStateKey: 'key' };
  542. const payload = toVariablePayload(identifier, { newType });
  543. reducerTester<VariablesState>()
  544. .givenReducer(sharedReducer, cloneDeep(queryAdapterState))
  545. .whenActionIsDispatched(changeVariableNameSucceeded(toVariablePayload(identifier, { newName: 'test' })))
  546. .whenActionIsDispatched(
  547. changeVariableProp(toVariablePayload(identifier, { propName: 'description', propValue: 'new description' }))
  548. )
  549. .whenActionIsDispatched(
  550. changeVariableProp(toVariablePayload(identifier, { propName: 'label', propValue: 'new label' }))
  551. )
  552. .whenActionIsDispatched(changeVariableType(payload))
  553. .thenStateShouldEqual({
  554. ...constantAdapterState,
  555. '0': {
  556. ...constantAdapterState[0],
  557. rootStateKey: 'key',
  558. name: 'test',
  559. description: 'new description',
  560. label: 'new label',
  561. type: 'constant',
  562. },
  563. });
  564. });
  565. });
  566. });