blob: 17cad4fc392f909dab71bd1ae42d8d93a513cf93 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Einav Weiss Keidard2f57942018-02-14 14:00:07 +02002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import deepFreeze from 'deep-freeze';
AviZi280f8012017-06-09 02:39:56 +030017import pickBy from 'lodash/pickBy';
Michael Landoefa037d2017-02-19 12:57:33 +020018import mockRest from 'test-utils/MockRest.js';
svishnev57c5c4a2018-04-22 14:14:31 +030019import { cloneAndSet, buildListFromFactory } from 'test-utils/Util.js';
20import { storeCreator } from 'sdc-app/AppStore.js';
Michael Landoefa037d2017-02-19 12:57:33 +020021import LicenseAgreementActionHelper from 'sdc-app/onboarding/licenseModel/licenseAgreement/LicenseAgreementActionHelper.js';
22
svishnev57c5c4a2018-04-22 14:14:31 +030023import {
24 LicenseAgreementStoreFactory,
25 LicenseAgreementDispatchFactory,
26 LicenseAgreementPostFactory,
27 LicenseAgreementPutFactory
28} from 'test-utils/factories/licenseModel/LicenseAgreementFactories.js';
talig8e9c0652017-12-20 14:30:43 +020029import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
svishnev57c5c4a2018-04-22 14:14:31 +030030import { SyncStates } from 'sdc-app/common/merge/MergeEditorConstants.js';
talig8e9c0652017-12-20 14:30:43 +020031import CurrentScreenFactory from 'test-utils/factories/common/CurrentScreenFactory.js';
Michael Landoefa037d2017-02-19 12:57:33 +020032
33describe('License Agreement Module Tests', () => {
svishnev57c5c4a2018-04-22 14:14:31 +030034 const LICENSE_MODEL_ID = '777';
35 const version = VersionFactory.build();
36 const itemPermissionAndProps = CurrentScreenFactory.build({}, { version });
37 const returnedVersionFields = {
38 baseId: version.baseId,
39 description: version.description,
40 id: version.id,
41 name: version.name,
42 status: version.status
43 };
Michael Landoefa037d2017-02-19 12:57:33 +020044
svishnev57c5c4a2018-04-22 14:14:31 +030045 it('Load License Agreement List', () => {
46 const licenseAgreementList = buildListFromFactory(
47 LicenseAgreementStoreFactory
48 );
Michael Landoefa037d2017-02-19 12:57:33 +020049
svishnev57c5c4a2018-04-22 14:14:31 +030050 const store = storeCreator();
51 deepFreeze(store.getState());
AviZi280f8012017-06-09 02:39:56 +030052
svishnev57c5c4a2018-04-22 14:14:31 +030053 const expectedStore = cloneAndSet(
54 store.getState(),
55 'licenseModel.licenseAgreement.licenseAgreementList',
56 licenseAgreementList
57 );
Michael Landoefa037d2017-02-19 12:57:33 +020058
svishnev57c5c4a2018-04-22 14:14:31 +030059 mockRest.addHandler('fetch', ({ options, data, baseUrl }) => {
60 expect(baseUrl).toEqual(
61 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
62 version.id
63 }/license-agreements`
64 );
65 expect(data).toEqual(undefined);
66 expect(options).toEqual(undefined);
67 return { results: licenseAgreementList };
68 });
69 return LicenseAgreementActionHelper.fetchLicenseAgreementList(
70 store.dispatch,
71 { licenseModelId: LICENSE_MODEL_ID, version }
72 ).then(() => {
73 expect(store.getState()).toEqual(expectedStore);
74 });
75 });
Michael Landoefa037d2017-02-19 12:57:33 +020076
svishnev57c5c4a2018-04-22 14:14:31 +030077 it('Delete License Agreement', () => {
78 const licenseAgreementList = buildListFromFactory(
79 LicenseAgreementStoreFactory,
80 1
81 );
82 const store = storeCreator({
83 currentScreen: { ...itemPermissionAndProps },
84 licenseModel: {
85 licenseAgreement: {
86 licenseAgreementList
87 }
88 }
89 });
90 deepFreeze(store.getState());
91 const expectedCurrentScreenProps = {
92 ...itemPermissionAndProps,
93 itemPermission: {
94 ...itemPermissionAndProps.itemPermission,
95 isDirty: true
96 }
97 };
98 const toBeDeletedLicenseAgreementId = licenseAgreementList[0].id;
99 let expectedStore = cloneAndSet(
100 store.getState(),
101 'licenseModel.licenseAgreement.licenseAgreementList',
102 []
103 );
104 expectedStore = cloneAndSet(
105 expectedStore,
106 'currentScreen.itemPermission',
107 expectedCurrentScreenProps.itemPermission
108 );
Michael Landoefa037d2017-02-19 12:57:33 +0200109
svishnev57c5c4a2018-04-22 14:14:31 +0300110 mockRest.addHandler('destroy', ({ data, options, baseUrl }) => {
111 expect(baseUrl).toEqual(
112 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
113 version.id
114 }/license-agreements/${toBeDeletedLicenseAgreementId}`
115 );
116 expect(data).toEqual(undefined);
117 expect(options).toEqual(undefined);
118 });
119 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
120 expect(baseUrl).toEqual(
121 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${
122 version.id
123 }`
124 );
125 expect(data).toEqual(undefined);
126 expect(options).toEqual(undefined);
127 return {
128 ...returnedVersionFields,
129 state: {
130 synchronizationState: SyncStates.UP_TO_DATE,
131 dirty: true
132 }
133 };
134 });
Michael Landoefa037d2017-02-19 12:57:33 +0200135
svishnev57c5c4a2018-04-22 14:14:31 +0300136 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
137 expect(baseUrl).toEqual(
138 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}`
139 );
140 expect(data).toEqual(undefined);
141 expect(options).toEqual(undefined);
142 return {
143 ...returnedVersionFields
144 };
145 });
Michael Landoefa037d2017-02-19 12:57:33 +0200146
svishnev57c5c4a2018-04-22 14:14:31 +0300147 return LicenseAgreementActionHelper.deleteLicenseAgreement(
148 store.dispatch,
149 {
150 licenseAgreementId: toBeDeletedLicenseAgreementId,
151 licenseModelId: LICENSE_MODEL_ID,
152 version
153 }
154 ).then(() => {
155 expect(store.getState()).toEqual(expectedStore);
156 });
157 });
Michael Landoefa037d2017-02-19 12:57:33 +0200158
svishnev57c5c4a2018-04-22 14:14:31 +0300159 it('Add License Agreement', () => {
160 const store = storeCreator({
161 currentScreen: { ...itemPermissionAndProps }
162 });
163 deepFreeze(store.getState());
Michael Landoefa037d2017-02-19 12:57:33 +0200164
svishnev57c5c4a2018-04-22 14:14:31 +0300165 const licenseAgreementToAdd = LicenseAgreementDispatchFactory.build();
AviZi280f8012017-06-09 02:39:56 +0300166
svishnev57c5c4a2018-04-22 14:14:31 +0300167 const LicenseAgreementPostRequest = LicenseAgreementPostFactory.build(
168 pickBy(
169 licenseAgreementToAdd,
170 (val, key) => key !== 'featureGroupsIds'
171 )
172 );
AviZi280f8012017-06-09 02:39:56 +0300173
svishnev57c5c4a2018-04-22 14:14:31 +0300174 deepFreeze(LicenseAgreementPostRequest);
Michael Landoefa037d2017-02-19 12:57:33 +0200175
svishnev57c5c4a2018-04-22 14:14:31 +0300176 const licenseAgreementIdFromResponse = 'ADDED_ID';
177 const licenseAgreementAfterAdd = LicenseAgreementStoreFactory.build({
178 ...licenseAgreementToAdd,
179 id: licenseAgreementIdFromResponse
180 });
181 deepFreeze(licenseAgreementAfterAdd);
182 const licenseAgreementList = [licenseAgreementAfterAdd];
183 const expectedCurrentScreenProps = {
184 ...itemPermissionAndProps,
185 itemPermission: {
186 ...itemPermissionAndProps.itemPermission,
187 isDirty: true
188 }
189 };
190 const featureGroupsList = licenseAgreementList[0].featureGroupsIds;
191 let expectedStore = cloneAndSet(
192 store.getState(),
193 'licenseModel.licenseAgreement.licenseAgreementList',
194 [licenseAgreementAfterAdd]
195 );
196 expectedStore = cloneAndSet(
197 expectedStore,
198 'currentScreen.itemPermission',
199 expectedCurrentScreenProps.itemPermission
200 );
Michael Landoefa037d2017-02-19 12:57:33 +0200201
svishnev57c5c4a2018-04-22 14:14:31 +0300202 mockRest.addHandler('post', ({ options, data, baseUrl }) => {
203 expect(baseUrl).toEqual(
204 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
205 version.id
206 }/license-agreements`
207 );
208 expect(data).toEqual(LicenseAgreementPostRequest);
209 expect(options).toEqual(undefined);
210 return {
211 value: licenseAgreementIdFromResponse
212 };
213 });
214 mockRest.addHandler('fetch', ({ options, data, baseUrl }) => {
215 expect(baseUrl).toEqual(
216 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
217 version.id
218 }/license-agreements`
219 );
220 expect(data).toEqual(undefined);
221 expect(options).toEqual(undefined);
222 return { results: licenseAgreementList };
223 });
224 mockRest.addHandler('fetch', ({ options, data, baseUrl }) => {
225 expect(baseUrl).toEqual(
226 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
227 version.id
228 }/feature-groups`
229 );
230 expect(data).toEqual(undefined);
231 expect(options).toEqual(undefined);
232 return { results: featureGroupsList };
233 });
234 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
235 expect(baseUrl).toEqual(
236 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${
237 version.id
238 }`
239 );
240 expect(data).toEqual(undefined);
241 expect(options).toEqual(undefined);
242 return {
243 ...returnedVersionFields,
244 state: {
245 synchronizationState: SyncStates.UP_TO_DATE,
246 dirty: true
247 }
248 };
249 });
talig8e9c0652017-12-20 14:30:43 +0200250
svishnev57c5c4a2018-04-22 14:14:31 +0300251 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
252 expect(baseUrl).toEqual(
253 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}`
254 );
255 expect(data).toEqual(undefined);
256 expect(options).toEqual(undefined);
257 return {
258 ...returnedVersionFields
259 };
260 });
Michael Landoefa037d2017-02-19 12:57:33 +0200261
svishnev57c5c4a2018-04-22 14:14:31 +0300262 return LicenseAgreementActionHelper.saveLicenseAgreement(
263 store.dispatch,
264 {
265 licenseAgreement: licenseAgreementToAdd,
266 licenseModelId: LICENSE_MODEL_ID,
267 version
268 }
269 ).then(() => {
270 expect(store.getState()).toEqual(expectedStore);
271 });
272 });
Michael Landoefa037d2017-02-19 12:57:33 +0200273
svishnev57c5c4a2018-04-22 14:14:31 +0300274 it('Update License Agreement', () => {
275 const licenseAgreementList = buildListFromFactory(
276 LicenseAgreementStoreFactory,
277 1,
278 { featureGroupsIds: ['77'] }
279 );
280 const store = storeCreator({
281 currentScreen: { ...itemPermissionAndProps },
282 licenseModel: {
283 licenseAgreement: {
284 licenseAgreementList
285 }
286 }
287 });
288 deepFreeze(store.getState());
Michael Landoefa037d2017-02-19 12:57:33 +0200289
svishnev57c5c4a2018-04-22 14:14:31 +0300290 const previousLicenseAgreementData = licenseAgreementList[0];
291 const toBeUpdatedLicenseAgreementId = previousLicenseAgreementData.id;
292 const oldFeatureGroupIds =
293 previousLicenseAgreementData.featureGroupsIds;
AviZi280f8012017-06-09 02:39:56 +0300294
svishnev57c5c4a2018-04-22 14:14:31 +0300295 const newFeatureGroupsIds = ['update_id_1', 'update_id_2'];
Michael Landoefa037d2017-02-19 12:57:33 +0200296
svishnev57c5c4a2018-04-22 14:14:31 +0300297 const licenseAgreementUpdateData = LicenseAgreementStoreFactory.build({
298 id: toBeUpdatedLicenseAgreementId,
299 featureGroupsIds: newFeatureGroupsIds
300 });
301 deepFreeze(licenseAgreementUpdateData);
AviZi280f8012017-06-09 02:39:56 +0300302
svishnev57c5c4a2018-04-22 14:14:31 +0300303 const LicenseAgreementPutFactoryRequest = LicenseAgreementPutFactory.build(
304 {
305 addedFeatureGroupsIds: newFeatureGroupsIds,
306 removedFeatureGroupsIds: oldFeatureGroupIds
307 }
308 );
Michael Landoefa037d2017-02-19 12:57:33 +0200309
svishnev57c5c4a2018-04-22 14:14:31 +0300310 deepFreeze(LicenseAgreementPutFactoryRequest);
Michael Landoefa037d2017-02-19 12:57:33 +0200311
svishnev57c5c4a2018-04-22 14:14:31 +0300312 const expectedCurrentScreenProps = {
313 ...itemPermissionAndProps,
314 itemPermission: {
315 ...itemPermissionAndProps.itemPermission,
316 isDirty: true
317 }
318 };
319 let expectedStore = cloneAndSet(
320 store.getState(),
321 'licenseModel.licenseAgreement.licenseAgreementList',
322 [licenseAgreementUpdateData]
323 );
324 expectedStore = cloneAndSet(
325 expectedStore,
326 'currentScreen.itemPermission',
327 expectedCurrentScreenProps.itemPermission
328 );
talig8e9c0652017-12-20 14:30:43 +0200329
svishnev57c5c4a2018-04-22 14:14:31 +0300330 mockRest.addHandler('put', ({ data, options, baseUrl }) => {
331 expect(baseUrl).toEqual(
332 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
333 version.id
334 }/license-agreements/${toBeUpdatedLicenseAgreementId}`
335 );
336 expect(data).toEqual(LicenseAgreementPutFactoryRequest);
337 expect(options).toEqual(undefined);
338 });
339 mockRest.addHandler('fetch', ({ options, data, baseUrl }) => {
340 expect(baseUrl).toEqual(
341 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
342 version.id
343 }/license-agreements`
344 );
345 expect(data).toEqual(undefined);
346 expect(options).toEqual(undefined);
347 return { results: [licenseAgreementUpdateData] };
348 });
Michael Landoefa037d2017-02-19 12:57:33 +0200349
svishnev57c5c4a2018-04-22 14:14:31 +0300350 mockRest.addHandler('fetch', ({ options, data, baseUrl }) => {
351 expect(baseUrl).toEqual(
352 `/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${
353 version.id
354 }/feature-groups`
355 );
356 expect(data).toEqual(undefined);
357 expect(options).toEqual(undefined);
358 return { results: [] };
359 });
360
361 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
362 expect(baseUrl).toEqual(
363 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${
364 version.id
365 }`
366 );
367 expect(data).toEqual(undefined);
368 expect(options).toEqual(undefined);
369 return {
370 ...returnedVersionFields,
371 state: {
372 synchronizationState: SyncStates.UP_TO_DATE,
373 dirty: true
374 }
375 };
376 });
377
378 mockRest.addHandler('fetch', ({ data, options, baseUrl }) => {
379 expect(baseUrl).toEqual(
380 `/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}`
381 );
382 expect(data).toEqual(undefined);
383 expect(options).toEqual(undefined);
384 return {
385 ...returnedVersionFields
386 };
387 });
388
389 return LicenseAgreementActionHelper.saveLicenseAgreement(
390 store.dispatch,
391 {
392 licenseModelId: LICENSE_MODEL_ID,
393 version,
394 previousLicenseAgreement: previousLicenseAgreementData,
395 licenseAgreement: licenseAgreementUpdateData
396 }
397 ).then(() => {
398 expect(store.getState()).toEqual(expectedStore);
399 });
400 });
Michael Landoefa037d2017-02-19 12:57:33 +0200401});