Changes include Metadata support, Upload tosca policy model and Loop Template
[clamp.git] / ui-react / src / components / dialogs / Tosca / ViewBlueprintMicroServiceTemplatesModal.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 ViewBlueprintMicroServiceTemplatesModal from './ViewBlueprintMicroServiceTemplatesModal';
26 import { mount } from 'enzyme';
27
28 describe('Verify ViewBlueprintMicroServiceTemplatesModal', () => {
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                                                 "content": "MTCA version 1",
43                                                 "templateName": "MTCA",
44                                                 "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
45                                                 "updatedBy":"tester",
46                                                 "templateId":"aaaa_bbbb_cccc",
47                                                 "timestamp":"2019-09-06 19:09:42"
48                                         });
49                                 }
50                         });
51                 });
52                 const component = shallow(<ViewBlueprintMicroServiceTemplatesModal/>);
53         });
54         
55         it('Test API Exception', () => {
56                 fetch.mockImplementationOnce(() => {
57                         return Promise.resolve({
58                                 ok: false,
59                                 status: 500,
60                                 json: () => {
61                                         return Promise.resolve({
62                                                 "index": "1",
63                                                 "content": "MTCA version 1",
64                                                 "templateName": "MTCA",
65                                                 "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
66                                                 "updatedBy":"tester",
67                                                 "templateId":"aaaa_bbbb_cccc",
68                                                 "timestamp":"2019-09-06 19:09:42"
69                                         });
70                                 }
71                         });
72                 });
73                 const component = shallow(<ViewBlueprintMicroServiceTemplatesModal/>);
74         });
75
76         it('Test API Rejection', () => {
77                 const myMockFunc  = fetch.mockImplementationOnce(() => Promise.reject('error'));
78                 setTimeout( () => myMockFunc().catch(e => {
79                         console.log(e);
80                 }),
81                 100
82                 );
83                 new Promise(resolve => setTimeout(resolve, 200));
84                 const component = shallow(<ViewBlueprintMicroServiceTemplatesModal/>);
85                 expect(myMockFunc.mock.calls.length).toBe(1);
86         });
87
88         it('Test the tosca model view render method', () => {
89                 fetch.mockImplementationOnce(() => {
90                         return Promise.resolve({
91                                 ok: true,
92                                 status: 200,
93                                 json: () => {
94                                         return Promise.resolve({
95                                                 "index": "1",
96                                                 "content": "MTCA version 1",
97                                                 "templateName": "MTCA",
98                                                 "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
99                                                 "updatedBy":"tester",
100                                                 "templateId":"aaaa_bbbb_cccc",
101                                                 "timestamp":"2019-09-06 19:09:42"
102                                         });
103                                 }
104                         });
105                 });
106                 const component = shallow(<ViewBlueprintMicroServiceTemplatesModal/>);
107                 component.setState({ bpTemplNames: {
108                         "index": "1",
109                         "content": "MTCA version 1",
110                         "templateName": "MTCA",
111                         "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
112                         "updatedBy":"tester",
113                         "templateId":"aaaa_bbbb_cccc",
114                         "timestamp":"2019-09-06 19:09:42"
115                   }
116                 });
117                 expect(component).toMatchSnapshot();
118         });
119
120         it('Test Table icons', () => {
121                         fetch.mockImplementationOnce(() => {
122                                 return Promise.resolve({
123                                         ok: true,
124                                         status: 200,
125                                         json: () => {
126                                                 return Promise.resolve({
127                                                         "index": "1",
128                                                         "content": "MTCA version 1",
129                                                         "templateName": "MTCA",
130                                                         "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
131                                                         "updatedBy":"tester",
132                                                         "templateId":"aaaa_bbbb_cccc",
133                                                         "timestamp":"2019-09-06 19:09:42"
134                                                 });
135                                         }
136                                 });
137                         });
138                         const component = mount(<ViewBlueprintMicroServiceTemplatesModal/>);
139                         expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
140                 });
141
142                 it('Test handleYamlContent', () => {
143                         fetch.mockImplementationOnce(() => {
144                                 return Promise.resolve({
145                                         ok: true,
146                                         status: 200,
147                                         json: () => {
148                                                 return Promise.resolve({
149                                                         "index": "1",
150                                                         "content": "MTCA version 1",
151                                                         "templateName": "MTCA",
152                                                         "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
153                                                         "updatedBy":"tester",
154                                                         "templateId":"aaaa_bbbb_cccc",
155                                                         "timestamp":"2019-09-06 19:09:42"
156                                                 });
157                                         }
158                                 });
159                         });
160                         const yamlContent = 'MTCA Tosca model details';
161                         const component = shallow(<ViewBlueprintMicroServiceTemplatesModal/>);
162                         component.find('[value="Please select Blue print template to view the details"]').prop('onChange')({ target: { value: yamlContent }});
163                         expect(component.state('content')).toEqual(yamlContent);
164                 });
165                 
166                 it('Test handleClose', () => {
167                         fetch.mockImplementationOnce(() => {
168                                 return Promise.resolve({
169                                         ok: true,
170                                         status: 200,
171                                         json: () => {
172                                                 return Promise.resolve({
173                                                         "index": "1",
174                                                         "content": "MTCA version 1",
175                                                         "templateName": "MTCA",
176                                                         "templatePolicy.policyModelId" : "onap.policies.monitoring.docker.mtca.app",
177                                                         "updatedBy":"tester",
178                                                         "templateId":"aaaa_bbbb_cccc",
179                                                         "timestamp":"2019-09-06 19:09:42"
180                                                 });
181                                         }
182                                 });
183                         });
184                         const historyMock = { push: jest.fn() };
185                         const handleClose = jest.spyOn(ViewBlueprintMicroServiceTemplatesModal.prototype,'handleClose');
186                         const component = shallow(<ViewBlueprintMicroServiceTemplatesModal history={historyMock} />)
187                         component.find('[variant="secondary"]').prop('onClick')();
188                         expect(handleClose).toHaveBeenCalledTimes(1);
189                         expect(component.state('show')).toEqual(false);
190                         expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
191                         handleClose.mockClear();
192                 });
193         });