13a6035a3d3efa3b5a71439cc20401bf53699812
[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 historyMock = { push: jest.fn() };
107                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
108                         return Promise.resolve("test");
109                 });
110                 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
111                         return Promise.resolve({dictionaryElements:"testitem"});
112                 });
113                 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
114                         return Promise.resolve(200);
115                 });
116                 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
117                         return Promise.resolve(200);
118                 });
119                 const flushPromises = () => new Promise(setImmediate);
120                 const component = shallow(<ManageDictionaries history={historyMock} />)
121                 component.setState({ newDict: {
122                         "name": "test",
123                         "secondLevelDictionary": "0",
124                         "subDictionaryType": "string"
125                         }
126                 });
127                 component.setState({ delData: {
128                         "name": "test",
129                         "secondLevelDictionary": "0",
130                         "subDictionaryType": "string"
131                         }
132                 });
133                 const instance = component.instance();
134                 instance.getDictionaryElements("test");
135                 instance.clickHandler();
136                 instance.addDictionary();
137                 instance.deleteDictionary();
138                 await flushPromises();
139                 expect(component.state('dictionaryNames')).toEqual("test");
140                 expect(component.state('dictionaryElements')).toEqual("testitem");
141                 expect(component.state('dictNameFlag')).toEqual(false);
142         });
143
144         test('Test adding and deleting dictionaryelements', async () => {
145                 const historyMock = { push: jest.fn() };
146                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
147                         return Promise.resolve("test");
148                 });
149                 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
150                         return Promise.resolve(200);
151                 });
152                 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
153                         return Promise.resolve(200);
154                 });
155                 const flushPromises = () => new Promise(setImmediate);
156                 const component = shallow(<ManageDictionaries history={historyMock}/>)
157                 component.setState({ newDictItem: {
158                         "name": "test",
159                         "dictionaryElements" : {
160                                 "shortName": "shorttest",
161                                 }
162                 }});
163                 component.setState({ delDictItem: {
164                         "name": "test",
165                         "dictionaryElements" : {
166                                 "shortName": "shortTest",
167                                 }
168                 }});
169                 const instance = component.instance();
170                 instance.addDictionary();
171                 instance.deleteDictionary();
172                 await flushPromises();
173                 expect(component.state('dictionaryNames')).toEqual("test");
174         });
175
176         it('Test handleClose', () => {
177                 fetch.mockImplementationOnce(() => {
178                         return Promise.resolve({
179                                 ok: true,
180                                 status: 200,
181                                 json: () => {
182                                         return Promise.resolve({
183                                                 "name": "vtest",
184                         "secondLevelDictionary": "1",
185                                                 "subDictionaryType": "string",
186                         "updatedBy": "test",
187                         "updatedDate": "05-07-2019 19:09:42"
188                                         });
189                                 }
190                         });
191                 });
192                 const historyMock = { push: jest.fn() };
193                 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
194                 const component = shallow(<ManageDictionaries history={historyMock} />)
195                 component.find('[variant="secondary"]').prop('onClick')();
196                 expect(handleClose).toHaveBeenCalledTimes(1);
197                 expect(component.state('show')).toEqual(false);
198                 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
199                 handleClose.mockClear();
200         });
201 });