blob: fd7659e70e4e0f756437fa6f0de535161018e7da [file] [log] [blame]
svishnev1eb66b72018-01-11 14:39:45 +02001/*
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02002 * Copyright © 2016-2018 European Support Limited
talig8e9c0652017-12-20 14:30:43 +02003 *
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 Keidard2f57942018-02-14 14:00:07 +02007 *
svishnev1eb66b72018-01-11 14:39:45 +02008 * http://www.apache.org/licenses/LICENSE-2.0
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02009 *
talig8e9c0652017-12-20 14:30:43 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnev1eb66b72018-01-11 14:39:45 +020012 * 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.
talig8e9c0652017-12-20 14:30:43 +020015 */
16
17import React from 'react';
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020018import ShallowRenderer from 'react-test-renderer/shallow';
svishnev091edfd2018-03-19 12:15:19 +020019import {mount} from 'enzyme';
20import {Provider} from 'react-redux';
talig8e9c0652017-12-20 14:30:43 +020021import deepFreeze from 'deep-freeze';
22import mockRest from 'test-utils/MockRest.js';
23import Configuration from 'sdc-app/config/Configuration.js';
24import {storeCreator} from 'sdc-app/AppStore.js';
talig8e9c0652017-12-20 14:30:43 +020025import {mapStateToProps, mapActionsToProps} from 'sdc-app/onboarding/versionsPage/VersionsPage.js';
26import VersionsPageView from 'sdc-app/onboarding/versionsPage/VersionsPage.jsx';
27import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
28import VersionsPageCreationActionHelper from 'sdc-app/onboarding/versionsPage/creation/VersionsPageCreationActionHelper.js';
29import {itemTypes as versionPageItemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
30import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
31import {VersionsPageCreationFactory} from 'test-utils/factories/versionsPage/VersionsPageCreationFactories.js';
32import {VersionsPageAdditionalPropsFactory} from 'test-utils/factories/versionsPage/VersionsPageFactories.js';
svishnev091edfd2018-03-19 12:15:19 +020033import {cloneAndSet} from 'test-utils/Util.js';
talig8e9c0652017-12-20 14:30:43 +020034
35describe('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: {
svishnev091edfd2018-03-19 12:15:19 +020050 permissions: {
51 contributors: [],
52 owner: {
53 userId: 'fgfgfg',
54 permission: 'Owner',
55 fullName: 'Test User',
56 role: 'Test ROLE'
57 }
58 },
talig8e9c0652017-12-20 14:30:43 +020059 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
svishnev091edfd2018-03-19 12:15:19 +020084 const props = {...mapStateToProps(state), ...mapActionsToProps(dispatch, additionalProps)};
Einav Weiss Keidard2f57942018-02-14 14:00:07 +020085 const renderer = new ShallowRenderer();
talig8e9c0652017-12-20 14:30:43 +020086 renderer.render(<VersionsPageView {...props} />);
87
88 const renderedOutput = renderer.getRenderOutput();
89 expect(renderedOutput).toBeTruthy();
90
91 });
svishnev091edfd2018-03-19 12:15:19 +020092 /**
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
talig8e9c0652017-12-20 14:30:43 +0200109
110});
111
112describe('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});