blob: 5a3dd17c6e0e75449a079c05ed7157583ae521e3 [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';
AviZi280f8012017-06-09 02:39:56 +030019import {cloneAndSet, buildListFromFactory} from 'test-utils/Util.js';
Michael Landoefa037d2017-02-19 12:57:33 +020020import {storeCreator} from 'sdc-app/AppStore.js';
21import LicenseAgreementActionHelper from 'sdc-app/onboarding/licenseModel/licenseAgreement/LicenseAgreementActionHelper.js';
22
AviZi280f8012017-06-09 02:39:56 +030023import { LicenseAgreementStoreFactory, LicenseAgreementDispatchFactory, LicenseAgreementPostFactory, LicenseAgreementPutFactory } from 'test-utils/factories/licenseModel/LicenseAgreementFactories.js';
talig8e9c0652017-12-20 14:30:43 +020024import VersionFactory from 'test-utils/factories/common/VersionFactory.js';
25import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
26import CurrentScreenFactory from 'test-utils/factories/common/CurrentScreenFactory.js';
Michael Landoefa037d2017-02-19 12:57:33 +020027
28describe('License Agreement Module Tests', () => {
29
30 const LICENSE_MODEL_ID = '777';
talig8e9c0652017-12-20 14:30:43 +020031 const version = VersionFactory.build();
32 const itemPermissionAndProps = CurrentScreenFactory.build({}, {version});
33 const returnedVersionFields = {baseId: version.baseId, description: version.description, id: version.id, name: version.name, status: version.status};
Michael Landoefa037d2017-02-19 12:57:33 +020034
35 it('Load License Agreement List', () => {
AviZi280f8012017-06-09 02:39:56 +030036 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory);
37
Michael Landoefa037d2017-02-19 12:57:33 +020038 const store = storeCreator();
39 deepFreeze(store.getState());
40
41 const expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', licenseAgreementList);
42
43 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
AviZi280f8012017-06-09 02:39:56 +030044 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
45 expect(data).toEqual(undefined);
46 expect(options).toEqual(undefined);
Michael Landoefa037d2017-02-19 12:57:33 +020047 return {results: licenseAgreementList};
48 });
AviZi280f8012017-06-09 02:39:56 +030049 return LicenseAgreementActionHelper.fetchLicenseAgreementList(store.dispatch, {licenseModelId: LICENSE_MODEL_ID, version}).then(() => {
50 expect(store.getState()).toEqual(expectedStore);
Michael Landoefa037d2017-02-19 12:57:33 +020051 });
52 });
53
54 it('Delete License Agreement', () => {
AviZi280f8012017-06-09 02:39:56 +030055 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1);
Michael Landoefa037d2017-02-19 12:57:33 +020056 const store = storeCreator({
talig8e9c0652017-12-20 14:30:43 +020057 currentScreen: {...itemPermissionAndProps},
Michael Landoefa037d2017-02-19 12:57:33 +020058 licenseModel: {
59 licenseAgreement: {
60 licenseAgreementList
61 }
62 }
63 });
64 deepFreeze(store.getState());
talig8e9c0652017-12-20 14:30:43 +020065 const expectedCurrentScreenProps = {
66 ...itemPermissionAndProps,
67 itemPermission: {
68 ...itemPermissionAndProps.itemPermission,
69 isDirty: true
70 }
71 };
AviZi280f8012017-06-09 02:39:56 +030072 const toBeDeletedLicenseAgreementId = licenseAgreementList[0].id;
talig8e9c0652017-12-20 14:30:43 +020073 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', []);
74 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
Michael Landoefa037d2017-02-19 12:57:33 +020075
76 mockRest.addHandler('destroy', ({data, options, baseUrl}) => {
AviZi280f8012017-06-09 02:39:56 +030077 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeDeletedLicenseAgreementId}`);
78 expect(data).toEqual(undefined);
79 expect(options).toEqual(undefined);
Michael Landoefa037d2017-02-19 12:57:33 +020080 });
talig8e9c0652017-12-20 14:30:43 +020081 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
82 expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
83 expect(data).toEqual(undefined);
84 expect(options).toEqual(undefined);
85 return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
86 });
Michael Landoefa037d2017-02-19 12:57:33 +020087
88 return LicenseAgreementActionHelper.deleteLicenseAgreement(store.dispatch, {
89 licenseAgreementId: toBeDeletedLicenseAgreementId,
AviZi280f8012017-06-09 02:39:56 +030090 licenseModelId: LICENSE_MODEL_ID,
91 version
Michael Landoefa037d2017-02-19 12:57:33 +020092 }).then(() => {
AviZi280f8012017-06-09 02:39:56 +030093 expect(store.getState()).toEqual(expectedStore);
Michael Landoefa037d2017-02-19 12:57:33 +020094 });
95 });
96
97 it('Add License Agreement', () => {
talig8e9c0652017-12-20 14:30:43 +020098 const store = storeCreator({
99 currentScreen: {...itemPermissionAndProps}
100 });
Michael Landoefa037d2017-02-19 12:57:33 +0200101 deepFreeze(store.getState());
Michael Landoefa037d2017-02-19 12:57:33 +0200102
AviZi280f8012017-06-09 02:39:56 +0300103 const licenseAgreementToAdd = LicenseAgreementDispatchFactory.build();
104
105 const LicenseAgreementPostRequest = LicenseAgreementPostFactory.build(
106 pickBy(licenseAgreementToAdd, (val, key) => key !== 'featureGroupsIds')
107 );
108
109 deepFreeze(LicenseAgreementPostRequest);
Michael Landoefa037d2017-02-19 12:57:33 +0200110
111 const licenseAgreementIdFromResponse = 'ADDED_ID';
AviZi280f8012017-06-09 02:39:56 +0300112 const licenseAgreementAfterAdd = LicenseAgreementStoreFactory.build({
Michael Landoefa037d2017-02-19 12:57:33 +0200113 ...licenseAgreementToAdd,
114 id: licenseAgreementIdFromResponse
AviZi280f8012017-06-09 02:39:56 +0300115 });
Michael Landoefa037d2017-02-19 12:57:33 +0200116 deepFreeze(licenseAgreementAfterAdd);
ilanapcb1d4832017-09-27 11:41:19 +0300117 const licenseAgreementList = [licenseAgreementAfterAdd];
talig8e9c0652017-12-20 14:30:43 +0200118 const expectedCurrentScreenProps = {
119 ...itemPermissionAndProps,
120 itemPermission: {
121 ...itemPermissionAndProps.itemPermission,
122 isDirty: true
123 }
124 };
Einav Weiss Keidard2f57942018-02-14 14:00:07 +0200125 const featureGroupsList = licenseAgreementList[0].featureGroupsIds;
talig8e9c0652017-12-20 14:30:43 +0200126 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementAfterAdd]);
127 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
Michael Landoefa037d2017-02-19 12:57:33 +0200128
AviZi280f8012017-06-09 02:39:56 +0300129 mockRest.addHandler('post', ({options, data, baseUrl}) => {
130 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
131 expect(data).toEqual(LicenseAgreementPostRequest);
132 expect(options).toEqual(undefined);
Michael Landoefa037d2017-02-19 12:57:33 +0200133 return {
134 value: licenseAgreementIdFromResponse
135 };
136 });
ilanapcb1d4832017-09-27 11:41:19 +0300137 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
138 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
139 expect(data).toEqual(undefined);
140 expect(options).toEqual(undefined);
141 return {results: licenseAgreementList};
142 });
143 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
144 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
145 expect(data).toEqual(undefined);
146 expect(options).toEqual(undefined);
147 return {results: featureGroupsList};
148 });
talig8e9c0652017-12-20 14:30:43 +0200149 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
150 expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
151 expect(data).toEqual(undefined);
152 expect(options).toEqual(undefined);
153 return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
154
155 });
Michael Landoefa037d2017-02-19 12:57:33 +0200156 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
157 licenseAgreement: licenseAgreementToAdd,
AviZi280f8012017-06-09 02:39:56 +0300158 licenseModelId: LICENSE_MODEL_ID,
159 version
Michael Landoefa037d2017-02-19 12:57:33 +0200160 }).then(() => {
AviZi280f8012017-06-09 02:39:56 +0300161 expect(store.getState()).toEqual(expectedStore);
Michael Landoefa037d2017-02-19 12:57:33 +0200162 });
163 });
164
165 it('Update License Agreement', () => {
AviZi280f8012017-06-09 02:39:56 +0300166 const licenseAgreementList = buildListFromFactory(LicenseAgreementStoreFactory, 1, {featureGroupsIds: ['77']});
Michael Landoefa037d2017-02-19 12:57:33 +0200167 const store = storeCreator({
talig8e9c0652017-12-20 14:30:43 +0200168 currentScreen: {...itemPermissionAndProps},
Michael Landoefa037d2017-02-19 12:57:33 +0200169 licenseModel: {
170 licenseAgreement: {
171 licenseAgreementList
172 }
173 }
174 });
175 deepFreeze(store.getState());
176
Michael Landoefa037d2017-02-19 12:57:33 +0200177 const previousLicenseAgreementData = licenseAgreementList[0];
AviZi280f8012017-06-09 02:39:56 +0300178 const toBeUpdatedLicenseAgreementId = previousLicenseAgreementData.id;
179 const oldFeatureGroupIds = previousLicenseAgreementData.featureGroupsIds;
Michael Landoefa037d2017-02-19 12:57:33 +0200180
AviZi280f8012017-06-09 02:39:56 +0300181 const newFeatureGroupsIds = ['update_id_1', 'update_id_2'];
182
183 const licenseAgreementUpdateData = LicenseAgreementStoreFactory.build({
184 id: toBeUpdatedLicenseAgreementId,
185 featureGroupsIds: newFeatureGroupsIds
186 });
Michael Landoefa037d2017-02-19 12:57:33 +0200187 deepFreeze(licenseAgreementUpdateData);
188
AviZi280f8012017-06-09 02:39:56 +0300189 const LicenseAgreementPutFactoryRequest = LicenseAgreementPutFactory.build({
190 addedFeatureGroupsIds: newFeatureGroupsIds,
191 removedFeatureGroupsIds: oldFeatureGroupIds
192 });
193
194 deepFreeze(LicenseAgreementPutFactoryRequest);
Michael Landoefa037d2017-02-19 12:57:33 +0200195
talig8e9c0652017-12-20 14:30:43 +0200196 const expectedCurrentScreenProps = {
197 ...itemPermissionAndProps,
198 itemPermission: {
199 ...itemPermissionAndProps.itemPermission,
200 isDirty: true
201 }
202 };
203 let expectedStore = cloneAndSet(store.getState(), 'licenseModel.licenseAgreement.licenseAgreementList', [licenseAgreementUpdateData]);
204 expectedStore = cloneAndSet(expectedStore, 'currentScreen.itemPermission', expectedCurrentScreenProps.itemPermission);
Michael Landoefa037d2017-02-19 12:57:33 +0200205
AviZi280f8012017-06-09 02:39:56 +0300206 mockRest.addHandler('put', ({data, options, baseUrl}) => {
207 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements/${toBeUpdatedLicenseAgreementId}`);
208 expect(data).toEqual(LicenseAgreementPutFactoryRequest);
209 expect(options).toEqual(undefined);
Michael Landoefa037d2017-02-19 12:57:33 +0200210 });
ilanapcb1d4832017-09-27 11:41:19 +0300211 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
212 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/license-agreements`);
213 expect(data).toEqual(undefined);
214 expect(options).toEqual(undefined);
215 return {results: [licenseAgreementUpdateData]};
216 });
talig8e9c0652017-12-20 14:30:43 +0200217 mockRest.addHandler('fetch', ({data, options, baseUrl}) => {
218 expect(baseUrl).toEqual(`/onboarding-api/v1.0/items/${LICENSE_MODEL_ID}/versions/${version.id}`);
219 expect(data).toEqual(undefined);
220 expect(options).toEqual(undefined);
221 return {...returnedVersionFields, state: {synchronizationState: SyncStates.UP_TO_DATE, dirty: true}};
222 });
ilanapcb1d4832017-09-27 11:41:19 +0300223 mockRest.addHandler('fetch', ({options, data, baseUrl}) => {
224 expect(baseUrl).toEqual(`/onboarding-api/v1.0/vendor-license-models/${LICENSE_MODEL_ID}/versions/${version.id}/feature-groups`);
225 expect(data).toEqual(undefined);
226 expect(options).toEqual(undefined);
227 return {results: newFeatureGroupsIds};
228 });
talig8e9c0652017-12-20 14:30:43 +0200229
Michael Landoefa037d2017-02-19 12:57:33 +0200230 return LicenseAgreementActionHelper.saveLicenseAgreement(store.dispatch, {
231 licenseModelId: LICENSE_MODEL_ID,
AviZi280f8012017-06-09 02:39:56 +0300232 version,
Michael Landoefa037d2017-02-19 12:57:33 +0200233 previousLicenseAgreement: previousLicenseAgreementData,
234 licenseAgreement: licenseAgreementUpdateData
235 }).then(() => {
AviZi280f8012017-06-09 02:39:56 +0300236 expect(store.getState()).toEqual(expectedStore);
Michael Landoefa037d2017-02-19 12:57:33 +0200237 });
238 });
239
240});