xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 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'; |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 25 | import DeployLoopModal from './DeployLoopModal'; |
| 26 | import LoopCache from '../../../api/LoopCache'; |
| 27 | import LoopActionService from '../../../api/LoopActionService'; |
| 28 | import LoopService from '../../../api/LoopService'; |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 29 | |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 30 | describe('Verify DeployLoopModal', () => { |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 31 | const loopCache = new LoopCache({ |
| 32 | "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", |
| 33 | "globalPropertiesJson": { |
| 34 | "dcaeDeployParameters": { |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 35 | "testMs": { |
| 36 | "location_id": "", |
| 37 | "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" |
| 38 | } |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | it('Test the render method', () => { |
| 44 | const component = shallow( |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 45 | <DeployLoopModal loopCache={loopCache}/> |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | expect(component).toMatchSnapshot(); |
| 49 | }); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 50 | |
| 51 | it('Test handleClose', () => { |
| 52 | const historyMock = { push: jest.fn() }; |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 53 | const handleClose = jest.spyOn(DeployLoopModal.prototype,'handleClose'); |
| 54 | const component = shallow(<DeployLoopModal history={historyMock} loopCache={loopCache}/>) |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 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(); |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 66 | const showAlert = jest.fn(); |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 67 | const handleSave = jest.spyOn(DeployLoopModal.prototype,'handleSave'); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 68 | 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 | }); |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 89 | |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 90 | const component = shallow(<DeployLoopModal history={historyMock} |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 91 | loopCache={loopCache} updateLoopFunction={updateLoopFunction} showAlert={showAlert} />) |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 92 | |
| 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([ '/']); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 100 | handleSave.mockClear(); |
| 101 | }); |
| 102 | |
| 103 | it('Onchange event', () => { |
| 104 | const event = { target: { name: "location_id", value: "testLocation"} }; |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 105 | const component = shallow(<DeployLoopModal loopCache={loopCache}/>); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 106 | |
| 107 | component.find('[name="location_id"]').simulate('change', event); |
| 108 | component.update(); |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 109 | expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.testMs.location_id).toEqual("testLocation"); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 110 | }); |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 111 | }); |