reducer.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { cloneDeep } from 'lodash';
  2. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  3. import { getVariableTestContext } from '../state/helpers';
  4. import { VariablesState } from '../state/types';
  5. import { AdHocVariableFilter, AdHocVariableModel } from '../types';
  6. import { toVariablePayload } from '../utils';
  7. import { createAdHocVariableAdapter } from './adapter';
  8. import { adHocVariableReducer, filterAdded, filterRemoved, filtersRestored, filterUpdated } from './reducer';
  9. describe('adHocVariableReducer', () => {
  10. const adapter = createAdHocVariableAdapter();
  11. describe('when filterAdded is dispatched', () => {
  12. it('then state should be correct', () => {
  13. const id = '0';
  14. const { initialState } = getVariableTestContext(adapter, { id });
  15. const filter = createFilter('a');
  16. const payload = toVariablePayload({ id, type: 'adhoc' }, filter);
  17. reducerTester<VariablesState>()
  18. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  19. .whenActionIsDispatched(filterAdded(payload))
  20. .thenStateShouldEqual({
  21. [id]: {
  22. ...initialState[id],
  23. filters: [{ value: 'a', operator: '=', condition: '', key: 'a' }],
  24. } as AdHocVariableModel,
  25. });
  26. });
  27. });
  28. describe('when filterAdded is dispatched and filter already exists', () => {
  29. it('then state should be correct', () => {
  30. const id = '0';
  31. const filterA = createFilter('a');
  32. const filterB = createFilter('b');
  33. const { initialState } = getVariableTestContext(adapter, { id, filters: [filterA] });
  34. const payload = toVariablePayload({ id, type: 'adhoc' }, filterB);
  35. reducerTester<VariablesState>()
  36. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  37. .whenActionIsDispatched(filterAdded(payload))
  38. .thenStateShouldEqual({
  39. [id]: {
  40. ...initialState[id],
  41. filters: [
  42. { value: 'a', operator: '=', condition: '', key: 'a' },
  43. { value: 'b', operator: '=', condition: '', key: 'b' },
  44. ],
  45. } as AdHocVariableModel,
  46. });
  47. });
  48. });
  49. describe('when filterRemoved is dispatched to remove second filter', () => {
  50. it('then state should be correct', () => {
  51. const id = '0';
  52. const filterA = createFilter('a');
  53. const filterB = createFilter('b');
  54. const index = 1;
  55. const { initialState } = getVariableTestContext(adapter, { id, filters: [filterA, filterB] });
  56. const payload = toVariablePayload({ id, type: 'adhoc' }, index);
  57. reducerTester<VariablesState>()
  58. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  59. .whenActionIsDispatched(filterRemoved(payload))
  60. .thenStateShouldEqual({
  61. [id]: {
  62. ...initialState[id],
  63. filters: [{ value: 'a', operator: '=', condition: '', key: 'a' }],
  64. } as AdHocVariableModel,
  65. });
  66. });
  67. });
  68. describe('when filterRemoved is dispatched to remove first filter', () => {
  69. it('then state should be correct', () => {
  70. const id = '0';
  71. const filterA = createFilter('a');
  72. const filterB = createFilter('b');
  73. const index = 0;
  74. const { initialState } = getVariableTestContext(adapter, { id, filters: [filterA, filterB] });
  75. const payload = toVariablePayload({ id, type: 'adhoc' }, index);
  76. reducerTester<VariablesState>()
  77. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  78. .whenActionIsDispatched(filterRemoved(payload))
  79. .thenStateShouldEqual({
  80. [id]: {
  81. ...initialState[id],
  82. filters: [{ value: 'b', operator: '=', condition: '', key: 'b' }],
  83. } as AdHocVariableModel,
  84. });
  85. });
  86. });
  87. describe('when filterRemoved is dispatched to all filters', () => {
  88. it('then state should be correct', () => {
  89. const id = '0';
  90. const filterA = createFilter('a');
  91. const index = 0;
  92. const { initialState } = getVariableTestContext(adapter, { id, filters: [filterA] });
  93. const payload = toVariablePayload({ id, type: 'adhoc' }, index);
  94. reducerTester<VariablesState>()
  95. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  96. .whenActionIsDispatched(filterRemoved(payload))
  97. .thenStateShouldEqual({
  98. [id]: {
  99. ...initialState[id],
  100. filters: [] as AdHocVariableFilter[],
  101. } as AdHocVariableModel,
  102. });
  103. });
  104. });
  105. describe('when filterUpdated is dispatched', () => {
  106. it('then state should be correct', () => {
  107. const id = '0';
  108. const original = createFilter('a');
  109. const other = createFilter('b');
  110. const filter = createFilter('aa');
  111. const index = 1;
  112. const { initialState } = getVariableTestContext(adapter, { id, filters: [other, original] });
  113. const payload = toVariablePayload({ id, type: 'adhoc' }, { index, filter });
  114. reducerTester<VariablesState>()
  115. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  116. .whenActionIsDispatched(filterUpdated(payload))
  117. .thenStateShouldEqual({
  118. [id]: {
  119. ...initialState[id],
  120. filters: [
  121. { value: 'b', operator: '=', condition: '', key: 'b' },
  122. { value: 'aa', operator: '=', condition: '', key: 'aa' },
  123. ],
  124. } as AdHocVariableModel,
  125. });
  126. });
  127. });
  128. describe('when filterUpdated is dispatched to update operator', () => {
  129. it('then state should be correct', () => {
  130. const id = '0';
  131. const original = createFilter('a');
  132. const other = createFilter('b');
  133. const filter = createFilter('aa', '>');
  134. const index = 1;
  135. const { initialState } = getVariableTestContext(adapter, { id, filters: [other, original] });
  136. const payload = toVariablePayload({ id, type: 'adhoc' }, { index, filter });
  137. reducerTester<VariablesState>()
  138. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  139. .whenActionIsDispatched(filterUpdated(payload))
  140. .thenStateShouldEqual({
  141. [id]: {
  142. ...initialState[id],
  143. filters: [
  144. { value: 'b', operator: '=', condition: '', key: 'b' },
  145. { value: 'aa', operator: '>', condition: '', key: 'aa' },
  146. ],
  147. } as AdHocVariableModel,
  148. });
  149. });
  150. });
  151. describe('when filtersRestored is dispatched', () => {
  152. it('then state should be correct', () => {
  153. const id = '0';
  154. const original = [createFilter('a'), createFilter('b')];
  155. const restored = [createFilter('aa'), createFilter('bb')];
  156. const { initialState } = getVariableTestContext(adapter, { id, filters: original });
  157. const payload = toVariablePayload({ id, type: 'adhoc' }, restored);
  158. reducerTester<VariablesState>()
  159. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  160. .whenActionIsDispatched(filtersRestored(payload))
  161. .thenStateShouldEqual({
  162. [id]: {
  163. ...initialState[id],
  164. filters: [
  165. { value: 'aa', operator: '=', condition: '', key: 'aa' },
  166. { value: 'bb', operator: '=', condition: '', key: 'bb' },
  167. ],
  168. } as AdHocVariableModel,
  169. });
  170. });
  171. });
  172. describe('when filtersRestored is dispatched on variabel with no filters', () => {
  173. it('then state should be correct', () => {
  174. const id = '0';
  175. const restored = [createFilter('aa'), createFilter('bb')];
  176. const { initialState } = getVariableTestContext(adapter, { id });
  177. const payload = toVariablePayload({ id, type: 'adhoc' }, restored);
  178. reducerTester<VariablesState>()
  179. .givenReducer(adHocVariableReducer, cloneDeep(initialState))
  180. .whenActionIsDispatched(filtersRestored(payload))
  181. .thenStateShouldEqual({
  182. [id]: {
  183. ...initialState[id],
  184. filters: [
  185. { value: 'aa', operator: '=', condition: '', key: 'aa' },
  186. { value: 'bb', operator: '=', condition: '', key: 'bb' },
  187. ],
  188. } as AdHocVariableModel,
  189. });
  190. });
  191. });
  192. });
  193. function createFilter(value: string, operator = '='): AdHocVariableFilter {
  194. return {
  195. value,
  196. operator,
  197. condition: '',
  198. key: value,
  199. };
  200. }