Files
AndreaSlopFotografie/web/src/components/Analytics/UmamiAnalytics.test.tsx
T
SluijsensandClaude Sonnet 5.5 49b1d5f0ba Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment
One build per environment: PROD_* variables for the production artifact,
TEST_* for the test build and the CSP check. The build fails when a variable
is empty. Tests use the production values from config/monitoringTestEnv.ts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 19:29:51 +01:00

59 lines
1.9 KiB
TypeScript

import { render } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import UmamiAnalytics from './UmamiAnalytics';
const umamiScripts = () =>
document.querySelectorAll<HTMLScriptElement>(
'script[src="https://analytics.slpsoftware.nl/script.js"]',
);
afterEach(() => {
vi.unstubAllEnvs();
umamiScripts().forEach((script) => script.remove());
});
describe('UmamiAnalytics', () => {
it('voegt geen script toe op een onbekende host', () => {
render(<UmamiAnalytics hostname="localhost" />);
expect(umamiScripts()).toHaveLength(0);
});
it.each([
['production', 'andreaslop.nl', '3edbb7d1-4388-4f2d-9cef-11ade88ace96'],
['test', 'test.andreaslop.nl', 'test-website'],
])(
'telt in de %s-build op %s met de juiste website',
(environment, hostname, websiteId) => {
vi.stubEnv('VITE_APP_ENV', environment);
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', websiteId);
render(<UmamiAnalytics hostname={hostname} />);
const [script] = umamiScripts();
expect(script.defer).toBe(true);
expect(script.dataset).toMatchObject({
websiteId,
domains: hostname,
doNotTrack: 'true',
});
},
);
it('voegt geen script toe op de host van de andere omgeving', () => {
render(<UmamiAnalytics hostname="test.andreaslop.nl" />);
expect(umamiScripts()).toHaveLength(0);
});
it('voegt het script maar één keer toe', () => {
render(<UmamiAnalytics hostname="andreaslop.nl" />);
render(<UmamiAnalytics hostname="andreaslop.nl" />);
expect(umamiScripts()).toHaveLength(1);
});
it('rendert niets zichtbaars', () => {
const { container } = render(
<UmamiAnalytics hostname="andreaslop.nl" />,
);
expect(container).toBeEmptyDOMElement();
});
});