transactionReducer.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { reducerTester } from '../../../../test/core/redux/reducerTester';
  2. import { TransactionStatus } from '../types';
  3. import { removeVariable, variableStateNotStarted } from './sharedReducer';
  4. import {
  5. initialTransactionState,
  6. transactionReducer,
  7. TransactionState,
  8. variablesClearTransaction,
  9. variablesCompleteTransaction,
  10. variablesInitTransaction,
  11. } from './transactionReducer';
  12. describe('transactionReducer', () => {
  13. describe('when variablesInitTransaction is dispatched', () => {
  14. it('then state should be correct', () => {
  15. reducerTester<TransactionState>()
  16. .givenReducer(transactionReducer, { ...initialTransactionState })
  17. .whenActionIsDispatched(variablesInitTransaction({ uid: 'a uid' }))
  18. .thenStateShouldEqual({ ...initialTransactionState, uid: 'a uid', status: TransactionStatus.Fetching });
  19. });
  20. });
  21. describe('when variablesCompleteTransaction is dispatched', () => {
  22. describe('and transaction uid is the same', () => {
  23. it('then state should be correct', () => {
  24. reducerTester<TransactionState>()
  25. .givenReducer(transactionReducer, {
  26. ...initialTransactionState,
  27. uid: 'before',
  28. status: TransactionStatus.Fetching,
  29. })
  30. .whenActionIsDispatched(variablesCompleteTransaction({ uid: 'before' }))
  31. .thenStateShouldEqual({ ...initialTransactionState, uid: 'before', status: TransactionStatus.Completed });
  32. });
  33. });
  34. describe('and transaction uid is not the same', () => {
  35. it('then state should be correct', () => {
  36. reducerTester<TransactionState>()
  37. .givenReducer(transactionReducer, {
  38. ...initialTransactionState,
  39. uid: 'before',
  40. status: TransactionStatus.Fetching,
  41. })
  42. .whenActionIsDispatched(variablesCompleteTransaction({ uid: 'after' }))
  43. .thenStateShouldEqual({ ...initialTransactionState, uid: 'before', status: TransactionStatus.Fetching });
  44. });
  45. });
  46. });
  47. describe('when variablesClearTransaction is dispatched', () => {
  48. it('then state should be correct', () => {
  49. reducerTester<TransactionState>()
  50. .givenReducer(transactionReducer, {
  51. ...initialTransactionState,
  52. uid: 'before',
  53. status: TransactionStatus.Completed,
  54. })
  55. .whenActionIsDispatched(variablesClearTransaction())
  56. .thenStateShouldEqual({ ...initialTransactionState });
  57. });
  58. });
  59. describe('extraReducers', () => {
  60. describe('isDirty', () => {
  61. describe('when called during fetch', () => {
  62. it('then isDirty should not be changed', () => {
  63. reducerTester<TransactionState>()
  64. .givenReducer(transactionReducer, {
  65. ...initialTransactionState,
  66. status: TransactionStatus.Fetching,
  67. })
  68. .whenActionIsDispatched(removeVariable({} as any))
  69. .thenStateShouldEqual({ uid: null, status: TransactionStatus.Fetching, isDirty: false });
  70. });
  71. });
  72. describe('when called after clean', () => {
  73. it('then isDirty should not be changed', () => {
  74. reducerTester<TransactionState>()
  75. .givenReducer(transactionReducer, {
  76. ...initialTransactionState,
  77. status: TransactionStatus.NotStarted,
  78. })
  79. .whenActionIsDispatched(removeVariable({} as any))
  80. .thenStateShouldEqual({ uid: null, status: TransactionStatus.NotStarted, isDirty: false });
  81. });
  82. });
  83. describe('when called after complete with action that affects isDirty', () => {
  84. it('then isDirty should be changed', () => {
  85. reducerTester<TransactionState>()
  86. .givenReducer(transactionReducer, {
  87. ...initialTransactionState,
  88. status: TransactionStatus.Completed,
  89. })
  90. .whenActionIsDispatched(removeVariable({} as any))
  91. .thenStateShouldEqual({ uid: null, status: TransactionStatus.Completed, isDirty: true });
  92. });
  93. });
  94. describe('when called after complete with action that does not affect isDirty', () => {
  95. it('then isDirty should be changed', () => {
  96. reducerTester<TransactionState>()
  97. .givenReducer(transactionReducer, {
  98. ...initialTransactionState,
  99. status: TransactionStatus.Completed,
  100. })
  101. .whenActionIsDispatched(variableStateNotStarted({} as any))
  102. .thenStateShouldEqual({ uid: null, status: TransactionStatus.Completed, isDirty: false });
  103. });
  104. });
  105. });
  106. });
  107. });