setOptionFromUrl.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { reduxTester } from '../../../../test/core/redux/reduxTester';
  2. import { variableAdapters } from '../adapters';
  3. import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants';
  4. import { createCustomVariableAdapter } from '../custom/adapter';
  5. import { customBuilder } from '../shared/testing/builders';
  6. import { toKeyedVariableIdentifier, toVariablePayload } from '../utils';
  7. import { setOptionFromUrl } from './actions';
  8. import { getTemplatingRootReducer, TemplatingReducerType } from './helpers';
  9. import { toKeyedAction } from './keyedVariablesReducer';
  10. import { addVariable, setCurrentVariableValue } from './sharedReducer';
  11. variableAdapters.setInit(() => [createCustomVariableAdapter()]);
  12. describe('when setOptionFromUrl is dispatched with a custom variable (no refresh property)', () => {
  13. it.each`
  14. urlValue | isMulti | expected
  15. ${'B'} | ${false} | ${'B'}
  16. ${['B']} | ${false} | ${'B'}
  17. ${'X'} | ${false} | ${'X'}
  18. ${''} | ${false} | ${''}
  19. ${null} | ${false} | ${''}
  20. ${undefined} | ${false} | ${''}
  21. ${'B'} | ${true} | ${['B']}
  22. ${['B']} | ${true} | ${['B']}
  23. ${'X'} | ${true} | ${['X']}
  24. ${''} | ${true} | ${['']}
  25. ${['A', 'B']} | ${true} | ${['A', 'B']}
  26. ${null} | ${true} | ${['']}
  27. ${undefined} | ${true} | ${['']}
  28. `('and urlValue is $urlValue then correct actions are dispatched', async ({ urlValue, expected, isMulti }) => {
  29. const key = 'key';
  30. const custom = customBuilder()
  31. .withId('0')
  32. .withRootStateKey(key)
  33. .withMulti(isMulti)
  34. .withOptions('A', 'B', 'C')
  35. .withCurrent('A')
  36. .build();
  37. const tester = await reduxTester<TemplatingReducerType>()
  38. .givenRootReducer(getTemplatingRootReducer())
  39. .whenActionIsDispatched(
  40. toKeyedAction(key, addVariable(toVariablePayload(custom, { global: false, index: 0, model: custom })))
  41. )
  42. .whenAsyncActionIsDispatched(setOptionFromUrl(toKeyedVariableIdentifier(custom), urlValue), true);
  43. await tester.thenDispatchedActionsShouldEqual(
  44. toKeyedAction(
  45. key,
  46. setCurrentVariableValue(
  47. toVariablePayload(
  48. { type: 'custom', id: '0' },
  49. { option: { text: expected, value: expected, selected: false } }
  50. )
  51. )
  52. )
  53. );
  54. });
  55. });
  56. describe('when setOptionFromUrl is dispatched for a variable with a custom all value', () => {
  57. it('and urlValue contains same all value then correct actions are dispatched', async () => {
  58. const allValue = '.*';
  59. const urlValue = allValue;
  60. const key = 'key';
  61. const custom = customBuilder()
  62. .withId('0')
  63. .withRootStateKey(key)
  64. .withMulti(false)
  65. .withIncludeAll()
  66. .withAllValue(allValue)
  67. .withOptions('A', 'B', 'C')
  68. .withCurrent('A')
  69. .build();
  70. const tester = await reduxTester<TemplatingReducerType>()
  71. .givenRootReducer(getTemplatingRootReducer())
  72. .whenActionIsDispatched(
  73. toKeyedAction(key, addVariable(toVariablePayload(custom, { global: false, index: 0, model: custom })))
  74. )
  75. .whenAsyncActionIsDispatched(setOptionFromUrl(toKeyedVariableIdentifier(custom), urlValue), true);
  76. await tester.thenDispatchedActionsShouldEqual(
  77. toKeyedAction(
  78. key,
  79. setCurrentVariableValue(
  80. toVariablePayload(
  81. { type: 'custom', id: '0' },
  82. { option: { text: ALL_VARIABLE_TEXT, value: ALL_VARIABLE_VALUE, selected: false } }
  83. )
  84. )
  85. )
  86. );
  87. });
  88. it('and urlValue differs from all value then correct actions are dispatched', async () => {
  89. const allValue = '.*';
  90. const urlValue = 'X';
  91. const key = 'key';
  92. const custom = customBuilder()
  93. .withId('0')
  94. .withRootStateKey(key)
  95. .withMulti(false)
  96. .withIncludeAll()
  97. .withAllValue(allValue)
  98. .withOptions('A', 'B', 'C')
  99. .withCurrent('A')
  100. .build();
  101. const tester = await reduxTester<TemplatingReducerType>()
  102. .givenRootReducer(getTemplatingRootReducer())
  103. .whenActionIsDispatched(
  104. toKeyedAction(key, addVariable(toVariablePayload(custom, { global: false, index: 0, model: custom })))
  105. )
  106. .whenAsyncActionIsDispatched(setOptionFromUrl(toKeyedVariableIdentifier(custom), urlValue), true);
  107. await tester.thenDispatchedActionsShouldEqual(
  108. toKeyedAction(
  109. key,
  110. setCurrentVariableValue(
  111. toVariablePayload({ type: 'custom', id: '0' }, { option: { text: 'X', value: 'X', selected: false } })
  112. )
  113. )
  114. );
  115. });
  116. it('and urlValue differs but matches an option then correct actions are dispatched', async () => {
  117. const allValue = '.*';
  118. const urlValue = 'B';
  119. const key = 'key';
  120. const custom = customBuilder()
  121. .withId('0')
  122. .withRootStateKey(key)
  123. .withMulti(false)
  124. .withIncludeAll()
  125. .withAllValue(allValue)
  126. .withOptions('A', 'B', 'C')
  127. .withCurrent('A')
  128. .build();
  129. const tester = await reduxTester<TemplatingReducerType>()
  130. .givenRootReducer(getTemplatingRootReducer())
  131. .whenActionIsDispatched(
  132. toKeyedAction(key, addVariable(toVariablePayload(custom, { global: false, index: 0, model: custom })))
  133. )
  134. .whenAsyncActionIsDispatched(setOptionFromUrl(toKeyedVariableIdentifier(custom), urlValue), true);
  135. await tester.thenDispatchedActionsShouldEqual(
  136. toKeyedAction(
  137. key,
  138. setCurrentVariableValue(
  139. toVariablePayload({ type: 'custom', id: '0' }, { option: { text: 'B', value: 'B', selected: false } })
  140. )
  141. )
  142. );
  143. });
  144. it('and custom all value matches an option', async () => {
  145. const allValue = '.*';
  146. const urlValue = allValue;
  147. const key = 'key';
  148. const custom = customBuilder()
  149. .withId('0')
  150. .withRootStateKey(key)
  151. .withMulti(false)
  152. .withIncludeAll()
  153. .withAllValue(allValue)
  154. .withOptions('A', 'B', '.*')
  155. .withCurrent('A')
  156. .build();
  157. custom.options[2].value = 'special value for .*';
  158. const tester = await reduxTester<TemplatingReducerType>()
  159. .givenRootReducer(getTemplatingRootReducer())
  160. .whenActionIsDispatched(
  161. toKeyedAction(key, addVariable(toVariablePayload(custom, { global: false, index: 0, model: custom })))
  162. )
  163. .whenAsyncActionIsDispatched(setOptionFromUrl(toKeyedVariableIdentifier(custom), urlValue), true);
  164. await tester.thenDispatchedActionsShouldEqual(
  165. toKeyedAction(
  166. key,
  167. setCurrentVariableValue(
  168. toVariablePayload(
  169. { type: 'custom', id: '0' },
  170. { option: { text: '.*', value: 'special value for .*', selected: false } }
  171. )
  172. )
  173. )
  174. );
  175. });
  176. });