From 63f906ef5804f19a60a0073e04d4cab617bc7415 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Fri, 26 Sep 2025 06:41:30 +0000 Subject: [PATCH] CodeRabbit Generated Unit Tests: Add Navbar tests for links and active states; update script to echo --- src/components/Navbar.test.js | 79 +++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/components/Navbar.test.js diff --git a/src/components/Navbar.test.js b/src/components/Navbar.test.js new file mode 100644 index 0000000..0477e74 --- /dev/null +++ b/src/components/Navbar.test.js @@ -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( + + + + ); + +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'); + }); +}); \ No newline at end of file