reducer.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/Organizations/reducer'
  22. import actions from 'app/containers/Organizations/actions'
  23. import { mockAnonymousAction } from 'test/utils/fixtures'
  24. import { mockStore } from './fixtures'
  25. describe('organizationReducer', () => {
  26. const {
  27. orgId,
  28. projects,
  29. orgProjects,
  30. organization,
  31. organizations,
  32. role,
  33. members,
  34. roles,
  35. memberId
  36. } = mockStore
  37. let state
  38. beforeEach(() => {
  39. state = initialState
  40. })
  41. it('should return the initial state', () => {
  42. expect(reducer(void 0, mockAnonymousAction)).toEqual(state)
  43. })
  44. it('should handle the organizationMemberDeleted action correctly', () => {
  45. const expectedResult = produce(state, (draft) => {
  46. if (draft.currentOrganizationMembers) {
  47. draft.currentOrganizationMembers = draft.currentOrganizationMembers.filter(
  48. (d) => d.id !== orgId
  49. )
  50. }
  51. })
  52. expect(reducer(state, actions.organizationMemberDeleted(orgId))).toEqual(
  53. expectedResult
  54. )
  55. })
  56. it('should handle the organizationsProjectsLoaded action correctly', () => {
  57. const expectedResult = produce(state, (draft) => {
  58. draft.currentOrganizationProjects = orgProjects.list
  59. draft.currentOrganizationProjectsDetail = orgProjects
  60. })
  61. expect(
  62. reducer(state, actions.organizationsProjectsLoaded(orgProjects))
  63. ).toEqual(expectedResult)
  64. })
  65. it('should handle the organizationsMembersLoaded action correctly', () => {
  66. const expectedResult = produce(state, (draft) => {
  67. draft.currentOrganizationMembers = members.map((member) => {
  68. return {
  69. ...member,
  70. roles: 'loading'
  71. }
  72. })
  73. })
  74. expect(reducer(state, actions.organizationsMembersLoaded(members))).toEqual(
  75. expectedResult
  76. )
  77. })
  78. it('should handle the getRoleListByMemberIdFail action correctly', () => {
  79. const expectedResult = produce(state, (draft) => {
  80. const mId = memberId
  81. if (draft.currentOrganizationMembers) {
  82. draft.currentOrganizationMembers = draft.currentOrganizationMembers.map(
  83. (member) =>
  84. member.user.id === mId ? { ...member, roles: undefined } : member
  85. )
  86. }
  87. })
  88. expect(
  89. reducer(state, actions.getRoleListByMemberIdFail('error', memberId))
  90. ).toEqual(expectedResult)
  91. })
  92. it('should handle the getRoleListByMemberIdSuccess action correctly', () => {
  93. const expectedResult = produce(state, (draft) => {
  94. if (draft.currentOrganizationMembers) {
  95. draft.currentOrganizationMembers = draft.currentOrganizationMembers.map(
  96. (member) =>
  97. member.user.id === memberId ? { ...member, roles } : member
  98. )
  99. }
  100. })
  101. expect(
  102. reducer(state, actions.getRoleListByMemberIdSuccess(roles, memberId))
  103. ).toEqual(expectedResult)
  104. })
  105. it('should handle the organizationsRoleLoaded action correctly', () => {
  106. const expectedResult = produce(state, (draft) => {
  107. draft.currentOrganizationRole = role
  108. })
  109. expect(reducer(state, actions.organizationsRoleLoaded(role))).toEqual(
  110. expectedResult
  111. )
  112. })
  113. it('should handle the organizationsLoaded action correctly', () => {
  114. const expectedResult = produce(state, (draft) => {
  115. draft.organizations = organizations
  116. })
  117. expect(reducer(state, actions.organizationsLoaded(organizations))).toEqual(
  118. expectedResult
  119. )
  120. })
  121. it('should handle the organizationAdded action correctly', () => {
  122. const expectedResult = produce(state, (draft) => {
  123. if (draft.organizations) {
  124. draft.organizations.unshift(organization)
  125. } else {
  126. draft.organizations = [organization]
  127. }
  128. })
  129. expect(reducer(state, actions.organizationAdded(organization))).toEqual(
  130. expectedResult
  131. )
  132. })
  133. it('should handle the organizationEdited action correctly', () => {
  134. const expectedResult = produce(state, (draft) => {
  135. draft.organizations.splice(
  136. draft.organizations.findIndex((d) => d.id === organization.id),
  137. 1,
  138. organization
  139. )
  140. })
  141. expect(reducer(state, actions.organizationEdited(organization))).toEqual(
  142. expectedResult
  143. )
  144. })
  145. it('should handle the organizationDeleted action correctly', () => {
  146. const expectedResult = produce(state, (draft) => {
  147. draft.organizations = draft.organizations.filter((d) => d.id !== orgId)
  148. })
  149. expect(reducer(state, actions.organizationDeleted(orgId))).toEqual(
  150. expectedResult
  151. )
  152. })
  153. it('should handle the loadOrganizationDetail action correctly', () => {
  154. const expectedResult = produce(state, (draft) => {
  155. draft.currentOrganizationLoading = true
  156. })
  157. expect(reducer(state, actions.loadOrganizationDetail(orgId))).toEqual(
  158. expectedResult
  159. )
  160. })
  161. it('should handle the organizationDetailLoaded action correctly', () => {
  162. const expectedResult = produce(state, (draft) => {
  163. draft.currentOrganizationLoading = false
  164. draft.currentOrganization = organization
  165. })
  166. expect(
  167. reducer(state, actions.organizationDetailLoaded(organization))
  168. ).toEqual(expectedResult)
  169. })
  170. it('should handle the roleAdded action correctly', () => {
  171. const expectedResult = produce(state, (draft) => {
  172. draft.roleModalLoading = false
  173. })
  174. expect(reducer(state, actions.roleAdded(role))).toEqual(expectedResult)
  175. })
  176. it('should handle the addRoleFail action correctly', () => {
  177. const expectedResult = produce(state, (draft) => {
  178. draft.roleModalLoading = false
  179. })
  180. expect(reducer(state, actions.addRoleFail())).toEqual(expectedResult)
  181. })
  182. it('should handle the searchMember action correctly', () => {
  183. const expectedResult = produce(state, (draft) => {
  184. draft.inviteMemberfetching = true
  185. })
  186. expect(reducer(state, actions.searchMember('keyword'))).toEqual(
  187. expectedResult
  188. )
  189. })
  190. it('should handle the memberSearched action correctly', () => {
  191. const expectedResult = produce(state, (draft) => {
  192. draft.inviteMemberLists = members
  193. draft.inviteMemberfetching = false
  194. })
  195. expect(reducer(state, actions.memberSearched(members))).toEqual(
  196. expectedResult
  197. )
  198. })
  199. it('should handle the searchMemberFail action correctly', () => {
  200. const expectedResult = produce(state, (draft) => {
  201. draft.inviteMemberfetching = true
  202. })
  203. expect(reducer(state, actions.searchMemberFail())).toEqual(expectedResult)
  204. })
  205. it('should handle the setCurrentProject action correctly', () => {
  206. const expectedResult = produce(state, (draft) => {
  207. draft.projectDetail = orgProjects
  208. })
  209. expect(reducer(state, actions.setCurrentProject(orgProjects))).toEqual(
  210. expectedResult
  211. )
  212. })
  213. it('should handle the projectAdminLoaded action correctly', () => {
  214. const expectedResult = produce(state, (draft) => {
  215. draft.projectAdmins = projects
  216. })
  217. expect(reducer(state, actions.projectAdminLoaded(projects))).toEqual(
  218. expectedResult
  219. )
  220. })
  221. it('should handle the projectRolesLoaded action correctly', () => {
  222. const expectedResult = produce(state, (draft) => {
  223. draft.projectRoles = role
  224. })
  225. expect(reducer(state, actions.projectRolesLoaded(role))).toEqual(
  226. expectedResult
  227. )
  228. })
  229. })