Merge "Change generated json schema"
[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         it('Test API Successful', () => {
34                 fetch.mockImplementationOnce(() => {
35                         return Promise.resolve({
36                                 ok: true,
37                                 status: 200,
38                                 json: () => {
39                                         return Promise.resolve({
40                                                 "index": "1",
41                                                 "name": "MTCA version 1",
42                                                 "modelService.serviceDetails.name": "MTCA",
43                                                 "allowedLoopType" : "CLOSED",
44                                                 "maximumInstancesAllowed":1,
45                                                 "updatedDate":"2019-09-06 19:09:42"
46                                         });
47                                 }
48                         });
49                 });
50                 const component = shallow(<ViewLoopTemplatesModal/>);
51         });
52         
53         it('Test API Exception', () => {
54                 fetch.mockImplementationOnce(() => {
55                         return Promise.resolve({
56                                 ok: false,
57                                 status: 500,
58                                 json: () => {
59                                         return Promise.resolve({
60                                                 "index": "1",
61                                                 "name": "MTCA version 1",
62                                                 "modelService.serviceDetails.name": "MTCA",
63                                                 "allowedLoopType" : "CLOSED",
64                                                 "maximumInstancesAllowed":1,
65                                                 "updatedDate":"2019-09-06 19:09:42"
66                                         });
67                                 }
68                         });
69                 });
70                 const component = shallow(<ViewLoopTemplatesModal/>);
71         });
72
73         it('Test API Rejection', () => {
74                 const myMockFunc  = fetch.mockImplementationOnce(() => Promise.reject('error'));
75                 setTimeout( () => myMockFunc().catch(e => {
76                         console.log(e);
77                 }),
78                 100
79                 );
80                 const component = shallow(<ViewLoopTemplatesModal/>);
81                 expect(myMockFunc.mock.calls.length).toBe(1);
82         });
83
84         it('Test the tosca model view render method', () => {
85                 fetch.mockImplementationOnce(() => {
86                         return Promise.resolve({
87                                 ok: true,
88                                 status: 200,
89                                 json: () => {
90                                         return Promise.resolve({
91                                                 "index": "1",
92                                                 "name": "MTCA version 1",
93                                                 "modelService.serviceDetails.name": "MTCA",
94                                                 "allowedLoopType" : "CLOSED",
95                                                 "maximumInstancesAllowed":1,
96                                                 "updatedDate":"2019-09-06 19:09:42"
97                                         });
98                                 }
99                         });
100                 });
101                 const component = shallow(<ViewLoopTemplatesModal/>);
102                 component.setState({ loopTemplateData: {
103                                                 "index": "1",
104                                                 "name": "MTCA version 1",
105                                                 "modelService.serviceDetails.name": "MTCA",
106                                                 "allowedLoopType" : "CLOSED",
107                                                 "maximumInstancesAllowed":1,
108                                                 "updatedDate":"2019-09-06 19:09:42"
109                   }
110                 });
111                 expect(component).toMatchSnapshot();
112         });
113
114         it('Test Table icons', () => {
115                         fetch.mockImplementationOnce(() => {
116                                 return Promise.resolve({
117                                         ok: true,
118                                         status: 200,
119                                         json: () => {
120                                                 return Promise.resolve({
121                                                 "index": "1",
122                                                 "name": "MTCA version 1",
123                                                 "modelService.serviceDetails.name": "MTCA",
124                                                 "allowedLoopType" : "CLOSED",
125                                                 "maximumInstancesAllowed":1,
126                                                 "updatedDate":"2019-09-06 19:09:42"
127                                                 });
128                                         }
129                                 });
130                         });
131                         const component = mount(<ViewLoopTemplatesModal/>);
132                         expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
133                 });
134                 
135                 it('Test handleClose', () => {
136                         fetch.mockImplementationOnce(() => {
137                                 return Promise.resolve({
138                                         ok: true,
139                                         status: 200,
140                                         json: () => {
141                                                 return Promise.resolve({
142                                                 "index": "1",
143                                                 "name": "MTCA version 1",
144                                                 "modelService.serviceDetails.name": "MTCA",
145                                                 "allowedLoopType" : "CLOSED",
146                                                 "maximumInstancesAllowed":1,
147                                                 "updatedDate":"2019-09-06 19:09:42"
148                                                 });
149                                         }
150                                 });
151                         });
152                         const historyMock = { push: jest.fn() };
153                         const handleClose = jest.spyOn(ViewLoopTemplatesModal.prototype,'handleClose');
154                         const component = shallow(<ViewLoopTemplatesModal history={historyMock} />)
155                         component.find('[variant="secondary"]').prop('onClick')();
156                         expect(handleClose).toHaveBeenCalledTimes(1);
157                         expect(component.state('show')).toEqual(false);
158                         expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
159                         handleClose.mockClear();
160                 });
161         });