reducer.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, { displayInitialState } from 'app/containers/Display/reducer'
  22. import actions from 'app/containers/Display/actions'
  23. import VizActions from 'app/containers/Viz/actions'
  24. import { ActionTypes as VizActionTypes } from 'containers/Viz/constants'
  25. import {
  26. mockDisplayId,
  27. mockSlideId,
  28. mockSlideList,
  29. mockHttpError,
  30. mockGraphLayerFormed,
  31. mockSlideLayersOperationInfo,
  32. mockWidgetFormed,
  33. mockPasswordToken,
  34. mockPassword,
  35. mockChangedOperationInfo,
  36. mockDeltaSize,
  37. mockFinish,
  38. mockSlideSize,
  39. mockDeltaPosition,
  40. mockSelected,
  41. mockExclusive,
  42. mockBaseLines,
  43. mockShareLinkParams,
  44. mockShareToken,
  45. mockAuthShareToken,
  46. mockDisplayTitle,
  47. mockFormedViews,
  48. mockCurrentDisplayWidgets,
  49. mockDefaultSlideLayersOperationGraphInfo,
  50. mockGraphLayerId,
  51. mockDisplayState,
  52. mockChartLayerId,
  53. mockSlideLayersInfoGraphSingle,
  54. mockSlideSingleGraphLayerFormed,
  55. defaultSharePanelState
  56. } from './fixtures'
  57. describe('displayReducer', () => {
  58. let state
  59. beforeEach(() => {
  60. state = mockDisplayState.display
  61. })
  62. it('should handle the editSlidesSuccess action correctly', () => {
  63. const expectedResult = produce(state, (draft) => {
  64. draft.lastOperationType = VizActionTypes.EDIT_SLIDES_SUCCESS
  65. })
  66. expect(
  67. reducer(state, VizActions.slidesEdited(mockDisplayId, mockSlideList))
  68. ).toEqual(expectedResult)
  69. })
  70. it('should handle the loadSlideDetail action correctly', () => {
  71. const expectedResult = produce(state, (draft) => {
  72. draft.loading.slideLayers = true
  73. })
  74. expect(
  75. reducer(state, actions.loadSlideDetail(mockDisplayId, mockSlideId))
  76. ).toEqual(expectedResult)
  77. })
  78. it('should handle the slideDetailLoaded action correctly', () => {
  79. const expectedResult = produce(state, (draft) => {
  80. draft.currentDisplayWidgets = mockCurrentDisplayWidgets
  81. draft.currentSlideId = mockSlideId
  82. draft.slideLayers[draft.currentSlideId] = mockSlideSingleGraphLayerFormed
  83. draft.slideLayersInfo = mockSlideLayersInfoGraphSingle
  84. draft.slideLayersOperationInfo[draft.currentSlideId] = mockDefaultSlideLayersOperationGraphInfo
  85. })
  86. expect(
  87. reducer(
  88. state,
  89. actions.slideDetailLoaded(
  90. mockSlideId,
  91. [mockGraphLayerFormed],
  92. [mockWidgetFormed],
  93. mockFormedViews
  94. )
  95. )
  96. ).toEqual(expectedResult)
  97. })
  98. it('should handle the loadSlideDetailFailure action correctly', () => {
  99. const expectedResult = produce(state, (draft) => {
  100. draft.loading.slideLayers = false
  101. })
  102. expect(reducer(state, actions.loadSlideDetailFail(mockHttpError))).toEqual(
  103. expectedResult
  104. )
  105. })
  106. it('should handle the slideLayersDeleted action correctly', () => {
  107. const expectedResult = produce(state, (draft) => {
  108. draft.lastOperationType = 'davinci/Display/DELETE_SLIDE_LAYERS_SUCCESS'
  109. draft.lastLayers = [draft.slideLayers[mockSlideId][mockGraphLayerId]]
  110. delete draft.slideLayers[mockSlideId][mockGraphLayerId]
  111. delete draft.slideLayersInfo[mockSlideId][mockGraphLayerId]
  112. delete draft.slideLayersOperationInfo[mockSlideId][mockGraphLayerId]
  113. })
  114. expect(
  115. reducer(
  116. state,
  117. actions.slideLayersDeleted(mockSlideId, [mockGraphLayerId])
  118. )
  119. ).toEqual(expectedResult)
  120. })
  121. it('should handle the displayPasswordShareLinkLoaded action correctly', () => {
  122. const expectedResult = produce(state, (draft) => {
  123. draft.currentDisplayPasswordShareToken = mockPasswordToken
  124. draft.currentDisplayPasswordPassword = mockPassword
  125. draft.loading.shareToken = false
  126. })
  127. expect(
  128. reducer(
  129. state,
  130. actions.displayPasswordShareLinkLoaded(mockPasswordToken, mockPassword)
  131. )
  132. ).toEqual(expectedResult)
  133. })
  134. it('should handle the slideLayersEdited action correctly', () => {
  135. const expectedResult = produce(state, (draft) => {
  136. draft.lastOperationType = 'davinci/Display/EDIT_SLIDE_LAYERS_SUCCESS'
  137. draft.lastLayers = [draft.slideLayers[mockSlideId][mockGraphLayerId]]
  138. })
  139. expect(
  140. reducer(
  141. state,
  142. actions.slideLayersEdited(mockSlideId, [mockGraphLayerFormed])
  143. )
  144. ).toEqual(expectedResult)
  145. })
  146. it('should handle the changeLayerOperationInfo action correctly', () => {
  147. const expectedResult = produce(state, (draft) => {
  148. draft.slideLayersOperationInfo = mockSlideLayersOperationInfo
  149. })
  150. expect(
  151. reducer(
  152. state,
  153. actions.changeLayerOperationInfo(mockGraphLayerId, mockChangedOperationInfo)
  154. )
  155. ).toEqual(expectedResult)
  156. })
  157. it('should handle the resizeLayerAdjusted action correctly', () => {
  158. const expectedResult = produce(state, (draft) => {
  159. draft.slideLayersOperationInfo[draft.currentSlideId][
  160. mockGraphLayerId
  161. ].resizing = false
  162. })
  163. expect(
  164. reducer(
  165. state,
  166. actions.resizeLayerAdjusted([mockGraphLayerId], mockDeltaSize, true)
  167. )
  168. ).toEqual(expectedResult)
  169. })
  170. it('should handle the dragLayerAdjusted action correctly', () => {
  171. const expectedResult = produce(state, (draft) => {
  172. draft.slideLayersOperationInfo[draft.currentSlideId][
  173. mockGraphLayerId
  174. ].dragging = false
  175. })
  176. expect(
  177. reducer(
  178. state,
  179. actions.dragLayerAdjusted(
  180. [mockChartLayerId],
  181. mockSlideSize,
  182. mockDeltaPosition,
  183. mockFinish
  184. )
  185. )
  186. ).toEqual(expectedResult)
  187. })
  188. it('should handle the selectLayer action correctly', () => {
  189. const expectedResult = produce(state, (draft) => {
  190. draft.slideLayersOperationInfo[draft.currentSlideId][mockGraphLayerId] = {
  191. dragging: false,
  192. editing: false,
  193. resizing: false,
  194. selected: true
  195. }
  196. })
  197. expect(
  198. reducer(
  199. state,
  200. actions.selectLayer(mockGraphLayerId, mockSelected, mockExclusive)
  201. )
  202. ).toEqual(expectedResult)
  203. })
  204. it('should handle the clearLayersOperationInfo action correctly', () => {
  205. const expectedResult = produce(state, (draft) => {
  206. draft.slideLayersOperationInfo = mockSlideLayersOperationInfo
  207. })
  208. expect(
  209. reducer(state, actions.clearLayersOperationInfo(mockChangedOperationInfo))
  210. ).toEqual(expectedResult)
  211. })
  212. it('should handle the clearEditorBaselines action correctly', () => {
  213. const expectedResult = produce(state, (draft) => {
  214. draft.editorBaselines = []
  215. draft.operateItemParams = []
  216. draft.slideLayersOperationInfo[draft.currentSlideId][
  217. mockGraphLayerId
  218. ].dragging = false
  219. })
  220. expect(reducer(state, actions.clearEditorBaselines())).toEqual(
  221. expectedResult
  222. )
  223. })
  224. it('should handle the showEditorBaselines action correctly', () => {
  225. const expectedResult = produce(state, (draft) => {
  226. draft.editorBaselines = [mockBaseLines]
  227. })
  228. expect(
  229. reducer(state, actions.showEditorBaselines([mockBaseLines]))
  230. ).toEqual(expectedResult)
  231. })
  232. it('should handle the slideLayersCopied action correctly', () => {
  233. const expectedResult = produce(state, (draft) => {
  234. draft.clipboardLayers = [mockGraphLayerFormed]
  235. })
  236. expect(
  237. reducer(state, actions.slideLayersCopied([mockGraphLayerFormed]))
  238. ).toEqual(expectedResult)
  239. })
  240. it('should handle the loadDisplayShareLink action correctly', () => {
  241. const expectedResult = produce(state, (draft) => {
  242. draft.loading.shareToken = true
  243. draft.currentDisplayAuthorizedShareToken = ''
  244. })
  245. expect(
  246. reducer(state, actions.loadDisplayShareLink(mockShareLinkParams))
  247. ).toEqual(expectedResult)
  248. })
  249. it('should handle the loadDisplayShareLink action correctly', () => {
  250. const expectedResult = produce(state, (draft) => {
  251. draft.loading.shareToken = true
  252. draft.currentDisplayAuthorizedShareToken = ''
  253. })
  254. expect(
  255. reducer(state, actions.loadDisplayShareLink(mockShareLinkParams))
  256. ).toEqual(expectedResult)
  257. })
  258. it('should handle the displayShareLinkLoaded action correctly', () => {
  259. const expectedResult = produce(state, (draft) => {
  260. draft.currentDisplayShareToken = mockShareToken
  261. draft.loading.shareToken = false
  262. })
  263. expect(
  264. reducer(state, actions.displayShareLinkLoaded(mockShareToken))
  265. ).toEqual(expectedResult)
  266. })
  267. it('should handle the displayAuthorizedShareLinkLoaded action correctly', () => {
  268. const expectedResult = produce(state, (draft) => {
  269. draft.currentDisplayAuthorizedShareToken = mockAuthShareToken
  270. draft.loading.shareToken = false
  271. })
  272. expect(
  273. reducer(
  274. state,
  275. actions.displayAuthorizedShareLinkLoaded(mockAuthShareToken)
  276. )
  277. ).toEqual(expectedResult)
  278. })
  279. it('should handle the loadDisplayShareLinkFail action correctly', () => {
  280. const expectedResult = produce(state, (draft) => {
  281. draft.loading.shareToken = false
  282. })
  283. expect(reducer(state, actions.loadDisplayShareLinkFail())).toEqual(
  284. expectedResult
  285. )
  286. })
  287. it('should handle the openSharePanel action correctly', () => {
  288. const expectedResult = produce(state, (draft) => {
  289. draft.sharePanel = {
  290. id: mockDisplayId,
  291. type: 'display',
  292. title: mockDisplayTitle,
  293. visible: true
  294. }
  295. })
  296. expect(
  297. reducer(state, actions.openSharePanel(mockDisplayId, mockDisplayTitle))
  298. ).toEqual(expectedResult)
  299. })
  300. it('should handle the closeSharePanel action correctly', () => {
  301. const expectedResult = produce(state, (draft) => {
  302. draft.sharePanel = defaultSharePanelState
  303. })
  304. expect(reducer(state, actions.closeSharePanel())).toEqual(expectedResult)
  305. })
  306. it('should handle the resetDisplayState action correctly', () => {
  307. const expectedResult = produce(state, (draft) => {
  308. return displayInitialState
  309. })
  310. expect(reducer(state, actions.resetDisplayState())).toEqual(expectedResult)
  311. })
  312. })