2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
23 import React from 'react';
24 import { shallow } from 'enzyme';
25 import LoopUI from './LoopUI';
26 import OnapConstants from './utils/OnapConstants';
28 import LoopCache from './api/LoopCache';
29 import LoopActionService from './api/LoopActionService';
30 import LoopService from './api/LoopService';
32 describe('Verify LoopUI', () => {
35 fetch.mockImplementation(() => {
36 return Promise.resolve({
39 text: () => "testUser"
45 const loopCache = new LoopCache({
46 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
50 "stateName": "UNKNOWN",
51 "description": "The policies defined have NOT yet been created on the policy engine"
56 "stateName": "BLUEPRINT_DEPLOYED",
57 "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"
63 it('Test the render method', async () => {
64 const flushPromises = () => new Promise(setImmediate);
66 const component = shallow(<LoopUI />)
68 loopName: "testLoopName",
72 await flushPromises();
73 expect(component).toMatchSnapshot();
76 test('Test closeLoop method', () => {
77 const historyMock = { push: jest.fn() };
78 const component = shallow(<LoopUI history={historyMock}/>)
79 const instance = component.instance();
82 expect(component.state('loopName')).toEqual(OnapConstants.defaultLoopName);
83 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
86 test('Test logout method', async () => {
87 const flushPromises = () => new Promise(setImmediate);
88 const component = shallow(<LoopUI />)
89 const instance = component.instance();
91 await flushPromises();
92 expect(component.state('userName')).toEqual("testUser");
95 test('Test loadLoop method refresh suc', async () => {
96 const historyMock = { push: jest.fn() };
97 LoopService.getLoop = jest.fn().mockImplementation(() => {
98 return Promise.resolve({
105 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
106 return Promise.resolve({name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"});
109 const flushPromises = () => new Promise(setImmediate);
110 const component = shallow(<LoopUI history={historyMock}/>)
111 const instance = component.instance();
112 instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
114 await flushPromises();
116 const resLoopCache = instance.getLoopCache();
118 expect(resLoopCache.getComponentStates()).toBeUndefined();
119 expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
122 test('Test loadLoop method refresh fail', async () => {
123 const historyMock = { push: jest.fn() };
124 LoopService.getLoop = jest.fn().mockImplementation(() => {
125 return Promise.resolve({
126 name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
130 "stateName": "UNKNOWN",
131 "description": "The policies defined have NOT yet been created on the policy engine"
136 "stateName": "BLUEPRINT_DEPLOYED",
137 "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"
143 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
144 return Promise.reject({error: "whatever"});
147 const flushPromises = () => new Promise(setImmediate);
148 const component = shallow(<LoopUI history={historyMock}/>)
149 const instance = component.instance();
150 instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
152 await flushPromises();
154 const resLoopCache = instance.getLoopCache();
156 expect(resLoopCache).toEqual(loopCache);
157 expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
160 test('Test alert methods', () => {
161 const component = shallow(<LoopUI />)
162 expect(component.state('showSucAlert')).toEqual(false);
164 const instance = component.instance();
165 instance.showSucAlert("testAlert");
166 expect(component.state('showSucAlert')).toEqual(true);
167 expect(component.state('showFailAlert')).toEqual(false);
168 expect(component.state('showMessage')).toEqual("testAlert");
170 instance.disableAlert();
172 expect(component.state('showSucAlert')).toEqual(false);
173 expect(component.state('showFailAlert')).toEqual(false);
175 instance.showFailAlert("testAlert2");
176 expect(component.state('showSucAlert')).toEqual(false);
177 expect(component.state('showFailAlert')).toEqual(true);
178 expect(component.state('showMessage')).toEqual("testAlert2");