Files
accounted/components/extensions/general/__tests__/inbox-address-copy.test.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

70 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { copyInboxAddress } from '@/components/extensions/general/inbox-address-copy'
beforeEach(() => {
vi.clearAllMocks()
vi.unstubAllGlobals()
})
afterEach(() => {
vi.unstubAllGlobals()
})
describe('copyInboxAddress', () => {
it('reports copied and forwards the exact address on success', async () => {
const writeText = vi.fn(async () => undefined)
vi.stubGlobal('navigator', { clipboard: { writeText } })
await expect(copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')).resolves.toBe('copied')
expect(writeText).toHaveBeenCalledWith('faktura-a1b2c3@inbox.gnubok.se')
})
it('reports failed when the write is rejected, never copied', async () => {
// The regression guard. The old handler swallowed this rejection in an empty
// catch and then showed "Adress kopierad" anyway, so the user walked away
// believing they had the inbox address and waited for invoices at an address
// they never captured.
vi.stubGlobal('navigator', {
clipboard: {
writeText: vi.fn(async () => {
throw new DOMException('Write permission denied.', 'NotAllowedError')
}),
},
})
const state = await copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')
expect(state).toBe('failed')
expect(state).not.toBe('copied')
})
it('reports failed on a non-Error rejection', async () => {
vi.stubGlobal('navigator', {
clipboard: { writeText: vi.fn(() => Promise.reject('nope')) },
})
await expect(copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')).resolves.toBe('failed')
})
it('reports failed when the Clipboard API is missing (insecure context)', async () => {
// No API means no programmatic fallback either: the header must keep the
// address visible and selectable instead of claiming a copy.
vi.stubGlobal('navigator', {})
const state = await copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')
expect(state).toBe('failed')
expect(state).not.toBe('copied')
})
it('reports failed when clipboard exists but writeText does not', async () => {
vi.stubGlobal('navigator', { clipboard: { read: vi.fn() } })
await expect(copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')).resolves.toBe('failed')
})
it('does not throw when there is no navigator at all', async () => {
vi.stubGlobal('navigator', undefined)
await expect(copyInboxAddress('faktura-a1b2c3@inbox.gnubok.se')).resolves.toBe('failed')
})
})