Show a confirmation toast when the Sentry test button is clicked
Continuous Integration / config (pull_request) Successful in 9s
Continuous Integration / prepare (pull_request) Successful in 1m12s
Continuous Integration / build (pull_request) Successful in 1m53s
Continuous Integration / test (pull_request) Successful in 1m49s
Continuous Integration / deploy-test (pull_request) Skipped

Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
2026-07-25 14:23:46 +02:00
co-authored by Junie
parent f1a3c3bdd0
commit 1207bc1453
3 changed files with 92 additions and 9 deletions
+38 -7
View File
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react';
import * as Sentry from '@sentry/react';
/**
@@ -8,6 +9,9 @@ import * as Sentry from '@sentry/react';
*/
const isVisible = import.meta.env.DEV || import.meta.env.VITE_APP_ENV === 'test';
/** Hoe lang de bevestigingsmelding zichtbaar blijft voordat hij vanzelf verdwijnt. */
const TOAST_DURATION_MS = 4000;
/**
* Losstaand van het component zodat dit direct (zonder DOM/React event-dispatch)
* getest kan worden. React vangt fouten uit event handlers namelijk NIET op via
@@ -31,17 +35,44 @@ export function handleSentryTestErrorClick(): void {
* daadwerkelijk binnenkomen.
*/
export function SentryTestButton() {
const [showToast, setShowToast] = useState(false);
// Verberg de melding automatisch na een paar seconden.
useEffect(() => {
if (!showToast) return;
const hideTimer = setTimeout(() => setShowToast(false), TOAST_DURATION_MS);
return () => clearTimeout(hideTimer);
}, [showToast]);
if (!isVisible) {
return null;
}
function onClick() {
setShowToast(true);
// De daadwerkelijke fout (met log + metric) pas na deze tick versturen,
// zodat de melding hierboven eerst kan renderen/schilderen voordat de
// onafgevangen fout verderop wordt gegooid.
setTimeout(handleSentryTestErrorClick, 0);
}
return (
<button
type="button"
onClick={handleSentryTestErrorClick}
className="fixed bottom-4 right-4 z-50 rounded bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-lg hover:bg-red-700"
>
Break the world
</button>
<>
{showToast && (
<div
role="status"
className="fixed bottom-20 right-4 z-50 rounded bg-green-600 px-4 py-2 text-sm font-semibold text-white shadow-lg"
>
Test error verzonden naar Sentry
</div>
)}
<button
type="button"
onClick={onClick}
className="fixed bottom-4 right-4 z-50 rounded bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-lg hover:bg-red-700"
>
Break the world
</button>
</>
);
}
@@ -1,5 +1,5 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, act } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as Sentry from '@sentry/react';
import { SentryTestButton, handleSentryTestErrorClick } from '../SentryTestButton';
@@ -9,9 +9,30 @@ vi.mock('@sentry/react', () => ({
metrics: { count: vi.fn() },
}));
/**
* De echte fout wordt bewust ongevangen via een `setTimeout` gegooid (zie
* SentryTestButton.tsx). Om dat in deze fake-timer-tests te simuleren zonder
* dat de test zelf crasht op die intentionele throw, wordt het "doortikken"
* van de timers hierin afgevangen.
*/
function advanceTimersIgnoringIntentionalThrow(ms: number) {
try {
act(() => {
vi.advanceTimersByTime(ms);
});
} catch {
// verwacht: de intentionele test-fout uit handleSentryTestErrorClick
}
}
describe('SentryTestButton', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('is visible in the current (development/test) build', () => {
@@ -27,4 +48,34 @@ describe('SentryTestButton', () => {
});
expect(Sentry.metrics.count).toHaveBeenCalledWith('test_counter', 1);
});
it('shows a confirmation toast immediately when clicked, before the test error fires', () => {
render(<SentryTestButton />);
fireEvent.click(screen.getByRole('button', { name: 'Break the world' }));
expect(screen.getByRole('status')).toHaveTextContent('Test error verzonden naar Sentry');
// De log/metric/throw zijn pas gepland (setTimeout), nog niet uitgevoerd.
expect(Sentry.logger.info).not.toHaveBeenCalled();
advanceTimersIgnoringIntentionalThrow(0);
expect(Sentry.logger.info).toHaveBeenCalledWith('User triggered test error', {
action: 'test_error_button_click',
});
expect(Sentry.metrics.count).toHaveBeenCalledWith('test_counter', 1);
});
it('hides the toast again after it has been shown for a while', () => {
render(<SentryTestButton />);
fireEvent.click(screen.getByRole('button', { name: 'Break the world' }));
expect(screen.getByRole('status')).toBeInTheDocument();
// De intentionele fout (0ms-timer) stopt het "doortikken" van timers
// binnen dezelfde advanceTimersByTime-call, dus eerst die apart afhandelen
// voordat de resterende hide-timer (4000ms) doorgetikt kan worden.
advanceTimersIgnoringIntentionalThrow(0);
advanceTimersIgnoringIntentionalThrow(4000);
expect(screen.queryByRole('status')).not.toBeInTheDocument();
});
});