You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
import { describe, it, expect, vi } from 'vitest'
|
|
import { render, screen, fireEvent } from '@testing-library/svelte'
|
|
import ConfirmModal from './ConfirmModal.svelte'
|
|
|
|
describe('ConfirmModal', () => {
|
|
it('renders nothing when open is false', () => {
|
|
render(ConfirmModal, {
|
|
props: { open: false, title: 'Confirm', message: 'Are you sure?' },
|
|
})
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('renders title and message when open', () => {
|
|
render(ConfirmModal, {
|
|
props: {
|
|
open: true,
|
|
title: 'Delete deck',
|
|
message: 'This cannot be undone.',
|
|
},
|
|
})
|
|
expect(screen.getByRole('dialog')).toBeInTheDocument()
|
|
expect(screen.getByText('Delete deck')).toBeInTheDocument()
|
|
expect(screen.getByText('This cannot be undone.')).toBeInTheDocument()
|
|
})
|
|
|
|
it('calls onConfirm when Confirm is clicked', async () => {
|
|
const onConfirm = vi.fn()
|
|
render(ConfirmModal, {
|
|
props: {
|
|
open: true,
|
|
title: 'Confirm',
|
|
message: 'Proceed?',
|
|
confirmLabel: 'Yes',
|
|
onConfirm,
|
|
},
|
|
})
|
|
await fireEvent.click(screen.getByRole('button', { name: 'Yes' }))
|
|
expect(onConfirm).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('calls onCancel when Cancel is clicked', async () => {
|
|
const onCancel = vi.fn()
|
|
render(ConfirmModal, {
|
|
props: {
|
|
open: true,
|
|
title: 'Confirm',
|
|
message: 'Proceed?',
|
|
cancelLabel: 'No',
|
|
onCancel,
|
|
},
|
|
})
|
|
await fireEvent.click(screen.getByRole('button', { name: 'No' }))
|
|
expect(onCancel).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('calls onCancel on Escape key', async () => {
|
|
const onCancel = vi.fn()
|
|
render(ConfirmModal, {
|
|
props: { open: true, title: 'Confirm', message: 'Proceed?', onCancel },
|
|
})
|
|
const dialog = screen.getByRole('dialog')
|
|
await fireEvent.keyDown(dialog, { key: 'Escape' })
|
|
expect(onCancel).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('calls onCancel when backdrop is clicked', async () => {
|
|
const onCancel = vi.fn()
|
|
const { container } = render(ConfirmModal, {
|
|
props: { open: true, title: 'Confirm', message: 'Proceed?', onCancel },
|
|
})
|
|
const backdrop = container.querySelector('.modal-backdrop')
|
|
expect(backdrop).toBeTruthy()
|
|
await fireEvent.click(backdrop)
|
|
expect(onCancel).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|