add reducer action that can merge any object by path 80/98880/1
authorEylon Malin <eylon.malin@intl.att.com>
Wed, 27 Nov 2019 08:19:27 +0000 (10:19 +0200)
committerEylon Malin <eylon.malin@intl.att.com>
Wed, 27 Nov 2019 08:19:27 +0000 (10:19 +0200)
Issue-ID: VID-603
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: Ib34ed65d2ebb1f1dc1780205e4a60ab8515c8334

vid-webpack-master/src/app/shared/storeUtil/utils/general/general.actions.ts
vid-webpack-master/src/app/shared/storeUtil/utils/general/general.reducers.spec.ts
vid-webpack-master/src/app/shared/storeUtil/utils/general/general.reducers.ts

index 7a10eba..79dd3c7 100644 (file)
@@ -5,6 +5,7 @@ import {ServiceType} from "../../../models/serviceType";
 import {ITreeNode} from "angular-tree-component/dist/defs/api";
 
 export enum GeneralActions {
+  MERGE_OBJECT_BY_PATH = "MERGE_OBJECT_BY_PATH",
   UPDATE_LCP_REGIONS_AND_TENANTS = "UPDATE_LCP_REGIONS_AND_TENANTS",
   UPDATE_SUBSCRIBERS = "UPDATE_SUBSCRIBERS",
   UPDATE_PRODUCT_FAMILIES = "UPDATE_PRODUCT_FAMILIES",
@@ -78,6 +79,11 @@ export interface UpdateServiceTypesAction extends Action {
   subscriberId: string;
 }
 
+export interface MergeObjectByPathAction extends Action{
+  path: String[];
+  payload: object;
+}
+
 export const updateLcpRegionsAndTenants: ActionCreator<UpdateLcpRegionsAndTenantsAction> = lcpRegionsAndTenants => ({
   type: GeneralActions.UPDATE_LCP_REGIONS_AND_TENANTS,
   lcpRegionsAndTenants: lcpRegionsAndTenants
@@ -147,4 +153,10 @@ export const updateServiceTypes: ActionCreator<UpdateServiceTypesAction> = (serv
   subscriberId: subscriberId
 });
 
+export const mergeObjectByPathAction : ActionCreator<MergeObjectByPathAction> = (path, payload) => ({
+  type: GeneralActions.MERGE_OBJECT_BY_PATH,
+  path,
+  payload
+});
+
 
index ed45631..952a928 100644 (file)
@@ -1,22 +1,12 @@
 import {LcpRegion} from "../../../models/lcpRegion";
 import {Tenant} from "../../../models/tenant";
 import {generalReducer} from "./general.reducers";
-import {
-  ChangeInstanceCounterAction,
-  RemoveInstanceAction,
-  DuplicateBulkInstancesAction,
-  GeneralActions,
-  UpdateAicZonesAction,
-  UpdateCategoryParametersAction,
-  UpdateProductFamiliesAction,
-  UpdateServiceTypesAction,
-  UpdateSubscribersAction,
-  UpdateUserIdAction, UpdateNetworkCollectionFunction,
-} from "./general.actions";
+import {ChangeInstanceCounterAction, DuplicateBulkInstancesAction, GeneralActions, MergeObjectByPathAction, RemoveInstanceAction, UpdateAicZonesAction, UpdateCategoryParametersAction, UpdateNetworkCollectionFunction, UpdateProductFamiliesAction, UpdateServiceTypesAction, UpdateSubscribersAction, UpdateUserIdAction,} from "./general.actions";
 import {SelectOption} from "../../../models/selectOption";
 import {ServiceType} from "../../../models/serviceType";
 import {ITreeNode} from "angular-tree-component/dist/defs/api";
 import {VnfInstance} from "../../../models/vnfInstance";
+import each from "jest-each";
 
 describe('generalReducer', () => {
   test('#UPDATE_LCP_REGIONS_AND_TENANTS : should update lcp region and tenants', () => {
@@ -370,6 +360,45 @@ describe('generalReducer', () => {
     expect(state).toBeDefined();
     expect(state['networkFunctions']).toBeDefined();
   });
+
+  const originalMockObject = {
+    remain: 'forever',
+    obsolete: 'toBeChange'
+  };
+
+  each([
+    [
+      ['serviceInstance', 'serviceModelId', 'vnfs'],
+      {
+        remain: 'forever',
+        obsolete: 'newValue',
+        newField: 'newValue2'
+      }
+    ],
+    [
+      ['serviceInstance', 'nowhere', 'somewhere'],
+      originalMockObject
+    ],
+  ]).
+  test('#MERGE_OBJECT_BY_PATH: should update some object by path %s', (path, expected) => {
+    let state = generalReducer(<any>{serviceInstance : {
+          'serviceModelId' : {
+            vnfs : originalMockObject,
+            existingVNFCounterMap : {}
+          }
+        }},
+      <MergeObjectByPathAction>{
+        type: GeneralActions.MERGE_OBJECT_BY_PATH,
+        path,
+        payload: {
+          obsolete: 'newValue',
+          newField: 'newValue2'
+        }
+      });
+
+    expect(state).toBeDefined();
+    expect(state.serviceInstance['serviceModelId'].vnfs).toEqual(expected);
+  });
 });
 
 
index 5b265db..a343227 100644 (file)
@@ -1,12 +1,5 @@
 import {Action} from "redux";
-import {
-  ChangeInstanceCounterAction, RemoveInstanceAction, DuplicateBulkInstancesAction,
-  GeneralActions,
-  UpdateAicZonesAction, UpdateCategoryParametersAction,
-  UpdateLcpRegionsAndTenantsAction, UpdateNetworkCollectionFunction,
-  UpdateProductFamiliesAction, UpdateServiceTypesAction,
-  UpdateSubscribersAction, UpdateUserIdAction
-} from "./general.actions";
+import {ChangeInstanceCounterAction, DuplicateBulkInstancesAction, GeneralActions, MergeObjectByPathAction, RemoveInstanceAction, UpdateAicZonesAction, UpdateCategoryParametersAction, UpdateLcpRegionsAndTenantsAction, UpdateNetworkCollectionFunction, UpdateProductFamiliesAction, UpdateServiceTypesAction, UpdateSubscribersAction, UpdateUserIdAction} from "./general.actions";
 import {TypeNodeInformation} from "../../../../drawingBoard/service-planning/typeNodeInformation.model";
 import * as _ from "lodash";
 import {ITreeNode} from "angular-tree-component/dist/defs/api";
@@ -92,6 +85,16 @@ export function generalReducer(state: ServiceState, action: Action) : ServiceSta
       return newState;
     }
 
+    case GeneralActions.MERGE_OBJECT_BY_PATH : {
+      const mergeObjectByPathAction = <MergeObjectByPathAction>action;
+      let newState = _.cloneDeep(state);
+      let targetObject = _.get(newState, <any>mergeObjectByPathAction.path);
+      if (targetObject) {
+        targetObject = _.merge(targetObject, mergeObjectByPathAction.payload);
+      }
+      return newState;
+    }
+
 
   }
 }