Skip to content

Commit

Permalink
refactor(RHINENG-5926) ZeroState checks for RHEL systems (#2070)
Browse files Browse the repository at this point in the history
* refactor(RHINENG-5926) ZeroState checks for RHEL systems
  • Loading branch information
adonispuente committed Mar 11, 2024
1 parent 61ec759 commit 715cd47
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 89 deletions.
15 changes: 15 additions & 0 deletions src/Helpers/APIHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,18 @@ export const checkEdgePresence = () => {
.then((result) => result?.total > 0)
.catch(() => false);
};

export const getRhelSystems = ()=> {
let parameterArray = constructParameters(
{
page: 1,
perPage: 1,
filter: { system_profile: { operating_system: { RHEL: { version: { gte: 0 } } } } }
},
hostsParameterNames
);

return hosts.apiHostGetHostList(...parameterArray)
.then((result) => result?.total > 0)
.catch(() => false);
};
34 changes: 18 additions & 16 deletions src/Utilities/VulnerabilityRoutes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState, lazy, Suspense, Fragment, createContext } from 'react';
import PropTypes from 'prop-types';
import { Navigate, Route, Routes, useLocation } from 'react-router-dom';
import { getSystems, checkEdgePresence } from '../Helpers/APIHelper';
import { checkEdgePresence, getRhelSystems } from '../Helpers/APIHelper';
import { PATHS } from '../Helpers/constants';
import { intl } from './IntlProvider';
import messages from '../Messages';
Expand Down Expand Up @@ -50,6 +50,9 @@ export const AccountStatContext = createContext({
hasConventionalSystems: true,
hasEdgeDevices: false
});
export const checkForAccountSystems = (isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems) => {
return isEdgeParityEnabled && (hasEdgeDevices || hasConventionalSystems) || hasConventionalSystems;
};

