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 new Promise(resolve => setTimeout(resolve, 200));
82 const component = shallow(<ManageDictionaries />);
83 expect(myMockFunc.mock.calls.length).toBe(1);
86 it('Test Table icons', () => {
87 fetch.mockImplementationOnce(() => {
88 return Promise.resolve({
92 return Promise.resolve({
94 "secondLevelDictionary": "1",
95 "subDictionaryType": "string",
97 "updatedDate": "05-07-2019 19:09:42"
102 const component = mount(<ManageDictionaries />);
103 expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy();
106 test('Test get dictionaryNames/dictionaryElements, add/delete dictionary functions', async () => {
107 const historyMock = { push: jest.fn() };
108 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
109 return Promise.resolve("test");
111 TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => {
112 return Promise.resolve({dictionaryElements:"testitem"});
114 TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => {
115 return Promise.resolve(200);
117 TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => {
118 return Promise.resolve(200);
120 const flushPromises = () => new Promise(setImmediate);
121 const component = shallow(<ManageDictionaries history={historyMock} />)
122 component.setState({ newDict: {
124 "secondLevelDictionary": "0",
125 "subDictionaryType": "string"
128 component.setState({ delData: {
130 "secondLevelDictionary": "0",
131 "subDictionaryType": "string"
134 const instance = component.instance();
135 instance.getDictionaryElements("test");
136 instance.clickHandler();
137 instance.addDictionary();
138 instance.deleteDictionary();
139 await flushPromises();
140 expect(component.state('dictionaryNames')).toEqual("test");
141 expect(component.state('dictionaryElements')).toEqual("testitem");
142 expect(component.state('dictNameFlag')).toEqual(false);
145 test('Test adding and deleting dictionaryelements', async () => {
146 const historyMock = { push: jest.fn() };
147 TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => {
148 return Promise.resolve("test");
150 TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => {
151 return Promise.resolve(200);
153 TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => {
154 return Promise.resolve(200);
156 const flushPromises = () => new Promise(setImmediate);
157 const component = shallow(<ManageDictionaries history={historyMock}/>)
158 component.setState({ newDictItem: {
160 "dictionaryElements" : {
161 "shortName": "shorttest",
164 component.setState({ delDictItem: {
166 "dictionaryElements" : {
167 "shortName": "shortTest",
170 const instance = component.instance();
171 instance.addDictionary();
172 instance.deleteDictionary();
173 await flushPromises();
174 expect(component.state('dictionaryNames')).toEqual("test");
177 it('Test handleClose', () => {
178 fetch.mockImplementationOnce(() => {
179 return Promise.resolve({
183 return Promise.resolve({
185 "secondLevelDictionary": "1",
186 "subDictionaryType": "string",
188 "updatedDate": "05-07-2019 19:09:42"
193 const historyMock = { push: jest.fn() };
194 const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose');
195 const component = shallow(<ManageDictionaries history={historyMock} />)
196 component.find('[variant="secondary"]').prop('onClick')();
197 expect(handleClose).toHaveBeenCalledTimes(1);
198 expect(component.state('show')).toEqual(false);
199 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
200 handleClose.mockClear();