Fix the loop template view
[clamp.git] / ui-react / src / components / dialogs / Tosca / ViewLoopTemplatesModal.test.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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 ViewLoopTemplatesModal from './ViewLoopTemplatesModal';
26 import { mount } from 'enzyme';
27
28 describe('Verify ViewLoopTemplatesModal', () => {
29         beforeEach(() => {
30                 fetch.resetMocks();
31                 }
32         );
33
34         it('Test API Successful', () => {
35                 fetch.mockImplementationOnce(() => {
36                         return Promise.resolve({
37                                 ok: true,
38                                 status: 200,
39                                 json: () => {
40                                         return Promise.resolve({
41                                                 "index": "1",
42                                                 "name": "MTCA version 1",
43                                                 "modelService.serviceDetails.name": "MTCA",
44                                                 "allowedLoopType" : "CLOSED",
45                                                 "maximumInstancesAllowed":1,
46                                                 "updatedDate":"2019-09-06 19:09:42"
47                                         });
48                                 }
49                         });
50                 });
51                 const component = shallow(<ViewLoopTemplatesModal/>);
52         });
53         
54         it('Test API Exception', () => {
55                 fetch.mockImplementationOnce(() => {
56                         return Promise.resolve({
57                                 ok: false,
58                                 status: 500,
59                                 json: () => {
60                                         return Promise.resolve({
61                                                 "index": "1",
62                                                 "name": "MTCA version 1",
63                                                 "modelService.serviceDetails.name": "MTCA",
64                                                 "allowedLoopType" : "CLOSED",
65                                                 "maximumInstancesAllowed":1,
66                                                 "updatedDate":"2019-09-06 19:09:42"
67                                         });
68                                 }
69                         });
70                 });
71                 const component = shallow(<ViewLoopTemplatesModal/>);
72         });
73
74         it('Test API Rejection', () => {
75                 const myMockFunc  = fetch.mockImplementationOnce(() => Promise.reject('error'));
76                 setTimeout( () => myMockFunc().catch(e => {
77                         console.log(e);
78                 }),
79                 100
80                 );
81                 new Promise(resolve => setTimeout(resolve, 200));
82                 const component = shallow(<ViewLoopTemplatesModal/>);
83                 expect(myMockFunc.mock.calls.length).toBe(1);
84         });
85
86         it('Test the tosca model view render method', () => {
87                 fetch.mockImplementationOnce(() => {
88                         return Promise.resolve({
89                                 ok: true,
90                                 status: 200,
91                                 json: () => {
92                                         return Promise.resolve({
93                                                 "index": "1",
94                                                 "name": "MTCA version 1",
95                                                 "modelService.serviceDetails.name": "MTCA",
96                                                 "allowedLoopType" : "CLOSED",
97                                                 "maximumInstancesAllowed":1,
98                                                 "updatedDate":"2019-09-06 19:09:42"
99                                         });
100                                 }
101                         });
102                 });
103                 const component = shallow(<ViewLoopTemplatesModal/>);
104                 component.setState({ loopTemplateData: {
105                                                 "index": "1",
106                                                 "name": "MTCA version 1",
107                                                 "modelService.serviceDetails.name": "MTCA",
108                                                 "allowedLoopType" : "CLOSED",
109                                                 "maximumInstancesAllowed":1,
110                                                 "updatedDate":"2019-09-06 19:09:42"
111                   }
112                 });
113                 expect(component).toMatchSnapshot();
114         });
115
116         it('Test Table icons', () => {
117                         fetch.mockImplementationOnce(() => {
118                                 return Promise.resolve({
119                                         ok: true,
120                                         status: 200,
121                                         json: () => {
122                                                 return Promise.resolve({
123                                                 "index": "1",
124                                                 "name": "MTCA version 1",
125                                                 "modelService.serviceDetails.name": "MTCA",
126                                                 "allowedLoopType" : "CLOSED",
127                                                 "maximumInstancesAllowed":1,
128                                                 "updatedDate":"2019-09-06 19:09:42"
129                                                 });
130                                         }
131                                 });
132                         });
133                         const component = mount(<ViewLoopTemplatesModal/>);
134                         expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
135                 });
136
137                 it('Test handleYamlContent', () => {
138                         fetch.mockImplementationOnce(() => {
139                                 return Promise.resolve({
140                                         ok: true,
141                                         status: 200,
142                                         json: () => {
143                                                 return Promise.resolve({
144                                                 "index": "1",
145                                                 "name": "MTCA version 1",
146                                                 "modelService.serviceDetails.name": "MTCA",
147                                                 "allowedLoopType" : "CLOSED",
148                                                 "maximumInstancesAllowed":1,
149                                                 "updatedDate":"2019-09-06 19:09:42"
150                                                 });
151                                         }
152                                 });
153                         });
154                         const yamlContent = 'MTCA version 1';
155                         const component = shallow(<ViewLoopTemplatesModal/>);
156                         component.find('[value="Please select a loop template to display it"]').prop('onChange')({ target: { value: yamlContent }});
157                         expect(component.state('content')).toEqual(yamlContent);
158                 });
159                 
160                 it('Test handleClose', () => {
161                         fetch.mockImplementationOnce(() => {
162                                 return Promise.resolve({
163                                         ok: true,
164                                         status: 200,
165                                         json: () => {
166                                                 return Promise.resolve({
167                                                 "index": "1",
168                                                 "name": "MTCA version 1",
169                                                 "modelService.serviceDetails.name": "MTCA",
170                                                 "allowedLoopType" : "CLOSED",
171                                                 "maximumInstancesAllowed":1,
172                                                 "updatedDate":"2019-09-06 19:09:42"
173                                                 });
174                                         }
175                                 });
176                         });
177                         const historyMock = { push: jest.fn() };
178                         const handleClose = jest.spyOn(ViewLoopTemplatesModal.prototype,'handleClose');
179                         const component = shallow(<ViewLoopTemplatesModal history={historyMock} />)
180                         component.find('[variant="secondary"]').prop('onClick')();
181                         expect(handleClose).toHaveBeenCalledTimes(1);
182                         expect(component.state('show')).toEqual(false);
183                         expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
184                         handleClose.mockClear();
185                 });
186         });