2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
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'
30 const TestDictionaryElements = {
32 secondLevelDictionary: 0,
33 subDictionaryType: "",
36 shortName: "alertType",
38 description: "Type of Alert",
41 createdDate: "2020-06-12T13:58:51.443931Z",
42 updatedDate: "2020-06-13T16:27:57.084870Z",
49 const TestDictionaries =
53 secondLevelDictionary: 0,
54 subDictionaryType: "string",
55 dictionaryElements: [ TestDictionaryElements ],
56 createdDate: "2020-06-14T21:00:33.231166Z",
57 updatedDate: "2020-06-14T21:00:33.231166Z",
63 secondLevelDictionary: 1,
64 subDictionaryType: "string",
69 description: "Sub Element Description",
71 createdDate: "2020-06-14T21:04:44.402287Z",
72 updatedDate: "2020-06-14T21:04:44.402287Z",
77 createdDate: "2020-06-14T21:01:16.390250Z",
78 updatedDate: "2020-06-14T21:01:16.390250Z",
85 const historyMock = { push: jest.fn() };
87 let errorMessage = '';
89 window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return });
91 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
92 return Promise.resolve(TestDictionaries);
95 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
96 return Promise.resolve({ ok: true, status: 200 });
99 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
100 return Promise.resolve("200");
103 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
104 return Promise.resolve(TestDictionaryElements);
107 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
108 return Promise.resolve("200");
111 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
112 return Promise.resolve("200");
116 describe('Verify ManageDictionaries', () => {
122 it('Test API Successful', () => {
123 fetch.mockImplementationOnce(() => {
124 return Promise.resolve({
128 return Promise.resolve({
130 "secondLevelDictionary": 1,
131 "subDictionaryType": "string",
133 "updatedDate": "05-07-2019 19:09:42"
138 const component = shallow(<ManageDictionaries />);
139 expect(component).toMatchSnapshot();
142 it('Test API Exception', () => {
143 fetch.mockImplementationOnce(() => {
144 return Promise.resolve({
148 return Promise.resolve({
150 "secondLevelDictionary": 1,
151 "subDictionaryType": "string",
153 "updatedDate": "05-07-2019 19:09:42"
158 const component = shallow(<ManageDictionaries />);
161 it('Test Table icons', () => {
163 const component = mount(<ManageDictionaries />);
164 expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
167 test('Test add/replace and delete dictionary requests', async () => {
169 const component = shallow(<ManageDictionaries history={historyMock}/>)
170 const instance = component.instance();
172 const flushPromises = () => new Promise(setImmediate);
174 instance.addReplaceDictionaryRequest({name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string"});
175 instance.deleteDictionaryRequest("test");
177 await flushPromises();
179 expect(component.state('currentSelectedDictionary')).toEqual(null);
180 expect(component.state('dictionaries')).toEqual(TestDictionaries);
183 test('Test update dictionary row', async () => {
185 const component = shallow(<ManageDictionaries history={historyMock}/>)
186 const instance = component.instance();
187 const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
189 await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined);
193 test('Test add dictionary row', async () => {
195 const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest');
196 const component = shallow(<ManageDictionaries />)
197 const instance = component.instance();
198 const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
200 await instance.addDictionaryRow(rowData);
201 expect(addReplaceRequest).toHaveBeenCalledWith(rowData);
205 test('Test add dictionary row with errors name already exists', async () => {
207 const component = shallow(<ManageDictionaries />)
208 const instance = component.instance();
209 let rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" };
211 await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
215 test('Test add dictionary row with errors illegal chars in name', async () => {
217 const component = shallow(<ManageDictionaries />)
218 const instance = component.instance();
219 let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
221 await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
225 test('Test update dictionary row with errors illegal chars in name', async () => {
227 const component = shallow(<ManageDictionaries />)
228 const instance = component.instance();
229 let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
231 await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined);
235 test('Test add dictionary row with errors (illegal chars)', async () => {
237 const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest');
238 const component = shallow(<ManageDictionaries />)
239 const instance = component.instance();
240 let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
242 await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
247 test('Test delete dictionary row', async () => {
249 const deleteRequest = jest.spyOn(ManageDictionaries.prototype,'deleteDictionaryRequest');
250 const component = shallow(<ManageDictionaries />)
251 const instance = component.instance();
252 const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
254 await instance.deleteDictionaryRow(rowData);
255 expect(deleteRequest).toHaveBeenCalledWith("newdict");
259 test('Test handle select dictionary row click', async () => {
261 const component = shallow(<ManageDictionaries />)
262 const instance = component.instance();
263 const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
265 instance.handleDictionaryRowClick("event", rowData);
266 expect(component.state('currentSelectedDictionary')).toEqual("newdict");
269 test('Test dictionary element row add, update, delete', async () => {
273 createdDate: "2020-06-15T13:59:20.467381Z",
274 description: "Description",
276 shortName: "someElem",
279 updatedDate: "2020-06-15T13:59:20.467381Z"
282 const component = shallow(<ManageDictionaries/>)
283 const instance = component.instance();
286 description: "Description",
288 shortName: "someElem",
292 await instance.clickHandler();
293 await instance.getDictionaryElements("test");
295 await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined);
296 await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined);
297 await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined);
300 test('Test dictionary element row add with errors', async () => {
305 shortName: "some#Elem",
309 const component = shallow(<ManageDictionaries/>)
310 const instance = component.instance();
312 await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
315 test('Test dictionary element update with error illegal name', async () => {
320 shortName: "some#Elem",
324 const component = shallow(<ManageDictionaries/>)
325 const instance = component.instance();
327 await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined);
330 test('Test dictionary element addition with duplicate name error', async () => {
333 description: "description",
335 shortName: "alertType",
339 const component = shallow(<ManageDictionaries/>)
340 const instance = component.instance();
342 component.setState({ currentSelectedDictionary: 'test' });
344 await instance.getDictionaryElements();
345 await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
348 test('Test dictionary element addition with empty name error', async () => {
351 description: "description",
357 const component = shallow(<ManageDictionaries/>)
358 const instance = component.instance();
360 component.setState({ currentSelectedDictionary: 'test' });
362 await instance.getDictionaryElements();
363 await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
367 it('Test Import CSV Sunny Day', async () => {
369 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
370 return Promise.resolve({ ok: true, status: 200 });
373 let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
374 rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"';
376 let expectedResult = [
378 description: "Alert Type Description",
380 shortName: "alertType",
386 const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest');
388 const component = shallow(<ManageDictionaries />)
389 const instance = component.instance();
391 await expect(instance.importCsvData(rawCsvData)).toEqual('');
392 expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult);
395 it('Test Import CSV Mandatory Field Check Errors', () => {
397 let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
398 rawCsvData += '"","","","","","",""';
400 // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors.
401 // Getting Enzyme to properly match them with embedded newlines turned out to be impossible
402 // and maybe not desirable anyway; so our test for "success" here is simply that the
403 // routine returns a non-empty error string.
405 const component = shallow(<ManageDictionaries />)
406 const instance = component.instance();
407 expect(instance.importCsvData(rawCsvData)).not.toEqual('');
410 it('Test Import CSV Errors in Row Data', async () => {
412 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
413 return Promise.resolve({ ok: true, status: 200 });
416 let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
417 rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"';
419 let expectedResult = [
421 description: "Alert Type Description",
423 shortName: "alertType",
429 const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest');
431 const component = shallow(<ManageDictionaries />)
432 const instance = component.instance();
434 await expect(instance.importCsvData(rawCsvData)).not.toEqual('');
438 it('Test handleClose', () => {
439 fetch.mockImplementationOnce(() => {
440 return Promise.resolve({
444 return Promise.resolve({
446 "secondLevelDictionary": 1,
447 "subDictionaryType": "string",
449 "updatedDate": "05-07-2019 19:09:42"
454 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
455 const component = shallow(<ManageDictionaries history={historyMock} />)
456 component.find('[variant="secondary"]').prop('onClick')();
457 expect(handleClose).toHaveBeenCalledTimes(1);
458 expect(component.state('show')).toEqual(false);
459 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
460 handleClose.mockClear();