blob: 5f1dcd5fc623ad9bf2fd50a2ec5fae5e7e5cedd3 [file] [log] [blame]
xuegao841ddaa2019-09-19 14:23:14 +02001/*-
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 */
23import React from 'react';
24import { shallow } from 'enzyme';
sebdet2e9ae122019-11-15 16:28:55 +010025import DeployLoopModal from './DeployLoopModal';
26import LoopCache from '../../../api/LoopCache';
27import LoopActionService from '../../../api/LoopActionService';
28import LoopService from '../../../api/LoopService';
xuegao841ddaa2019-09-19 14:23:14 +020029
sebdet2e9ae122019-11-15 16:28:55 +010030describe('Verify DeployLoopModal', () => {
xuegao841ddaa2019-09-19 14:23:14 +020031 const loopCache = new LoopCache({
32 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
33 "globalPropertiesJson": {
34 "dcaeDeployParameters": {
xuegao9047def2019-12-13 11:50:24 +010035 "testMs": {
36 "location_id": "",
37 "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"
38 }
xuegao841ddaa2019-09-19 14:23:14 +020039 }
40 }
41 });
42
43 it('Test the render method', () => {
44 const component = shallow(
sebdet2e9ae122019-11-15 16:28:55 +010045 <DeployLoopModal loopCache={loopCache}/>
xuegao841ddaa2019-09-19 14:23:14 +020046 )
47
48 expect(component).toMatchSnapshot();
49 });
xuegao564fdb02019-10-02 11:18:10 +020050
51 it('Test handleClose', () => {
52 const historyMock = { push: jest.fn() };
sebdet2e9ae122019-11-15 16:28:55 +010053 const handleClose = jest.spyOn(DeployLoopModal.prototype,'handleClose');
54 const component = shallow(<DeployLoopModal history={historyMock} loopCache={loopCache}/>)
xuegao564fdb02019-10-02 11:18:10 +020055
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();
xuegao0efeb6b2019-10-07 14:36:34 +020066 const showAlert = jest.fn();
sebdet2e9ae122019-11-15 16:28:55 +010067 const handleSave = jest.spyOn(DeployLoopModal.prototype,'handleSave');
xuegao564fdb02019-10-02 11:18:10 +020068 LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => {
69 return Promise.resolve({
70 ok: true,
71 status: 200,
72 text: () => "OK"
73 });
74 });
75 LoopActionService.performAction = jest.fn().mockImplementation(() => {
76 return Promise.resolve({
77 ok: true,
78 status: 200,
79 json: () => {}
80 });
81 });
82 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
83 return Promise.resolve({
84 ok: true,
85 status: 200,
86 json: () => {}
87 });
88 });
xuegao0efeb6b2019-10-07 14:36:34 +020089
sebdet2e9ae122019-11-15 16:28:55 +010090 const component = shallow(<DeployLoopModal history={historyMock}
xuegao0efeb6b2019-10-07 14:36:34 +020091 loopCache={loopCache} updateLoopFunction={updateLoopFunction} showAlert={showAlert} />)
xuegao564fdb02019-10-02 11:18:10 +020092
93 component.find('[variant="primary"]').prop('onClick')();
94 await flushPromises();
95 component.update();
96
97 expect(handleSave).toHaveBeenCalledTimes(1);
98 expect(component.state('show')).toEqual(false);
99 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
xuegao564fdb02019-10-02 11:18:10 +0200100 handleSave.mockClear();
101 });
102
103 it('Onchange event', () => {
104 const event = { target: { name: "location_id", value: "testLocation"} };
sebdet2e9ae122019-11-15 16:28:55 +0100105 const component = shallow(<DeployLoopModal loopCache={loopCache}/>);
xuegao564fdb02019-10-02 11:18:10 +0200106
107 component.find('[name="location_id"]').simulate('change', event);
108 component.update();
xuegao9047def2019-12-13 11:50:24 +0100109 expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.testMs.location_id).toEqual("testLocation");
xuegao564fdb02019-10-02 11:18:10 +0200110 });
xuegao841ddaa2019-09-19 14:23:14 +0200111});