d4021c97c515e2a3d79b641f99aebaef6d7abc2c
[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                     "operational_policy": {
46                         "controlLoop": {},
47                         "policies": []
48                     }
49                 },
50                 "policyModel": {"policyPdpGroup": {"supportedPdpGroups":[{"monitoring": ["xacml"]}]}},
51                 "jsonRepresentation" : {"schema": {}}
52              }]
53     };
54     const loopCache = new LoopCache(loopCacheStr);
55     const historyMock = { push: jest.fn() };
56     const flushPromises = () => new Promise(setImmediate);
57     const match = {params: {policyName:"OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca", policyInstanceType: "OPERATIONAL-POLICY"}}
58         
59     it('Test handleClose', () => {
60       const handleClose = jest.spyOn(PolicyModal.prototype,'handleClose');
61       const component = mount(<PolicyModal history={historyMock} match={match} loopCache={loopCache}/>)
62
63       component.find('[variant="secondary"]').prop('onClick')();
64
65       expect(handleClose).toHaveBeenCalledTimes(1);
66       expect(component.state('show')).toEqual(false);
67       expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
68     });
69
70     it('Test handleSave', async () => {
71         const loadLoopFunction = jest.fn();
72         const handleSave = jest.spyOn(PolicyModal.prototype,'handleSave');
73         const component = mount(<PolicyModal history={historyMock} 
74                           loopCache={loopCache} match={match} loadLoopFunction={loadLoopFunction} />)
75
76         component.find('[variant="primary"]').get(0).props.onClick();
77         await flushPromises();
78         component.update();
79
80         expect(handleSave).toHaveBeenCalledTimes(1);
81         expect(component.state('show')).toEqual(false);
82         expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
83     });
84
85     it('Test handleRefresh', async () => {
86         LoopService.refreshOperationalPolicyJson = jest.fn().mockImplementation(() => {
87             return Promise.resolve(loopCacheStr);
88         });
89         const updateLoopFunction = jest.fn();
90         const handleRefresh = jest.spyOn(PolicyModal.prototype,'handleRefresh');
91         const component = mount(<PolicyModal loopCache={loopCache} match={match} updateLoopFunction={updateLoopFunction} />)
92
93         component.find('[variant="primary"]').get(1).props.onClick();
94         await flushPromises();
95         component.update();
96
97         expect(handleRefresh).toHaveBeenCalledTimes(1);
98         expect(component.state('show')).toEqual(true);
99         expect(component.state('showSucAlert')).toEqual(true);
100         expect(component.state('showMessage')).toEqual("Successfully refreshed");
101     });
102
103     it('Test handlePdpGroupChange', () => {
104         const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
105         component.setState({ 
106                 "pdpGroup": [{"option1":["subPdp1","subPdp2"]}],
107                 "chosenPdpGroup": "option2"
108         });
109         expect(component.state('chosenPdpGroup')).toEqual("option2");
110
111         const instance = component.instance();
112         const event = {label:"option1", value:"option1"}
113         instance.handlePdpGroupChange(event);
114         expect(component.state('chosenPdpGroup')).toEqual("option1");
115         expect(component.state('chosenPdpSubgroup')).toEqual("");
116         expect(component.state('pdpSubgroupList')).toEqual([{label:"subPdp1", value:"subPdp1"}, {label:"subPdp2", value:"subPdp2"}]);
117     });
118
119     it('Test handlePdpSubgroupChange', () => {
120         const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
121
122         const instance = component.instance();
123         const event = {label:"option1", value:"option1"}
124         instance.handlePdpSubgroupChange(event);
125         expect(component.state('chosenPdpSubgroup')).toEqual("option1");
126     });
127 });