Skip to content

Improvement/artesca 15276 remove dataservice artesca plus veeam #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions src/react/Routes.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { screen, waitFor } from '@testing-library/react';
import { useDispatch, useSelector } from 'react-redux';
import { useIsVeeamVBROnly } from './ISV/hooks/useIsVeeamVBROnly';
import InternalRoutes, { PrivateRoutes } from './Routes';
import { renderWithRouterMatch } from './utils/testUtil';

// Mock useIsVeeamVBROnly as it comes from ShellHooks
jest.mock('./ISV/hooks/useIsVeeamVBROnly');
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: jest.fn(),
useDispatch: jest.fn(),
}));

describe('Routes component', () => {
const mockUseIsVeeamVBROnly = useIsVeeamVBROnly as jest.Mock;
const mockUseSelector = useSelector as jest.Mock;
const mockUseDispatch = useDispatch as jest.Mock;
const selectors = {
loadingAccounts: () => screen.queryByText(/Loading accounts/i),
loadingDataServices: () => screen.queryByText(/Loading Data Services/i),
createDataService: () => screen.queryByText(/Create new Data Service/i),
loadingClients: () => screen.queryByText(/Loading clients/i),
dataServicesLink: () => screen.queryByText(/Data Services/i),
};

beforeEach(() => {
jest.clearAllMocks();

// Default mock implementations
mockUseDispatch.mockReturnValue(jest.fn());

// Setup the default state for useSelector
mockUseSelector.mockImplementation((selector) => {
// Create a mock state that has the necessary fields
const mockState = {
auth: {
isClientsLoaded: true,
config: {
managementEndpoint: 'http://test-endpoint.com',
},
oidcLogout: jest.fn(),
},
oidc: {
user: {
access_token: 'mock-token',
expired: false,
expires_at: Date.now() / 1000 + 3600, // 1 hour from now
},
},
configuration: {
latest: {
version: 1,
},
},
};

// Pass the mock state to the selector function
return selector(mockState);
});
});

it('should show loading state when isClientsLoaded is false', async () => {
// Override the default mock to set isClientsLoaded to false
mockUseSelector.mockImplementation((selector) => {
const mockState = {
auth: {
isClientsLoaded: false,
config: {
managementEndpoint: 'http://test-endpoint.com',
},
},
oidc: {
user: {
access_token: 'mock-token',
expired: false,
},
},
};
return selector(mockState);
});

// Render the PrivateRoutes component
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/accounts',
});

// Verify that loading state is shown
await waitFor(() => {
expect(selectors.loadingClients()).toBeInTheDocument();
});
});

it('should render the Create Data Service page in standard configuration', async () => {
// Mock the hook to return false
mockUseIsVeeamVBROnly.mockReturnValue(false);

// Render with the create-dataservice route
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/create-dataservice',
});

await waitFor(() => {
expect(selectors.createDataService()).toBeInTheDocument();
});
});

it('should not render the Create Data Service page in ARTESCA+VEEAM configuration', async () => {
// Mock the hook to return true
mockUseIsVeeamVBROnly.mockReturnValue(true);

// Render with the create-dataservice route
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/create-dataservice',
});

// Verify that it redirects to accounts
await waitFor(() => {
expect(selectors.createDataService()).not.toBeInTheDocument();
expect(selectors.loadingAccounts()).toBeInTheDocument();
});
});

it('should render the Data Services page in standard configuration', async () => {
// Mock the hook to return false
mockUseIsVeeamVBROnly.mockReturnValue(false);

// Render with the dataservices route
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/dataservices',
});

await waitFor(() => {
expect(selectors.loadingDataServices()).toBeInTheDocument();
});
});

it('should not render the Data Services page in ARTESCA+VEEAM configuration', async () => {
// Mock the hook to return true
mockUseIsVeeamVBROnly.mockReturnValue(true);

// Render with the dataservices route
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/dataservices',
});

await waitFor(() => {
expect(selectors.loadingDataServices()).not.toBeInTheDocument();
});
});
it('should redirect incorrect routes to Accounts page', async () => {
renderWithRouterMatch(<PrivateRoutes />, {
path: '/*',
route: '/incorrect-route',
});

await waitFor(() => {
expect(selectors.loadingAccounts()).toBeInTheDocument();
});
});

