From 53b46adf3359b37cd83fea036c1b87f8b55683db Mon Sep 17 00:00:00 2001 From: brunomilitzer Date: Thu, 28 Oct 2021 15:34:43 +0100 Subject: Added JEST Unit Tests for Monitoring Functionality Created Unit Tests to cover monitoring functionality Issue-ID: POLICY-3561 Change-Id: I48035f3960447c66d29005d5eba2e8332c55b591 Signed-off-by: brunomilitzer --- .../dialogs/ControlLoop/AccordionHeader.js | 116 +++++++++++++++++++++ .../dialogs/ControlLoop/AccordionHeader.test.js | 38 +++++++ .../dialogs/ControlLoop/InstancePropertiesModal.js | 6 +- .../ControlLoop/InstantiationElementItem.test.js | 33 ++++++ .../ControlLoop/InstantiationElements.test.js | 33 ++++++ .../dialogs/ControlLoop/InstantiationItem.js | 80 +------------- .../dialogs/ControlLoop/InstantiationItem.test.js | 53 ++++++++++ .../dialogs/ControlLoop/MonitorInstantiation.js | 8 +- .../ControlLoop/MonitorInstantiation.test.js | 20 ++-- .../__snapshots__/AccordionHeader.test.js.snap | 3 + .../InstantiationElementItem.test.js.snap | 3 + .../InstantiationElements.test.js.snap | 3 + .../__snapshots__/InstantiationItem.test.js.snap | 26 +++++ 13 files changed, 324 insertions(+), 98 deletions(-) create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElementItem.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElements.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.test.js create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/AccordionHeader.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElementItem.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElements.test.js.snap create mode 100644 gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationItem.test.js.snap (limited to 'gui-clamp/ui-react/src/components/dialogs') diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.js new file mode 100644 index 0000000..2997239 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.js @@ -0,0 +1,116 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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 styled from "styled-components"; +import { Accordion, Button } from "react-bootstrap"; +import React, { useEffect, useState } from "react"; + +const UninitialisedHeader = styled.div` + margin: 0; + padding: 0 0 1px 0; + border-bottom: 1px solid #7f7f7f; + background: #cccccc; + font-weight: normal; + border-radius: 0; +` + +const PassiveHeader = styled.div` + margin: 0; + padding: 0 0 1px 0; + border-bottom: 1px solid #7f7f7f; + background: #ffe87c; + font-weight: normal; + border-radius: 0; +` + +const RunningHeader = styled.div` + margin: 0; + padding: 0 0 1px 0; + border-bottom: 1px solid #7f7f7f; + background: #7ec699; + font-weight: normal; + border-radius: 0; +` + +const ToggleButton = styled(Button)` + color: #000000; + text-decoration: none; + + :hover, :active { + color: #000000; + text-decoration: none !important; + } +` + +const AccordionHeader = (props) => { + + const toggleState = () => { + switch (props.orderedState) { + case 'UNINITIALISED': + return renderUninitialisedOrderedState(); + case 'PASSIVE': + return renderPassiveOrderedState(); + case 'RUNNING': + return renderRunningOrderedState(); + } + } + + const renderUninitialisedOrderedState = () => { + + return ( + + + { props.title } + + + ) + } + + const renderPassiveOrderedState = () => { + console.log("renderPassiveOrderedState called"); + + return ( + + + { props.title } + + + ) + } + + const renderRunningOrderedState = () => { + console.log("renderRunningOrderedState called"); + + return ( + + + { props.title } + + + ) + } + + return ( + toggleState() + ); +} + +export default AccordionHeader; \ No newline at end of file diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.test.js new file mode 100644 index 0000000..7685340 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/AccordionHeader.test.js @@ -0,0 +1,38 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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 { shallow } from "enzyme"; +import React from "react"; +import AccordionHeader from "./AccordionHeader"; +import toJson from "enzyme-to-json"; + +describe('Verify AccordionHeader', () => { + + const index = 0; + const title = "PMSH Instance"; + const orderState = "UNINITIALISED"; + const container = shallow(); + + it("renders correctly", () => { + expect(toJson(container)).toMatchSnapshot(); + }); + +}); \ No newline at end of file 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 8ad855e..7a473e7 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstancePropertiesModal.js @@ -235,9 +235,11 @@ const InstancePropertiesModal = (props) => { } const handleSave = async () => { - console.log("handleSave called") + console.log("handleSave called"); - console.log("instanceName to be saved is: " + instanceName) + console.log("instanceName to be saved is: " + instanceName); + + console.log(JSON.stringify(toscaFullTemplate)); updateTemplate(jsonEditor.getValue()); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElementItem.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElementItem.test.js new file mode 100644 index 0000000..136983a --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElementItem.test.js @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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 { shallow } from "enzyme"; +import toJson from "enzyme-to-json"; +import React from "react"; +import InstantiationElementItem from "./InstantiationElementItem"; + +describe('Verify InstantiationElementItem', () => { + const container = shallow(); + + it("renders correctly", () => { + expect(toJson(container)).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElements.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElements.test.js new file mode 100644 index 0000000..127a886 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationElements.test.js @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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 toJson from "enzyme-to-json"; +import { shallow } from "enzyme"; +import React from "react"; +import InstantiationElements from "./InstantiationElements"; + +describe('Verify InstantiationElements', () => { + const container = shallow(); + + it("renders correctly", () => { + expect(toJson(container)).toMatchSnapshot(); + }); +}); \ No newline at end of file diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.js index a59770b..7b8e453 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.js @@ -21,6 +21,7 @@ import React from "react"; import styled from 'styled-components'; import { Accordion, Button, Card } from "react-bootstrap"; +import AccordionHeader from "./AccordionHeader"; const AccordionBody = styled.div` margin: 0; @@ -34,89 +35,12 @@ const CardBody = styled(Card.Body)` margin: 0; ` -const UninitialisedHeader = styled.div` - margin: 0; - padding: 0 0 1px 0; - border-bottom: 1px solid #7f7f7f; - background: #cccccc; - font-weight: normal; - border-radius: 0; -` - -const PassiveHeader = styled.div` - margin: 0; - padding: 0 0 1px 0; - border-bottom: 1px solid #7f7f7f; - background: #ffe87c; - font-weight: normal; - border-radius: 0; -` - -const RunningHeader = styled.div` - margin: 0; - padding: 0 0 1px 0; - border-bottom: 1px solid #7f7f7f; - background: #7ec699; - font-weight: normal; - border-radius: 0; -` - -const ToggleButton = styled(Button)` - color: #000000; - text-decoration: none; - - :hover, :active { - color: #000000; - text-decoration: none !important; - } -` - const InstantiationItem = (props) => { - const toggleState = () => { - switch (props.orderedState) { - case 'UNINITIALISED': - return renderUninitialisedOrderedState() - case 'PASSIVE': - return renderPassiveOrderedState(); - case 'RUNNING': - return renderRunningOrderedState(); - } - } - - const renderUninitialisedOrderedState = () => { - return ( - - - { props.title } - - - ) - } - - const renderPassiveOrderedState = () => { - return ( - - - { props.title } - - - ) - } - - const renderRunningOrderedState = () => { - return ( - - - { props.title } - - - ) - } return ( - { toggleState() } + { props.children } diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.test.js new file mode 100644 index 0000000..fe4aaf8 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/InstantiationItem.test.js @@ -0,0 +1,53 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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 React from "react"; +import { shallow } from "enzyme"; +import toJson from "enzyme-to-json"; + +import InstantiationItem from "./InstantiationItem"; + +describe('Verify InstantiationItem', () => { + + const index = 0; + const title = "PMSH Instance"; + const orderState = "UNINITIALISED"; + const container = shallow(); + + it("renders without crashing", () => { + shallow(); + }); + + it("renders correctly", () => { + expect(toJson(container)).toMatchSnapshot(); + }); + + it("should contain an Accordion", () => { + const accordion = container.find('Accordion'); + expect(accordion).toHaveLength(1); + }); + + it("should contain an AccordionHeader", () => { + const accordion = container.find('AccordionHeader'); + expect(accordion).toHaveLength(1); + }); + +}); \ No newline at end of file diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.js index dad4185..4186477 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.js @@ -43,13 +43,13 @@ const MonitorInstantiation = (props) => { const controlLoopInstantiation = await ControlLoopService.getControlLoopInstantiation() .catch(error => error.message); - const controlLoopInstantiationJson = await controlLoopInstantiation.json() - console.log(controlLoopInstantiationJson) + const controlLoopInstantiationJson = await controlLoopInstantiation.json(); + if (!controlLoopInstantiation.ok || controlLoopInstantiationJson['controlLoopList'].length === 0) { setControlLoopInstantiationOk(false) setControlLoopInstantiationError(controlLoopInstantiationJson) } else { - setControlLoopList(controlLoopInstantiationJson[['controlLoopList']]) + setControlLoopList(controlLoopInstantiationJson['controlLoopList']); } }, []) @@ -67,7 +67,7 @@ const MonitorInstantiation = (props) => { { controlLoopList.map((clList, index) => ( - + )) diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.test.js b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.test.js index 5e30924..cce6225 100644 --- a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.test.js +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/MonitorInstantiation.test.js @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -import { mount, shallow } from "enzyme"; +import { shallow } from "enzyme"; import toJson from "enzyme-to-json"; import { createMemoryHistory } from "history"; import React from "react"; @@ -25,39 +25,31 @@ import MonitorInstantiation from "./MonitorInstantiation"; import { act } from "react-dom/test-utils"; describe('Verify MonitoringInstantiation', () => { - - it("renders without crashing", () => { - shallow(); - }); + const container = shallow(); + const containerWithHistory = shallow(); it("renders correctly", () => { - const tree = shallow(); - expect(toJson(tree)).toMatchSnapshot(); + expect(toJson(container)).toMatchSnapshot(); }); it('should have a Button element', () => { - const container = shallow() expect(container.find('Button').length).toEqual(1); }); 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'); + containerWithHistory.find('[variant="secondary"]').simulate('click'); expect(logSpy).toHaveBeenCalledWith('handleClose called'); }); }); it('handleClose called when top-right button clicked', () => { - const history = createMemoryHistory(); - const component = mount() const logSpy = jest.spyOn(console, 'log'); act(() => { - component.find('[size="xl"]').get(0).props.onHide(); + containerWithHistory.find('[size="xl"]').get(0).props.onHide(); expect(logSpy).toHaveBeenCalledWith('handleClose called'); }); }); diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/AccordionHeader.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/AccordionHeader.test.js.snap new file mode 100644 index 0000000..b4a707b --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/AccordionHeader.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify AccordionHeader renders correctly 1`] = `""`; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElementItem.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElementItem.test.js.snap new file mode 100644 index 0000000..2c5d298 --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElementItem.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify InstantiationElementItem renders correctly 1`] = ``; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElements.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElements.test.js.snap new file mode 100644 index 0000000..8fa928e --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationElements.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify InstantiationElements renders correctly 1`] = ``; diff --git a/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationItem.test.js.snap b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationItem.test.js.snap new file mode 100644 index 0000000..a3a230a --- /dev/null +++ b/gui-clamp/ui-react/src/components/dialogs/ControlLoop/__snapshots__/InstantiationItem.test.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Verify InstantiationItem renders correctly 1`] = ` + + + + + + + + +`; -- cgit 1.2.3-korg