2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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 CreateLoopModal from './CreateLoopModal';
26 import LoopService from '../../../api/LoopService';
27 import TemplateService from '../../../api/TemplateService';
29 describe('Verify CreateLoopModal', () => {
31 it('Test the render method', async () => {
32 const flushPromises = () => new Promise(setImmediate);
33 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
34 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
36 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
37 return Promise.resolve([]);
40 const component = shallow(<CreateLoopModal/>);
41 expect(component).toMatchSnapshot();
42 await flushPromises();
44 expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]);
47 it('handleDropdownListChange event', async () => {
48 const flushPromises = () => new Promise(setImmediate);
50 const component = shallow(<CreateLoopModal/>);
51 component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} });
52 await flushPromises();
54 expect(component.state('chosenTemplateName')).toEqual("template1");
55 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1");
56 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
58 component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} });
59 await flushPromises();
61 expect(component.state('chosenTemplateName')).toEqual("template2");
62 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2");
63 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
66 it('handleModelName event', async () => {
67 const flushPromises = () => new Promise(setImmediate);
68 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
69 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
71 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
72 return Promise.resolve([]);
74 const event = {target: {value : "model1"} };
75 const component = shallow(<CreateLoopModal/>);
76 await flushPromises();
77 component.find('input').simulate('change', event);
79 expect(component.state('modelName')).toEqual("model1");
82 it('Test handleClose', () => {
83 const historyMock = { push: jest.fn() };
84 const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
85 const component = shallow(<CreateLoopModal history={historyMock} />)
87 component.find('[variant="secondary"]').prop('onClick')();
89 expect(handleClose).toHaveBeenCalledTimes(1);
90 expect(component.state('show')).toEqual(false);
91 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
93 handleClose.mockClear();
96 it('Test handleCreate Fail', () => {
97 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
98 const component = shallow(<CreateLoopModal/>)
100 component.find('[variant="primary"]').prop('onClick')();
102 expect(handleCreate).toHaveBeenCalledTimes(1);
103 expect(component.state('show')).toEqual(true);
105 handleCreate.mockClear();
108 it('Test handleCreate Suc', async () => {
109 const flushPromises = () => new Promise(setImmediate);
110 const historyMock = { push: jest.fn() };
111 const loadLoopFunction = jest.fn();
113 LoopService.createLoop = jest.fn().mockImplementation(() => {
114 return Promise.resolve({
121 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
122 const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
124 modelName: "modelNameTest",
125 chosenTemplateName: "template1"
128 component.find('[variant="primary"]').prop('onClick')();
129 await flushPromises();
132 expect(handleCreate).toHaveBeenCalledTimes(1);
133 expect(component.state('show')).toEqual(false);
134 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
136 handleCreate.mockClear();