saga.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/Organizations/actions'
  25. import {
  26. getOrganizations,
  27. addOrganization,
  28. editOrganization,
  29. deleteOrganization,
  30. getOrganizationDetail,
  31. getOrganizationsProjects,
  32. getOrganizationsMembers,
  33. getOrganizationsRole,
  34. addRole,
  35. getRoleListByMemberId,
  36. deleteRole,
  37. editRole,
  38. relRoleMember,
  39. getRelRoleMember,
  40. searchMember,
  41. inviteMember,
  42. deleteOrganizationMember,
  43. changeOrganizationMemberRole,
  44. getProjectAdmins,
  45. getVizVisbility,
  46. postVizVisbility
  47. } from 'app/containers/Organizations/sagas'
  48. import { mockStore } from './fixtures'
  49. import { getMockResponse } from 'test/utils/fixtures'
  50. describe('Organizations Sagas', () => {
  51. const { projects, organization, orgId, organizations, roles, role, member, members } = mockStore
  52. describe('getOrganizations Saga', () => {
  53. it('should dispatch the organizationsLoaded action if it requests the data successfully', () => {
  54. return expectSaga(getOrganizations)
  55. .provide([[matchers.call.fn(request), getMockResponse(projects)]])
  56. .put(actions.organizationsLoaded(projects))
  57. .run()
  58. })
  59. it('should call the loadOrganizationsFail action if the response errors', () => {
  60. const errors = new Error('error')
  61. return expectSaga(getOrganizations)
  62. .provide([[matchers.call.fn(request), throwError(errors)]])
  63. .put(actions.loadOrganizationsFail())
  64. .run()
  65. })
  66. })
  67. describe('addOrganization Saga', () => {
  68. const addOrganizationActions = actions.addOrganization(organization, () => void 0)
  69. it('should dispatch the organizationAdded action if it requests the data successfully', () => {
  70. return expectSaga(addOrganization, addOrganizationActions)
  71. .provide([[matchers.call.fn(request), getMockResponse(projects)]])
  72. .put(actions.organizationAdded(projects))
  73. .run()
  74. })
  75. it('should call the addOrganizationFail action if the response errors', () => {
  76. const errors = new Error('error')
  77. return expectSaga(addOrganization, addOrganizationActions)
  78. .provide([[matchers.call.fn(request), throwError(errors)]])
  79. .put(actions.addOrganizationFail())
  80. .run()
  81. })
  82. })
  83. describe('editOrganization Saga', () => {
  84. const editOrganizationActions = actions.editOrganization(organization)
  85. it('should dispatch the organizationEdited action if it requests the data successfully', () => {
  86. return expectSaga(editOrganization, editOrganizationActions)
  87. .provide([[matchers.call.fn(request), getMockResponse(organization)]])
  88. .put(actions.organizationEdited(organization))
  89. .run()
  90. })
  91. it('should call the editOrganizationFail action if the response errors', () => {
  92. const errors = new Error('error')
  93. return expectSaga(editOrganization, editOrganizationActions)
  94. .provide([[matchers.call.fn(request), throwError(errors)]])
  95. .put(actions.editOrganizationFail())
  96. .run()
  97. })
  98. })
  99. describe('deleteOrganization Saga', () => {
  100. const deleteOrganizationActions = actions.deleteOrganization(orgId, () => void 0)
  101. it('should dispatch the organizationDeleted action if it requests the data successfully', () => {
  102. return expectSaga(deleteOrganization, deleteOrganizationActions)
  103. .provide([[matchers.call.fn(request), getMockResponse(organization)]])
  104. .put(actions.organizationDeleted(orgId))
  105. .run()
  106. })
  107. it('should call the deleteOrganizationFail action if the response errors', () => {
  108. const errors = new Error('error')
  109. return expectSaga(deleteOrganization, deleteOrganizationActions)
  110. .provide([[matchers.call.fn(request), throwError(errors)]])
  111. .put(actions.deleteOrganizationFail())
  112. .run()
  113. })
  114. })
  115. describe('getOrganizationDetail Saga', () => {
  116. const loadOrganizationDetailActions = actions.loadOrganizationDetail(orgId)
  117. it('should dispatch the organizationDetailLoaded action if it requests the data successfully', () => {
  118. return expectSaga(getOrganizationDetail, loadOrganizationDetailActions)
  119. .provide([[matchers.call.fn(request), getMockResponse(organization)]])
  120. .put(actions.organizationDetailLoaded(organization))
  121. .run()
  122. })
  123. })
  124. describe('getOrganizationsProjects Saga', () => {
  125. const [id, keyword, pageNum, pageSize] = [orgId, 'password', 1, 20]
  126. const loadOrganizationProjectsActions = actions.loadOrganizationProjects({id, keyword, pageNum, pageSize})
  127. it('should dispatch the organizationsProjectsLoaded action if it requests the data successfully', () => {
  128. return expectSaga(getOrganizationsProjects, loadOrganizationProjectsActions)
  129. .provide([[matchers.call.fn(request), getMockResponse(organizations)]])
  130. .put(actions.organizationsProjectsLoaded(organizations))
  131. .run()
  132. })
  133. it('should call the loadOrganizationsProjectsFail action if the response errors', () => {
  134. const errors = new Error('error')
  135. return expectSaga(getOrganizationsProjects, loadOrganizationProjectsActions)
  136. .provide([[matchers.call.fn(request), throwError(errors)]])
  137. .put(actions.loadOrganizationsProjectsFail())
  138. .run()
  139. })
  140. })
  141. describe('getOrganizationsMembers Saga', () => {
  142. const loadOrganizationMembersActions = actions.loadOrganizationMembers(orgId)
  143. it('should dispatch the organizationsMembersLoaded action if it requests the data successfully', () => {
  144. return expectSaga(getOrganizationsMembers, loadOrganizationMembersActions)
  145. .provide([[matchers.call.fn(request), getMockResponse(organizations)]])
  146. .put(actions.organizationsMembersLoaded(organizations))
  147. .run()
  148. })
  149. it('should call the loadOrganizationsMembersFail action if the response errors', () => {
  150. const errors = new Error('error')
  151. return expectSaga(getOrganizationsMembers, loadOrganizationMembersActions)
  152. .provide([[matchers.call.fn(request), throwError(errors)]])
  153. .put(actions.loadOrganizationsMembersFail())
  154. .run()
  155. })
  156. })
  157. describe('getOrganizationsMembers Saga', () => {
  158. const loadOrganizationRoleActions = actions.loadOrganizationRole(orgId)
  159. it('should dispatch the organizationsRoleLoaded action if it requests the data successfully', () => {
  160. return expectSaga(getOrganizationsRole, loadOrganizationRoleActions)
  161. .provide([[matchers.call.fn(request), getMockResponse(organizations)]])
  162. .put(actions.organizationsRoleLoaded(organizations))
  163. .run()
  164. })
  165. it('should call the loadOrganizationsRoleFail action if the response errors', () => {
  166. const errors = new Error('error')
  167. return expectSaga(getOrganizationsRole, loadOrganizationRoleActions)
  168. .provide([[matchers.call.fn(request), throwError(errors)]])
  169. .put(actions.loadOrganizationsRoleFail())
  170. .run()
  171. })
  172. })
  173. describe('addRole Saga', () => {
  174. const [name, description, id, resolve] = ['name', 'desc', orgId, () => void 0]
  175. const loadOrganizationRoleActions = actions.addRole(name, description, id, resolve)
  176. it('should dispatch the roleAdded action if it requests the data successfully', () => {
  177. return expectSaga(addRole, loadOrganizationRoleActions)
  178. .provide([[matchers.call.fn(request), getMockResponse(roles)]])
  179. .put(actions.roleAdded(roles))
  180. .run()
  181. })
  182. it('should call the addRoleFail action if the response errors', () => {
  183. const errors = new Error('error')
  184. return expectSaga(addRole, loadOrganizationRoleActions)
  185. .provide([[matchers.call.fn(request), throwError(errors)]])
  186. .put(actions.addRoleFail())
  187. .run()
  188. })
  189. })
  190. describe('getRoleListByMemberId Saga', () => {
  191. const [memberId, orgId, resolve] = [1, 1, () => void 0]
  192. const loadOrganizationRoleActions = actions.getRoleListByMemberId(memberId, orgId, resolve)
  193. it('should dispatch the getRoleListByMemberIdSuccess action if it requests the data successfully', () => {
  194. return expectSaga(getRoleListByMemberId, loadOrganizationRoleActions)
  195. .provide([[matchers.call.fn(request), getMockResponse(roles)]])
  196. .put(actions.getRoleListByMemberIdSuccess(roles, memberId))
  197. .run()
  198. })
  199. it('should call the getRoleListByMemberIdFail action if the response errors', () => {
  200. const errors = new Error('error')
  201. return expectSaga(getRoleListByMemberId, loadOrganizationRoleActions)
  202. .provide([[matchers.call.fn(request), throwError(errors)]])
  203. .put(actions.getRoleListByMemberIdFail(errors, memberId))
  204. .run()
  205. })
  206. })
  207. describe('deleteRole Saga', () => {
  208. const [id, resolve ] = [1, () => void 0]
  209. const loadOrganizationRoleActions = actions.deleteRole(id, resolve)
  210. it('should dispatch the roleDeleted action if it requests the data successfully', () => {
  211. return expectSaga(deleteRole, loadOrganizationRoleActions)
  212. .provide([[matchers.call.fn(request), getMockResponse(role)]])
  213. .put(actions.roleDeleted(role))
  214. .run()
  215. })
  216. it('should call the deleteRoleFail action if the response errors', () => {
  217. const errors = new Error('error')
  218. return expectSaga(deleteRole, loadOrganizationRoleActions)
  219. .provide([[matchers.call.fn(request), throwError(errors)]])
  220. .put(actions.deleteRoleFail())
  221. .run()
  222. })
  223. })
  224. describe('editRole Saga', () => {
  225. const [name, description, id, resolve] = ['name', 'desc', 1, () => void 0]
  226. const editRoleActions = actions.editRole(name, description, id, resolve)
  227. it('should dispatch the roleEdited action if it requests the data successfully', () => {
  228. return expectSaga(editRole, editRoleActions)
  229. .provide([[matchers.call.fn(request), getMockResponse(role)]])
  230. .put(actions.roleEdited(role))
  231. .run()
  232. })
  233. it('should call the editRoleFail action if the response errors', () => {
  234. const errors = new Error('error')
  235. return expectSaga(editRole, editRoleActions)
  236. .provide([[matchers.call.fn(request), throwError(errors)]])
  237. .put(actions.editRoleFail())
  238. .run()
  239. })
  240. })
  241. describe('relRoleMember Saga', () => {
  242. const [id, memberIds, resolve] = [ 1, [1], () => void 0]
  243. const relRoleMemberActions = actions.relRoleMember(id, memberIds, resolve)
  244. it('should dispatch the relRoleMemberSuccess action if it requests the data successfully', () => {
  245. return expectSaga(relRoleMember, relRoleMemberActions)
  246. .provide([[matchers.call.fn(request), getMockResponse(role)]])
  247. .put(actions.relRoleMemberSuccess())
  248. .run()
  249. })
  250. it('should call the relRoleMemberFail action if the response errors', () => {
  251. const errors = new Error('error')
  252. return expectSaga(relRoleMember, relRoleMemberActions)
  253. .provide([[matchers.call.fn(request), throwError(errors)]])
  254. .put(actions.relRoleMemberFail())
  255. .run()
  256. })
  257. })
  258. describe('relRoleMember Saga', () => {
  259. const [id, resolve] = [ 1, () => void 0]
  260. const getRelRoleMemberActions = actions.getRelRoleMember(id, resolve)
  261. it('should dispatch the getRelRoleMemberSuccess action if it requests the data successfully', () => {
  262. return expectSaga(getRelRoleMember, getRelRoleMemberActions)
  263. .provide([[matchers.call.fn(request), getMockResponse(role)]])
  264. .put(actions.getRelRoleMemberSuccess())
  265. .run()
  266. })
  267. it('should call the getRelRoleMemberFail action if the response errors', () => {
  268. const errors = new Error('error')
  269. return expectSaga(getRelRoleMember, getRelRoleMemberActions)
  270. .provide([[matchers.call.fn(request), throwError(errors)]])
  271. .put(actions.getRelRoleMemberFail())
  272. .run()
  273. })
  274. })
  275. describe('searchMember Saga', () => {
  276. const searchMemberActions = actions.searchMember('keywords')
  277. it('should dispatch the memberSearched action if it requests the data successfully', () => {
  278. return expectSaga(searchMember, searchMemberActions)
  279. .provide([[matchers.call.fn(request), getMockResponse(member)]])
  280. .put(actions.memberSearched(member))
  281. .run()
  282. })
  283. it('should call the searchMemberFail action if the response errors', () => {
  284. const errors = new Error('error')
  285. return expectSaga(searchMember, searchMemberActions)
  286. .provide([[matchers.call.fn(request), throwError(errors)]])
  287. .put(actions.searchMemberFail())
  288. .run()
  289. })
  290. })
  291. describe('inviteMember Saga', () => {
  292. const [ orgId, members, needEmail, resolve] = [1, [member], false, () => void 0 ]
  293. const inventMemberActions = actions.inviteMember(orgId, members, needEmail, resolve)
  294. it('should dispatch the inviteMemberSuccess action if it requests the data successfully', () => {
  295. return expectSaga(inviteMember, inventMemberActions)
  296. .provide([[matchers.call.fn(request), getMockResponse(member)]])
  297. .put(actions.inviteMemberSuccess(member))
  298. .run()
  299. })
  300. it('should call the inviteMemberFail action if the response errors', () => {
  301. const errors = new Error('error')
  302. return expectSaga(inviteMember, inventMemberActions)
  303. .provide([[matchers.call.fn(request), throwError(errors)]])
  304. .put(actions.inviteMemberFail())
  305. .run()
  306. })
  307. })
  308. describe('deleteOrganizationMember Saga', () => {
  309. const [ relationId, resolve] = [1, () => void 0 ]
  310. const deleteOrganizationMemberActions = actions.deleteOrganizationMember(relationId, resolve)
  311. it('should dispatch the organizationMemberDeleted action if it requests the data successfully', () => {
  312. return expectSaga(deleteOrganizationMember, deleteOrganizationMemberActions)
  313. .provide([[matchers.call.fn(request), getMockResponse(relationId)]])
  314. .put(actions.organizationMemberDeleted(relationId))
  315. .run()
  316. })
  317. it('should call the deleteOrganizationMemberFail action if the response errors', () => {
  318. const errors = new Error('error')
  319. return expectSaga(deleteOrganizationMember, deleteOrganizationMemberActions)
  320. .provide([[matchers.call.fn(request), throwError(errors)]])
  321. .put(actions.deleteOrganizationMemberFail())
  322. .run()
  323. })
  324. })
  325. describe('changeOrganizationMemberRole Saga', () => {
  326. const [ relationId, newRole, resolve ] = [1, role, () => void 0 ]
  327. const changeOrganizationMemberRoleActions = actions.changeOrganizationMemberRole(relationId, newRole, resolve)
  328. it('should dispatch the organizationMemberRoleChanged action if it requests the data successfully', () => {
  329. return expectSaga(changeOrganizationMemberRole, changeOrganizationMemberRoleActions)
  330. .provide([[matchers.call.fn(request), getMockResponse(member)]])
  331. .put(actions.organizationMemberRoleChanged(relationId, member))
  332. .run()
  333. })
  334. it('should call the changeOrganizationMemberRoleFail action if the response errors', () => {
  335. const errors = new Error('error')
  336. return expectSaga(changeOrganizationMemberRole, changeOrganizationMemberRoleActions)
  337. .provide([[matchers.call.fn(request), throwError(errors)]])
  338. .put(actions.changeOrganizationMemberRoleFail())
  339. .run()
  340. })
  341. })
  342. describe('getProjectAdmins Saga', () => {
  343. const changeOrganizationMemberRoleActions = actions.loadProjectAdmin(orgId)
  344. it('should dispatch the projectAdminLoaded action if it requests the data successfully', () => {
  345. return expectSaga(getProjectAdmins, changeOrganizationMemberRoleActions)
  346. .provide([[matchers.call.fn(request), getMockResponse(members)]])
  347. .put(actions.projectAdminLoaded(members))
  348. .run()
  349. })
  350. it('should call the loadProjectAdminFail action if the response errors', () => {
  351. const errors = new Error('error')
  352. return expectSaga(getProjectAdmins, changeOrganizationMemberRoleActions)
  353. .provide([[matchers.call.fn(request), throwError(errors)]])
  354. .put(actions.loadProjectAdminFail())
  355. .run()
  356. })
  357. })
  358. describe('getVizVisbility Saga', () => {
  359. const [roleId, projectId, resolve] = [ orgId, orgId, () => void 0]
  360. const getVizVisbilityActions = actions.getVizVisbility(roleId, projectId, resolve)
  361. it('should dispatch the getVizVisbilitySaga action if it requests the data successfully', () => {
  362. return expectSaga(getVizVisbility, getVizVisbilityActions)
  363. .provide([[matchers.call.fn(request), getMockResponse({})]])
  364. .run()
  365. })
  366. })
  367. describe('postVizVisbility Saga', () => {
  368. const [id, permission, resolve] = [ orgId, {}, () => void 0]
  369. const postVizVisbilityActions = actions.postVizVisbility(id, permission, resolve)
  370. it('should dispatch the postVizVisbilityActions action if it requests the data successfully', () => {
  371. return expectSaga(getVizVisbility, postVizVisbilityActions)
  372. .provide([[matchers.call.fn(request), getMockResponse({})]])
  373. .run()
  374. })
  375. })
  376. })