reducer.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  2. import { toVariablePayload } from '../utils';
  3. import {
  4. addVariableEditorError,
  5. changeVariableEditorExtended,
  6. changeVariableNameFailed,
  7. changeVariableNameSucceeded,
  8. cleanEditorState,
  9. clearIdInEditor,
  10. initialVariableEditorState,
  11. removeVariableEditorError,
  12. setIdInEditor,
  13. variableEditorMounted,
  14. variableEditorReducer,
  15. VariableEditorState,
  16. variableEditorUnMounted,
  17. } from './reducer';
  18. describe('variableEditorReducer', () => {
  19. describe('when setIdInEditor is dispatched', () => {
  20. it('then state should be correct', () => {
  21. const payload = { id: '0' };
  22. reducerTester<VariableEditorState>()
  23. .givenReducer(variableEditorReducer, { ...initialVariableEditorState })
  24. .whenActionIsDispatched(setIdInEditor(payload))
  25. .thenStateShouldEqual({
  26. ...initialVariableEditorState,
  27. id: '0',
  28. });
  29. });
  30. });
  31. describe('when clearIdInEditor is dispatched', () => {
  32. it('then state should be correct', () => {
  33. reducerTester<VariableEditorState>()
  34. .givenReducer(variableEditorReducer, { ...initialVariableEditorState, id: '0' })
  35. .whenActionIsDispatched(clearIdInEditor())
  36. .thenStateShouldEqual({
  37. ...initialVariableEditorState,
  38. });
  39. });
  40. });
  41. describe('when variableEditorMounted is dispatched', () => {
  42. it('then state should be correct', () => {
  43. const payload = { name: 'A name' };
  44. reducerTester<VariableEditorState>()
  45. .givenReducer(variableEditorReducer, { ...initialVariableEditorState })
  46. .whenActionIsDispatched(variableEditorMounted(payload))
  47. .thenStateShouldEqual({
  48. ...initialVariableEditorState,
  49. name: 'A name',
  50. });
  51. });
  52. });
  53. describe('when variableEditorUnMounted is dispatched', () => {
  54. it('then state should be correct', () => {
  55. const initialState = {
  56. ...initialVariableEditorState,
  57. id: '0',
  58. name: 'A name',
  59. isValid: false,
  60. errors: { update: 'Something wrong' },
  61. extended: null,
  62. };
  63. const payload = toVariablePayload({ id: '0', type: 'textbox' });
  64. reducerTester<VariableEditorState>()
  65. .givenReducer(variableEditorReducer, initialState)
  66. .whenActionIsDispatched(variableEditorUnMounted(payload))
  67. .thenStateShouldEqual({ ...initialVariableEditorState });
  68. });
  69. });
  70. describe('when changeVariableNameSucceeded is dispatched there are other errors', () => {
  71. it('then state should be correct', () => {
  72. const initialState = {
  73. ...initialVariableEditorState,
  74. name: 'A duplicate name',
  75. isValid: false,
  76. errors: { name: 'Duplicate', update: 'Update failed' },
  77. };
  78. const payload = toVariablePayload({ id: '0', type: 'textbox' }, { newName: 'New Name' });
  79. reducerTester<VariableEditorState>()
  80. .givenReducer(variableEditorReducer, initialState)
  81. .whenActionIsDispatched(changeVariableNameSucceeded(payload))
  82. .thenStateShouldEqual({
  83. ...initialState,
  84. isValid: false,
  85. errors: { update: 'Update failed' },
  86. name: 'New Name',
  87. });
  88. });
  89. });
  90. describe('when changeVariableNameSucceeded is dispatched there are no other errors', () => {
  91. it('then state should be correct', () => {
  92. const initialState = {
  93. ...initialVariableEditorState,
  94. name: 'A duplicate name',
  95. isValid: false,
  96. errors: { name: 'Duplicate' },
  97. };
  98. const payload = toVariablePayload({ id: '0', type: 'textbox' }, { newName: 'New Name' });
  99. reducerTester<VariableEditorState>()
  100. .givenReducer(variableEditorReducer, initialState)
  101. .whenActionIsDispatched(changeVariableNameSucceeded(payload))
  102. .thenStateShouldEqual({
  103. ...initialState,
  104. isValid: true,
  105. errors: {},
  106. name: 'New Name',
  107. });
  108. });
  109. });
  110. describe('when changeVariableNameFailed is dispatched', () => {
  111. it('then state should be correct', () => {
  112. const payload = { newName: 'Duplicate name', errorText: 'Name is an duplicate' };
  113. reducerTester<VariableEditorState>()
  114. .givenReducer(variableEditorReducer, { ...initialVariableEditorState })
  115. .whenActionIsDispatched(changeVariableNameFailed(payload))
  116. .thenStateShouldEqual({
  117. ...initialVariableEditorState,
  118. isValid: false,
  119. errors: { name: 'Name is an duplicate' },
  120. name: 'Duplicate name',
  121. });
  122. });
  123. });
  124. describe('when addVariableEditorError is dispatched', () => {
  125. it('then state should be correct', () => {
  126. const payload = { errorProp: 'someProp', errorText: 'someProp failed' };
  127. reducerTester<VariableEditorState>()
  128. .givenReducer(variableEditorReducer, { ...initialVariableEditorState })
  129. .whenActionIsDispatched(addVariableEditorError(payload))
  130. .thenStateShouldEqual({
  131. ...initialVariableEditorState,
  132. isValid: false,
  133. errors: { someProp: 'someProp failed' },
  134. });
  135. });
  136. });
  137. describe('when removeVariableEditorError is dispatched and there are other errors', () => {
  138. it('then state should be correct', () => {
  139. const payload = { errorProp: 'someProp' };
  140. reducerTester<VariableEditorState>()
  141. .givenReducer(variableEditorReducer, {
  142. ...initialVariableEditorState,
  143. errors: { update: 'Update failed', someProp: 'someProp failed' },
  144. isValid: false,
  145. })
  146. .whenActionIsDispatched(removeVariableEditorError(payload))
  147. .thenStateShouldEqual({
  148. ...initialVariableEditorState,
  149. isValid: false,
  150. errors: { update: 'Update failed' },
  151. });
  152. });
  153. });
  154. describe('when removeVariableEditorError is dispatched and there are no other errors', () => {
  155. it('then state should be correct', () => {
  156. const payload = { errorProp: 'someProp' };
  157. reducerTester<VariableEditorState>()
  158. .givenReducer(variableEditorReducer, {
  159. ...initialVariableEditorState,
  160. errors: { someProp: 'someProp failed' },
  161. isValid: false,
  162. })
  163. .whenActionIsDispatched(removeVariableEditorError(payload))
  164. .thenStateShouldEqual({
  165. ...initialVariableEditorState,
  166. isValid: true,
  167. errors: {},
  168. });
  169. });
  170. });
  171. describe('when changeVariableEditorExtended is dispatched', () => {
  172. it('then state should be correct', () => {
  173. const payload = { dataSourceTypes: [] };
  174. reducerTester<VariableEditorState>()
  175. .givenReducer(variableEditorReducer, { ...initialVariableEditorState })
  176. .whenActionIsDispatched(changeVariableEditorExtended(payload))
  177. .thenStateShouldEqual({
  178. ...initialVariableEditorState,
  179. extended: {
  180. dataSourceTypes: [],
  181. },
  182. });
  183. });
  184. });
  185. describe('when cleanEditorState is dispatched', () => {
  186. it('then state should be correct', () => {
  187. reducerTester<VariableEditorState>()
  188. .givenReducer(variableEditorReducer, {
  189. ...initialVariableEditorState,
  190. isValid: false,
  191. errors: { name: 'Name is an duplicate' },
  192. name: 'Duplicate name',
  193. })
  194. .whenActionIsDispatched(cleanEditorState())
  195. .thenStateShouldEqual({ ...initialVariableEditorState });
  196. });
  197. });
  198. });