diff options
author | 2022-01-10 17:06:32 +0000 | |
---|---|---|
committer | 2022-01-11 12:05:55 +0000 | |
commit | 3eb9e8975b8d9e1f44106b3c9c4c6d8b711ad3a2 (patch) | |
tree | 00d41e96baa2fe2096245bb0f4ab99b594c3578a /gui-clamp/ui-react/src/components | |
parent | 4be96f85c65df9384b11d68e87ca9e03e27a083c (diff) |
Added Test Code Coverage
Issue-ID: POLICY-3563
Change-Id: I65cd2bfc72b973baa8f2b28a14e5d364ca4562a2
Signed-off-by: brunomilitzer <bruno.militzer@est.tech>
Diffstat (limited to 'gui-clamp/ui-react/src/components')
8 files changed, 198 insertions, 102 deletions
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(<CommissioningModal history={ history }/>) - // const logSpy = jest.spyOn(console, 'log'); - act(() => { component.find('[variant="secondary"]').simulate('click'); @@ -182,7 +180,6 @@ describe('Verify CommissioningModal', () => { mount(<CommissioningModal history={ history }/>) 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(<InstancePropertiesModal />); }); @@ -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(<InstancePropertiesModal />) + 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(<InstantiationManagementModal/>); - }); - it("renders correctly", () => { - const tree = shallow(<InstantiationManagementModal />); - expect(toJson(tree)).toMatchSnapshot(); - }); + it("renders without crashing", () => { + shallow(<InstantiationManagementModal/>); + }); + it("renders correctly", () => { + const tree = shallow(<InstantiationManagementModal/>); + expect(toJson(tree)).toMatchSnapshot(); + }); - it('should have a close Button element', () => { - const container = shallow(<InstantiationManagementModal />); - const button = container.find('[variant="secondary"]').at(2); + it('should have a close Button element', () => { + const container = shallow(<InstantiationManagementModal/>); + 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(<InstantiationManagementModal />); - const button = container.find('[variant="primary"]').at(0); + it('should have a Create Instance Button element', () => { + const container = shallow(<InstantiationManagementModal/>); + 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(<InstantiationManagementModal />); - const button = container.find('[variant="secondary"]').at(0); + it('should have a Monitor Instantiations Button element', () => { + const container = shallow(<InstantiationManagementModal/>); + 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(<InstantiationManagementModal history={ history } />); - const button = container.find('[variant="secondary"]').at(2); + it('handleClose called when bottom button clicked', () => { + const container = shallow(<InstantiationManagementModal history={history}/>); + 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(<InstantiationManagementModal history={ history } />); + it('handleClose called when top-right button clicked', () => { + const container = shallow(<InstantiationManagementModal history={history}/>); - 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(<InstantiationManagementModal history={ history } />); - const button = container.find('[variant="secondary"]').at(1); + it('clearErrors called when clear error message button clicked', () => { + const container = shallow(<InstantiationManagementModal history={history}/>); + 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( - <BrowserRouter> - <InstantiationManagementModal /> - </BrowserRouter> - ); - 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( + <BrowserRouter> + <InstantiationManagementModal/> + </BrowserRouter> + ); + 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( + <BrowserRouter> + <InstantiationManagementModal history={history}/> + </BrowserRouter> + ); + 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`] = ` <Styled(Modal) backdrop="static" keyboard={false} diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/utils/InstantiationUtils.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/utils/InstantiationUtils.test.js new file mode 100644 index 0000000..4077ff8 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/utils/InstantiationUtils.test.js @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +import InstantiationUtils from "./InstantiationUtils"; +import instanceProps from "../testFiles/instanceProps.json"; +import fullTemp from "../testFiles/fullTemplate.json"; + +const instanceProperties = JSON.parse(JSON.stringify(instanceProps)) +const fullTemplate = JSON.parse(JSON.stringify(fullTemp)) + +describe('Verify InstantiationUtils', () => { + + 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 = '<div id="editor"></div>'; + + await expect((await InstantiationUtils.parseJsonSchema(fullTemplatePromise, instancePropertiesPromise)).jsonEditor).toBeTruthy() + await expect((await InstantiationUtils.parseJsonSchema(fullTemplatePromise, instancePropertiesPromise)).fullTemplate).toBeTruthy() + }) +});
\ No newline at end of file |