Skip to content
Open
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
79 changes: 79 additions & 0 deletions src/components/Navbar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import Navbar from './Navbar';

const navLinks = [
{ label: 'Home', path: '/' },
{ label: 'Pathfinding', path: '/pathfinding' },
{ label: 'Game Theory', path: '/game-theory' },
{ label: 'Project Calculator', path: '/project-calculator' },
{ label: 'Income Tax', path: '/income-tax' },
{ label: 'AlgoCraft', path: '/algo-ds' },
];

const renderNavbar = (initialPath = '/') =>
render(
<MemoryRouter initialEntries={[initialPath]}>
<Navbar />
</MemoryRouter>
);

describe('Navbar', () => {
it('renders the site title and all navigation links with correct destinations', () => {
renderNavbar();

expect(screen.getByRole('heading', { name: /nickspace/i })).toBeInTheDocument();

const links = screen.getAllByRole('link');
expect(links).toHaveLength(navLinks.length);

navLinks.forEach(({ label, path }) => {
const link = screen.getByRole('link', { name: label });
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute('href', expect.stringContaining(path));
expect(link).toHaveClass('nav-link');
});
});

it('marks the Home link as active when on the root path', () => {
renderNavbar('/');

const homeLink = screen.getByRole('link', { name: 'Home' });
expect(homeLink).toHaveClass('active');

navLinks
.filter(({ label }) => label !== 'Home')
.forEach(({ label }) => {
expect(screen.getByRole('link', { name: label })).not.toHaveClass('active');
});
});

it('marks only the matching link as active for secondary routes', () => {
renderNavbar('/pathfinding');

const pathfindingLink = screen.getByRole('link', { name: 'Pathfinding' });
expect(pathfindingLink).toHaveClass('active');

navLinks
.filter(({ label }) => label !== 'Pathfinding')
.forEach(({ label }) => {
expect(screen.getByRole('link', { name: label })).not.toHaveClass('active');
});
});

it('does not mark any link as active for unmatched paths', () => {
renderNavbar('/non-existent');

navLinks.forEach(({ label }) => {
expect(screen.getByRole('link', { name: label })).not.toHaveClass('active');
});
});

it('requires exact path matches before applying the active class', () => {
renderNavbar('/pathfinding/');

const pathfindingLink = screen.getByRole('link', { name: 'Pathfinding' });
expect(pathfindingLink).not.toHaveClass('active');
});
});