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