sagas.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 { expectSaga } from 'redux-saga-test-plan'
  21. import * as matchers from 'redux-saga-test-plan/matchers'
  22. import { throwError } from 'redux-saga-test-plan/providers'
  23. import {call} from 'redux-saga/effects'
  24. import request from 'app/utils/request'
  25. import actions from 'app/containers/Schedule/actions'
  26. import {
  27. getSchedules,
  28. getScheduleDetail,
  29. addSchedule,
  30. deleteSchedule,
  31. editSchedule,
  32. getSuggestMails,
  33. getVizsData,
  34. changeScheduleStatus,
  35. executeScheduleImmediately
  36. } from 'app/containers/Schedule/sagas'
  37. import { mockStore } from './fixtures'
  38. import { getMockResponse } from 'test/utils/fixtures'
  39. describe('Schedule Sagas', () => {
  40. const {
  41. schedule,
  42. projectId,
  43. schedules,
  44. keywords,
  45. jobStatus,
  46. mails,
  47. api
  48. } = mockStore
  49. describe('getSchedules Saga', () => {
  50. const getSchedulesActions = actions.loadSchedules(projectId)
  51. it('should dispatch the schedulesLoaded action if it requests the data successfully', () => {
  52. return expectSaga(getSchedules, getSchedulesActions)
  53. .provide([[matchers.call.fn(request), getMockResponse(projectId)]])
  54. .dispatch(actions.schedulesLoaded(schedules))
  55. .run()
  56. })
  57. it('should call the loadSchedulesFail action if the response errors', () => {
  58. const errors = new Error('error')
  59. return expectSaga(getSchedules, getSchedulesActions)
  60. .provide([[matchers.call.fn(request), throwError(errors)]])
  61. .put(actions.loadSchedulesFail())
  62. .run()
  63. })
  64. })
  65. describe('getScheduleDetail Saga', () => {
  66. const getScheduleDetailActions = actions.loadScheduleDetail(schedule.id)
  67. it('should dispatch the scheduleDetailLoaded action if it requests the data successfully', () => {
  68. return expectSaga(getScheduleDetail, getScheduleDetailActions)
  69. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  70. .dispatch(actions.scheduleDetailLoaded(schedule))
  71. .run()
  72. })
  73. it('should call the loadScheduleDetailFail action if the response errors', () => {
  74. const errors = new Error('error')
  75. return expectSaga(getScheduleDetail, getScheduleDetailActions)
  76. .provide([[matchers.call.fn(request), throwError(errors)]])
  77. .put(actions.loadScheduleDetailFail())
  78. .run()
  79. })
  80. })
  81. describe('addSchedule Saga', () => {
  82. const addScheduleActions = actions.addSchedule(schedule, () => void 0)
  83. it('should dispatch the scheduleAdded action if it requests the data successfully', () => {
  84. return expectSaga(addSchedule, addScheduleActions)
  85. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  86. .dispatch(actions.scheduleAdded(schedule))
  87. .run()
  88. })
  89. it('should call the addScheduleFail action if the response errors', () => {
  90. const errors = new Error('error')
  91. return expectSaga(addSchedule, addScheduleActions)
  92. .provide([[matchers.call.fn(request), throwError(errors)]])
  93. .put(actions.addScheduleFail())
  94. .run()
  95. })
  96. })
  97. describe('deleteSchedule Saga', () => {
  98. const deleteScheduleActions = actions.deleteSchedule(schedule.id)
  99. it('should dispatch the scheduleDeleted action if it requests the data successfully', () => {
  100. return expectSaga(deleteSchedule, deleteScheduleActions)
  101. .provide([[matchers.call.fn(request), getMockResponse(schedule.id)]])
  102. .put(actions.scheduleDeleted(schedule.id))
  103. .run()
  104. })
  105. it('should call the deleteScheduleFail action if the response errors', () => {
  106. const errors = new Error('error')
  107. return expectSaga(deleteSchedule, deleteScheduleActions)
  108. .provide([[matchers.call.fn(request), throwError(errors)]])
  109. .put(actions.deleteScheduleFail())
  110. .run()
  111. })
  112. })
  113. describe('editSchedule Saga', () => {
  114. const editScheduleActions = actions.editSchedule(schedule, () => void 0)
  115. it('should dispatch the scheduleEdited action if it requests the data successfully', () => {
  116. return expectSaga(editSchedule, editScheduleActions)
  117. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  118. .put(actions.scheduleEdited(schedule))
  119. .run()
  120. })
  121. it('should call the editScheduleFail action if the response errors', () => {
  122. const errors = new Error('error')
  123. return expectSaga(editSchedule, editScheduleActions)
  124. .provide([[matchers.call.fn(request), throwError(errors)]])
  125. .put(actions.editScheduleFail())
  126. .run()
  127. })
  128. })
  129. describe('getSuggestMails Saga', () => {
  130. const loadSuggestMailsActions = actions.loadSuggestMails(keywords)
  131. it('should dispatch the suggestMailsLoaded action if it requests the data successfully', () => {
  132. return expectSaga(getSuggestMails, loadSuggestMailsActions)
  133. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  134. .dispatch(actions.suggestMailsLoaded(mails))
  135. .run()
  136. })
  137. it('should call the loadSuggestMailsFail action if the response errors', () => {
  138. const errors = new Error('error')
  139. return expectSaga(getSuggestMails, loadSuggestMailsActions)
  140. .provide([[matchers.call.fn(request), throwError(errors)]])
  141. .put(actions.loadSuggestMailsFail())
  142. .run()
  143. })
  144. })
  145. describe('executeScheduleImmediately Saga', () => {
  146. const executeScheduleImmediatelyActions = actions.executeScheduleImmediately(
  147. schedule.id,
  148. () => void 0
  149. )
  150. it('should dispatch the executeScheduleImmediatelyActions action if it requests the data successfully', () => {
  151. return expectSaga(
  152. executeScheduleImmediately,
  153. executeScheduleImmediatelyActions
  154. )
  155. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  156. .run()
  157. })
  158. })
  159. describe('changeScheduleStatus Saga', () => {
  160. const changeScheduleStatusActions = actions.changeSchedulesStatus(
  161. schedule.id,
  162. jobStatus
  163. )
  164. it('should dispatch the scheduleStatusChanged action if it requests the data successfully', () => {
  165. return expectSaga(changeScheduleStatus, changeScheduleStatusActions)
  166. .provide([[matchers.call.fn(request), getMockResponse(schedule)]])
  167. .dispatch(actions.scheduleStatusChanged(schedule))
  168. .run()
  169. })
  170. it('should call the changeSchedulesStatusFail action if the response errors', () => {
  171. const errors = new Error('error')
  172. return expectSaga(changeScheduleStatus, changeScheduleStatusActions)
  173. .provide([[matchers.call.fn(request), throwError(errors)]])
  174. .put(actions.changeSchedulesStatusFail())
  175. .run()
  176. })
  177. })
  178. describe('getVizsData Saga', () => {
  179. const loadVizsActions = actions.loadVizs(projectId)
  180. it('should dispatch the vizsLoaded action if it requests the data successfully', () => {
  181. return expectSaga(getVizsData, loadVizsActions)
  182. .provide([
  183. [call(request, api), getMockResponse(projectId)],
  184. [matchers.call.fn(request), getMockResponse(projectId)]
  185. ])
  186. .dispatch(actions.vizsLoaded(schedule))
  187. .run()
  188. })
  189. it('should call the loadVizsFail action if the response errors', () => {
  190. const errors = new Error('error')
  191. return expectSaga(getVizsData, loadVizsActions)
  192. .provide([[matchers.call.fn(request), throwError(errors)]])
  193. .put(actions.loadVizsFail())
  194. .run()
  195. })
  196. })
  197. })