4363da92353ccd202949ba26d70105b758e579a7
[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.log(e);
78                 }),
79                 100
80                 );
81                 new Promise(resolve => setTimeout(resolve, 200));
82                 const component = shallow(<ManageDictionaries />);
83                 expect(myMockFunc.mock.calls.length).toBe(1);
84         });
85
86         it('Test Table icons', () => {
87                 fetch.mockImplementationOnce(() => {
88                         return Promise.resolve({
89                                 ok: true,
90                                 status: 200,
91                                 json: () => {
92                                         return Promise.resolve({
93                                                 "name": "vtest",
94                   "secondLevelDictionary": "1",
95                                                 "subDictionaryType": "string",
96                         "updatedBy": "test",
97                   "updatedDate": "05-07-2019 19:09:42"
98                                         });
99                                 }
100                         });
101                 });
102                 const component = mount(<ManageDictionaries />);
103                 expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
104         });
105
106         test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => {
107                 const historyMock = { push: jest.fn() };
108                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
109                         return Promise.resolve("test");
110                 });
111                 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
112                         return Promise.resolve({dictionaryElements:"testitem"});
113                 });
114                 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
115                         return Promise.resolve(200);
116                 });
117                 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
118                         return Promise.resolve(200);
119                 });
120                 const flushPromises = () => new Promise(setImmediate);
121                 const component = shallow(<ManageDictionaries history={historyMock} />)
122                 component.setState({ newDict: {
123                         "name": "test",
124                         "secondLevelDictionary": "0",
125                         "subDictionaryType": "string"
126                         }
127                 });
128                 component.setState({ delData: {
129                         "name": "test",
130                         "secondLevelDictionary": "0",
131                         "subDictionaryType": "string"
132                         }
133                 });
134                 const instance = component.instance();
135                 instance.getDictionaryElements("test");
136                 instance.clickHandler();
137                 instance.addDictionary();
138                 instance.deleteDictionary();
139                 await flushPromises();
140                 expect(component.state('dictionaryNames')).toEqual("test");
141                 expect(component.state('dictionaryElements')).toEqual("testitem");
142                 expect(component.state('dictNameFlag')).toEqual(false);
143         });
144
145         test('Test adding and deleting dictionaryelements', async () => {
146                 const historyMock = { push: jest.fn() };
147                 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
148                         return Promise.resolve("test");
149                 });
150                 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
151                         return Promise.resolve(200);
152                 });
153                 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
154                         return Promise.resolve(200);
155                 });
156                 const flushPromises = () => new Promise(setImmediate);
157                 const component = shallow(<ManageDictionaries history={historyMock}/>)
158                 component.setState({ newDictItem: {
159                         "name": "test",
160                         "dictionaryElements" : {
161                                 "shortName": "shorttest",
162                                 }
163                 }});
164                 component.setState({ delDictItem: {
165                         "name": "test",
166                         "dictionaryElements" : {
167                                 "shortName": "shortTest",
168                                 }
169                 }});
170                 const instance = component.instance();
171                 instance.addDictionary();
172                 instance.deleteDictionary();
173                 await flushPromises();
174                 expect(component.state('dictionaryNames')).toEqual("test");
175         });
176
177         it('Test handleClose', () => {
178                 fetch.mockImplementationOnce(() => {
179                         return Promise.resolve({
180                                 ok: true,
181                                 status: 200,
182                                 json: () => {
183                                         return Promise.resolve({
184                                                 "name": "vtest",
185                         "secondLevelDictionary": "1",
186                                                 "subDictionaryType": "string",
187                         "updatedBy": "test",
188                         "updatedDate": "05-07-2019 19:09:42"
189                                         });
190                                 }
191                         });
192                 });
193                 const historyMock = { push: jest.fn() };
194                 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
195                 const component = shallow(<ManageDictionaries history={historyMock} />)
196                 component.find('[variant="secondary"]').prop('onClick')();
197                 expect(handleClose).toHaveBeenCalledTimes(1);
198                 expect(component.state('show')).toEqual(false);
199                 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
200                 handleClose.mockClear();
201         });
202 });