blob: 15b296019152ee8edf7472396027aeb2629c78d2 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17import React from 'react';
18import {mount} from 'enzyme';
19import {mapStateToProps} from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependencies.js';
20import {
21 SoftwareProductDependenciesResponseFactory,
22 SoftwareProductDependenciesStoreFactory} from 'test-utils/factories/softwareProduct/SoftwareProductDependenciesFactories.js';
23import {VSPComponentsFactory} from 'test-utils/factories/softwareProduct/SoftwareProductComponentsFactories.js';
talig8e9c0652017-12-20 14:30:43 +020024import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
AviZi280f8012017-06-09 02:39:56 +030025import {storeCreator} from 'sdc-app/AppStore.js';
26import {cloneAndSet} from 'test-utils/Util.js';
27import mockRest from 'test-utils/MockRest.js';
28
29import SoftwareProductComponentsActionHelper from 'sdc-app/onboarding/softwareProduct/components/SoftwareProductComponentsActionHelper.js';
talig8e9c0652017-12-20 14:30:43 +020030import {relationTypes, NEW_RULE_TEMP_ID} from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependenciesConstants.js';
AviZi280f8012017-06-09 02:39:56 +030031import SoftwareProductDependenciesActionHelper from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependenciesActionHelper.js';
32import SoftwareProductDependenciesView from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependenciesView.jsx';
33import {VSPEditorFactory} from 'test-utils/factories/softwareProduct/SoftwareProductEditorFactories.js';
34
talig8e9c0652017-12-20 14:30:43 +020035function addNewRowElement(arr, data) {
36 if (data === undefined) {
37 arr.push({id: NEW_RULE_TEMP_ID, targetId: null, sourceId: null, relationType: relationTypes.DEPENDS_ON});
38 } else {
39 arr.push(data);
40 }
41}
42
AviZi280f8012017-06-09 02:39:56 +030043describe('Software Product Dependencies Module Tests', function () {
44 const softwareProductId = '555';
talig8e9c0652017-12-20 14:30:43 +020045 const version = VersionFactory.build();
46
AviZi280f8012017-06-09 02:39:56 +030047
48 it('mapStateToProps mapper exists', () => {
49 expect(mapStateToProps).toBeTruthy();
50 });
talig8e9c0652017-12-20 14:30:43 +020051
AviZi280f8012017-06-09 02:39:56 +030052 it('Get Software Product Dependencies List', () => {
53 const store = storeCreator();
54 const dispatch = store.dispatch;
55
56 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(2);
57 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
talig8e9c0652017-12-20 14:30:43 +020058 addNewRowElement(DependenciesListStore);
AviZi280f8012017-06-09 02:39:56 +030059 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
60
61 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
talig8e9c0652017-12-20 14:30:43 +020062 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
AviZi280f8012017-06-09 02:39:56 +030063 expect(data).toEqual(undefined);
64 expect(options).toEqual(undefined);
65 return {results: DependenciesListResponse};
66 });
67
68 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
69 const state = store.getState();
70 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
71 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
72 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
AviZi280f8012017-06-09 02:39:56 +030073 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
AviZi280f8012017-06-09 02:39:56 +030074 expect(state).toEqual(newExpectedStore);
75 });
76 });
77
talig8e9c0652017-12-20 14:30:43 +020078 /*
79 Test update:
80 - fetch initial item
81 - update existing item
82 - auto fetch again
83 */
84 it('Update Software Product Dependency', () => {
AviZi280f8012017-06-09 02:39:56 +030085 const store = storeCreator();
86 const dispatch = store.dispatch;
87
talig8e9c0652017-12-20 14:30:43 +020088 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(1);
AviZi280f8012017-06-09 02:39:56 +030089 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
talig8e9c0652017-12-20 14:30:43 +020090 addNewRowElement(DependenciesListStore);
AviZi280f8012017-06-09 02:39:56 +030091 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
92
93 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
talig8e9c0652017-12-20 14:30:43 +020094 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
AviZi280f8012017-06-09 02:39:56 +030095 expect(data).toEqual(undefined);
96 expect(options).toEqual(undefined);
97 return {results: DependenciesListResponse};
98 });
AviZi280f8012017-06-09 02:39:56 +030099 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
talig8e9c0652017-12-20 14:30:43 +0200100
AviZi280f8012017-06-09 02:39:56 +0300101 const state = store.getState();
102 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
103 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
104 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
talig8e9c0652017-12-20 14:30:43 +0200105 let item = expectedStoreDependencies.find((dep) => dep.id !== NEW_RULE_TEMP_ID);
106 item.targetId = 'testChangeTarget';
107 item.sourceId = 'testChangesource';
108 // removing 'new row' from response
109 expectedStoreDependencies = expectedStoreDependencies.slice(0, expectedStoreDependencies.length - 1);
110 let expDependenciesListStore = expectedStoreDependencies.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
AviZi280f8012017-06-09 02:39:56 +0300111
talig8e9c0652017-12-20 14:30:43 +0200112 mockRest.addHandler('put', ({data, options, baseUrl}) => {
113 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies/${item.id}`);
114 expect(data.targetId).toEqual('testChangeTarget');
115 expect(data.sourceId).toEqual('testChangesource');
116 expect(options).toEqual(undefined);
117 return {results: null};
118 });
119 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
120 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
121 expect(data).toEqual(undefined);
122 expect(options).toEqual(undefined);
123 return {results: expDependenciesListStore};
124 });
125
126 addNewRowElement(expectedStoreDependencies);
AviZi280f8012017-06-09 02:39:56 +0300127 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
talig8e9c0652017-12-20 14:30:43 +0200128 return SoftwareProductDependenciesActionHelper.updateDependency(dispatch, {softwareProductId, version, item}).then(() => {
129 const newState = store.getState();
130 expect(newState).toEqual(newExpectedStore);
AviZi280f8012017-06-09 02:39:56 +0300131
talig8e9c0652017-12-20 14:30:43 +0200132 });
AviZi280f8012017-06-09 02:39:56 +0300133 });
134 });
135
talig8e9c0652017-12-20 14:30:43 +0200136 /*
137 - Fetch item list
138 - Delete item from list
139 - Fetch again
140 */
141 it('Delete Software Product Dependency', () => {
142 const store = storeCreator();
143 const dispatch = store.dispatch;
144 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(1);
145 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
146 addNewRowElement(DependenciesListStore);
147 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
148
149 let deleteItem = DependenciesListStore.find((dep) => dep.id !== NEW_RULE_TEMP_ID);
150
151 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
152 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
153 expect(data).toEqual(undefined);
154 expect(options).toEqual(undefined);
155 return {results: DependenciesListResponse};
156 });
157 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
158 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies/${deleteItem.id}`);
159 expect(data).toEqual(undefined);
160 expect(options).toEqual(undefined);
161 return {results: null};
162 });
163 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
164 const state = store.getState();
165 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
166 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
167 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}))
168
169 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
170 expect(state).toEqual(newExpectedStore);
171
172 expectedStoreDependencies = expectedStoreDependencies.filter((dep) => dep.id !== deleteItem.id);
173 const postDeleteExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
174
175 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
176 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
177 expect(data).toEqual(undefined);
178 expect(options).toEqual(undefined);
179 return {results: []};
180 });
181
182 return SoftwareProductDependenciesActionHelper.removeDependency(dispatch, {softwareProductId, version, item: deleteItem}).then(() => {
183 const state = store.getState();
184 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
185 const currentDependencies = postDeleteExpectedStore.softwareProduct.softwareProductDependencies;
186 expect(depndenciesWithGeneratedId).toEqual(currentDependencies);
187 });
188 });
189 });
190
191 /*
192 - Create initial list
193 - Update the new row and make sure there is no API call
194 - Submit the new row
195 - Getch data with reset new row and new entity with info from the new item
196 */
197
198 it('Create Software Product Dependency', () => {
AviZi280f8012017-06-09 02:39:56 +0300199 const store = storeCreator();
200 const dispatch = store.dispatch;
201
talig8e9c0652017-12-20 14:30:43 +0200202 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(1);
203 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
204 addNewRowElement(DependenciesListStore);
205 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
AviZi280f8012017-06-09 02:39:56 +0300206
207 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
talig8e9c0652017-12-20 14:30:43 +0200208 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
AviZi280f8012017-06-09 02:39:56 +0300209 expect(data).toEqual(undefined);
210 expect(options).toEqual(undefined);
talig8e9c0652017-12-20 14:30:43 +0200211 return {results: DependenciesListResponse};
AviZi280f8012017-06-09 02:39:56 +0300212 });
talig8e9c0652017-12-20 14:30:43 +0200213 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
214 // setting properties on the 'new role' should not call an API
215 let addItem = {id: NEW_RULE_TEMP_ID, sourceId: 'sId', targetId : 'tId',relationType: relationTypes.DEPENDS_ON};
216 try {
217 SoftwareProductDependenciesActionHelper.updateDependency(dispatch, {softwareProductId, version, item: addItem}).then(()=> {
218 //go to error that fetch was not defined
219 });
220 } catch (error) {
221 if(error.name === 'TypeError') {
222 // Expected error because we expected there is no promise
223 } else {
224 fail('Error:' + error);
225 }
226 }
AviZi280f8012017-06-09 02:39:56 +0300227
AviZi280f8012017-06-09 02:39:56 +0300228 const state = store.getState();
229 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
230 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
231 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
talig8e9c0652017-12-20 14:30:43 +0200232 // creating the new item
233 let item = SoftwareProductDependenciesResponseFactory.buildList(1,
234 {sourceId: 'sId', targetId : 'tId',relationType: relationTypes.DEPENDS_ON})[0];
235 addNewRowElement(expectedStoreDependencies, item);
236 expectedStoreDependencies = expectedStoreDependencies.filter((dep) => dep.id !== NEW_RULE_TEMP_ID);
AviZi280f8012017-06-09 02:39:56 +0300237
talig8e9c0652017-12-20 14:30:43 +0200238 mockRest.addHandler('post', ({data, options, baseUrl}) => {
239 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
240 expect(data.targetId).toEqual('tId');
241 expect(data.sourceId).toEqual('sId');
242 expect(data.id).toEqual(undefined);
243 expect(options).toEqual(undefined);
244 return {results: item.id};
245 });
246 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
247 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
248 expect(data).toEqual(undefined);
249 expect(options).toEqual(undefined);
250 return {results: expectedStoreDependencies};
251 });
AviZi280f8012017-06-09 02:39:56 +0300252
talig8e9c0652017-12-20 14:30:43 +0200253 let PostCreateItemListStore = expectedStoreDependencies.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
254 addNewRowElement(PostCreateItemListStore);
255 const newExpectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', PostCreateItemListStore);
256
257 return SoftwareProductDependenciesActionHelper.createDependency(dispatch, {softwareProductId, version, item: addItem}).then(() => {
258 const newState = store.getState();
259 expect(newState.softwareProduct.softwareProductDependencies.length).toEqual(3);
260 expect(newState).toEqual(newExpectedStore);
261 });
262
AviZi280f8012017-06-09 02:39:56 +0300263 });
talig8e9c0652017-12-20 14:30:43 +0200264
AviZi280f8012017-06-09 02:39:56 +0300265 });
266
talig8e9c0652017-12-20 14:30:43 +0200267
AviZi280f8012017-06-09 02:39:56 +0300268 it('Get Software Product Dependencies List with loop, and render to JSX', () => {
269 const store = storeCreator();
270 const dispatch = store.dispatch;
271
272 let components = VSPComponentsFactory.buildList(2);
273 let vspEditor = VSPEditorFactory.build({id: softwareProductId, version});
274
275 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(2);
276 let firstDependecy = DependenciesListResponse[0];
277 let secondDependency = DependenciesListResponse[1];
278 firstDependecy.sourceId = components[0].id;
279 secondDependency.sourceId = components[1].id;
280 firstDependecy.targetId = secondDependency.sourceId;
281 secondDependency.targetId = firstDependecy.sourceId;
282
283 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build({...dependency, hasCycle: true}));
talig8e9c0652017-12-20 14:30:43 +0200284 addNewRowElement(DependenciesListStore);
AviZi280f8012017-06-09 02:39:56 +0300285 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
286
287 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
talig8e9c0652017-12-20 14:30:43 +0200288 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependencies`);
AviZi280f8012017-06-09 02:39:56 +0300289 expect(data).toEqual(undefined);
290 expect(options).toEqual(undefined);
291 return {results: DependenciesListResponse};
292 });
293
294 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
295 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/components`);
296 expect(data).toEqual(undefined);
297 expect(options).toEqual(undefined);
298 return {results: components};
299 });
300
301 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
302 return SoftwareProductComponentsActionHelper.fetchSoftwareProductComponents(dispatch, {softwareProductId, version});
303 }).then(() => {
ilanap1965d162018-01-04 11:34:59 +0200304
AviZi280f8012017-06-09 02:39:56 +0300305 const state = store.getState();
306 state.softwareProduct.softwareProductEditor = {data: vspEditor};
307 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
308 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
309 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
310
311 const newExpectedStore = {
talig8e9c0652017-12-20 14:30:43 +0200312 ...expectedStore,
AviZi280f8012017-06-09 02:39:56 +0300313 softwareProduct: {
talig8e9c0652017-12-20 14:30:43 +0200314 ...expectedStore.softwareProduct,
AviZi280f8012017-06-09 02:39:56 +0300315 softwareProductDependencies: expectedStoreDependencies,
316 softwareProductEditor: {data: vspEditor},
317 softwareProductComponents: {
318 ...expectedStore.softwareProduct.softwareProductComponents,
319 componentsList: components
320 }
321 }
322 };
323
324 expect(state).toEqual(newExpectedStore);
325
326 const props = mapStateToProps(state);
327 expect(props.softwareProductDependencies).toEqual(expectedStoreDependencies);
ilanap1965d162018-01-04 11:34:59 +0200328/*
329Fails on some weird react error about 2 react's loaded - may be some dependency
AviZi280f8012017-06-09 02:39:56 +0300330 const wrapper = mount(<SoftwareProductDependenciesView {...props}/>);
331 expect(wrapper).toBeTruthy();
ilanap1965d162018-01-04 11:34:59 +0200332*/
AviZi280f8012017-06-09 02:39:56 +0300333 });
334 });
talig8e9c0652017-12-20 14:30:43 +0200335
336});