blob: d1d4aa662a1db94e3a853e6950d5ff8d31869775 [file] [log] [blame]
drveerendra50320952020-03-04 20:30:44 -05001/*-
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 */
23import React from 'react';
24import { shallow } from 'enzyme';
25import { mount } from 'enzyme';
26import { render } from 'enzyme';
27import ManageDictionaries from './ManageDictionaries';
28import TemplateMenuService from '../../../api/TemplateService'
29
30describe('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",
Ted Humphreyb56cb112020-06-12 03:17:35 -040063 "secondLevelDictionary": "1",
drveerendra50320952020-03-04 20:30:44 -050064 "subDictionaryType": "string",
Ted Humphreyb56cb112020-06-12 03:17:35 -040065 "updatedBy": "test",
66 "updatedDate": "05-07-2019 19:09:42"
drveerendra50320952020-03-04 20:30:44 -050067 });
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 => {
JulienBe0cde2b52020-04-03 13:21:13 +020077 console.info(e);
drveerendra50320952020-03-04 20:30:44 -050078 }),
79 100
80 );
drveerendra50320952020-03-04 20:30:44 -050081 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",
Ted Humphreyb56cb112020-06-12 03:17:35 -040093 "secondLevelDictionary": "1",
drveerendra50320952020-03-04 20:30:44 -050094 "subDictionaryType": "string",
Ted Humphreyb56cb112020-06-12 03:17:35 -040095 "updatedBy": "test",
96 "updatedDate": "05-07-2019 19:09:42"
drveerendra50320952020-03-04 20:30:44 -050097 });
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 () => {
Ted Humphreyb56cb112020-06-12 03:17:35 -0400106 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 ];
drveerendra50320952020-03-04 20:30:44 -0500127 const historyMock = { push: jest.fn() };
128 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
Ted Humphreyb56cb112020-06-12 03:17:35 -0400129 return Promise.resolve(dictionaries);
drveerendra50320952020-03-04 20:30:44 -0500130 });
131 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
Ted Humphreyb56cb112020-06-12 03:17:35 -0400132 return Promise.resolve(dictionaries[0]);
drveerendra50320952020-03-04 20:30:44 -0500133 });
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} />)
drveerendra50320952020-03-04 20:30:44 -0500142 const instance = component.instance();
Ted Humphreyb56cb112020-06-12 03:17:35 -0400143 instance.getDictionaryElements("DefaultActors");
drveerendra50320952020-03-04 20:30:44 -0500144 instance.clickHandler();
Ted Humphreyb56cb112020-06-12 03:17:35 -0400145 instance.addReplaceDictionaryRequest();
146 instance.deleteDictionaryRequest();
drveerendra50320952020-03-04 20:30:44 -0500147 await flushPromises();
Ted Humphreyb56cb112020-06-12 03:17:35 -0400148 expect(component.state('dictionaries')).toEqual(dictionaries);
drveerendra50320952020-03-04 20:30:44 -0500149 });
150
151 test('Test adding and deleting dictionaryelements', async () => {
152 const historyMock = { push: jest.fn() };
Ted Humphreyb56cb112020-06-12 03:17:35 -0400153 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 ];
drveerendra50320952020-03-04 20:30:44 -0500174 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
Ted Humphreyb56cb112020-06-12 03:17:35 -0400175 return Promise.resolve(dictionaries);
drveerendra50320952020-03-04 20:30:44 -0500176 });
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}/>)
drveerendra50320952020-03-04 20:30:44 -0500185 const instance = component.instance();
Ted Humphreyb56cb112020-06-12 03:17:35 -0400186 instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} );
187 instance.deleteDictionaryRequest('EventDictionary');
drveerendra50320952020-03-04 20:30:44 -0500188 await flushPromises();
Ted Humphreyb56cb112020-06-12 03:17:35 -0400189 expect(component.state('currentSelectedDictionary')).toEqual(null);
drveerendra50320952020-03-04 20:30:44 -0500190 });
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",
Ted Humphreyb56cb112020-06-12 03:17:35 -0400200 "secondLevelDictionary": "1",
drveerendra50320952020-03-04 20:30:44 -0500201 "subDictionaryType": "string",
Ted Humphreyb56cb112020-06-12 03:17:35 -0400202 "updatedBy": "test",
203 "updatedDate": "05-07-2019 19:09:42"
drveerendra50320952020-03-04 20:30:44 -0500204 });
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});