blob: 5bbefe2284705454aebec4d3d5f44b50cd238be6 [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';
sebdetb13a7902019-11-15 17:26:45 +010025import LoopPropertiesModal from './LoopPropertiesModal';
26import LoopCache from '../../../api/LoopCache';
27import LoopService from '../../../api/LoopService';
xuegao841ddaa2019-09-19 14:23:14 +020028
sebdetb13a7902019-11-15 17:26:45 +010029describe('Verify LoopPropertiesModal', () => {
xuegao841ddaa2019-09-19 14:23:14 +020030 const loopCache = new LoopCache({
31 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
32 "globalPropertiesJson": {
33 "dcaeDeployParameters": {
34 "location_id": "",
35 "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"
36 }
37 }
38 });
39
40 it('Test the render method', () => {
41 const component = shallow(
sebdetb13a7902019-11-15 17:26:45 +010042 <LoopPropertiesModal loopCache={loopCache}/>
xuegao841ddaa2019-09-19 14:23:14 +020043 )
44 component.setState({ show: true,
45 temporaryPropertiesJson: {
46 "dcaeDeployParameters": {
47 "location_id": "",
48 "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"
49 }
50 }
51 });
52
53 expect(component.state('temporaryPropertiesJson')).toEqual({
54 "dcaeDeployParameters": {
55 "location_id": "",
56 "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"}
57 });
58 expect(component.state('show')).toEqual(true);
59
60 expect(component).toMatchSnapshot();
61 });
xuegao564fdb02019-10-02 11:18:10 +020062
63 it('Test handleClose', () => {
64 const historyMock = { push: jest.fn() };
sebdetb13a7902019-11-15 17:26:45 +010065 const handleClose = jest.spyOn(LoopPropertiesModal.prototype,'handleClose');
66 const component = shallow(<LoopPropertiesModal history={historyMock} loopCache={loopCache}/>)
xuegao564fdb02019-10-02 11:18:10 +020067
68 component.find('[variant="secondary"]').prop('onClick')();
69
70 expect(handleClose).toHaveBeenCalledTimes(1);
71 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
72 });
73
74 it('Test handleSave successful', async () => {
75 const flushPromises = () => new Promise(setImmediate);
76 const historyMock = { push: jest.fn() };
77 const loadLoopFunction = jest.fn();
sebdetb13a7902019-11-15 17:26:45 +010078 const handleSave = jest.spyOn(LoopPropertiesModal.prototype,'handleSave');
xuegao564fdb02019-10-02 11:18:10 +020079 LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => {
80 return Promise.resolve({
81 ok: true,
82 status: 200,
83 text: () => "OK"
84 });
85 });
86
sebdetb13a7902019-11-15 17:26:45 +010087 const component = shallow(<LoopPropertiesModal history={historyMock}
xuegao564fdb02019-10-02 11:18:10 +020088 loopCache={loopCache} loadLoopFunction={loadLoopFunction} />)
89
90 component.find('[variant="primary"]').prop('onClick')();
91 await flushPromises();
92 component.update();
93
94 expect(handleSave).toHaveBeenCalledTimes(1);
95 expect(component.state('show')).toEqual(false);
96 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
97 });
98
99 it('Onchange event', () => {
100 const event = {target:{name:"dcaeDeployParameters", value:"{\"location_id\": \"testLocation\",\"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"}"}};
sebdetb13a7902019-11-15 17:26:45 +0100101 const component = shallow(<LoopPropertiesModal loopCache={loopCache}/>);
xuegao564fdb02019-10-02 11:18:10 +0200102
103 component.find('FormControl').simulate('change', event);
104 component.update();
105
106 expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.location_id).toEqual("testLocation");
107 });
xuegao841ddaa2019-09-19 14:23:14 +0200108});