Fix naming convention
[clamp.git] / ui-react / src / components / dialogs / Loop / DeployLoopModal.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 DeployLoopModal from './DeployLoopModal';
26 import LoopCache from '../../../api/LoopCache';
27 import LoopActionService from '../../../api/LoopActionService';
28 import LoopService from '../../../api/LoopService';
29
30 describe('Verify DeployLoopModal', () => {
31         const loopCache = new LoopCache({
32                 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
33                 "globalPropertiesJson": {
34                         "dcaeDeployParameters": {
35                                 "location_id": "",
36                                 "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"
37                         }
38                 }
39         });
40
41         it('Test the render method', () => {
42                 const component = shallow(
43                         <DeployLoopModal loopCache={loopCache}/>
44                 )
45
46         expect(component).toMatchSnapshot();
47         });
48         
49         it('Test handleClose', () => {
50                 const historyMock = { push: jest.fn() };
51                 const handleClose = jest.spyOn(DeployLoopModal.prototype,'handleClose');
52                 const component = shallow(<DeployLoopModal history={historyMock} loopCache={loopCache}/>)
53
54                 component.find('[variant="secondary"]').prop('onClick')();
55
56                 expect(handleClose).toHaveBeenCalledTimes(1);
57                 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
58         });
59
60         it('Test handleSave successful', async () => {
61                 const flushPromises = () => new Promise(setImmediate);
62                 const historyMock = { push: jest.fn() };
63                 const updateLoopFunction = jest.fn();
64                 const showAlert = jest.fn();
65                 const handleSave = jest.spyOn(DeployLoopModal.prototype,'handleSave');
66                 LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => {
67                         return Promise.resolve({
68                                 ok: true,
69                                 status: 200,
70                                 text: () => "OK"
71                         });
72                 });
73                 LoopActionService.performAction = jest.fn().mockImplementation(() => {
74                         return Promise.resolve({
75                                 ok: true,
76                                 status: 200,
77                                 json: () => {}
78                         });
79                 });
80                 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
81                         return Promise.resolve({
82                                 ok: true,
83                                 status: 200,
84                                 json: () => {}
85                         });
86                 });
87
88                 const component = shallow(<DeployLoopModal history={historyMock} 
89                                                 loopCache={loopCache} updateLoopFunction={updateLoopFunction} showAlert={showAlert} />)
90
91                 component.find('[variant="primary"]').prop('onClick')();
92                 await flushPromises();
93                 component.update();
94
95                 expect(handleSave).toHaveBeenCalledTimes(1);
96                 expect(component.state('show')).toEqual(false);
97                 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
98                 handleSave.mockClear();
99         });
100
101         it('Onchange event', () => {
102                 const event = { target: { name: "location_id", value: "testLocation"} };
103                 const component = shallow(<DeployLoopModal loopCache={loopCache}/>);
104                 const forms = component.find('StateManager');
105
106                 component.find('[name="location_id"]').simulate('change', event);
107                 component.update();
108                 expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.location_id).toEqual("testLocation");
109         });
110 });