bf0b910965408ca70111fd88fd527319abbb25bb
[clamp.git] / ui-react / src / components / dialogs / Policy / PolicyModal.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23 import React from 'react';
24 import { mount } from 'enzyme';
25 import PolicyModal from './PolicyModal';
26 import LoopCache from '../../../api/LoopCache';
27 import LoopService from '../../../api/LoopService';
28
29 describe('Verify PolicyModal', () => {
30     beforeEach(() => {
31         fetch.resetMocks();
32         fetch.mockImplementation(() => {
33             return Promise.resolve({
34                 ok: true,
35                 status: 200,
36                 text: () => "OK"
37             });
38         });
39     })
40     const loopCacheStr = {
41             "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
42             "operationalPolicies": [{
43                 "name": "OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca",
44                 "configurationsJson": {
45                     "guard_policies": {},
46                     "operational_policy": {
47                         "controlLoop": {},
48                         "policies": []
49                     }
50                 },
51                 "policyModel": {"policyPdpGroup": {"supportedPdpGroups":[{"monitoring": ["xacml"]}]}},
52                 "jsonRepresentation" : {"schema": {}}
53              }]
54     };
55     const loopCache = new LoopCache(loopCacheStr);
56     const historyMock = { push: jest.fn() };
57     const flushPromises = () => new Promise(setImmediate);
58     const match = {params: {policyName:"OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca", policyInstanceType: "OPERATIONAL-POLICY"}}
59         
60     it('Test handleClose', () => {
61       const handleClose = jest.spyOn(PolicyModal.prototype,'handleClose');
62       const component = mount(<PolicyModal history={historyMock} match={match} loopCache={loopCache}/>)
63
64       component.find('[variant="secondary"]').prop('onClick')();
65
66       expect(handleClose).toHaveBeenCalledTimes(1);
67       expect(component.state('show')).toEqual(false);
68       expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
69     });
70
71     it('Test handleSave', async () => {
72         const loadLoopFunction = jest.fn();
73         const handleSave = jest.spyOn(PolicyModal.prototype,'handleSave');
74         const component = mount(<PolicyModal history={historyMock} 
75                           loopCache={loopCache} match={match} loadLoopFunction={loadLoopFunction} />)
76
77         component.find('[variant="primary"]').get(0).props.onClick();
78         await flushPromises();
79         component.update();
80
81         expect(handleSave).toHaveBeenCalledTimes(1);
82         expect(component.state('show')).toEqual(false);
83         expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
84     });
85
86     it('Test handleRefresh', async () => {
87         LoopService.refreshOperationalPolicyJson = jest.fn().mockImplementation(() => {
88             return Promise.resolve(loopCacheStr);
89         });
90         const updateLoopFunction = jest.fn();
91         const handleRefresh = jest.spyOn(PolicyModal.prototype,'handleRefresh');
92         const component = mount(<PolicyModal loopCache={loopCache} match={match} updateLoopFunction={updateLoopFunction} />)
93
94         component.find('[variant="primary"]').get(1).props.onClick();
95         await flushPromises();
96         component.update();
97
98         expect(handleRefresh).toHaveBeenCalledTimes(1);
99         expect(component.state('show')).toEqual(true);
100         expect(component.state('showSucAlert')).toEqual(true);
101         expect(component.state('showMessage')).toEqual("Successfully refreshed");
102     });
103
104     it('Test handlePdpGroupChange', () => {
105         const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
106         component.setState({ 
107                 "pdpGroup": [{"option1":["subPdp1","subPdp2"]}],
108                 "chosenPdpGroup": "option2"
109         });
110         expect(component.state('chosenPdpGroup')).toEqual("option2");
111
112         const instance = component.instance();
113         const event = {label:"option1", value:"option1"}
114         instance.handlePdpGroupChange(event);
115         expect(component.state('chosenPdpGroup')).toEqual("option1");
116         expect(component.state('chosenPdpSubgroup')).toEqual("");
117         expect(component.state('pdpSubgroupList')).toEqual([{label:"subPdp1", value:"subPdp1"}, {label:"subPdp2", value:"subPdp2"}]);
118     });
119
120     it('Test handlePdpSubgroupChange', () => {
121         const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
122
123         const instance = component.instance();
124         const event = {label:"option1", value:"option1"}
125         instance.handlePdpSubgroupChange(event);
126         expect(component.state('chosenPdpSubgroup')).toEqual("option1");
127     });
128 });