blob: 8ef53b412827f76fbeba945f3671ec76eafe14eb [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
sebdet44fac5a2021-04-28 11:12:45 +020029let errorMessage = '';
30window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return });
31
32
xuegao28aa29f2020-05-04 15:33:06 +020033describe('Verify CreateLoopModal', () => {
34
35 it('Test the render method', async () => {
Ted Humphrey083e5a22020-07-08 16:48:40 -040036 const flushPromises = () => new Promise(setImmediate);
sebdetc0ec0fc2020-05-18 12:31:11 +020037 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
Ted Humphrey083e5a22020-07-08 16:48:40 -040038 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
39 });
40 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
41 return Promise.resolve([]);
42 });
43
44 const component = shallow(<CreateLoopModal/>);
xuegao28aa29f2020-05-04 15:33:06 +020045 expect(component).toMatchSnapshot();
Ted Humphrey083e5a22020-07-08 16:48:40 -040046 await flushPromises();
47 component.update();
sebdetc0ec0fc2020-05-18 12:31:11 +020048 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 +020049 });
50
51 it('handleDropdownListChange event', async () => {
52 const flushPromises = () => new Promise(setImmediate);
xuegao28aa29f2020-05-04 15:33:06 +020053
54 const component = shallow(<CreateLoopModal/>);
sebdetc0ec0fc2020-05-18 12:31:11 +020055 component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} });
xuegao28aa29f2020-05-04 15:33:06 +020056 await flushPromises();
57 component.update();
58 expect(component.state('chosenTemplateName')).toEqual("template1");
sebdetc0ec0fc2020-05-18 12:31:11 +020059 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1");
60 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
61
62 component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} });
xuegao28aa29f2020-05-04 15:33:06 +020063 await flushPromises();
64 component.update();
65 expect(component.state('chosenTemplateName')).toEqual("template2");
sebdetc0ec0fc2020-05-18 12:31:11 +020066 expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2");
67 expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop");
xuegao28aa29f2020-05-04 15:33:06 +020068 });
69
Ted Humphrey083e5a22020-07-08 16:48:40 -040070 it('handleModelName event', async () => {
71 const flushPromises = () => new Promise(setImmediate);
72 TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => {
73 return Promise.resolve([{"name":"template1"},{"name":"template2"}]);
74 });
75 TemplateService.getLoopNames = jest.fn().mockImplementation(() => {
76 return Promise.resolve([]);
77 });
xuegao28aa29f2020-05-04 15:33:06 +020078 const event = {target: {value : "model1"} };
79 const component = shallow(<CreateLoopModal/>);
Ted Humphrey083e5a22020-07-08 16:48:40 -040080 await flushPromises();
xuegao28aa29f2020-05-04 15:33:06 +020081 component.find('input').simulate('change', event);
82 component.update();
83 expect(component.state('modelName')).toEqual("model1");
84 });
85
xuegao28aa29f2020-05-04 15:33:06 +020086 it('Test handleClose', () => {
87 const historyMock = { push: jest.fn() };
88 const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose');
89 const component = shallow(<CreateLoopModal history={historyMock} />)
90
91 component.find('[variant="secondary"]').prop('onClick')();
92
93 expect(handleClose).toHaveBeenCalledTimes(1);
94 expect(component.state('show')).toEqual(false);
95 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
96
97 handleClose.mockClear();
98 });
99
100 it('Test handleCreate Fail', () => {
101 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
102 const component = shallow(<CreateLoopModal/>)
103
104 component.find('[variant="primary"]').prop('onClick')();
105
106 expect(handleCreate).toHaveBeenCalledTimes(1);
107 expect(component.state('show')).toEqual(true);
108
109 handleCreate.mockClear();
110 });
111
112 it('Test handleCreate Suc', async () => {
113 const flushPromises = () => new Promise(setImmediate);
114 const historyMock = { push: jest.fn() };
115 const loadLoopFunction = jest.fn();
116
117 LoopService.createLoop = jest.fn().mockImplementation(() => {
118 return Promise.resolve({
119 ok: true,
120 status: 200,
121 json: () => {}
122 });
123 });
124
125 const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate');
126 const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>)
127 component.setState({
128 modelName: "modelNameTest",
129 chosenTemplateName: "template1"
130 });
131
132 component.find('[variant="primary"]').prop('onClick')();
133 await flushPromises();
134 component.update();
135
136 expect(handleCreate).toHaveBeenCalledTimes(1);
137 expect(component.state('show')).toEqual(false);
138 expect(historyMock.push.mock.calls[0]).toEqual([ '/']);
139
140 handleCreate.mockClear();
141 });
142
143});