blob: 1caa22dc703d3ace0487b963f467ab39e38fa866 [file] [log] [blame]
xuegao28aa29f2020-05-04 15:33:06 +02001/*-
2 * ============LICENSE_START=======================================================
3 * ONAP CLAMP
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
6 * reserved.
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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 * ===================================================================
21 *
22 */
23import React from 'react';
24import { shallow } from 'enzyme';
25import CreateLoopModal from './CreateLoopModal';
26import LoopService from '../../../api/LoopService';
27import TemplateService from '../../../api/TemplateService';
28
29describe('Verify CreateLoopModal', () => {
30
31 it('Test the render method', async () => {
Ted Humphrey083e5a22020-07-08 16:48:40 -040032 const flushPromises = () => new Promise(setImmediate);
sebdetc0ec0fc2020-05-18 12:31:11 +020033 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
Ted Humphrey083e5a22020-07-08 16:48:40 -040034 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
35 });
36 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
37 return Promise.resolve([]);
38 });
39
40 const component = shallow(<CreateLoopModal/>);
xuegao28aa29f2020-05-04 15:33:06 +020041 expect(component).toMatchSnapshot();
Ted Humphrey083e5a22020-07-08 16:48:40 -040042 await flushPromises();
43 component.update();
sebdetc0ec0fc2020-05-18 12:31:11 +020044 expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]);
xuegao28aa29f2020-05-04 15:33:06 +020045 });
46
47 it('handleDropdownListChange event', async () => {
48 const flushPromises = () => new Promise(setImmediate);
xuegao28aa29f2020-05-04 15:33:06 +020049
50 const component = shallow(<CreateLoopModal/>);
sebdetc0ec0fc2020-05-18 12:31:11 +020051 component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} });
xuegao28aa29f2020-05-04 15:33:06 +020052 await flushPromises();
53 component.update();
54 expect(component.state('chosenTemplateName')).toEqual("template1");
sebdetc0ec0fc2020-05-18 12:31:11 +020055 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1");
56 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
57
58 component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} });
xuegao28aa29f2020-05-04 15:33:06 +020059 await flushPromises();
60 component.update();
61 expect(component.state('chosenTemplateName')).toEqual("template2");
sebdetc0ec0fc2020-05-18 12:31:11 +020062 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2");
63 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
xuegao28aa29f2020-05-04 15:33:06 +020064 });
65
Ted Humphrey083e5a22020-07-08 16:48:40 -040066 it('handleModelName event', async () => {
67 const flushPromises = () => new Promise(setImmediate);
68 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
69 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
70 });
71 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
72 return Promise.resolve([]);
73 });
xuegao28aa29f2020-05-04 15:33:06 +020074 const event = {target: {value : "model1"} };
75 const component = shallow(<CreateLoopModal/>);
Ted Humphrey083e5a22020-07-08 16:48:40 -040076 await flushPromises();
xuegao28aa29f2020-05-04 15:33:06 +020077 component.find('input').simulate('change', event);
78 component.update();
79 expect(component.state('modelName')).toEqual("model1");
80 });
81
xuegao28aa29f2020-05-04 15:33:06 +020082 it('Test handleClose', () => {
83 const historyMock = { push: jest.fn() };
84 const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
85 const component = shallow(<CreateLoopModal history={historyMock} />)
86
87 component.find('[variant="secondary"]').prop('onClick')();
88
89 expect(handleClose).toHaveBeenCalledTimes(1);
90 expect(component.state('show')).toEqual(false);
91 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
92
93 handleClose.mockClear();
94 });
95
96 it('Test handleCreate Fail', () => {
97 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
98 const component = shallow(<CreateLoopModal/>)
99
100 component.find('[variant="primary"]').prop('onClick')();
101
102 expect(handleCreate).toHaveBeenCalledTimes(1);
103 expect(component.state('show')).toEqual(true);
104
105 handleCreate.mockClear();
106 });
107
108 it('Test handleCreate Suc', async () => {
109 const flushPromises = () => new Promise(setImmediate);
110 const historyMock = { push: jest.fn() };
111 const loadLoopFunction = jest.fn();
112
113 LoopService.createLoop = jest.fn().mockImplementation(() => {
114 return Promise.resolve({
115 ok: true,
116 status: 200,
117 json: () => {}
118 });
119 });
120
121 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
122 const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
123 component.setState({
124 modelName: "modelNameTest",
125 chosenTemplateName: "template1"
126 });
127
128 component.find('[variant="primary"]').prop('onClick')();
129 await flushPromises();
130 component.update();
131
132 expect(handleCreate).toHaveBeenCalledTimes(1);
133 expect(component.state('show')).toEqual(false);
134 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
135
136 handleCreate.mockClear();
137 });
138
139});