Files
goodgo-platform/apps/web/app/[locale]/__tests__/not-found.spec.tsx
Ho Ngoc Hai 0168f1f6f5 test(web): add component tests for Navbar, NotFound and Error pages [GOO-105]
- navbar.spec.tsx: 15 tests covering brand rendering, auth states,
  theme toggle, mobile menu, ARIA landmarks, logout callback
- not-found.spec.tsx: 4 tests covering 404 display, home/search links
- error.spec.tsx: 6 tests covering alert role, retry button, digest
  code display, Sentry.captureException call, auto-retry timer

All 116 web test files (937 tests) pass. Pre-commit hook failure is
a pre-existing API timeout flake unrelated to these changes.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-24 10:17:23 +07:00

67 lines
2.1 KiB
TypeScript

/* eslint-disable import-x/order */
/**
* Tests for the locale-aware 404 Not Found page.
* Located at app/[locale]/not-found.tsx — uses next-intl and @/i18n/navigation.
*/
import { render, screen } from '@testing-library/react';
import * as React from 'react';
import { describe, expect, it, vi } from 'vitest';
// Mock next-intl with Vietnamese messages
const viMessages = await import('@/messages/vi.json');
vi.mock('next-intl', () => ({
useTranslations: (namespace?: string) => {
const messages = viMessages.default ?? viMessages;
const ns = namespace
? (messages[namespace as keyof typeof messages] as Record<string, unknown> | undefined)
: (messages as unknown as Record<string, unknown>);
return (key: string) => {
if (!ns) return key;
const parts = key.split('.');
let val: unknown = ns;
for (const p of parts) {
val = (val as Record<string, unknown>)?.[p];
}
return typeof val === 'string' ? val : key;
};
},
useLocale: () => 'vi',
}));
vi.mock('@/i18n/navigation', () => ({
Link: ({ children, href, className }: { children: React.ReactNode; href: string; className?: string }) => (
<a href={href} className={className}>{children}</a>
),
useRouter: () => ({ push: vi.fn() }),
usePathname: () => '/not-found',
}));
import NotFound from '../not-found';
describe('NotFound (locale) page', () => {
it('renders the 404 numeric display', () => {
render(<NotFound />);
expect(screen.getByText('404')).toBeInTheDocument();
});
it('renders a home link', () => {
render(<NotFound />);
const homeLinks = screen.getAllByRole('link');
const hasHomeLink = homeLinks.some((a) => a.getAttribute('href') === '/');
expect(hasHomeLink).toBe(true);
});
it('renders a search link', () => {
render(<NotFound />);
const links = screen.getAllByRole('link');
const hasSearchLink = links.some((a) => a.getAttribute('href') === '/search');
expect(hasSearchLink).toBe(true);
});
it('renders the page title text', () => {
render(<NotFound />);
const headings = screen.getAllByRole('heading');
expect(headings.length).toBeGreaterThan(0);
});
});