export const InsightsElement = ({ element: Element, title, globalFilterEnabled, ...elementProps }) => {
let location = useLocation();
Expand All @@ -63,15 +66,15 @@ export const InsightsElement = ({ element: Element, title, globalFilterEnabled,

useEffect(() => {
const fetchData = async () => {
getSystems()
getRhelSystems()
.then(async result => {
if (isEdgeParityEnabled) {
//if there is at least 1 edge device in the account level, not only in vulnerability
const edgeDevicePresent = await checkEdgePresence();
setHasEdgeDevices(edgeDevicePresent);
}

setHasConventionalSystems(result?.meta?.total_items > 0);
setHasConventionalSystems(result);
setLoading(false);
})
.catch(error => {
Expand Down Expand Up @@ -105,8 +108,6 @@ export const InsightsElement = ({ element: Element, title, globalFilterEnabled,
chrome.hideGlobalFilter(!globalFilterEnabled);
}, [location.pathname]);

const accountHasSystem = isEdgeParityEnabled && (hasEdgeDevices || hasConventionalSystems) || hasConventionalSystems;

if (isLoading) {
return (
<Bullseye>
Expand All @@ -116,24 +117,25 @@ export const InsightsElement = ({ element: Element, title, globalFilterEnabled,
}

return hasAccess
? accountHasSystem
? <AccountStatContext.Provider value={{ hasConventionalSystems, hasEdgeDevices }}>
? <AsyncComponent
appId="vulnerability_zero_state"
appName="dashboard"
module="./AppZeroState"
scope="dashboard"
ErrorComponent={<ErrorState />}
app="Vulnerability"
aria-label="Zero state"
customFetchResults={checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)}
>
<AccountStatContext.Provider value={{ hasConventionalSystems, hasEdgeDevices }}>
<Element
{...elementProps}
hasEdgeDevices={hasEdgeDevices}
hasConventionalSystems={hasEdgeDevices}
aria-label="Insights element"
/>
</AccountStatContext.Provider>
: <AsyncComponent
appId="vulnerability_zero_state"
appName="dashboard"
module="./AppZeroState"
scope="dashboard"
ErrorComponent={<ErrorState />}
app="Vulnerability"
aria-label="Zero state"
/>
</AsyncComponent>
: <NotAuthorized
title="This application requires Inventory permissions"
description={
Expand Down
111 changes: 38 additions & 73 deletions src/Utilities/VulnerabilityRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import '@testing-library/jest-dom';
import { screen, waitFor } from '@testing-library/react';
import { ComponentWithContext } from './TestingUtilities';
import { render } from '@testing-library/react';
import { InsightsElement } from "./VulnerabilityRoutes";
import useFeatureFlag from './useFeatureFlag';
import { getSystems, checkEdgePresence } from '../Helpers/APIHelper';
import '@testing-library/jest-dom';
import { InsightsElement, checkForAccountSystems } from "./VulnerabilityRoutes";


jest.mock('./useFeatureFlag', () => ({
...jest.requireActual('./useFeatureFlag'),
__esModule: true,
default: jest.fn(() => false)
}))

jest.mock('@redhat-cloud-services/frontend-components/AsyncComponent', () => ({
__esModule: true,
default: jest.fn((props) => (
Expand All @@ -23,98 +22,64 @@ jest.mock('@redhat-cloud-services/frontend-components/AsyncComponent', () => ({
)),
}));

jest.mock('../Helpers/APIHelper', () => ({
...jest.requireActual('../Helpers/APIHelper'),
getSystems: jest.fn(() => Promise.resolve({ meta: { total_items: 1 } })),
checkEdgePresence: jest.fn(() => Promise.resolve(true)),
}));

const TestElement = (props) => <div {...props} />;

afterEach(() => {
jest.clearAllMocks();
});
describe('VulnerabilityRoutes', () => {
describe('InsightsElement', () => {
it('Should show loading state while account stats are being loaded', () =>{
it('Should show loading state while account stats are being loaded', () => {
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);
<InsightsElement element={TestElement} />
</ComponentWithContext>);

expect(
screen.getByLabelText('Spinner')
).toBeVisible();
});

describe(('edge parit disabled'), () => {
it('Should show zero state when there is no conventional, regardless of edge system', async () =>{
getSystems.mockReturnValueOnce(Promise.resolve({ meta: { total_items: 0 } }));
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);

await waitFor(() => {
expect(
screen.getByLabelText('Zero state')
).toBeVisible();
});
it('Should show zero state when there is no conventional, regardless of edge system', async () => {
let isEdgeParityEnabled = false;
let hasEdgeDevices = true;
let hasConventionalSystems = false;
expect(checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)).toBe(false)

});
it('Should element when there is a conventional system, regardless of edge system', async () =>{
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);

await waitFor(() => {
expect(
screen.getByLabelText('Insights element')
).toBeVisible();
});

it('Should element when there is a conventional system, regardless of edge system', async () => {
let isEdgeParityEnabled = false;
let hasEdgeDevices = true;
let hasConventionalSystems = true;
expect(checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)).toBe(true)
});
});
})

describe(('edge parit enabled'), () => {
beforeEach(() => {
useFeatureFlag.mockReturnValue(true);
});
it('Should show zero state when there is no conventional and edge systems', async () =>{
getSystems.mockReturnValueOnce(Promise.resolve({ meta: { total_items: 0 } }));
checkEdgePresence.mockReturnValueOnce(Promise.resolve(false));
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);

await waitFor(() => {
expect(
screen.getByLabelText('Zero state')
).toBeVisible();
});
it('Should show zero state when there is no conventional and no edge systems', async () => {
let isEdgeParityEnabled = true;
let hasEdgeDevices = false;
let hasConventionalSystems = false;
expect(checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)).toBe(false)
});
it('Should element when there is no conventional, but there is an edge systems', async () =>{
getSystems.mockReturnValueOnce(Promise.resolve({ meta: { total_items: 0 } }));
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);

await waitFor(() => {
expect(
screen.getByLabelText('Insights element')
).toBeVisible();
});
it('Should element when there is no conventional, but there is an edge systems', async () => {
let isEdgeParityEnabled = true;
let hasEdgeDevices = true;
let hasConventionalSystems = false;
expect(checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)).toBe(true)
});
it('Should element when there is conventional, but there is no edge systems', async () =>{
getSystems.mockReturnValueOnce(Promise.resolve({ meta: { total_items: 1 } }));
render(<ComponentWithContext>
<InsightsElement element={TestElement} />
</ComponentWithContext>);

await waitFor(() => {
expect(
screen.getByLabelText('Insights element')
).toBeVisible();
});
it('Should element when there is conventional, but there is no edge systems', async () => {
let isEdgeParityEnabled = true;
let hasEdgeDevices = false;
let hasConventionalSystems = true;
expect(checkForAccountSystems(isEdgeParityEnabled, hasEdgeDevices, hasConventionalSystems)).toBe(true)
});
})

});


});
})
})

0 comments on commit 715cd47

Please sign in to comment.