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