describe('sidebar entries', () => {
it('should hide Data Services from sidebar in ARTESCA+VEEAM configuration', async () => {
// Mock the hook to return true
mockUseIsVeeamVBROnly.mockReturnValue(true);

// Render InternalRoutes with any route
renderWithRouterMatch(<InternalRoutes />, {
path: '/*',
route: '/accounts',
});

// Check that Data Services link is not in the sidebar
await waitFor(() => {
expect(selectors.dataServicesLink()).not.toBeInTheDocument();
});
});

it('should show Data Services in sidebar in standard configuration', async () => {
// Mock the hook to return false
mockUseIsVeeamVBROnly.mockReturnValue(false);

// Render InternalRoutes with any route
renderWithRouterMatch(<InternalRoutes />, {
path: '/*',
route: '/accounts',
});

// Check that Data Services link is in the sidebar
await waitFor(() => {
expect(selectors.dataServicesLink()).toBeInTheDocument();
});
});
});
});
62 changes: 37 additions & 25 deletions src/react/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { useConfig } from './next-architecture/ui/ConfigProvider';
import { useAuthGroups } from './utils/hooks';
import NoMatch from './NoMatch';

Check warning on line 28 in src/react/Routes.tsx

View workflow job for this annotation

GitHub Actions / tests

'NoMatch' is defined but never used
import Accounts from './account/Accounts';
import { Locations } from './locations/Locations';
import Endpoints from './endpoint/Endpoints';
Expand All @@ -46,6 +46,7 @@
import AccountCreateUser from './account/AccountCreateUser';
import CreateAccountPolicy from './account/CreateAccountPolicy';
import { ISVSteps } from './ISV/components/ISVSteps';
import { useIsVeeamVBROnly } from './ISV/hooks/useIsVeeamVBROnly';

export const RemoveTrailingSlash = ({ ...rest }) => {
const location = useLocation();
Expand Down Expand Up @@ -97,13 +98,15 @@
}
};

function PrivateRoutes() {
export function PrivateRoutes() {
const dispatch = useDispatch();
const isClientsLoaded = useSelector(
(state: AppState) => state.auth.isClientsLoaded,
);
const user = useSelector((state: AppState) => state.oidc.user);
const config = useConfig();
const isArtescaPlusVeeamEnabled = useIsVeeamVBROnly();

const managementEndpoint = useSelector(
(state: AppState) => state.auth?.config?.managementEndpoint,
);
Expand Down Expand Up @@ -208,22 +211,26 @@
</DataServiceRoleProvider>
}
/>
<Route
path="create-dataservice/*"
element={
<DataServiceRoleProvider>
<EndpointCreate />
</DataServiceRoleProvider>
}
/>
<Route
path="dataservices/*"
element={
<DataServiceRoleProvider>
<Endpoints />
</DataServiceRoleProvider>
}
/>
{!isArtescaPlusVeeamEnabled && (
<>
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The React fragment here wraps only a single <Route> component. You can remove the fragment to simplify the JSX.

Copilot uses AI. Check for mistakes.

<Route
path="create-dataservice/*"
element={
<DataServiceRoleProvider>
<EndpointCreate />
</DataServiceRoleProvider>
}
/>
<Route
path="dataservices/*"
element={
<DataServiceRoleProvider>
<Endpoints />
</DataServiceRoleProvider>
}
/>
</>
)}
<Route
path="locations/*"
element={
Expand Down Expand Up @@ -386,6 +393,7 @@
const { isStorageManager } = useAuthGroups();
const config = useConfig();
const navigate = useBasenameRelativeNavigate();
const isArtescaPlusVeeamEnabled = useIsVeeamVBROnly();
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This hook is called in both PrivateRoutes and InternalRoutes, leading to duplication. Consider lifting this logic into a shared parent component or a custom hook wrapper to reduce repetition.

Copilot uses AI. Check for mistakes.


const doesRouteMatch = useCallback(
(paths: string | string[]) => {
Expand Down Expand Up @@ -475,14 +483,18 @@
},
active: doesRouteMatch('/locations'),
},
{
label: 'Data Services',
icon: <Icon name="Cubes" />,
onClick: () => {
navigate('/dataservices');
},
active: doesRouteMatch('/dataservices'),
},
...(isArtescaPlusVeeamEnabled
? []
: [
{
label: 'Data Services',
icon: <Icon name="Cubes" />,
onClick: () => {
navigate('/dataservices');
},
active: doesRouteMatch('/dataservices'),
},
]),
]
: []),
],
Expand Down
Loading