18ec6fdce543cc6f6bc8fab42f4b379dfbe47c1c
[clamp.git] / ui-react / src / components / dialogs / Loop / CreateLoopModal.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 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 CreateLoopModal from './CreateLoopModal';
26 import LoopService from '../../../api/LoopService';
27 import TemplateService from '../../../api/TemplateService';
28
29 describe('Verify CreateLoopModal', () => {
30
31   it('Test the render method', async () => {
32         const flushPromises = () => new Promise(setImmediate);
33     TemplateService.getTemplateNames = jest.fn().mockImplementation(() => {
34         return Promise.resolve(["template1","template2"]);
35         });
36         
37         const component = shallow(<CreateLoopModal/>);
38     expect(component).toMatchSnapshot();
39         await flushPromises();
40         component.update();
41                 
42     expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1"}, {"label": "template2", "value": "template2"}]);
43   });
44
45   it('handleDropdownListChange event', async () => {
46         const flushPromises = () => new Promise(setImmediate);
47     const event = {value: 'template1'};
48     TemplateService.getBlueprintMicroServiceTemplateSvg = jest.fn().mockImplementation(() => {
49                 return Promise.resolve("");
50         });
51
52     const component = shallow(<CreateLoopModal/>);
53     component.find('StateManager').simulate('change', event);
54     await flushPromises();
55     component.update();
56     expect(component.state('chosenTemplateName')).toEqual("template1");
57     expect(component.state('content')).toEqual("Error1");
58     
59     TemplateService.getBlueprintMicroServiceTemplateSvg = jest.fn().mockImplementation(() => {
60                 return Promise.resolve("svgContentTest");
61         });
62         component.find('StateManager').simulate('change', {value: 'template2'});
63     await flushPromises();
64     component.update();
65     expect(component.state('chosenTemplateName')).toEqual("template2");
66     expect(component.state('content')).toEqual("svgContentTest");
67   });
68
69
70
71   it('handleModelName event', () => {
72     const event = {target: {value : "model1"} };
73     const component = shallow(<CreateLoopModal/>);
74     component.find('input').simulate('change', event);
75     component.update();
76     expect(component.state('modelName')).toEqual("model1");
77   });
78
79
80   it('Test handleClose', () => {
81     const historyMock = { push: jest.fn() }; 
82     const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
83     const component = shallow(<CreateLoopModal history={historyMock} />)
84
85     component.find('[variant="secondary"]').prop('onClick')();
86
87     expect(handleClose).toHaveBeenCalledTimes(1);
88     expect(component.state('show')).toEqual(false);
89     expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
90     
91     handleClose.mockClear();
92   });
93
94   it('Test handleCreate Fail', () => {
95     const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
96     const component = shallow(<CreateLoopModal/>)
97
98     component.find('[variant="primary"]').prop('onClick')();
99
100     expect(handleCreate).toHaveBeenCalledTimes(1);
101     expect(component.state('show')).toEqual(true);
102
103     handleCreate.mockClear();
104   });
105
106   it('Test handleCreate Suc', async () => {
107         const flushPromises = () => new Promise(setImmediate);
108     const historyMock = { push: jest.fn() };
109     const loadLoopFunction = jest.fn();  
110     
111     LoopService.createLoop = jest.fn().mockImplementation(() => {
112                         return Promise.resolve({
113                                 ok: true,
114                                 status: 200,
115                                 json: () => {}
116                         });
117                 });
118
119     const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
120     const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
121     component.setState({
122                         modelName: "modelNameTest",
123                         chosenTemplateName: "template1"
124                 });
125
126     component.find('[variant="primary"]').prop('onClick')();
127         await flushPromises();
128         component.update();
129
130     expect(handleCreate).toHaveBeenCalledTimes(1);
131     expect(component.state('show')).toEqual(false);
132     expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
133
134     handleCreate.mockClear();
135   });
136
137 });