brunomilitzer | 87111ee | 2021-05-18 12:50:32 +0100 | [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 LoopUI from './LoopUI'; |
| 26 | import OnapConstants from './utils/OnapConstants'; |
| 27 | |
| 28 | import LoopCache from './api/LoopCache'; |
| 29 | import LoopActionService from './api/LoopActionService'; |
| 30 | import LoopService from './api/LoopService'; |
| 31 | |
| 32 | describe('Verify LoopUI', () => { |
| 33 | beforeEach(() => { |
| 34 | fetch.resetMocks(); |
| 35 | fetch.mockImplementation(() => { |
| 36 | return Promise.resolve({ |
| 37 | ok: true, |
| 38 | status: 200, |
| 39 | text: () => "testUser" |
| 40 | |
| 41 | }); |
| 42 | }); |
| 43 | }) |
| 44 | |
| 45 | const loopCache = new LoopCache({ |
| 46 | "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", |
| 47 | "components": { |
| 48 | "POLICY": { |
| 49 | "componentState": { |
| 50 | "stateName": "UNKNOWN", |
| 51 | "description": "The policies defined have NOT yet been created on the policy engine" |
| 52 | } |
| 53 | }, |
| 54 | "DCAE": { |
| 55 | "componentState": { |
| 56 | "stateName": "BLUEPRINT_DEPLOYED", |
| 57 | "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | it('Test the render method', async () => { |
| 64 | const flushPromises = () => new Promise(setImmediate); |
| 65 | |
| 66 | const component = shallow(<LoopUI/>) |
| 67 | component.setState({ |
| 68 | loopName: "testLoopName", |
| 69 | showSucAlert: false, |
| 70 | showFailAlert: false |
| 71 | }); |
| 72 | await flushPromises(); |
| 73 | expect(component).toMatchSnapshot(); |
| 74 | }); |
| 75 | |
| 76 | test('Test closeLoop method', () => { |
| 77 | const historyMock = { push: jest.fn() }; |
| 78 | const component = shallow(<LoopUI history={ historyMock }/>) |
| 79 | const instance = component.instance(); |
| 80 | instance.closeLoop(); |
| 81 | |
| 82 | expect(component.state('loopName')).toEqual(OnapConstants.defaultLoopName); |
| 83 | expect(historyMock.push.mock.calls[0]).toEqual(['/']); |
| 84 | }) |
| 85 | |
| 86 | test('Test loadLoop method refresh suc', async () => { |
| 87 | const historyMock = { push: jest.fn() }; |
| 88 | LoopService.getLoop = jest.fn().mockImplementation(() => { |
| 89 | return Promise.resolve({ |
| 90 | ok: true, |
| 91 | status: 200, |
| 92 | text: () => { |
| 93 | } |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { |
| 98 | return Promise.resolve({ name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" }); |
| 99 | }); |
| 100 | |
| 101 | const flushPromises = () => new Promise(setImmediate); |
| 102 | const component = shallow(<LoopUI history={ historyMock }/>) |
| 103 | const instance = component.instance(); |
| 104 | instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); |
| 105 | |
| 106 | await flushPromises(); |
| 107 | |
| 108 | const resLoopCache = instance.getLoopCache(); |
| 109 | |
| 110 | expect(resLoopCache.getComponentStates()).toBeUndefined(); |
| 111 | expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); |
| 112 | }) |
| 113 | |
| 114 | test('Test loadLoop method refresh fail', async () => { |
| 115 | const historyMock = { push: jest.fn() }; |
| 116 | LoopService.getLoop = jest.fn().mockImplementation(() => { |
| 117 | return Promise.resolve({ |
| 118 | name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", |
| 119 | "components": { |
| 120 | "POLICY": { |
| 121 | "componentState": { |
| 122 | "stateName": "UNKNOWN", |
| 123 | "description": "The policies defined have NOT yet been created on the policy engine" |
| 124 | } |
| 125 | }, |
| 126 | "DCAE": { |
| 127 | "componentState": { |
| 128 | "stateName": "BLUEPRINT_DEPLOYED", |
| 129 | "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { |
| 137 | return Promise.reject({ error: "whatever" }); |
| 138 | }); |
| 139 | |
| 140 | const flushPromises = () => new Promise(setImmediate); |
| 141 | const component = shallow(<LoopUI history={ historyMock }/>) |
| 142 | const instance = component.instance(); |
| 143 | instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); |
| 144 | |
| 145 | await flushPromises(); |
| 146 | |
| 147 | const resLoopCache = instance.getLoopCache(); |
| 148 | |
| 149 | expect(resLoopCache).toEqual(loopCache); |
| 150 | expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); |
| 151 | }) |
| 152 | |
| 153 | test('Test alert methods', () => { |
| 154 | const component = shallow(<LoopUI/>) |
| 155 | expect(component.state('showSucAlert')).toEqual(false); |
| 156 | |
| 157 | const instance = component.instance(); |
| 158 | instance.showSucAlert("testAlert"); |
| 159 | expect(component.state('showSucAlert')).toEqual(true); |
| 160 | expect(component.state('showFailAlert')).toEqual(false); |
| 161 | expect(component.state('showMessage')).toEqual("testAlert"); |
| 162 | |
| 163 | instance.disableAlert(); |
| 164 | |
| 165 | expect(component.state('showSucAlert')).toEqual(false); |
| 166 | expect(component.state('showFailAlert')).toEqual(false); |
| 167 | |
| 168 | instance.showFailAlert("testAlert2"); |
| 169 | expect(component.state('showSucAlert')).toEqual(false); |
| 170 | expect(component.state('showFailAlert')).toEqual(true); |
| 171 | expect(component.state('showMessage')).toEqual("testAlert2"); |
| 172 | }) |
| 173 | }); |