blob: 5325a58959327fd36dc358042d398c721d2be6c9 [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';
24import VersionControllerUtilsFactory from 'test-utils/factories/softwareProduct/VersionControllerUtilsFactory.js';
25import {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';
30import SoftwareProductDependenciesActionHelper from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependenciesActionHelper.js';
31import SoftwareProductDependenciesView from 'sdc-app/onboarding/softwareProduct/dependencies/SoftwareProductDependenciesView.jsx';
32import {VSPEditorFactory} from 'test-utils/factories/softwareProduct/SoftwareProductEditorFactories.js';
33
34describe('Software Product Dependencies Module Tests', function () {
35 const softwareProductId = '555';
36 const version = VersionControllerUtilsFactory.build().version;
37
38 it('mapStateToProps mapper exists', () => {
39 expect(mapStateToProps).toBeTruthy();
40 });
41
42 it('Get Software Product Dependencies List', () => {
43 const store = storeCreator();
44 const dispatch = store.dispatch;
45
46 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(2);
47 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
48 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
49
50 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
51 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependency-model`);
52 expect(data).toEqual(undefined);
53 expect(options).toEqual(undefined);
54 return {results: DependenciesListResponse};
55 });
56
57 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
58 const state = store.getState();
59 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
60 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
61 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
62
63 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
64
65 expect(state).toEqual(newExpectedStore);
66 });
67 });
68
69 it('Update Software Product Dependencies List', () => {
70 const store = storeCreator();
71 const dispatch = store.dispatch;
72
73 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(3);
74 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build(dependency));
75 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
76
77 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
78 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependency-model`);
79 expect(data).toEqual(undefined);
80 expect(options).toEqual(undefined);
81 return {results: DependenciesListResponse};
82 });
83
84 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
85
86 const state = store.getState();
87 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
88 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
89 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
90
91 let newDependency = SoftwareProductDependenciesStoreFactory.build();
92 expectedStoreDependencies.push(newDependency);
93
94 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
95
96 SoftwareProductDependenciesActionHelper.updateDependencyList(dispatch, {dependenciesList: expectedStoreDependencies});
97 const newState = store.getState();
98 expect(newState).toEqual(newExpectedStore);
99 });
100 });
101
102 it('Add And Save Software Product Dependencies List', () => {
103 const store = storeCreator();
104 const dispatch = store.dispatch;
105
106 let mockServerDependencies = [];
107
108 SoftwareProductDependenciesActionHelper.addDependency(dispatch);
109 let state = store.getState();
110 let dependencies = state.softwareProduct.softwareProductDependencies;
111 expect(dependencies.length).toEqual(1);
112 expect(dependencies[0].sourceId).toEqual(null);
113 expect(dependencies[0].targetId).toEqual(null);
114
115 let newDependencies = SoftwareProductDependenciesStoreFactory.buildList(1);
116 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', newDependencies);
117 SoftwareProductDependenciesActionHelper.updateDependencyList(dispatch, {dependenciesList: newDependencies});
118
119 mockRest.addHandler('post', ({data, options, baseUrl}) => {
120 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependency-model`);
121 expect(data).toEqual({componentDependencyModels: newDependencies.map(item => ({sourceId: item.sourceId, targetId: item.targetId, relationType: item.relationType}) )});
122 expect(options).toEqual(undefined);
123 mockServerDependencies = [...data.componentDependencyModels];
124 return {returnCode: 'OK'};
125 });
126
127 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
128 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependency-model`);
129 expect(data).toEqual(undefined);
130 expect(options).toEqual(undefined);
131 return {results: mockServerDependencies};
132 });
133
134 return SoftwareProductDependenciesActionHelper.saveDependencies(dispatch, {softwareProductId, version, dependenciesList: newDependencies}).then(() => {
135 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version});
136 }).then(() => {
137 const state = store.getState();
138 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
139 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
140 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
141
142 const newExpectedStore = cloneAndSet(expectedStore, 'softwareProduct.softwareProductDependencies', expectedStoreDependencies);
143
144 expect(state).toEqual(newExpectedStore);
145 });
146 });
147
148 it('Get Software Product Dependencies List with loop, and render to JSX', () => {
149 const store = storeCreator();
150 const dispatch = store.dispatch;
151
152 let components = VSPComponentsFactory.buildList(2);
153 let vspEditor = VSPEditorFactory.build({id: softwareProductId, version});
154
155 let DependenciesListResponse = SoftwareProductDependenciesResponseFactory.buildList(2);
156 let firstDependecy = DependenciesListResponse[0];
157 let secondDependency = DependenciesListResponse[1];
158 firstDependecy.sourceId = components[0].id;
159 secondDependency.sourceId = components[1].id;
160 firstDependecy.targetId = secondDependency.sourceId;
161 secondDependency.targetId = firstDependecy.sourceId;
162
163 let DependenciesListStore = DependenciesListResponse.map(dependency => SoftwareProductDependenciesStoreFactory.build({...dependency, hasCycle: true}));
164 const expectedStore = cloneAndSet(store.getState(), 'softwareProduct.softwareProductDependencies', DependenciesListStore);
165
166 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
167 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/component-dependency-model`);
168 expect(data).toEqual(undefined);
169 expect(options).toEqual(undefined);
170 return {results: DependenciesListResponse};
171 });
172
173 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
174 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-software-products/${softwareProductId}/versions/${version.id}/components`);
175 expect(data).toEqual(undefined);
176 expect(options).toEqual(undefined);
177 return {results: components};
178 });
179
180 return SoftwareProductDependenciesActionHelper.fetchDependencies(dispatch, {softwareProductId, version}).then(() => {
181 return SoftwareProductComponentsActionHelper.fetchSoftwareProductComponents(dispatch, {softwareProductId, version});
182 }).then(() => {
183 const state = store.getState();
184 state.softwareProduct.softwareProductEditor = {data: vspEditor};
185 const depndenciesWithGeneratedId = state.softwareProduct.softwareProductDependencies;
186 const currentDependencies = expectedStore.softwareProduct.softwareProductDependencies;
187 let expectedStoreDependencies = currentDependencies.map((dependency, index) => ({...dependency, id: depndenciesWithGeneratedId[index].id}));
188
189 const newExpectedStore = {
190 ...expectedStore,
191 softwareProduct: {
192 ...expectedStore.softwareProduct,
193 softwareProductDependencies: expectedStoreDependencies,
194 softwareProductEditor: {data: vspEditor},
195 softwareProductComponents: {
196 ...expectedStore.softwareProduct.softwareProductComponents,
197 componentsList: components
198 }
199 }
200 };
201
202 expect(state).toEqual(newExpectedStore);
203
204 const props = mapStateToProps(state);
205 expect(props.softwareProductDependencies).toEqual(expectedStoreDependencies);
206 const wrapper = mount(<SoftwareProductDependenciesView {...props}/>);
207 expect(wrapper).toBeTruthy();
208 });
209 });
210});