Address Manage Dictionary issues
[clamp.git] / ui-react / src / components / dialogs / ManageDictionaries / ManageDictionaries.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 { mount } from 'enzyme';
26 import { render } from 'enzyme';
27 import ManageDictionaries from './ManageDictionaries';
28 import TemplateMenuService from '../../../api/TemplateService'
29
30 describe('Verify ManageDictionaries', () => {
31         beforeEach(() => {
32                 fetch.resetMocks();
33         });
34
35         it('Test API Successful', () => {
36                 fetch.mockImplementationOnce(() => {
37                         return Promise.resolve({
38                                 ok: true,
39                                 status: 200,
40                                 json: () => {
41                                         return Promise.resolve({
42                                                 "name": "vtest",
43                                                 "secondLevelDictionary": "1",
44                                                 "subDictionaryType": "string",
45                                                 "updatedBy": "test",
46                                                 "updatedDate": "05-07-2019 19:09:42"
47                                         });
48                                 }
49                         });
50                 });
51                 const component = shallow(<ManageDictionaries />);
52                 expect(component).toMatchSnapshot();
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                                                 "name": "vtest",
63                                                 "secondLevelDictionary": "1",
64                                                 "subDictionaryType": "string",
65                                                 "updatedBy": "test",
66                                                 "updatedDate": "05-07-2019 19:09:42"
67                                         });
68                                 }
69                         });
70                 });
71                 const component = shallow(<ManageDictionaries />);
72         });
73
74         it('Test API Rejection', () => {
75                 const myMockFunc  = fetch.mockImplementationOnce(() => Promise.reject('error'));
76                 setTimeout( () => myMockFunc().catch(e => {
77                         console.info(e);
78                 }),
79                 100
80                 );
81                 const component = shallow(<ManageDictionaries />);
82                 expect(myMockFunc.mock.calls.length).toBe(1);
83         });
84
85         it('Test Table icons', () => {
86                 fetch.mockImplementationOnce(() => {
87                         return Promise.resolve({
88                                 ok: true,
89                                 status: 200,
90                                 json: () => {
91                                         return Promise.resolve({
92                                                 "name": "vtest",
93                                                 "secondLevelDictionary": "1",
94                                                 "subDictionaryType": "string",
95                                                 "updatedBy": "test",
96                                                 "updatedDate": "05-07-2019 19:09:42"
97                                         });
98                                 }
99                         });
100                 });
101                 const component = mount(<ManageDictionaries />);
102                 expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
103         });
104
105         test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => {
106                 const dictionaries = [
107                         {
108                                 name: "DefaultActors",
109                                 secondLevelDictionary: 0,
110                                 subDictionaryType: "",
111                                 dictionaryElements: [
112                                         {
113                                                 "shortName": "SDNR",
114                                                 "name": "SDNR Change",
115                                                 "description": "SDNR component",
116                                                 "type": "string",
117                                                 "createdDate": "2020-06-07T18:57:18.130858Z",
118                                                 "updatedDate": "2020-06-11T13:10:52.239282Z",
119                                                 "updatedBy": "admin"
120                                         }
121                                 ],
122                                 createdDate: "2020-06-07T22:21:08.428742Z",
123                                 updatedDate: "2020-06-10T00:41:49.122908Z",
124                                 updatedBy: "Not found"
125                         }
126                 ];
127                 const historyMock = { push: jest.fn() };
128                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
129                         return Promise.resolve(dictionaries);
130                 });
131                 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
132                         return Promise.resolve(dictionaries[0]);
133                 });
134                 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
135                         return Promise.resolve(200);
136                 });
137                 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
138                         return Promise.resolve(200);
139                 });
140                 const flushPromises = () => new Promise(setImmediate);
141                 const component = shallow(<ManageDictionaries history={historyMock} />)
142                 const instance = component.instance();
143                 instance.getDictionaryElements("DefaultActors");
144                 instance.clickHandler();
145                 instance.addReplaceDictionaryRequest();
146                 instance.deleteDictionaryRequest();
147                 await flushPromises();
148                 expect(component.state('dictionaries')).toEqual(dictionaries);
149         });
150
151         test('Test adding and deleting dictionaryelements', async () => {
152                 const historyMock = { push: jest.fn() };
153                 const dictionaries = [
154                         {
155                                 name: "DefaultActors",
156                                 secondLevelDictionary: 0,
157                                 subDictionaryType: "",
158                                 dictionaryElements: [
159                                         {
160                                                 "shortName": "SDNR",
161                                                 "name": "SDNR Change",
162                                                 "description": "SDNR component",
163                                                 "type": "string",
164                                                 "createdDate": "2020-06-07T18:57:18.130858Z",
165                                                 "updatedDate": "2020-06-11T13:10:52.239282Z",
166                                                 "updatedBy": "admin"
167                                         }
168                                 ],
169                                 createdDate: "2020-06-07T22:21:08.428742Z",
170                                 updatedDate: "2020-06-10T00:41:49.122908Z",
171                                 updatedBy: "Not found"
172                         }
173                 ];
174                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
175                         return Promise.resolve(dictionaries);
176                 });
177                 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
178                         return Promise.resolve(200);
179                 });
180                 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
181                         return Promise.resolve(200);
182                 });
183                 const flushPromises = () => new Promise(setImmediate);
184                 const component = shallow(<ManageDictionaries history={historyMock}/>)
185                 const instance = component.instance();
186                 instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} );
187                 instance.deleteDictionaryRequest('EventDictionary');
188                 await flushPromises();
189                 expect(component.state('currentSelectedDictionary')).toEqual(null);
190         });
191
192         it('Test handleClose', () => {
193                 fetch.mockImplementationOnce(() => {
194                         return Promise.resolve({
195                                 ok: true,
196                                 status: 200,
197                                 json: () => {
198                                         return Promise.resolve({
199                                                 "name": "vtest",
200                                                 "secondLevelDictionary": "1",
201                                                 "subDictionaryType": "string",
202                                                 "updatedBy": "test",
203                                                 "updatedDate": "05-07-2019 19:09:42"
204                                         });
205                                 }
206                         });
207                 });
208                 const historyMock = { push: jest.fn() };
209                 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
210                 const component = shallow(<ManageDictionaries history={historyMock} />)
211                 component.find('[variant="secondary"]').prop('onClick')();
212                 expect(handleClose).toHaveBeenCalledTimes(1);
213                 expect(component.state('show')).toEqual(false);
214                 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
215                 handleClose.mockClear();
216         });
217 });