From 5d87357d69ec81962fd6128a8aa12086b97f199f Mon Sep 17 00:00:00 2001 From: brunomilitzer Date: Thu, 10 Mar 2022 11:59:39 +0000 Subject: Added Jest Tests for Overall Coverage Issue-ID: POLICY-3873 Change-Id: Ie3516ad0178b7960e3ecc380eb0ad3a48a78ce1f Signed-off-by: brunomilitzer --- .../dialogs/ACM/InstancePropertiesModal.js | 8 ++- .../dialogs/ACM/InstancePropertiesModal.test.js | 63 +++++++++++++++++++--- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.js b/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.js index 2dc2cb3..5181b64 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.js +++ b/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.js @@ -65,11 +65,9 @@ const InstancePropertiesModal = (props) => { const [instancePropertiesGlobal, setInstancePropertiesGlobal] = useState({}); const [serviceTemplateResponseOk, setServiceTemplateResponseOk] = useState(true); const [instancePropertiesResponseOk, setInstancePropertiesResponseOk] = useState(true); - const [instanceName, setInstanceName] = useState('') + const [instanceName, setInstanceName] = useState(''); useEffect(async () => { - setJsonEditor(null); - const toscaTemplateResponse = await ACMService.getToscaTemplate(templateName, templateVersion) .catch(error => error.message); @@ -111,8 +109,8 @@ const InstancePropertiesModal = (props) => { } const handleSave = async () => { - console.log("handleSave called"); - if (instanceName !== '' || instanceName.length > 0) { + if (instanceName !== '' || instanceName !== undefined || instanceName.length > 0) { + console.log("handleSave called"); console.log("instanceName to be saved is: " + instanceName); diff --git a/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.test.js b/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.test.js index 5c617bf..5b60bce 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.test.js +++ b/gui-clamp/ui-react/src/components/dialogs/ACM/InstancePropertiesModal.test.js @@ -17,14 +17,19 @@ * ============LICENSE_END========================================================= */ -import { mount, shallow } from "enzyme"; +import {shallow} from "enzyme"; import React from "react"; import InstancePropertiesModal from "./InstancePropertiesModal"; import toJson from "enzyme-to-json"; import { createMemoryHistory } from "history"; import { act } from "react-dom/test-utils"; +import ACMService from "../../../api/ACMService"; +import fullTemp from "./testFiles/fullTemplate.json"; +import instanceProps from "./testFiles/instanceProps.json"; -let logSpy = jest.spyOn(console, 'log') +const instanceProperties = JSON.parse(JSON.stringify(instanceProps)) +const fullTemplate = JSON.parse(JSON.stringify(fullTemp)) +let logSpy = jest.spyOn(console, 'log'); describe('Verify InstancePropertiesModal', () => { @@ -66,7 +71,10 @@ describe('Verify InstancePropertiesModal', () => { }); it('handleCreateUpdateToscaInstanceProperties called when save button clicked', () => { - const component = mount() + const component = shallow(); + + const instanceNameState = jest.spyOn(React, "useState"); + instanceNameState.mockImplementation(instanceName => [instanceName, 'Test']); act(() => { component.find('[variant="primary"]').simulate('click'); @@ -76,7 +84,7 @@ describe('Verify InstancePropertiesModal', () => { it('handleClose called when close button clicked', () => { const history = createMemoryHistory(); - const component = mount() + const component = shallow() act(() => { component.find('[variant="secondary"]').simulate('click'); @@ -85,7 +93,7 @@ describe('Verify InstancePropertiesModal', () => { }); it('handleSave called when save button clicked', () => { - const component = mount() + const component = shallow() act(() => { component.find('[variant="primary"]').simulate('click'); @@ -95,9 +103,50 @@ describe('Verify InstancePropertiesModal', () => { it('Check useEffect is being called', async () => { const useEffect = jest.spyOn(React, "useEffect"); - mount() - await act(async () => { + const component = shallow() + act(async () => { + expect(useEffect).toHaveBeenCalled(); + component.update() + }); + }); + + it('Check useEffect is being called', async () => { + const useEffect = jest.spyOn(React, "useEffect"); + const component = shallow() + act(async () => { expect(useEffect).toHaveBeenCalled(); + component.update(); }); }); + + it('getToscaTemplate gets called in useEffect with error', async() => { + const fetchMock = jest.spyOn(ACMService, 'getToscaTemplate').mockImplementation(() => Promise.resolve({ + ok: false, + status: 200, + text: () => "OK", + json: () => fullTemplate + })) + + const component = shallow() + act(async () => { + expect(fetchMock).toHaveBeenCalled(); + component.update(); + }); + }); + + it('getCommonOrInstanceProperties gets called in useEffect with error', async() => { + const fetchMock = jest.spyOn(ACMService, 'getCommonOrInstanceProperties').mockImplementation(() => Promise.resolve({ + ok: false, + status: 200, + text: () => "OK", + json: () => instanceProperties + })) + + const component = shallow() + act(async () => { + expect(fetchMock).toHaveBeenCalled(); + component.update(); + }); + }); + }); -- cgit 1.2.3-korg