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'; |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 19 | import {mount} from 'enzyme'; |
| 20 | import {Provider} from 'react-redux'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 21 | import deepFreeze from 'deep-freeze'; |
| 22 | import mockRest from 'test-utils/MockRest.js'; |
| 23 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 24 | import {storeCreator} from 'sdc-app/AppStore.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 25 | import {mapStateToProps, mapActionsToProps} from 'sdc-app/onboarding/versionsPage/VersionsPage.js'; |
| 26 | import VersionsPageView from 'sdc-app/onboarding/versionsPage/VersionsPage.jsx'; |
| 27 | import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js'; |
| 28 | import VersionsPageCreationActionHelper from 'sdc-app/onboarding/versionsPage/creation/VersionsPageCreationActionHelper.js'; |
| 29 | import {itemTypes as versionPageItemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js'; |
| 30 | import VersionFactory from 'test-utils/factories/common/VersionFactory.js'; |
| 31 | import {VersionsPageCreationFactory} from 'test-utils/factories/versionsPage/VersionsPageCreationFactories.js'; |
| 32 | import {VersionsPageAdditionalPropsFactory} from 'test-utils/factories/versionsPage/VersionsPageFactories.js'; |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 33 | import {cloneAndSet} from 'test-utils/Util.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | |
| 35 | describe('Versions Page Module Tests', () => { |
| 36 | |
| 37 | const state = { |
| 38 | currentScreen: { |
| 39 | itemPermission: { |
| 40 | isCollaborator: true |
| 41 | }, |
| 42 | props: { |
| 43 | itemId: '23' |
| 44 | } |
| 45 | }, |
| 46 | users: { |
| 47 | userInfo: 'user123' |
| 48 | }, |
| 49 | versionsPage: { |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 50 | permissions: { |
| 51 | contributors: [], |
| 52 | owner: { |
| 53 | userId: 'fgfgfg', |
| 54 | permission: 'Owner', |
| 55 | fullName: 'Test User', |
| 56 | role: 'Test ROLE' |
| 57 | } |
| 58 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 59 | versionsList: {versions: []}, |
| 60 | versionCreation: {} |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | it ('mapStateToProps mapper exists', () => { |
| 65 | |
| 66 | expect(mapStateToProps).toBeTruthy(); |
| 67 | |
| 68 | }); |
| 69 | |
| 70 | it ('should have state in props', () => { |
| 71 | |
| 72 | const props = mapStateToProps(state); |
| 73 | expect(props.currentUser).toEqual('user123'); |
| 74 | |
| 75 | }); |
| 76 | |
| 77 | it('simple jsx test', () => { |
| 78 | |
| 79 | const store = storeCreator(); |
| 80 | const dispatch = store.dispatch; |
| 81 | |
| 82 | const additionalProps = VersionsPageAdditionalPropsFactory.build(); |
| 83 | |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 84 | const props = {...mapStateToProps(state), ...mapActionsToProps(dispatch, additionalProps)}; |
Einav Weiss Keidar | d2f5794 | 2018-02-14 14:00:07 +0200 | [diff] [blame] | 85 | const renderer = new ShallowRenderer(); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 86 | renderer.render(<VersionsPageView {...props} />); |
| 87 | |
| 88 | const renderedOutput = renderer.getRenderOutput(); |
| 89 | expect(renderedOutput).toBeTruthy(); |
| 90 | |
| 91 | }); |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 92 | /** |
| 93 | * turn on when ARCHIVE_ITEM feature toggle will be removed in VersionPage.jsx |
| 94 | */ |
| 95 | // it('archive btn test', () => { |
| 96 | |
| 97 | // const store = storeCreator(); |
| 98 | // const dispatch = store.dispatch; |
| 99 | |
| 100 | // const additionalProps = VersionsPageAdditionalPropsFactory.build(); |
| 101 | // const props = {...mapStateToProps(state), ...mapActionsToProps(dispatch, additionalProps)}; |
| 102 | // const wrapper = mount(<Provider store={store}><VersionsPageView {...props}/></Provider>); |
| 103 | // const depricateBtn = wrapper.find('.depricate-btn'); |
| 104 | // expect(depricateBtn).toBeTruthy(); |
| 105 | // expect(depricateBtn.text()).toBe('ARCHIVE'); |
| 106 | // }); |
| 107 | |
| 108 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 109 | |
| 110 | }); |
| 111 | |
| 112 | describe('Versions Page Actions', () => { |
| 113 | |
| 114 | let store, dispatch, restPrefix = '', version; |
| 115 | |
| 116 | beforeAll(() => { |
| 117 | restPrefix = Configuration.get('restPrefix'); |
| 118 | store = storeCreator(); |
| 119 | dispatch = store.dispatch; |
| 120 | deepFreeze(store.getState()); |
| 121 | version = VersionFactory.build(); |
| 122 | }); |
| 123 | |
| 124 | it('Select and deselect version', () => { |
| 125 | |
| 126 | let selectedVersionOf = (state) => state.versionsPage.versionsList.selectedVersion; |
| 127 | VersionsPageActionHelper.selectVersion(dispatch, {version}); |
| 128 | expect(selectedVersionOf(store.getState())).toEqual(version.id); |
| 129 | VersionsPageActionHelper.selectNone(dispatch); |
| 130 | expect(selectedVersionOf(store.getState())).toEqual(null); |
| 131 | |
| 132 | }); |
| 133 | |
| 134 | it('Create version', () => { |
| 135 | |
| 136 | const {id, baseId} = version; |
| 137 | |
| 138 | mockRest.addHandler('post', ({baseUrl}) => { |
| 139 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions/${baseId}/`); |
| 140 | return version; |
| 141 | }); |
| 142 | |
| 143 | mockRest.addHandler('fetch', ({baseUrl}) => { |
| 144 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`); |
| 145 | return {results: [version]}; |
| 146 | }); |
| 147 | |
| 148 | return VersionsPageCreationActionHelper.createVersion(dispatch, { |
| 149 | itemId: id, |
| 150 | baseVersion: {id: baseId}, |
| 151 | payload: VersionsPageCreationFactory.build() |
| 152 | }) |
| 153 | .then(result => { |
| 154 | expect(result.id).toEqual(id); |
| 155 | }); |
| 156 | |
| 157 | }); |
| 158 | |
| 159 | it('Fetch versions', () => { |
| 160 | |
| 161 | const {id} = version; |
| 162 | |
| 163 | mockRest.addHandler('fetch', ({baseUrl}) => { |
| 164 | expect(baseUrl).toEqual(`${restPrefix}/v1.0/items/${id}/versions`); |
| 165 | return {results: [version]}; |
| 166 | }); |
| 167 | |
| 168 | return VersionsPageActionHelper.fetchVersions(dispatch, { |
| 169 | itemType: versionPageItemTypes.SOFTWARE_PRODUCT, |
| 170 | itemId: id |
| 171 | }) |
| 172 | .then(() => { |
| 173 | const {versionsPage: {versionsList}} = store.getState(); |
| 174 | expect(versionsList.versions[0].id).toEqual(id); |
| 175 | expect(versionsList.itemId).toEqual(id); |
| 176 | expect(versionsList.itemType).toEqual(versionPageItemTypes.SOFTWARE_PRODUCT); |
| 177 | }); |
| 178 | |
| 179 | }); |
| 180 | |
| 181 | }); |