2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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 { mount } from 'enzyme';
25 import PolicyModal from './PolicyModal';
26 import LoopCache from '../../../api/LoopCache';
27 import LoopService from '../../../api/LoopService';
29 describe('Verify PolicyModal', () => {
32 fetch.mockImplementation(() => {
33 return Promise.resolve({
40 const loopCacheStr = {
41 "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca",
42 "operationalPolicies": [{
43 "name": "OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca",
44 "configurationsJson": {
45 "operational_policy": {
50 "policyModel": {"policyPdpGroup": {"supportedPdpGroups":[{"monitoring": ["xacml"]}]}},
51 "jsonRepresentation" : {"schema": {}}
54 const loopCache = new LoopCache(loopCacheStr);
55 const historyMock = { push: jest.fn() };
56 const flushPromises = () => new Promise(setImmediate);
57 const match = {params: {policyName:"OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca", policyInstanceType: "OPERATIONAL-POLICY"}}
59 it('Test handleClose', () => {
60 const handleClose = jest.spyOn(PolicyModal.prototype,'handleClose');
61 const component = mount(<PolicyModal history={historyMock} match={match} loopCache={loopCache}/>)
63 component.find('[variant="secondary"]').prop('onClick')();
65 expect(handleClose).toHaveBeenCalledTimes(1);
66 expect(component.state('show')).toEqual(false);
67 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
70 it('Test handleSave', async () => {
71 const loadLoopFunction = jest.fn();
72 const handleSave = jest.spyOn(PolicyModal.prototype,'handleSave');
73 const component = mount(<PolicyModal history={historyMock}
74 loopCache={loopCache} match={match} loadLoopFunction={loadLoopFunction} />)
76 component.find('[variant="primary"]').get(0).props.onClick();
77 await flushPromises();
80 expect(handleSave).toHaveBeenCalledTimes(1);
81 expect(component.state('show')).toEqual(false);
82 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
85 it('Test handleRefresh', async () => {
86 LoopService.refreshOperationalPolicyJson = jest.fn().mockImplementation(() => {
87 return Promise.resolve(loopCacheStr);
89 const updateLoopFunction = jest.fn();
90 const handleRefresh = jest.spyOn(PolicyModal.prototype,'handleRefresh');
91 const component = mount(<PolicyModal loopCache={loopCache} match={match} updateLoopFunction={updateLoopFunction} />)
93 component.find('[variant="primary"]').get(1).props.onClick();
94 await flushPromises();
97 expect(handleRefresh).toHaveBeenCalledTimes(1);
98 expect(component.state('show')).toEqual(true);
99 expect(component.state('showSucAlert')).toEqual(true);
100 expect(component.state('showMessage')).toEqual("Successfully refreshed");
103 it('Test handlePdpGroupChange', () => {
104 const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
106 "pdpGroup": [{"option1":["subPdp1","subPdp2"]}],
107 "chosenPdpGroup": "option2"
109 expect(component.state('chosenPdpGroup')).toEqual("option2");
111 const instance = component.instance();
112 const event = {label:"option1", value:"option1"}
113 instance.handlePdpGroupChange(event);
114 expect(component.state('chosenPdpGroup')).toEqual("option1");
115 expect(component.state('chosenPdpSubgroup')).toEqual("");
116 expect(component.state('pdpSubgroupList')).toEqual([{label:"subPdp1", value:"subPdp1"}, {label:"subPdp2", value:"subPdp2"}]);
119 it('Test handlePdpSubgroupChange', () => {
120 const component = mount(<PolicyModal loopCache={loopCache} match={match} />)
122 const instance = component.instance();
123 const event = {label:"option1", value:"option1"}
124 instance.handlePdpSubgroupChange(event);
125 expect(component.state('chosenPdpSubgroup')).toEqual("option1");