Added jest/enzyme tests for new components
[policy/gui.git] / gui-clamp / ui-react / src / components / dialogs / ReadAndConvertYaml.test.js
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2021 Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22 import React from 'react';
23 import { mount, shallow } from 'enzyme';
24 import ReadAndConvertYaml from './ReadAndConvertYaml';
25 import toJson from "enzyme-to-json";
26 import { act } from "react-dom/test-utils";
27 import { createMemoryHistory } from "history";
28
29 describe('Verify ReadAndConvertYaml', () => {
30
31   beforeEach(() => {
32     fetch.resetMocks();
33     fetch.mockImplementation(() => {
34       return Promise.resolve({
35         ok: true,
36         status: 200,
37         json: () => {
38           return Promise.resolve({
39             "tosca_definitions_version": "tosca_simple_yaml_1_1_0",
40             "data_types": {},
41             "policy_types": {},
42             "topology_template": {},
43             "name": "ToscaServiceTemplateSimple",
44             "version": "1.0.0",
45             "metadata": {},
46             "id": "0.19518677404255147"
47           });
48         }
49       });
50     });
51   })
52
53   it("renders without crashing", () => {
54     shallow(<ReadAndConvertYaml/>);
55   });
56
57   it("renders correctly", () => {
58     const tree = shallow(<ReadAndConvertYaml/>);
59     expect(toJson(tree)).toMatchSnapshot();
60   });
61
62   it('should have a GetToscaTemplate element', () => {
63     const container = shallow(<ReadAndConvertYaml/>)
64     expect(container.find('GetToscaTemplate').length).toEqual(1);
65   });
66
67   it('should call getToscaServiceTemplateHandler on click', async () => {
68     const component = mount(<ReadAndConvertYaml/>);
69     const logSpy = jest.spyOn(console, 'log');
70
71     act(async () => {
72       component.find('GetToscaTemplate').simulate('click');
73       expect(logSpy).toHaveBeenCalledWith('getToscaServiceTemplateHandler called');
74     });
75   });
76
77   it('handleClose called when bottom button clicked', () => {
78     const history = createMemoryHistory();
79     const component = mount(<ReadAndConvertYaml history={ history }/>)
80     const logSpy = jest.spyOn(console, 'log');
81
82
83     act(() => {
84       component.find('[variant="secondary"]').simulate('click');
85       expect(logSpy).toHaveBeenCalledWith('handleClose called');
86     });
87   });
88
89   it('handleClose called when top-right button clicked', () => {
90     const history = createMemoryHistory();
91     const component = mount(<ReadAndConvertYaml history={ history }/>)
92     const logSpy = jest.spyOn(console, 'log');
93
94
95     act(() => {
96       component.find('[size="xl"]').get(0).props.onHide();
97       expect(logSpy).toHaveBeenCalledWith('handleClose called');
98     });
99   });
100
101 });