From 3eb9e8975b8d9e1f44106b3c9c4c6d8b711ad3a2 Mon Sep 17 00:00:00 2001 From: brunomilitzer Date: Mon, 10 Jan 2022 17:06:32 +0000 Subject: Added Test Code Coverage Issue-ID: POLICY-3563 Change-Id: I65cd2bfc72b973baa8f2b28a14e5d364ca4562a2 Signed-off-by: brunomilitzer --- .../dialogs/ControlLoop/CommissioningModal.js | 3 +- .../dialogs/ControlLoop/CommissioningModal.test.js | 5 +- .../dialogs/ControlLoop/InstancePropertiesModal.js | 14 +- .../ControlLoop/InstancePropertiesModal.test.js | 34 +++- .../ControlLoop/InstantiationManagementModal.js | 6 +- .../InstantiationManagementModal.test.js | 182 ++++++++++++--------- .../InstantiationManagementModal.test.js.snap | 2 +- .../ControlLoop/utils/InstantiationUtils.test.js | 54 ++++++ 8 files changed, 198 insertions(+), 102 deletions(-) create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/utils/InstantiationUtils.test.js diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.js index b3b76c1..4f8aaae 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,6 @@ const CommissioningModal = (props) => { const [serviceTemplateResponseOk, setServiceTemplateResponseOk] = useState(true); const name = 'ToscaServiceTemplateSimple'; const version = '1.0.0'; - let editorTemp = null useEffect(async () => { const toscaTemplateResponse = await ControlLoopService.getToscaTemplate(name, version) diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.test.js index 5ace94d..9642145 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.test.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/CommissioningModal.test.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,8 +67,6 @@ describe('Verify CommissioningModal', () => { it('handleClose called when bottom button clicked', () => { const history = createMemoryHistory(); const component = mount() - // const logSpy = jest.spyOn(console, 'log'); - act(() => { component.find('[variant="secondary"]').simulate('click'); @@ -182,7 +180,6 @@ describe('Verify CommissioningModal', () => { mount() act(async () => { - // expect(renderJsonEditor).toHaveBeenCalled(); expect(setToscaJsonEditor).toHaveBeenCalledTimes(1); }); }); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js index ada396c..8c8d403 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,10 +66,10 @@ const InstancePropertiesModal = (props) => { const [instanceName, setInstanceName] = useState('') useEffect(async () => { - const toscaInstanceProperties = await ControlLoopService.getCommonOrInstanceProperties(templateName, templateVersion, false) - .catch(error => error.message); - const toscaTemplateResponse = await ControlLoopService.getToscaTemplate(templateName, templateVersion) + .catch(error => error.message); + + const toscaInstanceProperties = await ControlLoopService.getCommonOrInstanceProperties(templateName, templateVersion, false) .catch(error => error.message); if (!toscaInstanceProperties.ok) { @@ -107,9 +107,9 @@ const InstancePropertiesModal = (props) => { console.log("instanceName to be saved is: " + instanceName); - console.log(jsonEditor); - - //setToscaFullTemplate(InstantiationUtils.updateTemplate(jsonEditor.getValue(), toscaFullTemplate)); + if (jsonEditor != null) { + setToscaFullTemplate(InstantiationUtils.updateTemplate(jsonEditor.getValue(), toscaFullTemplate)); + } const response = await ControlLoopService.createInstanceProperties(instanceName, toscaFullTemplate) .catch(error => error.message); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.test.js index 454fcf9..5c617bf 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.test.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.test.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,17 +23,29 @@ import InstancePropertiesModal from "./InstancePropertiesModal"; import toJson from "enzyme-to-json"; import { createMemoryHistory } from "history"; import { act } from "react-dom/test-utils"; -import ControlLoopService from "../../../api/ControlLoopService"; -import instanceProps from "./testFiles/instanceProps.json"; -import fullTemp from "./testFiles/fullTemplate.json"; - let logSpy = jest.spyOn(console, 'log') -const instanceProperties = JSON.parse(JSON.stringify(instanceProps)) -const fullTemplate = JSON.parse(JSON.stringify(fullTemp)) describe('Verify InstancePropertiesModal', () => { + const unmockedFetch = global.fetch + beforeAll(() => { + global.fetch = () => + Promise.resolve({ + status: 200, + text: () => "OK", + json: () => "{GlobalFetch}" + }); + }); + + afterAll(() => { + global.fetch = unmockedFetch + }); + + beforeEach(() => { + logSpy.mockClear() + }); + it("renders without crashing", () => { shallow(); }); @@ -80,4 +92,12 @@ describe('Verify InstancePropertiesModal', () => { expect(logSpy).toHaveBeenCalledWith('handleSave called'); }); }); + + it('Check useEffect is being called', async () => { + const useEffect = jest.spyOn(React, "useEffect"); + mount() + await act(async () => { + expect(useEffect).toHaveBeenCalled(); + }); + }); }); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.js index 93b504a..b9db230 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,6 +74,8 @@ const InstantiationManagementModal = (props) => { console.log("deleteInstantiationHandler called"); setDeleteInstantiation(true); + console.log(instantiation); + if (instantiation.disableDelete) { return; } @@ -83,8 +85,6 @@ const InstantiationManagementModal = (props) => { const response = await ControlLoopService.deleteInstantiation(name, version); - console.log(response); - updateList(index); if (response.ok) { diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.test.js index cb41ccc..d6e33f2 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.test.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationManagementModal.test.js @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,111 +19,137 @@ * */ -import { mount, shallow } from "enzyme"; +import {mount, shallow} from "enzyme"; import React from "react"; import toJson from "enzyme-to-json"; import InstantiationManagementModal from "./InstantiationManagementModal"; -import { act } from "react-dom/test-utils"; -import { createMemoryHistory } from "history"; +import {act} from "react-dom/test-utils"; +import {createMemoryHistory} from "history"; import ControlLoopService from "../../../api/ControlLoopService"; import clLoopList from "./testFiles/controlLoopList.json"; -import { BrowserRouter } from "react-router-dom"; +import {BrowserRouter} from "react-router-dom"; const logSpy = jest.spyOn(console, 'log') const history = createMemoryHistory(); -describe('Verify MonitoringInstantiation', () => { - const flushPromises = () => new Promise(setImmediate); +describe('Verify Instantiation Management', () => { + const flushPromises = () => new Promise(setImmediate); - beforeEach(() => { - logSpy.mockClear(); - }); + beforeEach(() => { + logSpy.mockClear(); + }); - it("renders without crashing", () => { - shallow(); - }); - it("renders correctly", () => { - const tree = shallow(); - expect(toJson(tree)).toMatchSnapshot(); - }); + it("renders without crashing", () => { + shallow(); + }); + it("renders correctly", () => { + const tree = shallow(); + expect(toJson(tree)).toMatchSnapshot(); + }); - it('should have a close Button element', () => { - const container = shallow(); - const button = container.find('[variant="secondary"]').at(2); + it('should have a close Button element', () => { + const container = shallow(); + const button = container.find('[variant="secondary"]').at(2); - expect(button.text()).toEqual("Close"); - }); + expect(button.text()).toEqual("Close"); + }); - it('should have a Create Instance Button element', () => { - const container = shallow(); - const button = container.find('[variant="primary"]').at(0); + it('should have a Create Instance Button element', () => { + const container = shallow(); + const button = container.find('[variant="primary"]').at(0); - expect(button.text()).toEqual("Create Instance"); - }); + expect(button.text()).toEqual("Create Instance"); + }); - it('should have a Monitor Instantiations Button element', () => { - const container = shallow(); - const button = container.find('[variant="secondary"]').at(0); + it('should have a Monitor Instantiations Button element', () => { + const container = shallow(); + const button = container.find('[variant="secondary"]').at(0); - expect(button.text()).toEqual("Monitor Instantiations"); - }); + expect(button.text()).toEqual("Monitor Instantiations"); + }); - it('handleClose called when bottom button clicked', () => { - const container = shallow(); - const button = container.find('[variant="secondary"]').at(2); + it('handleClose called when bottom button clicked', () => { + const container = shallow(); + const button = container.find('[variant="secondary"]').at(2); - act(() => { - button.simulate('click'); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); + act(() => { + button.simulate('click'); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); }); - }); - it('handleClose called when top-right button clicked', () => { - const container = shallow(); + it('handleClose called when top-right button clicked', () => { + const container = shallow(); - act(() => { - container.find('[size="xl"]').get(0).props.onHide(); - expect(logSpy).toHaveBeenCalledWith('handleClose called'); + act(() => { + container.find('[size="xl"]').get(0).props.onHide(); + expect(logSpy).toHaveBeenCalledWith('handleClose called'); + }); }); - }); - it('clearErrors called when clear error message button clicked', () => { - const container = shallow(); - const button = container.find('[variant="secondary"]').at(1); + it('clearErrors called when clear error message button clicked', () => { + const container = shallow(); + const button = container.find('[variant="secondary"]').at(1); - act(() => { - button.simulate('click'); - expect(logSpy).toHaveBeenCalledWith('clearErrors called'); - }); - }); - - it('Check useEffect is being called', async () => { - jest.resetAllMocks(); - jest.spyOn(ControlLoopService, 'getControlLoopInstantiation') - .mockImplementationOnce(async () => { - return Promise.resolve({ - ok: true, - status: 200, - text: () => "OK", - json: () => { - return Promise.resolve(clLoopList); - } + act(() => { + button.simulate('click'); + expect(logSpy).toHaveBeenCalledWith('clearErrors called'); }); - }); + }); - const component = mount( - - - - ); - const useEffect = jest.spyOn(React, "useEffect"); + it('Check useEffect is being called', async () => { + jest.resetAllMocks(); + jest.spyOn(ControlLoopService, 'getControlLoopInstantiation') + .mockImplementationOnce(async () => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => "OK", + json: () => { + return Promise.resolve(clLoopList); + } + }); + }); + + const component = mount( + + + + ); + const useEffect = jest.spyOn(React, "useEffect"); + + await act(async () => { + await flushPromises() + component.update(); + await expect(useEffect).toHaveBeenCalled(); - await act(async () => { - await flushPromises() - component.update(); - await expect(useEffect).toHaveBeenCalled(); + }); + component.unmount(); + }); + it('set state gets called for setInstantiationList useEffect on success', async () => { + const setInstantiationList = jest.fn(); + const setDeleteInstantiation = true; + const history = createMemoryHistory(); + jest + .spyOn(global, 'fetch') + .mockImplementation(() => + Promise.resolve({ + ok: true, + status: 200, + text: () => "OK", + json: () => clLoopList + }) + ) + + mount( + + + + ); + act(async () => { + expect(setInstantiationList).toHaveBeenCalledTimes(1); + expect(setDeleteInstantiation).toHaveBeenCalledTimes(1); + }); }); - component.unmount(); - }); }); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationManagementModal.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationManagementModal.test.js.snap index 76f8780..21a8dd5 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationManagementModal.test.js.snap +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationManagementModal.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Verify MonitoringInstantiation renders correctly 1`] = ` +exports[`Verify Instantiation Management renders correctly 1`] = ` { + + const fullTemplatePromise = { + ok: true, + status: 200, + text: () => "OK", + json: () => { + return Promise.resolve(fullTemplate) + } + } + + const instancePropertiesPromise = { + ok: true, + status: 200, + text: () => "OK", + json: () => { + return Promise.resolve(instanceProperties) + } + } + + it('test parseJsonSchema output is correct', async () => { + // Have to mock "editor" dom element for json editor to work in testing + document.body.innerHTML = '
'; + + await expect((await InstantiationUtils.parseJsonSchema(fullTemplatePromise, instancePropertiesPromise)).jsonEditor).toBeTruthy() + await expect((await InstantiationUtils.parseJsonSchema(fullTemplatePromise, instancePropertiesPromise)).fullTemplate).toBeTruthy() + }) +}); \ No newline at end of file -- cgit 1.2.3-korg