Add new dialog for policy
[clamp.git] / ui-react / src / components / loop_viewer / svg / LoopSvg.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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 { shallow } from 'enzyme';
25 import LoopSvg from './LoopSvg';
26 import LoopCache from '../../../api/LoopCache';
27 import LoopService from '../../../api/LoopService';
28
29 describe('Verify LoopSvg', () => {
30     const loopCache = new LoopCache({
31         "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
32         "microServicePolicies": [{
33             "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca",
34             "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app",
35             "properties": {"domain": "measurementsForVfScaling"},
36             "shared": false,
37             "jsonRepresentation": {"schema": {}}
38         }],
39         "operationalPolicies": [{
40             "name": "OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca",
41             "configurationsJson": {
42                 "guard_policies": {},
43                 "operational_policy": {
44                     "controlLoop": {},
45                     "policies": []
46                 }
47             }
48         }]
49     });
50
51     it('Test the render method no loopName', () => {
52         const localLoopCache = new LoopCache({
53         "microServicePolicies": [{
54             "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca",
55             "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app",
56             "properties": {"domain": "measurementsForVfScaling"},
57             "shared": false,
58             "jsonRepresentation": {"schema": {}}
59           }]
60         });
61         const component = shallow(
62             <LoopSvg.WrappedComponent loopCache={localLoopCache}/>
63         );
64
65         expect(component).toMatchSnapshot();
66     });
67
68     it('Test the render method', () => {
69         const component = shallow(
70             <LoopSvg.WrappedComponent  loopCache={loopCache}/>
71         );
72
73         expect(component).toMatchSnapshot();
74     });
75
76     it('Test the render method svg not empty', async () => {
77         const flushPromises = () => new Promise(setImmediate);
78         LoopService.getSvg = jest.fn().mockImplementation(() => {
79             return Promise.resolve("<svg><text test</text></svg>");
80         });
81         const component = shallow(
82             <LoopSvg.WrappedComponent  loopCache={loopCache}/>
83         );
84         await flushPromises();
85         expect(component).toMatchSnapshot();
86     });
87
88     it('Test handleSvgClick', () => {
89         const historyMock = { push: jest.fn() };
90
91         const component = shallow(
92             <LoopSvg.WrappedComponent loopCache={loopCache} history={historyMock}/>
93         );
94         let dummyElement = document.createElement('div');
95         dummyElement.setAttribute("data-element-id","TCA_h2NMX_v1_0_ResourceInstanceName1_tca");
96
97         const event = { target: { parentNode: { parentNode:{ parentNode: dummyElement }}}};
98
99         component.simulate('click', event);
100         component.update();
101
102         expect(historyMock.push.mock.calls[0]).toEqual([ '/policyModal/MICRO-SERVICE-POLICY/TCA_h2NMX_v1_0_ResourceInstanceName1_tca']);
103
104         //click operational policy
105         dummyElement.setAttribute("data-element-id","OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca");
106         const event2 = { target: { parentNode: { parentNode:{ parentNode: dummyElement }}}};
107
108         component.simulate('click', event2);
109         component.update();
110
111         expect(historyMock.push.mock.calls[1]).toEqual([ '/policyModal/OPERATIONAL-POLICY/OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca']);
112     });
113
114     it('Test componentWillReceiveProps method', () => {
115         const localLoopCache = new LoopCache({
116         "microServicePolicies": [{
117             "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca",
118             "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app",
119             "properties": {"domain": "measurementsForVfScaling"},
120             "shared": false,
121             "jsonRepresentation": {"schema": {}}
122           }]
123         });
124         const component = shallow(
125             <LoopSvg.WrappedComponent loopCache={localLoopCache}/>
126         );
127
128         expect(component.state('componentModalMapping').size).toEqual(1);
129
130         component.setProps({loopCache: loopCache});
131         expect(component.state('componentModalMapping').size).toEqual(2);
132     });
133 });