import { render, screen, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { describe, it, expect, vi } from 'vitest' import BulkDeleteCertificateDialog from '../../dialogs/BulkDeleteCertificateDialog' import type { Certificate } from '../../../api/certificates' const makeCert = (overrides: Partial): Certificate => ({ uuid: 'cert-1', name: 'Test Cert', domains: 'test.example.com', issuer: 'Custom CA', expires_at: '2026-01-01T00:00:00Z', status: 'valid', provider: 'custom', has_key: true, in_use: false, ...overrides, }) const certs: Certificate[] = [ makeCert({ uuid: 'cert-1', name: 'Cert One', domains: 'one.example.com' }), makeCert({ uuid: 'cert-2', name: 'Cert Two', domains: 'two.example.com', provider: 'letsencrypt-staging', status: 'untrusted' }), makeCert({ uuid: 'cert-3', name: 'Cert Three', domains: 'three.example.com', provider: 'letsencrypt', status: 'expired' }), ] describe('BulkDeleteCertificateDialog', () => { it('renders dialog with count in title when 3 certs supplied', () => { render( ) const dialog = screen.getByRole('dialog') expect(within(dialog).getByRole('heading', { name: 'Delete 3 Certificate(s)' })).toBeInTheDocument() }) it('lists each certificate name in the scrollable list', () => { render( ) expect(screen.getByText('Cert One')).toBeInTheDocument() expect(screen.getByText('Cert Two')).toBeInTheDocument() expect(screen.getByText('Cert Three')).toBeInTheDocument() expect(screen.getByText('Custom')).toBeInTheDocument() expect(screen.getByText('Staging')).toBeInTheDocument() expect(screen.getByText('Expired LE')).toBeInTheDocument() }) it('calls onConfirm when the Delete button is clicked', async () => { const onConfirm = vi.fn() const user = userEvent.setup() render( ) const dialog = screen.getByRole('dialog') await user.click(within(dialog).getByRole('button', { name: 'Delete 3 Certificate(s)' })) expect(onConfirm).toHaveBeenCalled() }) it('calls onCancel when the Cancel button is clicked', async () => { const onCancel = vi.fn() const user = userEvent.setup() render( ) const dialog = screen.getByRole('dialog') await user.click(within(dialog).getByRole('button', { name: 'Cancel' })) expect(onCancel).toHaveBeenCalled() }) it('Delete button is loading/disabled when isDeleting is true', () => { render( ) const dialog = screen.getByRole('dialog') const deleteBtn = within(dialog).getByRole('button', { name: 'Delete 3 Certificate(s)' }) expect(deleteBtn).toBeDisabled() const cancelBtn = within(dialog).getByRole('button', { name: 'Cancel' }) expect(cancelBtn).toBeDisabled() }) it('returns null when certificates array is empty', () => { const { container } = render( ) expect(container.innerHTML).toBe('') }) it('renders "Expiring LE" label for a letsencrypt cert with status expiring', () => { const expiringCert = makeCert({ uuid: 'cert-4', name: 'Expiring Cert', domains: 'expiring.example.com', provider: 'letsencrypt', status: 'expiring' }) render( ) expect(screen.getByText('Expiring LE')).toBeInTheDocument() }) })