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 describe('Verify ManageDictionaries', () => {
35 it('Test API Successful', () => {
36 fetch.mockImplementationOnce(() => {
37 return Promise.resolve({
41 return Promise.resolve({
43 "secondLevelDictionary": "1",
44 "subDictionaryType": "string",
46 "updatedDate": "05-07-2019 19:09:42"
51 const component = shallow(<ManageDictionaries />);
52 expect(component).toMatchSnapshot();
55 it('Test API Exception', () => {
56 fetch.mockImplementationOnce(() => {
57 return Promise.resolve({
61 return Promise.resolve({
63 "secondLevelDictionary": "1",
64 "subDictionaryType": "string",
66 "updatedDate": "05-07-2019 19:09:42"
71 const component = shallow(<ManageDictionaries />);
74 it('Test API Rejection', () => {
75 const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error'));
76 setTimeout( () => myMockFunc().catch(e => {
81 const component = shallow(<ManageDictionaries />);
82 expect(myMockFunc.mock.calls.length).toBe(1);
85 it('Test Table icons', () => {
86 fetch.mockImplementationOnce(() => {
87 return Promise.resolve({
91 return Promise.resolve({
93 "secondLevelDictionary": "1",
94 "subDictionaryType": "string",
96 "updatedDate": "05-07-2019 19:09:42"
101 const component = mount(<ManageDictionaries />);
102 expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
105 test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => {
106 const historyMock = { push: jest.fn() };
107 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
108 return Promise.resolve("test");
110 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
111 return Promise.resolve({dictionaryElements:"testitem"});
113 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
114 return Promise.resolve(200);
116 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
117 return Promise.resolve(200);
119 const flushPromises = () => new Promise(setImmediate);
120 const component = shallow(<ManageDictionaries history={historyMock} />)
121 component.setState({ newDict: {
123 "secondLevelDictionary": "0",
124 "subDictionaryType": "string"
127 component.setState({ delData: {
129 "secondLevelDictionary": "0",
130 "subDictionaryType": "string"
133 const instance = component.instance();
134 instance.getDictionaryElements("test");
135 instance.clickHandler();
136 instance.addDictionary();
137 instance.deleteDictionary();
138 await flushPromises();
139 expect(component.state('dictionaryNames')).toEqual("test");
140 expect(component.state('dictionaryElements')).toEqual("testitem");
141 expect(component.state('dictNameFlag')).toEqual(false);
144 test('Test adding and deleting dictionaryelements', async () => {
145 const historyMock = { push: jest.fn() };
146 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
147 return Promise.resolve("test");
149 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
150 return Promise.resolve(200);
152 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
153 return Promise.resolve(200);
155 const flushPromises = () => new Promise(setImmediate);
156 const component = shallow(<ManageDictionaries history={historyMock}/>)
157 component.setState({ newDictItem: {
159 "dictionaryElements" : {
160 "shortName": "shorttest",
163 component.setState({ delDictItem: {
165 "dictionaryElements" : {
166 "shortName": "shortTest",
169 const instance = component.instance();
170 instance.addDictionary();
171 instance.deleteDictionary();
172 await flushPromises();
173 expect(component.state('dictionaryNames')).toEqual("test");
176 it('Test handleClose', () => {
177 fetch.mockImplementationOnce(() => {
178 return Promise.resolve({
182 return Promise.resolve({
184 "secondLevelDictionary": "1",
185 "subDictionaryType": "string",
187 "updatedDate": "05-07-2019 19:09:42"
192 const historyMock = { push: jest.fn() };
193 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
194 const component = shallow(<ManageDictionaries history={historyMock} />)
195 component.find('[variant="secondary"]').prop('onClick')();
196 expect(handleClose).toHaveBeenCalledTimes(1);
197 expect(component.state('show')).toEqual(false);
198 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
199 handleClose.mockClear();