reducer.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import produce from 'immer'
  21. import reducer, { initialState } from 'app/containers/Schedule/reducer'
  22. import actions from 'app/containers/Schedule/actions'
  23. import { EmptySchedule, EmptyWeChatWorkSchedule } from 'app/containers/Schedule/constants'
  24. import { mockAnonymousAction } from 'test/utils/fixtures'
  25. import { mockStore } from './fixtures'
  26. describe('projectsReducer', () => {
  27. const { scheduleId, schedules, schedule, jobType, mails, dashboard } = mockStore
  28. let state
  29. beforeEach(() => {
  30. state = initialState
  31. })
  32. it('should return the initial state', () => {
  33. expect(reducer(void 0, mockAnonymousAction)).toEqual(state)
  34. })
  35. it('should handle the loadSchedules action correctly', () => {
  36. const expectedResult = produce(state, (draft) => {
  37. draft.loading.table = true
  38. })
  39. expect(reducer(state, actions.loadSchedules(scheduleId))).toEqual(
  40. expectedResult
  41. )
  42. })
  43. it('should handle the schedulesLoaded action correctly', () => {
  44. const expectedResult = produce(state, (draft) => {
  45. draft.loading.table = false
  46. draft.schedules = schedules
  47. })
  48. expect(reducer(state, actions.schedulesLoaded(schedules))).toEqual(
  49. expectedResult
  50. )
  51. })
  52. it('should handle the relRoleProjectLoaded action correctly', () => {
  53. const expectedResult = produce(state, (draft) => {
  54. draft.loading.table = false
  55. draft.schedules = draft.schedules.filter(({ id }) => id !== scheduleId)
  56. })
  57. expect(reducer(state, actions.scheduleDeleted(scheduleId))).toEqual(
  58. expectedResult
  59. )
  60. })
  61. it('should handle the scheduleAdded action correctly', () => {
  62. const expectedResult = produce(state, (draft) => {
  63. draft.loading.edit = false
  64. draft.schedules.unshift(schedule)
  65. })
  66. expect(reducer(state, actions.scheduleAdded(schedule))).toEqual(
  67. expectedResult
  68. )
  69. })
  70. it('should handle the deleteScheduleFail action correctly', () => {
  71. const expectedResult = produce(state, (draft) => {
  72. draft.loading.table = false
  73. })
  74. expect(reducer(state, actions.deleteScheduleFail())).toEqual(expectedResult)
  75. })
  76. it('should handle the loadScheduleDetail action correctly', () => {
  77. const expectedResult = produce(state, (draft) => {
  78. draft.loading.schedule = true
  79. })
  80. expect(reducer(state, actions.loadScheduleDetail(scheduleId))).toEqual(
  81. expectedResult
  82. )
  83. })
  84. it('should handle the scheduleDetailLoaded action correctly', () => {
  85. const expectedResult = produce(state, (draft) => {
  86. draft.editingSchedule = schedule
  87. draft.loading.schedule = false
  88. })
  89. expect(reducer(state, actions.scheduleDetailLoaded(schedule))).toEqual(
  90. expectedResult
  91. )
  92. })
  93. it('should handle the loadScheduleDetailFail action correctly', () => {
  94. const expectedResult = produce(state, (draft) => {
  95. draft.loading.schedule = false
  96. })
  97. expect(reducer(state, actions.loadScheduleDetailFail())).toEqual(
  98. expectedResult
  99. )
  100. })
  101. it('should handle the addSchedule and editSchedule action correctly', () => {
  102. const expectedResult = produce(state, (draft) => {
  103. draft.loading.edit = true
  104. })
  105. expect(
  106. reducer(
  107. state,
  108. actions.addSchedule(schedule, () => void 0)
  109. )
  110. ).toEqual(expectedResult)
  111. expect(
  112. reducer(
  113. state,
  114. actions.editSchedule(schedule, () => void 0)
  115. )
  116. ).toEqual(expectedResult)
  117. })
  118. it('should handle the scheduleEdited action correctly', () => {
  119. const expectedResult = produce(state, (draft) => {
  120. draft.schedules.splice(
  121. draft.schedules.findIndex(({ id }) => id === scheduleId),
  122. 1,
  123. schedule
  124. )
  125. draft.loading.edit = false
  126. })
  127. expect(reducer(state, actions.scheduleEdited(schedule))).toEqual(
  128. expectedResult
  129. )
  130. })
  131. it('should handle the addScheduleFail action correctly', () => {
  132. const expectedResult = produce(state, (draft) => {
  133. draft.loading.edit = false
  134. })
  135. expect(reducer(state, actions.addScheduleFail())).toEqual(
  136. expectedResult
  137. )
  138. expect(reducer(state, actions.editScheduleFail())).toEqual(
  139. expectedResult
  140. )
  141. })
  142. it('should handle the scheduleStatusChanged action correctly', () => {
  143. const expectedResult = produce(state, (draft) => {
  144. draft.schedules.splice(
  145. draft.schedules.findIndex(
  146. ({ id }) => id === scheduleId
  147. ),
  148. 1,
  149. schedule
  150. )
  151. })
  152. expect(reducer(state, actions.scheduleStatusChanged(schedule))).toEqual(
  153. expectedResult
  154. )
  155. })
  156. it('should handle the changeScheduleJobType action correctly', () => {
  157. const expectedResult = produce(state, (draft) => {
  158. draft.editingSchedule = jobType === 'email' ? EmptySchedule : EmptyWeChatWorkSchedule
  159. })
  160. expect(reducer(state, actions.changeScheduleJobType(jobType))).toEqual(
  161. expectedResult
  162. )
  163. })
  164. it('should handle the suggestMailsLoaded action correctly', () => {
  165. const expectedResult = produce(state, (draft) => {
  166. draft.suggestMails = mails
  167. })
  168. expect(reducer(state, actions.suggestMailsLoaded(mails))).toEqual(
  169. expectedResult
  170. )
  171. })
  172. it('should handle the loadSuggestMailsFail action correctly', () => {
  173. const expectedResult = produce(state, (draft) => {
  174. draft.suggestMails = []
  175. })
  176. expect(reducer(state, actions.loadSuggestMailsFail())).toEqual(
  177. expectedResult
  178. )
  179. })
  180. it('should handle the portalDashboardsLoaded action correctly', () => {
  181. const expectedResult = produce(state, (draft) => {
  182. draft.portalDashboards[scheduleId] = [dashboard]
  183. })
  184. expect(reducer(state, actions.portalDashboardsLoaded(scheduleId, [dashboard]))).toEqual(
  185. expectedResult
  186. )
  187. })
  188. })