|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { cleanup, findByRole, fireEvent, getByLabelText, getByRole } from '@testing-library/vue' |
| 7 | +import { afterEach, expect, test } from 'vitest' |
| 8 | +import { showConfirmation } from './dialogs.ts' |
| 9 | + |
| 10 | +afterEach(cleanup) |
| 11 | + |
| 12 | +test('Show confirmation dialog', async () => { |
| 13 | + const promise = showConfirmation({ |
| 14 | + name: 'Dialog name', |
| 15 | + text: 'Dialog text', |
| 16 | + }) |
| 17 | + |
| 18 | + const dialog = await findByRole(document.documentElement, 'dialog') |
| 19 | + expect(dialog).toBeInstanceOf(HTMLElement) |
| 20 | + |
| 21 | + expect(getByLabelText(document.documentElement, 'Dialog name')).toBe(dialog) |
| 22 | + expect(dialog.textContent).toContain('Dialog text') |
| 23 | + |
| 24 | + const close = getByRole(dialog, 'button', { name: 'Close' }) |
| 25 | + expect(close).toBeInstanceOf(HTMLElement) |
| 26 | + const confirm = getByRole(dialog, 'button', { name: 'Confirm' }) |
| 27 | + expect(confirm).toBeInstanceOf(HTMLElement) |
| 28 | + await fireEvent(confirm, new MouseEvent('click', { bubbles: true })) |
| 29 | + expect(promise).resolves.toBe(true) |
| 30 | +}) |
| 31 | + |
| 32 | +test('show confirmation dialog with reject', async () => { |
| 33 | + const promise = showConfirmation({ |
| 34 | + name: 'Dialog name', |
| 35 | + text: 'Dialog text', |
| 36 | + labelConfirm: 'My confirm', |
| 37 | + labelReject: 'My reject', |
| 38 | + }) |
| 39 | + |
| 40 | + const dialog = await findByRole(document.documentElement, 'dialog') |
| 41 | + const confirm = getByRole(dialog, 'button', { name: 'My confirm' }) |
| 42 | + const reject = getByRole(dialog, 'button', { name: 'My reject' }) |
| 43 | + expect(confirm).toBeInstanceOf(HTMLElement) |
| 44 | + expect(reject).toBeInstanceOf(HTMLElement) |
| 45 | + await fireEvent(reject, new MouseEvent('click', { bubbles: true })) |
| 46 | + expect(promise).resolves.toBe(false) |
| 47 | +}) |
| 48 | + |
| 49 | +test('show confirmation dialog and close', async () => { |
| 50 | + const promise = showConfirmation({ |
| 51 | + name: 'Dialog name', |
| 52 | + text: 'Dialog text', |
| 53 | + }) |
| 54 | + |
| 55 | + const dialog = await findByRole(document.documentElement, 'dialog') |
| 56 | + const close = getByRole(dialog, 'button', { name: 'Close' }) |
| 57 | + expect(close).toBeInstanceOf(HTMLElement) |
| 58 | + await fireEvent(close, new MouseEvent('click', { bubbles: true })) |
| 59 | + expect(promise).rejects.toThrowError('Dialog closed') |
| 60 | +}) |
0 commit comments