c054b264028884a8f0849c202557509d0220247c
[vid.git] / vid-webpack-master / cypress / support / application / application.session.actions.ts
1 declare namespace Cypress {
2   interface Chainable {
3     setReduxState : typeof setReduxState;
4     getReduxState : typeof getReduxState;
5     clearSessionStorage: typeof clearSessionStorage;
6     setTestApiParamToGR: typeof setTestApiParamToGR;
7     setTestApiParamToVNF: typeof setTestApiParamToVNF;
8     buildReduxStateWithServiceRespone: typeof buildReduxStateWithServiceRespone;
9   }
10 }
11
12 /**********************************
13  Type to input with id some text
14  *********************************/
15 function setReduxState(state?: string) : void {
16   cy.readFile('cypress/support/jsonBuilders/mocks/jsons/basicRedux.json').then((res) => {
17     cy.window().then((win) => {
18       win.sessionStorage.setItem('reduxState',  JSON.stringify(state ? state : res));
19     });
20   });
21 }
22 function getReduxState(): Chainable<any> {
23   return cy.window().then((win) => {
24     let stateRaw = win.sessionStorage.getItem('reduxState');
25     return JSON.parse(stateRaw ?  stateRaw : '{}');
26   });
27 }
28
29 function clearSessionStorage() : void {
30   cy.window().then((win) => {
31     win.sessionStorage.clear();
32   });
33 }
34
35 function setTestApiParamToGR() : void {
36   cy.window().then((win) => {
37     win.sessionStorage.setItem('msoRequestParametersTestApiValue', 'GR_API');
38   });
39 }
40
41 function setTestApiParamToVNF() : void {
42   cy.window().then((win) => {
43     win.sessionStorage.setItem('msoRequestParametersTestApiValue', 'VNF_API');
44   });
45 }
46
47 function updateObject(obj: any, key: string, val: any, value:any) {
48   return JSON.parse(JSON.stringify(obj)
49     .replace(new RegExp(`"${key}":"${val}"`), `"${key}":"${value}"`))
50 }
51
52 function buildReduxStateWithServiceRespone(res: any, serviceId:string, isEcompGeneratedNaming:boolean) :void {
53   res = updateObject(res, "ecomp_generated_naming", !isEcompGeneratedNaming, isEcompGeneratedNaming);
54   cy.window().then((win) => {
55     win.sessionStorage.setItem('reduxState',  JSON.stringify({
56       "global": {
57         "name": null
58       },
59       "service": {
60         "serviceHierarchy": {
61           [serviceId] : res
62         },
63         "serviceInstance": {
64           [serviceId]: {
65             "existingVNFCounterMap": {},
66             "existingVnfGroupCounterMap": {},
67             "existingNetworksCounterMap": {},
68             "vnfs": {},
69             "vnfGroups": {},
70             "isEcompGeneratedNaming": isEcompGeneratedNaming,
71             "existingNames": {},
72             "vidNotions": res.service.vidNotions
73           }
74         }
75       }
76     }));
77   });
78 }
79
80 Cypress.Commands.add('setReduxState', setReduxState);
81 Cypress.Commands.add('getReduxState', getReduxState);
82 Cypress.Commands.add('clearSessionStorage', clearSessionStorage);
83 Cypress.Commands.add('setTestApiParamToGR', setTestApiParamToGR);
84 Cypress.Commands.add('setTestApiParamToVNF',setTestApiParamToVNF);
85 Cypress.Commands.add('buildReduxStateWithServiceRespone', buildReduxStateWithServiceRespone);