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.getTemplateNames = jest.fn().mockImplementation(() => {
34 return Promise.resolve(["template1","template2"]);
37 const component = shallow(<CreateLoopModal/>);
38 expect(component).toMatchSnapshot();
39 await flushPromises();
42 expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1"}, {"label": "template2", "value": "template2"}]);
45 it('handleDropdownListChange event', async () => {
46 const flushPromises = () => new Promise(setImmediate);
47 const event = {value: 'template1'};
48 TemplateService.getBlueprintMicroServiceTemplateSvg = jest.fn().mockImplementation(() => {
49 return Promise.resolve("");
52 const component = shallow(<CreateLoopModal/>);
53 component.find('StateManager').simulate('change', event);
54 await flushPromises();
56 expect(component.state('chosenTemplateName')).toEqual("template1");
57 expect(component.state('content')).toEqual("Error1");
59 TemplateService.getBlueprintMicroServiceTemplateSvg = jest.fn().mockImplementation(() => {
60 return Promise.resolve("svgContentTest");
62 component.find('StateManager').simulate('change', {value: 'template2'});
63 await flushPromises();
65 expect(component.state('chosenTemplateName')).toEqual("template2");
66 expect(component.state('content')).toEqual("svgContentTest");
71 it('handleModelName event', () => {
72 const event = {target: {value : "model1"} };
73 const component = shallow(<CreateLoopModal/>);
74 component.find('input').simulate('change', event);
76 expect(component.state('modelName')).toEqual("model1");
80 it('Test handleClose', () => {
81 const historyMock = { push: jest.fn() };
82 const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
83 const component = shallow(<CreateLoopModal history={historyMock} />)
85 component.find('[variant="secondary"]').prop('onClick')();
87 expect(handleClose).toHaveBeenCalledTimes(1);
88 expect(component.state('show')).toEqual(false);
89 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
91 handleClose.mockClear();
94 it('Test handleCreate Fail', () => {
95 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
96 const component = shallow(<CreateLoopModal/>)
98 component.find('[variant="primary"]').prop('onClick')();
100 expect(handleCreate).toHaveBeenCalledTimes(1);
101 expect(component.state('show')).toEqual(true);
103 handleCreate.mockClear();
106 it('Test handleCreate Suc', async () => {
107 const flushPromises = () => new Promise(setImmediate);
108 const historyMock = { push: jest.fn() };
109 const loadLoopFunction = jest.fn();
111 LoopService.createLoop = jest.fn().mockImplementation(() => {
112 return Promise.resolve({
119 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
120 const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
122 modelName: "modelNameTest",
123 chosenTemplateName: "template1"
126 component.find('[variant="primary"]').prop('onClick')();
127 await flushPromises();
130 expect(handleCreate).toHaveBeenCalledTimes(1);
131 expect(component.state('show')).toEqual(false);
132 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
134 handleCreate.mockClear();