set public and private links sets based on authentication state

This commit is contained in:
Ruidy Nemausat 2020-05-13 09:29:25 +02:00
parent 0d2ce5c6cb
commit 351205fdfe
3 changed files with 67 additions and 15 deletions

2
.gitignore vendored
View file

@ -8,6 +8,8 @@
# testing # testing
/coverage /coverage
/cypress/integration/examples /cypress/integration/examples
/cypress/fixtures/
/cypress/screenshots/
# production # production
/build /build

View file

@ -1,17 +1,16 @@
import React, {FC} from 'react'; import React, {FC} from 'react';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {faCode} from '@fortawesome/free-solid-svg-icons'; import {faCode, faSignOutAlt, faUser} from '@fortawesome/free-solid-svg-icons';
interface IProps {
isAuthenticated?: boolean;
loading?: boolean;
}
/** /**
* Main Navbar serves navigation routes. * Main Navbar serves navigation routes.
*/ */
const NavBar = () => ( const NavBar: FC<IProps> = ({isAuthenticated = true, loading = false}) => {
<nav className="navbar bg-dark"> const publicLinks = (
<h1>
<a href="dashboard.html">
<FontAwesomeIcon icon={faCode} /> DevBook
</a>
</h1>
<ul> <ul>
<li> <li>
<a href="profiles.html">Developers</a> <a href="profiles.html">Developers</a>
@ -23,7 +22,44 @@ const NavBar = () => (
<a href="login.html">Login</a> <a href="login.html">Login</a>
</li> </li>
</ul> </ul>
</nav> );
);
const privateLinks = (
<ul>
<li>
<a href="profiles.html">Developers</a>
</li>
<li>
<a href="posts.html">Posts</a>
</li>
<li>
<a href="dashboard.html">
<FontAwesomeIcon icon={faUser} />
<span className="hide-sm"> Dashboard</span>
</a>
</li>
<li>
<a href="login.html">
<FontAwesomeIcon icon={faSignOutAlt} />
<span className="hide-sm"> Log out</span>
</a>
</li>
</ul>
);
/** Display appropriated links after loading given authenticated prop */
const RenderLinks = !loading && isAuthenticated ? privateLinks : publicLinks;
return (
<nav className="navbar bg-dark">
<h1>
<a href="dashboard.html">
<FontAwesomeIcon icon={faCode} /> DevBook
</a>
</h1>
{RenderLinks}
</nav>
);
};
export default NavBar; export default NavBar;

View file

@ -1,28 +1,42 @@
import React from 'react'; import React from 'react';
import NavBar from '../NavBar'; import NavBar from '../NavBar';
import {render} from '@testing-library/react'; import {render, RenderResult} from '@testing-library/react';
describe('Navbar displays', () => { describe('Navbar displays', () => {
let context: RenderResult;
beforeEach(() => {
context = render(<NavBar />);
});
describe('when user is not authenticated', () => { describe('when user is not authenticated', () => {
test('landing page link', () => { test('landing page link', () => {
const {getByText} = render(<NavBar />); const {getByText} = context;
const link = getByText('DevBook'); const link = getByText('DevBook');
expect(link).toBeInTheDocument(); expect(link).toBeInTheDocument();
}); });
test('developers link', () => { test('developers link', () => {
const {getByText} = render(<NavBar />); const {getByText} = context;
const link = getByText('Developers'); const link = getByText('Developers');
expect(link).toBeInTheDocument(); expect(link).toBeInTheDocument();
}); });
test('register link', () => { test('register link', () => {
const {getByText} = render(<NavBar />); const {getByText} = context;
const link = getByText('Register'); const link = getByText('Register');
expect(link).toBeInTheDocument(); expect(link).toBeInTheDocument();
}); });
test('login page link', () => { test('login page link', () => {
const {getByText} = render(<NavBar />); const {getByText} = context;
const link = getByText('Login'); const link = getByText('Login');
expect(link).toBeInTheDocument(); expect(link).toBeInTheDocument();
}); });
}); });
describe('when user is authenticated', () => {
test('logout page link', () => {
const {getByText} = context;
const link = getByText('Logout');
expect(link).toBeInTheDocument();
});
});
}); });