svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 1 | /* |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame^] | 2 | * Copyright © 2016-2018 European Support Limited |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame^] | 7 | * |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame^] | 9 | * |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
svishnev | 1eb66b7 | 2018-01-11 14:39:45 +0200 | [diff] [blame] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | import React from 'react'; |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame^] | 18 | import ShallowRenderer from 'react-test-renderer/shallow'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 19 | import deepFreeze from 'deep-freeze'; |
| 20 | import mockRest from 'test-utils/MockRest.js'; |
| 21 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 22 | import {storeCreator} from 'sdc-app/AppStore.js'; |
| 23 | |
| 24 | import {mapStateToProps, mapActionsToProps} from 'sdc-app/onboarding/versionsPage/VersionsPage.js'; |
| 25 | import VersionsPageView from 'sdc-app/onboarding/versionsPage/VersionsPage.jsx'; |
| 26 | import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js'; |
| 27 | import VersionsPageCreationActionHelper from 'sdc-app/onboarding/versionsPage/creation/VersionsPageCreationActionHelper.js'; |
| 28 | import {itemTypes as versionPageItemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js'; |
| 29 | import VersionFactory from 'test-utils/factories/common/VersionFactory.js'; |
| 30 | import {VersionsPageCreationFactory} from 'test-utils/factories/versionsPage/VersionsPageCreationFactories.js'; |
| 31 | import {VersionsPageAdditionalPropsFactory} from 'test-utils/factories/versionsPage/VersionsPageFactories.js'; |
| 32 | |
| 33 | describe('Versions Page Module Tests', () => { |
| 34 | |
| 35 | const state = { |
| 36 | currentScreen: { |
| 37 | itemPermission: { |
| 38 | isCollaborator: true |
| 39 | }, |
| 40 | props: { |
| 41 | itemId: '23' |
| 42 | } |
| 43 | }, |
| 44 | users: { |
| 45 | userInfo: 'user123' |
| 46 | }, |
| 47 | versionsPage: { |
| 48 | permissions: {}, |
| 49 | versionsList: {versions: []}, |
| 50 | versionCreation: {} |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | it ('mapStateToProps mapper exists', () => { |
| 55 | |
| 56 | expect(mapStateToProps).toBeTruthy(); |
| 57 | |
| 58 | }); |
| 59 | |
| 60 | it ('should have state in props', () => { |
| 61 | |
| 62 | const props = mapStateToProps(state); |
| 63 | expect(props.currentUser).toEqual('user123'); |
| 64 | |
| 65 | }); |
| 66 | |
| 67 | it('simple jsx test', () => { |
| 68 | |
| 69 | const store = storeCreator(); |
| 70 | const dispatch = store.dispatch; |
| 71 | |
| 72 | const additionalProps = VersionsPageAdditionalPropsFactory.build(); |
| 73 | |
| 74 | const props = Object.assign({}, mapStateToProps(state), mapActionsToProps(dispatch, additionalProps)); |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame^] | 75 | const renderer = new ShallowRenderer(); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 76 | renderer.render(<VersionsPageView {...props} />); |
| 77 | |
| 78 | const renderedOutput = renderer.getRenderOutput(); |
| 79 | expect(renderedOutput).toBeTruthy(); |
| 80 | |
| 81 | }); |
| 82 | |
| 83 | }); |
| 84 | |
| 85 | describe('Versions Page Actions', () => { |
| 86 | |
| 87 | let store, dispatch, restPrefix = '', version; |
| 88 | |
| 89 | beforeAll(() => { |
| 90 | restPrefix = Configuration.get('restPrefix'); |
| 91 | store = storeCreator(); |
| 92 | dispatch = store.dispatch; |
| 93 | deepFreeze(store.getState()); |
| 94 | version = VersionFactory.build(); |
| 95 | }); |
| 96 | |
| 97 | it('Select and deselect version', () => { |
| 98 | |
| 99 | let selectedVersionOf = (state) => state.versionsPage.versionsList.selectedVersion; |
| 100 | VersionsPageActionHelper.selectVersion(dispatch, {version}); |
| 101 | expect(selectedVersionOf(store.getState())).toEqual(version.id); |
| 102 | VersionsPageActionHelper.selectNone(dispatch); |
| 103 | expect(selectedVersionOf(store.getState())).toEqual(null); |
| 104 | |
| 105 | }); |
| 106 | |
| 107 | it('Create version', () => { |
| 108 | |
| 109 | const {id, baseId} = version; |
| 110 | |
| 111 | mockRest.addHandler('post', ({baseUrl}) => { |
| 112 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions/${baseId}/`); |
| 113 | return version; |
| 114 | }); |
| 115 | |
| 116 | mockRest.addHandler('fetch', ({baseUrl}) => { |
| 117 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`); |
| 118 | return {results: [version]}; |
| 119 | }); |
| 120 | |
| 121 | return VersionsPageCreationActionHelper.createVersion(dispatch, { |
| 122 | itemId: id, |
| 123 | baseVersion: {id: baseId}, |
| 124 | payload: VersionsPageCreationFactory.build() |
| 125 | }) |
| 126 | .then(result => { |
| 127 | expect(result.id).toEqual(id); |
| 128 | }); |
| 129 | |
| 130 | }); |
| 131 | |
| 132 | it('Fetch versions', () => { |
| 133 | |
| 134 | const {id} = version; |
| 135 | |
| 136 | mockRest.addHandler('fetch', ({baseUrl}) => { |
| 137 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`); |
| 138 | return {results: [version]}; |
| 139 | }); |
| 140 | |
| 141 | return VersionsPageActionHelper.fetchVersions(dispatch, { |
| 142 | itemType: versionPageItemTypes.SOFTWARE_PRODUCT, |
| 143 | itemId: id |
| 144 | }) |
| 145 | .then(() => { |
| 146 | const {versionsPage: {versionsList}} = store.getState(); |
| 147 | expect(versionsList.versions[0].id).toEqual(id); |
| 148 | expect(versionsList.itemId).toEqual(id); |
| 149 | expect(versionsList.itemType).toEqual(versionPageItemTypes.SOFTWARE_PRODUCT); |
| 150 | }); |
| 151 | |
| 152 | }); |
| 153 | |
| 154 | }); |