xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 1 | /*- |
| 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 | */ |
| 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'; |
| 28 | |
sebdet | 44fac5a | 2021-04-28 11:12:45 +0200 | [diff] [blame^] | 29 | let errorMessage = ''; |
| 30 | window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return }); |
| 31 | |
| 32 | |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 33 | describe('Verify CreateLoopModal', () => { |
| 34 | |
| 35 | it('Test the render method', async () => { |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 36 | const flushPromises = () => new Promise(setImmediate); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 37 | TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => { |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 38 | 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/>); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 45 | expect(component).toMatchSnapshot(); |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 46 | await flushPromises(); |
| 47 | component.update(); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 48 | expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | it('handleDropdownListChange event', async () => { |
| 52 | const flushPromises = () => new Promise(setImmediate); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 53 | |
| 54 | const component = shallow(<CreateLoopModal/>); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 55 | component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} }); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 56 | await flushPromises(); |
| 57 | component.update(); |
| 58 | expect(component.state('chosenTemplateName')).toEqual("template1"); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 59 | 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"} }); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 63 | await flushPromises(); |
| 64 | component.update(); |
| 65 | expect(component.state('chosenTemplateName')).toEqual("template2"); |
sebdet | c0ec0fc | 2020-05-18 12:31:11 +0200 | [diff] [blame] | 66 | expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template2"); |
| 67 | expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop"); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 68 | }); |
| 69 | |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 70 | 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 | }); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 78 | const event = {target: {value : "model1"} }; |
| 79 | const component = shallow(<CreateLoopModal/>); |
Ted Humphrey | 083e5a2 | 2020-07-08 16:48:40 -0400 | [diff] [blame] | 80 | await flushPromises(); |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 81 | component.find('input').simulate('change', event); |
| 82 | component.update(); |
| 83 | expect(component.state('modelName')).toEqual("model1"); |
| 84 | }); |
| 85 | |
xuegao | 28aa29f | 2020-05-04 15:33:06 +0200 | [diff] [blame] | 86 | 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 | }); |