diff --git a/src/SmartComponents/SystemDetails/Details.js b/src/SmartComponents/SystemDetails/Details.js index 3c6d344ad..3039d21d7 100644 --- a/src/SmartComponents/SystemDetails/Details.js +++ b/src/SmartComponents/SystemDetails/Details.js @@ -1,7 +1,6 @@ import React from 'react'; import propTypes from 'prop-types'; import { Bullseye } from '@patternfly/react-core'; -import ComplianceEmptyState from 'PresentationalComponents/ComplianceEmptyState'; import { useQuery } from '@apollo/client'; import { Spinner } from '@redhat-cloud-services/frontend-components/Spinner'; import './compliance.scss'; @@ -11,8 +10,15 @@ import SystemPoliciesAndRules from './SystemPoliciesAndRules'; import { SYSTEM_QUERY } from './constants'; import useTestResults from './useTestResults'; import SystemPoliciesAndRulesRest from './SystemPoliciesAndRulesRest'; +import EmptyState from './EmptyState'; -export const DetailsRest = ({ inventoryId, hidePassed, ...props }) => { +export const DetailsRest = ({ + inventoryId, + hidePassed, + policiesCount, + insightsId, + ...props +}) => { const { testResults, testResultsLoading } = useTestResults(inventoryId); return ( @@ -23,7 +29,7 @@ export const DetailsRest = ({ inventoryId, hidePassed, ...props }) => { ) : testResults.length === 0 ? ( // we render no policy cards nor rules table if there are no reporting policies - + ) : ( { DetailsRest.propTypes = { inventoryId: propTypes.string, hidePassed: propTypes.bool, + policiesCount: propTypes.number, + insightsId: propTypes.string, }; export const DetailsGraphQL = ({ inventoryId, hidePassed, ...props }) => { @@ -59,16 +67,12 @@ export const DetailsGraphQL = ({ inventoryId, hidePassed, ...props }) => { return (
- {!data?.system || is404 ? ( - - ) : ( - - )} +
); }; @@ -95,6 +99,8 @@ export const Details = (props) => { Details.propTypes = { inventoryId: propTypes.string, hidePassed: propTypes.bool, + policiesCount: propTypes.number, + insightsId: propTypes.string, }; export default Details; diff --git a/src/SmartComponents/SystemDetails/EmptyState.js b/src/SmartComponents/SystemDetails/EmptyState.js index 8611616c5..e2e88aa0b 100644 --- a/src/SmartComponents/SystemDetails/EmptyState.js +++ b/src/SmartComponents/SystemDetails/EmptyState.js @@ -3,21 +3,46 @@ import propTypes from 'prop-types'; import { NotConnected } from '@redhat-cloud-services/frontend-components/NotConnected'; import NoPoliciesState from './NoPoliciesState'; import NoReportsState from './NoReportsState'; +import { useLocation } from 'react-router-dom'; +import { useParams } from 'react-router-dom'; +import useSystem from 'Utilities/hooks/api/useSystem'; +import { Bullseye, Spinner } from '@patternfly/react-core'; -const EmptyState = ({ system }) => { - if (!system?.insightsId) { +const EmptyState = ({ insightsId, policiesCount }) => { + const location = useLocation(); + const isInventoryInPath = location.pathname.includes('inventory'); + const { inventoryId } = useParams(); + console.log(`DEBUG insightsId ${!isInventoryInPath || !!insightsId}`) + const { data: { data } = {} } = useSystem({ + params: [inventoryId], + skip: !isInventoryInPath || !!insightsId, + }); + + const effectiveInsightsId = data?.insights_id || insightsId; + const effectivePoliciesCount = data?.policies.length || policiesCount; + + if (isInventoryInPath && !data) { + return ( + + + + ); + } + + if (!effectiveInsightsId) { return ; + } + + if (policiesCount === 0) { + return ; } else { - if (!system?.hasPolicy) { - return ; - } else if (system?.hasPolicy && system?.testResultProfiles?.length === 0) { - return ; - } + return ; } }; EmptyState.propTypes = { - system: propTypes.object, + insightsId: propTypes.string, + policiesCount: propTypes.number, }; export default EmptyState; diff --git a/src/SmartComponents/SystemDetails/NoReportsState.js b/src/SmartComponents/SystemDetails/NoReportsState.js index eb74c35d9..b0ce92e91 100644 --- a/src/SmartComponents/SystemDetails/NoReportsState.js +++ b/src/SmartComponents/SystemDetails/NoReportsState.js @@ -9,7 +9,7 @@ import { EmptyStateHeader, } from '@patternfly/react-core'; -const NoReportsState = ({ system }) => ( +const NoReportsState = ({ policiesCount }) => ( ( headingLevel="h1" /> - This system is part of {system?.policies?.length} - {system?.policies?.length > 1 ? ' policies' : ' policy'}, but has not - returned any results. + This system is part of {policiesCount} + {policiesCount > 1 ? ' policies' : ' policy'}, but has not returned any + results. Reports are returned when the system checks into Insights. By default, @@ -44,9 +44,7 @@ const NoReportsState = ({ system }) => ( ); NoReportsState.propTypes = { - system: propTypes.shape({ - policies: propTypes.array, - }), + policiesCount: propTypes.number, }; export default NoReportsState; diff --git a/src/SmartComponents/SystemDetails/NoReportsState.test.js b/src/SmartComponents/SystemDetails/NoReportsState.test.js index 553dfc835..f738e63e3 100644 --- a/src/SmartComponents/SystemDetails/NoReportsState.test.js +++ b/src/SmartComponents/SystemDetails/NoReportsState.test.js @@ -5,7 +5,7 @@ import NoReportsState from './NoReportsState'; describe('NoReportsState', () => { it('with a system having policies', () => { - render(); + render(); expect( screen.getByText( @@ -15,7 +15,7 @@ describe('NoReportsState', () => { }); it('with a system having multiple policies', () => { - render(); + render(); expect( screen.getByText( diff --git a/src/SmartComponents/SystemDetails/SystemDetails.js b/src/SmartComponents/SystemDetails/SystemDetails.js index 7f9d9b541..b05ef5afb 100644 --- a/src/SmartComponents/SystemDetails/SystemDetails.js +++ b/src/SmartComponents/SystemDetails/SystemDetails.js @@ -52,6 +52,8 @@ const SystemDetailBase = ({ loading, systemName, inventoryId, + policiesCount, + insightsId, }) => ( @@ -60,7 +62,12 @@ const SystemDetailBase = ({ -
+
@@ -77,6 +84,8 @@ SystemDetailBase.propTypes = { loading: propTypes.bool, systemName: propTypes.string, inventoryId: propTypes.string, + policiesCount: propTypes.number, + insightsId: propTypes.string, }; const SystemDetailsGraphQL = ({ route }) => { @@ -105,11 +114,23 @@ const SystemDetailsRest = ({ route }) => { loading, } = useSystem({ params: [inventoryId] }); const systemName = data?.display_name || inventoryId; + const policiesCount = data?.policies.length; + const insightsId = data?.insights_id; useTitleEntity(route, systemName); return ( - + ); }; diff --git a/src/SmartComponents/SystemDetails/SystemDetails.test.js b/src/SmartComponents/SystemDetails/SystemDetails.test.js index f5325eddd..4b50861d6 100644 --- a/src/SmartComponents/SystemDetails/SystemDetails.test.js +++ b/src/SmartComponents/SystemDetails/SystemDetails.test.js @@ -6,6 +6,7 @@ import { useQuery } from '@apollo/client'; import { useLocation } from 'react-router-dom'; import SystemDetails from './SystemDetails.js'; import useAPIV2FeatureFlag from '../../Utilities/hooks/useAPIV2FeatureFlag.js'; +import useSystem from 'Utilities/hooks/api/useSystem'; jest.mock('@apollo/client'); @@ -24,6 +25,8 @@ jest.mock('Utilities/hooks/useDocumentTitle', () => ({ jest.mock('../../Utilities/hooks/useAPIV2FeatureFlag.js'); +jest.mock('Utilities/hooks/api/useSystem', () => jest.fn()); + describe('SystemDetails', () => { const defaultLocation = { query: { @@ -34,6 +37,7 @@ describe('SystemDetails', () => { system: { insightsId: 'ID_YEAH', name: 'test.host.local', + policies: [], testResultProfiles: [], }, }; @@ -49,6 +53,17 @@ describe('SystemDetails', () => { }); it('expect to render the inventory details', () => { + require('react-router-dom').useLocation.mockReturnValue({ + pathname: '/insights/inventory', + }); + useSystem.mockImplementation(() => ({ + data: {}, + error: undefined, + loading: undefined, + })); + + expect(useSystem).not.toHaveBeenCalled(); + render( @@ -70,6 +85,28 @@ describe('SystemDetails', () => { expect(screen.getByText('Loading...')).toBeInTheDocument(); }); +}); + +describe('SystemDetails - REST', () => { + beforeEach(() => { + useAPIV2FeatureFlag.mockImplementation(() => true); + }); + + it('expect to render Inventory Details Wrapper', () => { + useSystem.mockImplementation(() => ({ + data: { data: { display_name: "foo", policies: [{}], insights_id: "123" }}, + error: undefined, + loading: undefined, + })); + render( + + + + ); - // TODO: add tests for the REST API component + expect( + screen.getByLabelText('Inventory Details Wrapper') + ).toBeInTheDocument(); + expect(screen.getByText('Loading...')).toBeInTheDocument(); + }) }); diff --git a/src/SmartComponents/SystemDetails/SystemPoliciesAndRules.js b/src/SmartComponents/SystemDetails/SystemPoliciesAndRules.js index f7e92942b..b68543a2f 100644 --- a/src/SmartComponents/SystemDetails/SystemPoliciesAndRules.js +++ b/src/SmartComponents/SystemDetails/SystemPoliciesAndRules.js @@ -19,7 +19,9 @@ const SystemPoliciesAndRules = ({ data: { system }, loading, hidePassed }) => { const [selectedPolicy, setSelectedPolicy] = useState( system.testResultProfiles[0]?.id ); - const policies = system?.testResultProfiles; + const testResultProfiles = system?.testResultProfiles; + const policiesCount = system?.policies.length; + const insightsId = system?.insightsId; const sorter = natsort({ desc: false, insensitive: true }); const sortedTestResultProfiles = system?.testResultProfiles.sort( @@ -36,12 +38,12 @@ const SystemPoliciesAndRules = ({ data: { system }, loading, hidePassed }) => { ) : apiV2Enabled === true ? ( ) : ( - + )}
- {system?.testResultProfiles?.length ? ( + {testResultProfiles?.length ? ( <> - {system.testResultProfiles.length > 1 && ( + {testResultProfiles.length > 1 && ( { /> ) : ( - + )} ); @@ -101,11 +103,14 @@ SystemPoliciesAndRules.propTypes = { data: propTypes.shape({ system: propTypes.shape({ hasPolicy: propTypes.bool, - policies: propTypes.shape({ - id: propTypes.string, - }), + policies: propTypes.arrayOf( + propTypes.shape({ + id: propTypes.string, + }) + ), profiles: propTypes.array, testResultProfiles: propTypes.array, + insightsId: propTypes.string, }), }), loading: propTypes.bool,