diff --git a/frontend/src/routes/+error.svelte b/frontend/src/routes/+error.svelte index f910cb33..56ce49c4 100644 --- a/frontend/src/routes/+error.svelte +++ b/frontend/src/routes/+error.svelte @@ -7,10 +7,16 @@ let copied = $state(false); function copyId() { const id = page.error?.errorId; if (!id) return; - navigator.clipboard.writeText(id).then(() => { - copied = true; - setTimeout(() => (copied = false), 2000); - }); + if (!navigator.clipboard) return; + navigator.clipboard.writeText(id).then( + () => { + copied = true; + setTimeout(() => (copied = false), 2000); + }, + () => { + /* clipboard denied or unavailable — select-all on the element remains */ + } + ); } diff --git a/frontend/src/routes/error.svelte.test.ts b/frontend/src/routes/error.svelte.test.ts index 7ed53214..0ceca01f 100644 --- a/frontend/src/routes/error.svelte.test.ts +++ b/frontend/src/routes/error.svelte.test.ts @@ -94,4 +94,38 @@ describe('+error.svelte', () => { await expect.element(browserPage.getByText('Fehler-ID')).not.toBeInTheDocument(); }); + + it('shows "Kopiert!" after clicking the copy button', async () => { + mockPage.status = 500; + mockPage.error = { message: 'Something broke', errorId: 'abc-123-def' }; + + Object.defineProperty(navigator, 'clipboard', { + value: { writeText: vi.fn().mockResolvedValue(undefined) }, + configurable: true, + writable: true + }); + + const ErrorPage = await loadComponent(); + render(ErrorPage); + + await browserPage.getByRole('button', { name: 'ID kopieren' }).click(); + await expect.element(browserPage.getByText('Kopiert!')).toBeVisible(); + }); + + it('does not show "Kopiert!" when clipboard write is rejected', async () => { + mockPage.status = 500; + mockPage.error = { message: 'Something broke', errorId: 'abc-123-def' }; + + Object.defineProperty(navigator, 'clipboard', { + value: { writeText: vi.fn().mockRejectedValue(new Error('denied')) }, + configurable: true, + writable: true + }); + + const ErrorPage = await loadComponent(); + render(ErrorPage); + + await browserPage.getByRole('button', { name: 'ID kopieren' }).click(); + await expect.element(browserPage.getByText('Kopiert!')).not.toBeInTheDocument(); + }); });