sagas.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 request from 'app/utils/request'
  24. import actions from 'app/containers/Projects/actions'
  25. import OrgActions from 'app/containers/Organizations/actions'
  26. import {
  27. getProjects,
  28. addProject,
  29. editProject,
  30. deleteProject,
  31. addProjectAdmin,
  32. addProjectRole,
  33. transferProject,
  34. searchProject,
  35. unStarProject,
  36. getProjectStarUser,
  37. getCollectProjects,
  38. editCollectProject,
  39. loadRelRoleProject,
  40. updateRelRoleProject,
  41. deleteRelRoleProject,
  42. getProjectRoles,
  43. excludeRole
  44. } from 'app/containers/Projects/sagas'
  45. import { mockStore } from './fixtures'
  46. import { getMockResponse } from 'test/utils/fixtures'
  47. describe('Projects Sagas', () => {
  48. const { projects, project, projectId } = mockStore
  49. describe('getProjects Saga', () => {
  50. it('should dispatch the projectsLoaded action if it requests the data successfully', () => {
  51. return expectSaga(getProjects, actions.loadProjects())
  52. .provide([[matchers.call.fn(request), getMockResponse(projects)]])
  53. .put(actions.projectsLoaded(projects))
  54. .run()
  55. })
  56. it('should call the loadProjectsFail action if the response errors', () => {
  57. const errors = new Error('error')
  58. return expectSaga(getProjects, actions.loadProjects())
  59. .provide([[matchers.call.fn(request), throwError(errors)]])
  60. .put(actions.loadProjectsFail())
  61. .run()
  62. })
  63. })
  64. describe('addProjects Saga', () => {
  65. const addProjectActions = actions.addProject(project, () => void 0)
  66. it('should dispatch the projectAdded action if it requests the data successfully', () => {
  67. return expectSaga(addProject, addProjectActions)
  68. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  69. .put(actions.projectAdded(project))
  70. .run()
  71. })
  72. it('should call the addProjectFail action if the response errors', () => {
  73. const errors = new Error('error')
  74. return expectSaga(addProject, addProjectActions)
  75. .provide([[matchers.call.fn(request), throwError(errors)]])
  76. .put(actions.addProjectFail())
  77. .run()
  78. })
  79. })
  80. describe('editProjects Saga', () => {
  81. const editProjectActions = actions.editProject(project, () => void 0)
  82. it('should dispatch the projectEdited action if it requests the data successfully', () => {
  83. return expectSaga(editProject, editProjectActions)
  84. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  85. .put(actions.projectEdited(project))
  86. .run()
  87. })
  88. it('should call the editProjectFail action if the response errors', () => {
  89. const errors = new Error('error')
  90. return expectSaga(editProject, editProjectActions)
  91. .provide([[matchers.call.fn(request), throwError(errors)]])
  92. .put(actions.editProjectFail())
  93. .run()
  94. })
  95. })
  96. describe('deleteProjects Saga', () => {
  97. const deleteProjectActions = actions.deleteProject(projectId, () => void 0)
  98. it('should dispatch the projectDeleted action if it requests the data successfully', () => {
  99. return expectSaga(deleteProject, deleteProjectActions)
  100. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  101. .put(actions.projectDeleted(projectId))
  102. .run()
  103. })
  104. it('should call the deleteProjectFail action if the response errors', () => {
  105. const errors = new Error('error')
  106. return expectSaga(deleteProject, deleteProjectActions)
  107. .provide([[matchers.call.fn(request), throwError(errors)]])
  108. .put(actions.deleteProjectFail())
  109. .run()
  110. })
  111. })
  112. describe('addProjectAdmin Saga', () => {
  113. const addProjectAdminActions = actions.addProjectAdmin(
  114. projectId,
  115. projectId,
  116. () => void 0
  117. )
  118. it('should call the addProjectAdminFail action if the response errors', () => {
  119. const errors = new Error('error')
  120. return expectSaga(addProjectAdmin, addProjectAdminActions)
  121. .provide([[matchers.call.fn(request), throwError(errors)]])
  122. .put(actions.addProjectFail())
  123. .run()
  124. })
  125. })
  126. describe('addProjectRole Saga', () => {
  127. const addProjectRoleActions = actions.addProjectRole(
  128. projectId,
  129. [projectId],
  130. () => void 0
  131. )
  132. it('should call the addProjectRoleFail action if the response errors', () => {
  133. const errors = new Error('error')
  134. return expectSaga(addProjectRole, addProjectRoleActions)
  135. .provide([[matchers.call.fn(request), throwError(errors)]])
  136. .put(actions.addProjectRoleFail())
  137. .run()
  138. })
  139. })
  140. describe('transferProject Saga', () => {
  141. const transferProjectActions = actions.transferProject(projectId, projectId)
  142. it('should dispatch the projectTransfered action if it requests the data successfully', () => {
  143. return expectSaga(transferProject, transferProjectActions)
  144. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  145. .put(actions.projectTransfered(project))
  146. .run()
  147. })
  148. it('should call the transferProjectFail action if the response errors', () => {
  149. const errors = new Error('error')
  150. return expectSaga(transferProject, transferProjectActions)
  151. .provide([[matchers.call.fn(request), throwError(errors)]])
  152. .put(actions.transferProjectFail())
  153. .run()
  154. })
  155. })
  156. describe('searchProject Saga', () => {
  157. const [keywords, pageNum, pageSize] = ['abc', 1, 20]
  158. const transferProjectActions = actions.searchProject({keywords, pageNum, pageSize})
  159. it('should dispatch the projectSearched action if it requests the data successfully', () => {
  160. return expectSaga(searchProject, transferProjectActions)
  161. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  162. .put(actions.projectSearched(project))
  163. .run()
  164. })
  165. it('should call the searchProjectFail action if the response errors', () => {
  166. const errors = new Error('error')
  167. return expectSaga(searchProject, transferProjectActions)
  168. .provide([[matchers.call.fn(request), throwError(errors)]])
  169. .put(actions.searchProjectFail())
  170. .run()
  171. })
  172. })
  173. describe('unStarProject Saga', () => {
  174. const transferProjectActions = actions.unStarProject(projectId, () => void 0)
  175. it('should dispatch the unStarProjectSuccess action if it requests the data successfully', () => {
  176. return expectSaga(unStarProject, transferProjectActions)
  177. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  178. .put(actions.unStarProjectSuccess(project))
  179. .run()
  180. })
  181. it('should call the unStarProjectFail action if the response errors', () => {
  182. const errors = new Error('error')
  183. return expectSaga(unStarProject, transferProjectActions)
  184. .provide([[matchers.call.fn(request), throwError(errors)]])
  185. .put(actions.unStarProjectFail())
  186. .run()
  187. })
  188. })
  189. describe('getProjectStarUser Saga', () => {
  190. const getProjectStarUserActions = actions.getProjectStarUser(projectId)
  191. it('should dispatch the getProjectStarUserSuccess action if it requests the data successfully', () => {
  192. return expectSaga(getProjectStarUser, getProjectStarUserActions)
  193. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  194. .put(actions.getProjectStarUserSuccess(project))
  195. .run()
  196. })
  197. it('should call the getProjectStarUserFail action if the response errors', () => {
  198. const errors = new Error('error')
  199. return expectSaga(getProjectStarUser, getProjectStarUserActions)
  200. .provide([[matchers.call.fn(request), throwError(errors)]])
  201. .put(actions.getProjectStarUserFail())
  202. .run()
  203. })
  204. })
  205. describe('getCollectProjects Saga', () => {
  206. it('should dispatch the collectProjectLoaded action if it requests the data successfully', () => {
  207. return expectSaga(getCollectProjects)
  208. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  209. .put(actions.collectProjectLoaded(project))
  210. .run()
  211. })
  212. it('should call the collectProjectFail action if the response errors', () => {
  213. const errors = new Error('error')
  214. return expectSaga(getCollectProjects)
  215. .provide([[matchers.call.fn(request), throwError(errors)]])
  216. .put(actions.collectProjectFail())
  217. .run()
  218. })
  219. })
  220. describe('editCollectProject Saga', () => {
  221. const [
  222. isFavorite,
  223. proId,
  224. resolve] = [false, projectId, () => void 0]
  225. const clickCollectProjectsActions = actions.clickCollectProjects(isFavorite, proId, resolve)
  226. it('should dispatch the collectProjectClicked action if it requests the data successfully', () => {
  227. return expectSaga(editCollectProject, clickCollectProjectsActions)
  228. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  229. .put(actions.collectProjectClicked({isFavorite, proId, resolve}))
  230. .run()
  231. })
  232. it('should call the clickCollectProjectFail action if the response errors', () => {
  233. const errors = new Error('error')
  234. return expectSaga(editCollectProject, clickCollectProjectsActions)
  235. .provide([[matchers.call.fn(request), throwError(errors)]])
  236. .put(actions.clickCollectProjectFail())
  237. .run()
  238. })
  239. })
  240. describe('loadRelRoleProject Saga', () => {
  241. const loadRelRoleProjectsActions = actions.loadRelRoleProject(
  242. projectId,
  243. projectId
  244. )
  245. it('should dispatch the relRoleProjectLoaded action if it requests the data successfully', () => {
  246. return expectSaga(loadRelRoleProject, loadRelRoleProjectsActions)
  247. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  248. .put(actions.relRoleProjectLoaded(project))
  249. .run()
  250. })
  251. it('should call the loadRelRoleProjectFail action if the response errors', () => {
  252. const errors = new Error('error')
  253. return expectSaga(loadRelRoleProject, loadRelRoleProjectsActions)
  254. .provide([[matchers.call.fn(request), throwError(errors)]])
  255. .put(actions.loadRelRoleProjectFail())
  256. .run()
  257. })
  258. })
  259. describe('updateRelRoleProject Saga', () => {
  260. const loadRelRoleProjectsActions = actions.updateRelRoleProject(
  261. projectId,
  262. projectId,
  263. project
  264. )
  265. it('should dispatch the relRoleProjectUpdated action if it requests the data successfully', () => {
  266. return expectSaga(updateRelRoleProject, loadRelRoleProjectsActions)
  267. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  268. .put(actions.relRoleProjectUpdated(project))
  269. .run()
  270. })
  271. it('should call the updateRelRoleProjectFail action if the response errors', () => {
  272. const errors = new Error('error')
  273. return expectSaga(updateRelRoleProject, loadRelRoleProjectsActions)
  274. .provide([[matchers.call.fn(request), throwError(errors)]])
  275. .put(actions.updateRelRoleProjectFail())
  276. .run()
  277. })
  278. })
  279. describe('deleteRelRoleProject Saga', () => {
  280. const deleteRelRoleProjectsActions = actions.deleteRelRoleProject(
  281. projectId,
  282. projectId,
  283. () => void 0
  284. )
  285. it('should dispatch the relRoleProjectDeleted action if it requests the data successfully', () => {
  286. return expectSaga(deleteRelRoleProject, deleteRelRoleProjectsActions)
  287. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  288. .put(actions.relRoleProjectDeleted(project))
  289. .run()
  290. })
  291. it('should call the deleteRelRoleProjectFail action if the response errors', () => {
  292. const errors = new Error('error')
  293. return expectSaga(deleteRelRoleProject, deleteRelRoleProjectsActions)
  294. .provide([[matchers.call.fn(request), throwError(errors)]])
  295. .put(actions.deleteRelRoleProjectFail())
  296. .run()
  297. })
  298. })
  299. describe('getProjectRoles Saga', () => {
  300. const loadProjectRolesActions = OrgActions.loadProjectRoles(
  301. projectId
  302. )
  303. it('should dispatch the projectRolesLoaded action if it requests the data successfully', () => {
  304. return expectSaga(getProjectRoles, loadProjectRolesActions)
  305. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  306. .put(OrgActions.projectRolesLoaded(project))
  307. .run()
  308. })
  309. it('should call the loadProjectRolesFail action if the response errors', () => {
  310. const errors = new Error('error')
  311. return expectSaga(getProjectRoles, loadProjectRolesActions)
  312. .provide([[matchers.call.fn(request), throwError(errors)]])
  313. .put(OrgActions.loadProjectRolesFail())
  314. .run()
  315. })
  316. })
  317. describe('excludeRole Saga', () => {
  318. const excludeRolesFailActions = actions.excludeRoles(
  319. projectId,
  320. 'type',
  321. () => void 0
  322. )
  323. it('should dispatch the rolesExcluded action if it requests the data successfully', () => {
  324. return expectSaga(excludeRole, excludeRolesFailActions)
  325. .provide([[matchers.call.fn(request), getMockResponse(project)]])
  326. .put(actions.rolesExcluded(project))
  327. .run()
  328. })
  329. it('should call the excludeRolesFail action if the response errors', () => {
  330. const errors = new Error('error')
  331. return expectSaga(excludeRole, excludeRolesFailActions)
  332. .provide([[matchers.call.fn(request), throwError(errors)]])
  333. .put(actions.excludeRolesFail(errors))
  334. .run()
  335. })
  336. })
  337. })