Skip to content

Commit

Permalink
Fix: Links of sidebar_menu now working at view_profile_section (#3086)
Browse files Browse the repository at this point in the history
* fix: Links of sidebar_menu now working at view_profile_section

* Added Test when no organisation is selected
  • Loading branch information
PratapRathi authored Dec 30, 2024
1 parent a3d613e commit e0da67b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
37 changes: 34 additions & 3 deletions src/components/ProfileDropdown/ProfileDropdown.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { act } from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { BrowserRouter } from 'react-router-dom';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import ProfileDropdown from './ProfileDropdown';
import { MockedProvider } from '@apollo/react-testing';
import { REVOKE_REFRESH_TOKEN } from 'GraphQl/Mutations/mutations';
Expand Down Expand Up @@ -238,15 +238,46 @@ describe('ProfileDropdown Component', () => {
expect(mockNavigate).toHaveBeenCalledWith('/user/settings');
});

test('navigates to /member/:orgId for non-user roles when orgId is not present', async () => {
window.history.pushState({}, 'Test page', '/orglist');
setItem('SuperAdmin', true); // Set as admin
setItem('id', '123');

render(
<MockedProvider mocks={MOCKS} addTypename={false}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<Routes>
<Route path="/orglist" element={<ProfileDropdown />} />
</Routes>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

await act(async () => {
userEvent.click(screen.getByTestId('togDrop'));
});

await act(async () => {
userEvent.click(screen.getByTestId('profileBtn'));
});

expect(mockNavigate).toHaveBeenCalledWith('/member/');
});

test('navigates to /member/:userID for non-user roles', async () => {
window.history.pushState({}, 'Test page', '/321');
setItem('SuperAdmin', true); // Set as admin
setItem('id', '123');

render(
<MockedProvider mocks={MOCKS} addTypename={false}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<ProfileDropdown />
<Routes>
<Route path="/:orgId" element={<ProfileDropdown />} />
</Routes>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
Expand All @@ -260,6 +291,6 @@ describe('ProfileDropdown Component', () => {
userEvent.click(screen.getByTestId('profileBtn'));
});

expect(mockNavigate).toHaveBeenCalledWith('/member/123');
expect(mockNavigate).toHaveBeenCalledWith('/member/321');
});
});
6 changes: 3 additions & 3 deletions src/components/ProfileDropdown/ProfileDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Avatar from 'components/Avatar/Avatar';
import React from 'react';
import { ButtonGroup, Dropdown } from 'react-bootstrap';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useParams } from 'react-router-dom';
import useLocalStorage from 'utils/useLocalstorage';
import styles from './ProfileDropdown.module.css';
import { REVOKE_REFRESH_TOKEN } from 'GraphQl/Mutations/mutations';
Expand Down Expand Up @@ -36,8 +36,8 @@ const profileDropdown = (): JSX.Element => {
const firstName = getItem('FirstName');
const lastName = getItem('LastName');
const userImage = getItem('UserImage');
const userID = getItem('id');
const navigate = useNavigate();
const { orgId } = useParams();

const logout = async (): Promise<void> => {
try {
Expand Down Expand Up @@ -103,7 +103,7 @@ const profileDropdown = (): JSX.Element => {
onClick={() =>
userRole === 'User'
? navigate(`/user/settings`)
: navigate(`/member/${userID}`)
: navigate(`/member/${orgId || ''}`)
}
aria-label="View Profile"
>
Expand Down

0 comments on commit e0da67b

Please sign in to comment.