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