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"}]);
37 const component = shallow(<CreateLoopModal/>);
38 expect(component).toMatchSnapshot();
39 await flushPromises();
42 expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]);
45 it('handleDropdownListChange event', async () => {
46 const flushPromises = () => new Promise(setImmediate);
48 const component = shallow(<CreateLoopModal/>);
49 component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} });
50 await flushPromises();
52 expect(component.state('chosenTemplateName')).toEqual("template1");
53 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1");
54 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
56 component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} });
57 await flushPromises();
59 expect(component.state('chosenTemplateName')).toEqual("template2");
60 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2");
61 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
66 it('handleModelName event', () => {
67 const event = {target: {value : "model1"} };
68 const component = shallow(<CreateLoopModal/>);
69 component.find('input').simulate('change', event);
71 expect(component.state('modelName')).toEqual("model1");
75 it('Test handleClose', () => {
76 const historyMock = { push: jest.fn() };
77 const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
78 const component = shallow(<CreateLoopModal history={historyMock} />)
80 component.find('[variant="secondary"]').prop('onClick')();
82 expect(handleClose).toHaveBeenCalledTimes(1);
83 expect(component.state('show')).toEqual(false);
84 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
86 handleClose.mockClear();
89 it('Test handleCreate Fail', () => {
90 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
91 const component = shallow(<CreateLoopModal/>)
93 component.find('[variant="primary"]').prop('onClick')();
95 expect(handleCreate).toHaveBeenCalledTimes(1);
96 expect(component.state('show')).toEqual(true);
98 handleCreate.mockClear();
101 it('Test handleCreate Suc', async () => {
102 const flushPromises = () => new Promise(setImmediate);
103 const historyMock = { push: jest.fn() };
104 const loadLoopFunction = jest.fn();
106 LoopService.createLoop = jest.fn().mockImplementation(() => {
107 return Promise.resolve({
114 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
115 const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
117 modelName: "modelNameTest",
118 chosenTemplateName: "template1"
121 component.find('[variant="primary"]').prop('onClick')();
122 await flushPromises();
125 expect(handleCreate).toHaveBeenCalledTimes(1);
126 expect(component.state('show')).toEqual(false);
127 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
129 handleCreate.mockClear();