5b6ea9e624f17a7f0c7078dbef997fdf92bd38fc
[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.getAllLoopTemplates = jest.fn().mockImplementation(() => {
34         return Promise.resolve([{"name":"template1"},{"name":"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", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]);
43   });
44
45   it('handleDropdownListChange event', async () => {
46         const flushPromises = () => new Promise(setImmediate);
47
48     const component = shallow(<CreateLoopModal/>);
49     component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} });
50     await flushPromises();
51     component.update();
52     expect(component.state('chosenTemplateName')).toEqual("template1");
53     expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1");
54     expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
55
56         component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} });
57     await flushPromises();
58     component.update();
59     expect(component.state('chosenTemplateName')).toEqual("template2");
60     expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2");
61     expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
62   });
63
64
65
66   it('handleModelName event', () => {
67     const event = {target: {value : "model1"} };
68     const component = shallow(<CreateLoopModal/>);
69     component.find('input').simulate('change', event);
70     component.update();
71     expect(component.state('modelName')).toEqual("model1");
72   });
73
74
75   it('Test handleClose', () => {
76     const historyMock = { push: jest.fn() }; 
77     const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
78     const component = shallow(<CreateLoopModal history={historyMock} />)
79
80     component.find('[variant="secondary"]').prop('onClick')();
81
82     expect(handleClose).toHaveBeenCalledTimes(1);
83     expect(component.state('show')).toEqual(false);
84     expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
85     
86     handleClose.mockClear();
87   });
88
89   it('Test handleCreate Fail', () => {
90     const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
91     const component = shallow(<CreateLoopModal/>)
92
93     component.find('[variant="primary"]').prop('onClick')();
94
95     expect(handleCreate).toHaveBeenCalledTimes(1);
96     expect(component.state('show')).toEqual(true);
97
98     handleCreate.mockClear();
99   });
100
101   it('Test handleCreate Suc', async () => {
102         const flushPromises = () => new Promise(setImmediate);
103     const historyMock = { push: jest.fn() };
104     const loadLoopFunction = jest.fn();  
105     
106     LoopService.createLoop = jest.fn().mockImplementation(() => {
107                         return Promise.resolve({
108                                 ok: true,
109                                 status: 200,
110                                 json: () => {}
111                         });
112                 });
113
114     const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
115     const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
116     component.setState({
117                         modelName: "modelNameTest",
118                         chosenTemplateName: "template1"
119                 });
120
121     component.find('[variant="primary"]').prop('onClick')();
122         await flushPromises();
123         component.update();
124
125     expect(handleCreate).toHaveBeenCalledTimes(1);
126     expect(component.state('show')).toEqual(false);
127     expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
128
129     handleCreate.mockClear();
130   });
131
132 });