drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 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", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 63 | "secondLevelDictionary": "1", |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 64 | "subDictionaryType": "string", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 65 | "updatedBy": "test", |
| 66 | "updatedDate": "05-07-2019 19:09:42" |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 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 => { |
JulienBe | 0cde2b5 | 2020-04-03 13:21:13 +0200 | [diff] [blame] | 77 | console.info(e); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 78 | }), |
| 79 | 100 |
| 80 | ); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 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", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 93 | "secondLevelDictionary": "1", |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 94 | "subDictionaryType": "string", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 95 | "updatedBy": "test", |
| 96 | "updatedDate": "05-07-2019 19:09:42" |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 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 () => { |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 106 | 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 | ]; |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 127 | const historyMock = { push: jest.fn() }; |
| 128 | TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 129 | return Promise.resolve(dictionaries); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 130 | }); |
| 131 | TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 132 | return Promise.resolve(dictionaries[0]); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 133 | }); |
| 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} />) |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 142 | const instance = component.instance(); |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 143 | instance.getDictionaryElements("DefaultActors"); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 144 | instance.clickHandler(); |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 145 | instance.addReplaceDictionaryRequest(); |
| 146 | instance.deleteDictionaryRequest(); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 147 | await flushPromises(); |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 148 | expect(component.state('dictionaries')).toEqual(dictionaries); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 149 | }); |
| 150 | |
| 151 | test('Test adding and deleting dictionaryelements', async () => { |
| 152 | const historyMock = { push: jest.fn() }; |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 153 | 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 | ]; |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 174 | TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 175 | return Promise.resolve(dictionaries); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 176 | }); |
| 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}/>) |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 185 | const instance = component.instance(); |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 186 | instance.addReplaceDictionaryRequest({ name: "EventDictionary", secondLevelDictionary: "0", subDictionaryType: "string"} ); |
| 187 | instance.deleteDictionaryRequest('EventDictionary'); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 188 | await flushPromises(); |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 189 | expect(component.state('currentSelectedDictionary')).toEqual(null); |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 190 | }); |
| 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 Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 200 | "secondLevelDictionary": "1", |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 201 | "subDictionaryType": "string", |
Ted Humphrey | b56cb11 | 2020-06-12 03:17:35 -0400 | [diff] [blame^] | 202 | "updatedBy": "test", |
| 203 | "updatedDate": "05-07-2019 19:09:42" |
drveerendra | 5032095 | 2020-03-04 20:30:44 -0500 | [diff] [blame] | 204 | }); |
| 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 | }); |