Add many jests tests and implement block on element type and
[clamp.git] / ui-react / src / components / dialogs / ManageDictionaries / ManageDictionaries.test.js
index d1d4aa6..a4c1335 100644 (file)
@@ -27,7 +27,94 @@ import { render } from 'enzyme';
 import ManageDictionaries from './ManageDictionaries';
 import TemplateMenuService from '../../../api/TemplateService'
 
+const TestDictionaryElements = {
+    name: "test",
+    secondLevelDictionary: 0,
+    subDictionaryType: "", 
+    dictionaryElements: [
+      {
+        shortName: "alertType",
+        name: "Alert Type",
+        description: "Type of Alert",
+        type: "string",
+        subDictionary: "", 
+        createdDate: "2020-06-12T13:58:51.443931Z",
+        updatedDate: "2020-06-13T16:27:57.084870Z",
+        updatedBy: "admin",
+        createdBy: "admin"
+      }
+    ]
+};
+
+const TestDictionaries =
+[
+  {
+    name: "test",
+    secondLevelDictionary: 0,
+    subDictionaryType: "string",
+    dictionaryElements: [ TestDictionaryElements ],
+    createdDate: "2020-06-14T21:00:33.231166Z",
+    updatedDate: "2020-06-14T21:00:33.231166Z",
+    updatedBy: "admin",
+    createdBy: "admin"
+  },
+  {
+    name: "testSub1",
+    secondLevelDictionary: 1,
+    subDictionaryType: "string",
+    dictionaryElements: [
+      {
+        shortName: "subElem",
+        name: "Sub Element",
+        description: "Sub Element Description",
+        type: "string",
+        createdDate: "2020-06-14T21:04:44.402287Z",
+        updatedDate: "2020-06-14T21:04:44.402287Z",
+        updatedBy: "admin",
+        createdBy: "admin"
+      }
+    ],
+    createdDate: "2020-06-14T21:01:16.390250Z",
+    updatedDate: "2020-06-14T21:01:16.390250Z",
+    updatedBy: "admin",
+    createdBy: "admin"
+  }
+];
+
+
+const historyMock = { push: jest.fn() };
+
+let errorMessage = '';
+
+window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return });
+
+TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
+       return Promise.resolve(TestDictionaries);
+});
+
+TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
+       return Promise.resolve({ ok: true, status: 200 });
+});
+
+TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
+       return Promise.resolve("200");
+});
+
+TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
+       return Promise.resolve(TestDictionaryElements);
+});
+
+TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
+       return Promise.resolve("200");
+});
+
+TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
+       return Promise.resolve("200");
+});
+
+
 describe('Verify ManageDictionaries', () => {
+
        beforeEach(() => {
                fetch.resetMocks();
        });
@@ -40,7 +127,7 @@ describe('Verify ManageDictionaries', () => {
                                json: () => {
                                        return Promise.resolve({
                                                "name": "vtest",
-                                               "secondLevelDictionary": "1",
+                                               "secondLevelDictionary": 1,
                                                "subDictionaryType": "string",
                                                "updatedBy": "test",
                                                "updatedDate": "05-07-2019 19:09:42"
@@ -60,7 +147,7 @@ describe('Verify ManageDictionaries', () => {
                                json: () => {
                                        return Promise.resolve({
                                                "name": "vtest",
-                                               "secondLevelDictionary": "1",
+                                               "secondLevelDictionary": 1,
                                                "subDictionaryType": "string",
                                                "updatedBy": "test",
                                                "updatedDate": "05-07-2019 19:09:42"
@@ -71,124 +158,283 @@ describe('Verify ManageDictionaries', () => {
                const component = shallow(<ManageDictionaries />);
        });
 
-       it('Test API Rejection', () => {
-               const myMockFunc  = fetch.mockImplementationOnce(() => Promise.reject('error'));
-               setTimeout( () => myMockFunc().catch(e => {
-                       console.info(e);
-               }),
-               100
-               );
-               const component = shallow(<ManageDictionaries />);
-               expect(myMockFunc.mock.calls.length).toBe(1);
-       });
-
        it('Test Table icons', () => {
-               fetch.mockImplementationOnce(() => {
-                       return Promise.resolve({
-                               ok: true,
-                               status: 200,
-                               json: () => {
-                                       return Promise.resolve({
-                                               "name": "vtest",
-                                               "secondLevelDictionary": "1",
-                                               "subDictionaryType": "string",
-                                               "updatedBy": "test",
-                                               "updatedDate": "05-07-2019 19:09:42"
-                                       });
-                               }
-                       });
-               });
+
                const component = mount(<ManageDictionaries />);
                expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
        });
 
-       test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => {
-               const dictionaries = [
-                       {
-                               name: "DefaultActors",
-                               secondLevelDictionary: 0,
-                               subDictionaryType: "",
-                               dictionaryElements: [
-                                       {
-                                               "shortName": "SDNR",
-                                               "name": "SDNR Change",
-                                               "description": "SDNR component",
-                                               "type": "string",
-                                               "createdDate": "2020-06-07T18:57:18.130858Z",
-                                               "updatedDate": "2020-06-11T13:10:52.239282Z",
-                                               "updatedBy": "admin"
-                                       }
-                               ],
-                               createdDate: "2020-06-07T22:21:08.428742Z",
-                               updatedDate: "2020-06-10T00:41:49.122908Z",
-                               updatedBy: "Not found"
-                       }
-               ];
-               const historyMock = { push: jest.fn() };
-               TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(dictionaries);
-               });
-               TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(dictionaries[0]);
-               });
-               TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(200);
-               });
-               TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(200);
-               });
-               const flushPromises = () => new Promise(setImmediate);
-               const component = shallow(<ManageDictionaries history={historyMock} />)
+       test('Test add/replace and delete dictionary requests', async () => {
+
+               const component = shallow(<ManageDictionaries history={historyMock}/>)
                const instance = component.instance();
-               instance.getDictionaryElements("DefaultActors");
-               instance.clickHandler();
-               instance.addReplaceDictionaryRequest();
-               instance.deleteDictionaryRequest();
+
+               const flushPromises = () => new Promise(setImmediate);
+
+               instance.addReplaceDictionaryRequest({name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string"});
+               instance.deleteDictionaryRequest("test");
+
                await flushPromises();
-               expect(component.state('dictionaries')).toEqual(dictionaries);
+
+               expect(component.state('currentSelectedDictionary')).toEqual(null);
+               expect(component.state('dictionaries')).toEqual(TestDictionaries);
        });
 
-       test('Test adding and deleting dictionaryelements', async () => {
-               const historyMock = { push: jest.fn() };
-               const dictionaries = [
-                       {
-                               name: "DefaultActors",
-                               secondLevelDictionary: 0,
-                               subDictionaryType: "",
-                               dictionaryElements: [
-                                       {
-                                               "shortName": "SDNR",
-                                               "name": "SDNR Change",
-                                               "description": "SDNR component",
-                                               "type": "string",
-                                               "createdDate": "2020-06-07T18:57:18.130858Z",
-                                               "updatedDate": "2020-06-11T13:10:52.239282Z",
-                                               "updatedBy": "admin"
-                                       }
-                               ],
-                               createdDate: "2020-06-07T22:21:08.428742Z",
-                               updatedDate: "2020-06-10T00:41:49.122908Z",
-                               updatedBy: "Not found"
-                       }
-               ];
-               TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(dictionaries);
-               });
-               TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(200);
-               });
-               TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
-                       return Promise.resolve(200);
-               });
-               const flushPromises = () => new Promise(setImmediate);
+       test('Test update dictionary row', async () => {
+
                const component = shallow(<ManageDictionaries history={historyMock}/>)
                const instance = component.instance();
-               instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} );
-               instance.deleteDictionaryRequest('EventDictionary');
-               await flushPromises();
-               expect(component.state('currentSelectedDictionary')).toEqual(null);
+               const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
+
+               await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined);
+
+       }, 2000);
+
+       test('Test add dictionary row', async () => {
+
+               const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest');
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
+
+               await instance.addDictionaryRow(rowData);
+                expect(addReplaceRequest).toHaveBeenCalledWith(rowData);
+
+       }, 2000);
+
+       test('Test add dictionary row with errors name already exists', async () => {
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               let  rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" };
+
+               await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
+
+       }, 2000);
+
+       test('Test add dictionary row with errors illegal chars in name', async () => {
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               let  rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
+
+               await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
+
+       }, 2000);
+
+       test('Test update dictionary row with errors illegal chars in name', async () => {
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               let  rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
+
+               await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined);
+       });
+
+
+       test('Test add dictionary row with errors (illegal chars)', async () => {
+
+               const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest');
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               let  rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" };
+
+               await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined);
+
+       }, 2000);
+
+
+       test('Test delete dictionary row', async () => {
+
+               const deleteRequest = jest.spyOn(ManageDictionaries.prototype,'deleteDictionaryRequest');
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
+
+               await instance.deleteDictionaryRow(rowData);
+                expect(deleteRequest).toHaveBeenCalledWith("newdict");
+
+       }, 2000);
+
+       test('Test handle select dictionary row click', async () => {
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" };
+
+               instance.handleDictionaryRowClick("event", rowData);
+               expect(component.state('currentSelectedDictionary')).toEqual("newdict");
+       }, 2000);
+
+       test('Test dictionary element row add, update, delete', async () => {
+
+               const rowData = {
+                       createdBy: "admin",
+                       createdDate: "2020-06-15T13:59:20.467381Z",
+                       description: "Description",
+                       name: "Some Elem",
+                       shortName: "someElem",
+                       type: "string",
+                       updatedBy: "admin",
+                       updatedDate: "2020-06-15T13:59:20.467381Z"
+               };
+
+               const component = shallow(<ManageDictionaries/>)
+               const instance = component.instance();
+
+               const badRowData = {
+                       description: "Description",
+                       name: "Some Elem",
+                       shortName: "someElem",
+                       type: "string"
+               };
+
+                await instance.clickHandler();
+                await instance.getDictionaryElements("test");
+
+               await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined);
+               await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined);
+               await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined);
+       });
+
+       test('Test dictionary element row add with errors', async () => {
+
+               const badRowData = {
+                       description: "",
+                       name: "",
+                       shortName: "some#Elem",
+                       type: ""
+               };
+
+               const component = shallow(<ManageDictionaries/>)
+               const instance = component.instance();
+
+               await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
+       });
+
+       test('Test dictionary element update with error illegal name', async () => {
+
+               const badRowData = {
+                       description: "",
+                       name: "test@@",
+                       shortName: "some#Elem",
+                       type: ""
+               };
+
+               const component = shallow(<ManageDictionaries/>)
+               const instance = component.instance();
+
+               await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined);
+       });
+
+       test('Test dictionary element addition with duplicate name error', async () => {
+
+               const badRowData = {
+                       description: "description",
+                       name: "Alert Type",
+                       shortName: "alertType",
+                       type: "string"
+               };
+
+               const component = shallow(<ManageDictionaries/>)
+               const instance = component.instance();
+
+               component.setState({ currentSelectedDictionary: 'test' });
+
+               await instance.getDictionaryElements();
+               await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
+       });
+
+       test('Test dictionary element addition with empty name error', async () => {
+
+               const badRowData = {
+                       description: "description",
+                       name: "Alert Type",
+                       shortName: "",
+                       type: "string"
+               };
+
+               const component = shallow(<ManageDictionaries/>)
+               const instance = component.instance();
+
+               component.setState({ currentSelectedDictionary: 'test' });
+
+               await instance.getDictionaryElements();
+               await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual("");
+       });
+
+
+       it('Test Import CSV Sunny Day', async () => {
+
+                TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
+                        return Promise.resolve({ ok: true, status: 200 });
+                });
+
+                let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
+                rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"';
+
+                let expectedResult = [
+                                {
+                                        description: "Alert Type Description",
+                                        name: "Alert Type",
+                                        shortName: "alertType",
+                                        subDictionary: "",
+                                        type: "string"
+                                }
+               ];
+
+               const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest');
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+
+               await expect(instance.importCsvData(rawCsvData)).toEqual('');
+                expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult);
+       });
+
+       it('Test Import CSV Mandatory Field Check Errors', () => {
+
+                let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
+                rawCsvData += '"","","","","","",""';
+
+               // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors.
+               // Getting Enzyme to properly match them with embedded newlines turned out to be impossible
+               // and maybe not desirable anyway; so our test for "success" here is simply that the
+               // routine returns a non-empty error string.
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+               expect(instance.importCsvData(rawCsvData)).not.toEqual('');
+       });
+
+       it('Test Import CSV Errors in Row Data', async () => {
+
+                TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
+                        return Promise.resolve({ ok: true, status: 200 });
+                });
+
+                let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
+                rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"';
+
+                let expectedResult = [
+                                {
+                                        description: "Alert Type Description",
+                                        name: "Alert Type",
+                                        shortName: "alertType",
+                                        subDictionary: "",
+                                        type: "string"
+                                }
+               ];
+
+               const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest');
+
+               const component = shallow(<ManageDictionaries />)
+               const instance = component.instance();
+
+               await expect(instance.importCsvData(rawCsvData)).not.toEqual('');
        });
 
+
        it('Test handleClose', () => {
                fetch.mockImplementationOnce(() => {
                        return Promise.resolve({
@@ -197,7 +443,7 @@ describe('Verify ManageDictionaries', () => {
                                json: () => {
                                        return Promise.resolve({
                                                "name": "vtest",
-                                               "secondLevelDictionary": "1",
+                                               "secondLevelDictionary": 1,
                                                "subDictionaryType": "string",
                                                "updatedBy": "test",
                                                "updatedDate": "05-07-2019 19:09:42"
@@ -205,7 +451,6 @@ describe('Verify ManageDictionaries', () => {
                                }
                        });
                });
-               const historyMock = { push: jest.fn() };
                const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
                const component = shallow(<ManageDictionaries history={historyMock} />)
                component.find('[variant="secondary"]').prop('onClick')();