Files
AndreaSlopFotografie/web/src/components/BackToTop/BackToTop.test.tsx
T
SluijsensandClaude Opus 5.5 4d6408b607 Completes the front-end shell with footer, status pages and SEO
Adds the footer, back-to-top button, skip link, placeholder routes,
404 and error pages inside the shell, plus per-page SEO from a single
page registry (canonical URLs, Open Graph image, JSON-LD, generated
sitemap.xml and robots.txt).

Theme tokens and recipes replace repeated styling, the hero loads only
the active and neighbouring slides, and the e-mail address is joined
in the browser to keep it out of the HTML and bundle.

Adds Vitest with property-based (fast-check) and axe tests, ESLint,
security headers for dev and preview, Lighthouse CI and a `pnpm check`
script that runs all quality gates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:05:02 +02:00

88 lines
2.9 KiB
TypeScript

import { act, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { axeResults } from '@/test/axe';
import { renderWithProviders } from '@/test/renderWithProviders';
import BackToTop from './BackToTop';
const scrollTo = (y: number) =>
act(() => {
Object.defineProperty(window, 'scrollY', {
value: y,
configurable: true,
});
window.dispatchEvent(new Event('scroll'));
});
const reducedMotion = (matches: boolean) =>
vi.spyOn(window, 'matchMedia').mockImplementation(
(query: string) =>
({
matches: matches && query.includes('reduce'),
media: query,
addEventListener: () => {},
removeEventListener: () => {},
}) as unknown as MediaQueryList,
);
const renderButton = () =>
renderWithProviders(
<>
<main id="main" tabIndex={-1}>
inhoud
</main>
<BackToTop />
</>,
);
const button = () => screen.getByTestId('back-to-top-button');
afterEach(() => {
Object.defineProperty(window, 'scrollY', { value: 0, configurable: true });
});
describe('BackToTop', () => {
it('is bovenaan inert: niet focusbaar en niet voorgelezen', async () => {
await renderButton();
expect(button()).toHaveAttribute('inert');
expect(
screen.queryByRole('button', { name: 'Terug naar boven' }),
).toBeNull();
});
it('verschijnt pas voorbij 60% van de viewporthoogte', async () => {
await renderButton();
const threshold = 0.6 * window.innerHeight;
await scrollTo(threshold);
expect(button()).toHaveAttribute('inert');
await scrollTo(threshold + 1);
expect(button()).not.toHaveAttribute('inert');
expect(screen.getByRole('button', { name: 'Terug naar boven' })).toBe(
button(),
);
});
it('scrolt soepel naar boven en zet de focus op de inhoud', async () => {
const scroll = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {});
const { container } = await renderButton();
await scrollTo(2000);
await userEvent.click(button());
expect(scroll).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' });
expect(document.activeElement).toBe(document.getElementById('main'));
expect(await axeResults(container)).toHaveNoViolations();
});
it('springt direct naar boven bij reduced motion', async () => {
reducedMotion(true);
const scroll = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {});
await renderButton();
await scrollTo(2000);
await userEvent.click(button());
expect(scroll).toHaveBeenCalledWith({ top: 0, behavior: 'instant' });
});
});