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