reducer.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Projects/reducer'
  22. import actions from 'app/containers/Projects/actions'
  23. import OrganizationActionTypes from 'app/containers/Organizations/actions'
  24. import { mockAnonymousAction } from 'test/utils/fixtures'
  25. import { mockStore } from './fixtures'
  26. describe('projectsReducer', () => {
  27. const { projectId, projects, project, user, role } = 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 projectsLoaded action correctly', () => {
  36. const expectedResult = produce(state, (draft) => {
  37. draft.projects = projects
  38. })
  39. expect(reducer(state, actions.projectsLoaded(projects))).toEqual(
  40. expectedResult
  41. )
  42. })
  43. it('should handle the relRoleProjectLoaded action correctly', () => {
  44. const expectedResult = produce(state, (draft) => {
  45. draft.currentProjectRole = projects
  46. })
  47. expect(reducer(state, actions.relRoleProjectLoaded(projects))).toEqual(
  48. expectedResult
  49. )
  50. })
  51. it('should handle the projectAdded action correctly', () => {
  52. const expectedResult = produce(state, (draft) => {
  53. if (draft.projects) {
  54. draft.projects.unshift(project)
  55. } else {
  56. draft.projects = [project]
  57. }
  58. })
  59. expect(reducer(state, actions.projectAdded(project))).toEqual(
  60. expectedResult
  61. )
  62. })
  63. it('should handle the projectDeleted action correctly', () => {
  64. const expectedResult = produce(state, (draft) => {
  65. if (draft.projects) {
  66. draft.projects = draft.projects.filter((d) => d.id !== project.id)
  67. draft.collectProjects = draft.collectProjects.filter(
  68. (d) => d.id !== project.id
  69. )
  70. }
  71. })
  72. expect(reducer(state, actions.projectDeleted(project.id))).toEqual(
  73. expectedResult
  74. )
  75. })
  76. it('should handle the loadProjectDetail action correctly', () => {
  77. const expectedResult = produce(state, (draft) => {
  78. draft.currentProjectLoading = true
  79. })
  80. expect(reducer(state, actions.loadProjectDetail(project.id))).toEqual(
  81. expectedResult
  82. )
  83. })
  84. it('should handle the clearCurrentProject action correctly', () => {
  85. const expectedResult = produce(state, (draft) => {
  86. draft.currentProject = null
  87. })
  88. expect(reducer(state, actions.clearCurrentProject())).toEqual(
  89. expectedResult
  90. )
  91. })
  92. it('should handle the projectDetailLoaded action correctly', () => {
  93. const expectedResult = produce(state, (draft) => {
  94. draft.currentProjectLoading = false
  95. draft.currentProject = project
  96. })
  97. expect(reducer(state, actions.projectDetailLoaded(project))).toEqual(
  98. expectedResult
  99. )
  100. })
  101. it('should handle the projectSearched action correctly', () => {
  102. const expectedResult = produce(state, (draft) => {
  103. draft.searchProject = project
  104. })
  105. expect(reducer(state, actions.projectSearched(project))).toEqual(
  106. expectedResult
  107. )
  108. })
  109. it('should handle the getProjectStarUserSuccess action correctly', () => {
  110. const expectedResult = produce(state, (draft) => {
  111. draft.starUserList = [user]
  112. })
  113. expect(reducer(state, actions.getProjectStarUserSuccess([user]))).toEqual(
  114. expectedResult
  115. )
  116. })
  117. it('should handle the collectProjectLoaded action correctly', () => {
  118. const expectedResult = produce(state, (draft) => {
  119. draft.collectProjects = projects
  120. })
  121. expect(reducer(state, actions.collectProjectLoaded(projects))).toEqual(
  122. expectedResult
  123. )
  124. })
  125. it('should handle the projectRolesLoaded action correctly', () => {
  126. const expectedResult = produce(state, (draft) => {
  127. draft.projectRoles = [role]
  128. })
  129. expect(
  130. reducer(state, OrganizationActionTypes.projectRolesLoaded([role]))
  131. ).toEqual(expectedResult)
  132. })
  133. })