Skip to content

Commit

Permalink
Update/add link to main page from nav bar (#286)
Browse files Browse the repository at this point in the history
* Add link to main page when clicking in HeaderNavBar

* Add unit test
  • Loading branch information
nathanfranklin authored Dec 5, 2024
1 parent 4baf8da commit d388b63
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
3 changes: 3 additions & 0 deletions react/src/components/HeaderNavBar/HeaderNavBar.module.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
.root {
background-color: black;
color: white;
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-between;
height: var(--hazmapper-header-navbar-height);
padding: 5px;
}
.userName {
color: white;
Expand Down
73 changes: 73 additions & 0 deletions react/src/components/HeaderNavBar/HeaderNavBar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import { fireEvent } from '@testing-library/react';
import * as ROUTES from '@hazmapper/constants/routes';
import { renderInTest } from '@hazmapper/test/testUtil';
import { HeaderNavBar } from './HeaderNavBar';

const mockNavigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
useLocation: () => ({
pathname: '/project-public/cd010f4d-3975-4fde-8bbd-b81ffb87273f',
}),
}));

const mockAuthenticatedUser = {
data: null,
isLoading: false,
error: null,
};

jest.mock('@hazmapper/hooks/user/useAuthenticatedUser', () => {
return {
__esModule: true,
default: () => {
return { ...mockAuthenticatedUser };
},
};
});

describe('HeaderNavBar', () => {
beforeEach(() => {
mockNavigate.mockClear();
mockAuthenticatedUser.data = null;
});

test('clicking login button should navigate to login with correct return URL', () => {
const { getByText } = renderInTest(<HeaderNavBar />);

const loginButton = getByText('Login');
fireEvent.click(loginButton);

const expectedPath = '/project-public/cd010f4d-3975-4fde-8bbd-b81ffb87273f';
const expectedUrl = `${ROUTES.LOGIN}?to=${encodeURIComponent(
expectedPath
)}`;

expect(mockNavigate).toHaveBeenCalledTimes(1);
expect(mockNavigate).toHaveBeenCalledWith(expectedUrl);
expect(mockNavigate).not.toHaveBeenCalledWith(ROUTES.MAIN);
});

test('clicking header should navigate to main', () => {
const { getByRole } = renderInTest(<HeaderNavBar />);

const header = getByRole('button', { name: 'return to project listings' });
fireEvent.click(header);

expect(mockNavigate).toHaveBeenCalledWith(ROUTES.MAIN);
});

test('displays username when user is authenticated', () => {
Object.assign(mockAuthenticatedUser, {
data: { username: 'testUser' },
});

const { getByText, queryByText } = renderInTest(<HeaderNavBar />);

expect(getByText('testUser')).toBeDefined();
expect(queryByText('Login')).toBeNull();
});
});
25 changes: 17 additions & 8 deletions react/src/components/HeaderNavBar/HeaderNavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import React from 'react';
import { Layout } from 'antd';
import useAuthenticatedUser from '@hazmapper/hooks/user/useAuthenticatedUser';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useLocation } from 'react-router-dom';
import { Button, InlineMessage, LoadingSpinner } from '@tacc/core-components';
import styles from './HeaderNavBar.module.css';
import * as ROUTES from '@hazmapper/constants/routes';

export const HeaderNavBar: React.FC = () => {
const { Header } = Layout;
const navigate = useNavigate();
const location = useLocation();

const {
data: userData,
isLoading: isUserLoading,
error: isUserError,
} = useAuthenticatedUser();

const handleLogin = () => {
const url = `/login?to=${encodeURIComponent(location.pathname)}`;
const handleLogin = (e: React.MouseEvent) => {
e.stopPropagation();

const url = `${ROUTES.LOGIN}?to=${encodeURIComponent(location.pathname)}`;
navigate(url);
};

Expand All @@ -34,20 +36,27 @@ export const HeaderNavBar: React.FC = () => {
}

return (
<Header className={styles.root}>
<div
className={styles.root}
onKeyDown={() => navigate(ROUTES.MAIN)}
onClick={() => navigate(ROUTES.MAIN)}
role="button"
aria-label="return to project listings"
tabIndex={0}
>
<img
width="150px"
src="./src/assets/hazmapper-header-logo.png"
alt="Hazmapper Logo"
/>
{userData && userData.username ? (
{userData?.username ? (
<div className={styles.userName}>{userData.username}</div>
) : (
<Button type="link" className={styles.userName} onClick={handleLogin}>
Login
</Button>
)}
</Header>
</div>
);
};
export default HeaderNavBar;

0 comments on commit d388b63

Please sign in to comment.