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'; |
| 25 | import DeployLoop from './DeployLoop'; |
| 26 | import LoopCache from '../../api/LoopCache'; |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 27 | import LoopActionService from '../../api/LoopActionService'; |
| 28 | import LoopService from '../../api/LoopService'; |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 29 | |
| 30 | describe('Verify DeployLoop', () => { |
| 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 | <DeployLoop loopCache={loopCache}/> |
| 44 | ) |
| 45 | |
| 46 | expect(component).toMatchSnapshot(); |
| 47 | }); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 48 | |
| 49 | it('Test handleClose', () => { |
| 50 | const historyMock = { push: jest.fn() }; |
| 51 | const handleClose = jest.spyOn(DeployLoop.prototype,'handleClose'); |
| 52 | const component = shallow(<DeployLoop 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(); |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 64 | const showAlert = jest.fn(); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 65 | const handleSave = jest.spyOn(DeployLoop.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 | }); |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 87 | |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 88 | const component = shallow(<DeployLoop history={historyMock} |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 89 | loopCache={loopCache} updateLoopFunction={updateLoopFunction} showAlert={showAlert} />) |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 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([ '/']); |
xuegao | 564fdb0 | 2019-10-02 11:18:10 +0200 | [diff] [blame] | 98 | handleSave.mockClear(); |
| 99 | }); |
| 100 | |
| 101 | it('Onchange event', () => { |
| 102 | const event = { target: { name: "location_id", value: "testLocation"} }; |
| 103 | const component = shallow(<DeployLoop 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 | }); |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 110 | }); |