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';
27 import LoopCache from './api/LoopCache';
28 import LoopActionService from './api/LoopActionService';
29 import LoopService from './api/LoopService';
31 describe('Verify LoopUI', () => {
34 fetch.mockImplementation(() => {
35 return Promise.resolve({
38 text: () => "testUser"
44 const loopCache = new LoopCache({
45 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
49 "stateName": "UNKNOWN",
50 "description": "The policies defined have NOT yet been created on the policy engine"
55 "stateName": "BLUEPRINT_DEPLOYED",
56 "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"
62 it('Test the render method', async () => {
63 const flushPromises = () => new Promise(setImmediate);
65 const component = shallow(<LoopUI />)
67 loopName: "testLoopName",
70 await flushPromises();
71 expect(component).toMatchSnapshot();
74 test('Test closeLoop method', () => {
75 const historyMock = { push: jest.fn() };
76 const component = shallow(<LoopUI history={historyMock}/>)
77 const instance = component.instance();
80 expect(component.state('loopName')).toEqual("Empty (NO loop loaded yet)");
81 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
84 test('Test logout method', async () => {
85 const flushPromises = () => new Promise(setImmediate);
86 const component = shallow(<LoopUI />)
87 const instance = component.instance();
89 await flushPromises();
90 expect(component.state('userName')).toEqual("testUser");
93 test('Test loadLoop method refresh suc', async () => {
94 const historyMock = { push: jest.fn() };
95 LoopService.getLoop = jest.fn().mockImplementation(() => {
96 return Promise.resolve({
103 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
104 return Promise.resolve({name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"});
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");
112 await flushPromises();
114 const resLoopCache = instance.getLoopCache();
116 expect(resLoopCache.getComponentStates()).toBeUndefined();
117 expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
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",
128 "stateName": "UNKNOWN",
129 "description": "The policies defined have NOT yet been created on the policy engine"
134 "stateName": "BLUEPRINT_DEPLOYED",
135 "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop"
141 LoopActionService.refreshStatus = jest.fn().mockImplementation(() => {
142 return Promise.reject({error: "whatever"});
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");
150 await flushPromises();
152 const resLoopCache = instance.getLoopCache();
154 expect(resLoopCache).toEqual(loopCache);
155 expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca");
158 test('Test alert methods', () => {
159 const component = shallow(<LoopUI />)
160 expect(component.state('showAlert')).toEqual(false);
162 const instance = component.instance();
163 instance.showAlert("testAlert");
164 expect(component.state('showAlert')).toEqual(true);
165 expect(component.state('showMessage')).toEqual("testAlert");
167 instance.disableAlert();
169 expect(component.state('showAlert')).toEqual(false);