import { describe, expect, it, vi } from 'vitest' import { ref } from 'vue' import type { Article } from '@/composables/useArticles' import { DEFAULT_ARTICLE_SORT } from '@/lib/articleListQuery' import { useArticleRowActions } from '../useArticleRowActions' const confirmDelete = vi.fn() vi.mock('@/composables/useArticleActions', () => ({ useArticleActions: () => ({ confirmDelete: (...args: unknown[]) => confirmDelete(...args), publish: vi.fn(), deleteArticle: vi.fn(), }), })) vi.mock('@/i18n/presentation', () => ({ useLocalizedToast: () => ({ add: vi.fn() }), })) const article = { id: 4, title: 'Winter checklist', body: '', is_online: false, created_at: '2026-09-02T09:00:00Z', updated_at: '2026-09-05T09:00:00Z', } as Article function setup() { return useArticleRowActions({ items: ref([article]), total: ref(1), counts: ref({ all: 1, online: 0, draft: 1 }), shown: ref({ page: 1, size: 25, q: '', status: '', sort: DEFAULT_ARTICLE_SORT, site: '', seo: '', }), reload: vi.fn(), }) } describe('handleDelete', () => { it('opens one confirm dialog when asked twice in a row', async () => { confirmDelete.mockReset() let settle: (value: boolean) => void = () => undefined confirmDelete.mockReturnValue(new Promise((resolve) => (settle = resolve))) const { handleDelete } = setup() const first = handleDelete(article) const second = handleDelete(article) expect(confirmDelete).toHaveBeenCalledTimes(1) settle(false) await Promise.all([first, second]) }) it('allows another dialog once the first one is settled', async () => { confirmDelete.mockReset() confirmDelete.mockResolvedValue(false) const { handleDelete } = setup() await handleDelete(article) await handleDelete(article) expect(confirmDelete).toHaveBeenCalledTimes(2) }) })