diff --git a/src/App.jsx b/src/App.jsx index fb172d86..b6e93b30 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -18,6 +18,7 @@ import Footer from './components/layout/Footer' import Support from './pages/Support' import RequireAnalysis from './components/RequireAnalysis' import NotFoundPage from './pages/NotFoundPage' +import DocumentTitle from './components/DocumentTitle' function Layout({ children }) { return ( @@ -44,6 +45,7 @@ function AppContent() { return ( + } /> } /> diff --git a/src/App.titles.test.jsx b/src/App.titles.test.jsx new file mode 100644 index 00000000..e3b5f6ad --- /dev/null +++ b/src/App.titles.test.jsx @@ -0,0 +1,105 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { fireEvent, render, screen, waitFor } from '@testing-library/react' +import { Link, MemoryRouter, useNavigate } from 'react-router-dom' +import App from './App' + +const analysis = vi.hoisted(() => ({ model: {}, loading: false, hydrating: false })) + +// Exercise the real App routes and guard without fetching organisation data +// or rendering charts and other unrelated page content. +vi.mock('./context/AppContext', () => ({ + AppProvider: ({ children }) => children, + useApp: () => analysis, +})) +vi.mock('./context/ThemeContext', () => ({ ThemeProvider: ({ children }) => children })) +vi.mock('./components/Navbar', () => ({ default: () => null })) +vi.mock('./components/RateLimitBanner', () => ({ default: () => null })) +vi.mock('./components/layout/Footer', () => ({ default: () => null })) +vi.mock('./pages/HomePage', () => ({ default: () =>

Home

})) +vi.mock('./pages/OverviewPage', () => ({ default: () => null })) +vi.mock('./pages/RepositoriesPage', () => ({ default: () => null })) +vi.mock('./pages/ContributorsPage', () => ({ default: () => null })) +vi.mock('./pages/ContributorProfilePage', () => ({ default: () => null })) +vi.mock('./pages/NetworkPage', () => ({ default: () => null })) +vi.mock('./pages/AnalyticsPage', () => ({ default: () => null })) +vi.mock('./pages/GovernancePage', () => ({ default: () => null })) +vi.mock('./pages/SettingsPage', () => ({ default: () => null })) +vi.mock('./pages/Support', () => ({ default: () => null })) +vi.mock('./pages/NotFoundPage', () => ({ default: () =>

Not found

})) + +function Navigation() { + const navigate = useNavigate() + return <> + Octocat + Hubot + + + +} + +function renderRoute(path) { + return render() +} + +describe('App document titles', () => { + let originalTitle + + beforeEach(() => { + originalTitle = document.title + document.title = 'OrgExplorer — GitHub Organization Analytics & Repository Insights' + analysis.model = {} + vi.spyOn(window, 'scrollTo').mockImplementation(() => {}) + }) + + afterEach(() => { + document.title = originalTitle + vi.restoreAllMocks() + }) + + it.each([ + ['/', 'OrgExplorer — Home'], + ['/overview', 'Overview — OrgExplorer'], + ['/repositories', 'Repositories — OrgExplorer'], + ['/contributors', 'Contributors — OrgExplorer'], + ['/contributors/octocat', 'octocat — Contributor Profile — OrgExplorer'], + ['/network', 'Network Graph — OrgExplorer'], + ['/analytics', 'Analytics — OrgExplorer'], + ['/governance', 'Governance Audit — OrgExplorer'], + ['/settings', 'Settings — OrgExplorer'], + ['/support-us', 'Support Us — OrgExplorer'], + ['/missing', '404 Not Found — OrgExplorer'], + ['/settings/?tab=token#help', 'Settings — OrgExplorer'], + ['/SETTINGS', 'Settings — OrgExplorer'], + ['/contributors/%6Fctocat', 'octocat — Contributor Profile — OrgExplorer'], + ])('sets the title for %s', (path, title) => { + renderRoute(path) + expect(document.title).toBe(title) + }) + + it.each(['/settings/extra', '/contributors/octocat/extra'])('uses the 404 title for %s', (path) => { + renderRoute(path) + expect(screen.getByRole('heading', { name: 'Not found' })).toBeInTheDocument() + expect(document.title).toBe('404 Not Found — OrgExplorer') + }) + + it('updates the title between profiles and when navigating back and forward', () => { + renderRoute('/settings') + fireEvent.click(screen.getByRole('link', { name: 'Octocat' })) + expect(document.title).toBe('octocat — Contributor Profile — OrgExplorer') + fireEvent.click(screen.getByRole('link', { name: 'Hubot' })) + expect(document.title).toBe('hubot — Contributor Profile — OrgExplorer') + fireEvent.click(screen.getByRole('button', { name: 'Back' })) + expect(document.title).toBe('octocat — Contributor Profile — OrgExplorer') + fireEvent.click(screen.getByRole('button', { name: 'Back' })) + expect(document.title).toBe('Settings — OrgExplorer') + fireEvent.click(screen.getByRole('button', { name: 'Forward' })) + expect(document.title).toBe('octocat — Contributor Profile — OrgExplorer') + }) + + it('uses the Home title after the analysis guard redirects', async () => { + analysis.model = null + renderRoute('/overview') + expect(await screen.findByRole('heading', { name: 'Home' })).toBeInTheDocument() + await waitFor(() => expect(document.title).toBe('OrgExplorer — Home')) + }) +}) diff --git a/src/components/DocumentTitle.jsx b/src/components/DocumentTitle.jsx new file mode 100644 index 00000000..5166c9a7 --- /dev/null +++ b/src/components/DocumentTitle.jsx @@ -0,0 +1,32 @@ +import { useEffect } from 'react' +import { matchRoutes, useLocation } from 'react-router-dom' + +// Use the router's matching rules, including full paths and decoded parameters. +const titleRoutes = [ + { path: '/', title: 'OrgExplorer — Home' }, + { path: '/overview', title: 'Overview — OrgExplorer' }, + { path: '/repositories', title: 'Repositories — OrgExplorer' }, + { path: '/contributors', title: 'Contributors — OrgExplorer' }, + { + path: '/contributors/:username', + title: ({ username }) => `${username} — Contributor Profile — OrgExplorer`, + }, + { path: '/network', title: 'Network Graph — OrgExplorer' }, + { path: '/analytics', title: 'Analytics — OrgExplorer' }, + { path: '/governance', title: 'Governance Audit — OrgExplorer' }, + { path: '/settings', title: 'Settings — OrgExplorer' }, + { path: '/support-us', title: 'Support Us — OrgExplorer' }, + { path: '*', title: '404 Not Found — OrgExplorer' }, +] + +export default function DocumentTitle() { + const { pathname } = useLocation() + const [{ route, params }] = matchRoutes(titleRoutes, pathname) + const title = typeof route.title === 'function' ? route.title(params) : route.title + + useEffect(() => { + document.title = title + }, [title]) + + return null +}