CommonConfirmationDialog.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import { waitForNextTick } from '#tests/support/utils.ts'
  4. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  5. import CommonConfirmationDialog from '../CommonConfirmationDialog.vue'
  6. const { confirmationOptions } = useConfirmation()
  7. beforeAll(() => {
  8. const app = document.createElement('div')
  9. app.id = 'app'
  10. document.body.appendChild(app)
  11. })
  12. afterAll(() => {
  13. document.body.innerHTML = ''
  14. })
  15. describe('dialog confirm behaviour', () => {
  16. beforeEach(() => {
  17. confirmationOptions.value = undefined
  18. })
  19. it('renders confirmation dialog with default values', async () => {
  20. const confirmCallbackSpy = vi.fn()
  21. const wrapper = renderComponent(CommonConfirmationDialog)
  22. confirmationOptions.value = {
  23. text: 'Test heading',
  24. confirmCallback: confirmCallbackSpy,
  25. cancelCallback: vi.fn(),
  26. }
  27. await waitForNextTick()
  28. expect(wrapper.getByText('Test heading')).toBeInTheDocument()
  29. expect(wrapper.getByText('Yes')).toBeInTheDocument()
  30. await wrapper.events.click(wrapper.getByRole('button', { name: 'Yes' }))
  31. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  32. })
  33. it('renders confirmation dialog with variant', async () => {
  34. const confirmCallbackSpy = vi.fn()
  35. const wrapper = renderComponent(CommonConfirmationDialog)
  36. confirmationOptions.value = {
  37. confirmationVariant: 'delete',
  38. confirmCallback: confirmCallbackSpy,
  39. cancelCallback: vi.fn(),
  40. }
  41. await waitForNextTick()
  42. expect(
  43. wrapper.getByRole('dialog', { name: 'Delete Object' }),
  44. ).toBeInTheDocument()
  45. expect(
  46. wrapper.getByText('Are you sure you want to delete this object?'),
  47. ).toBeInTheDocument()
  48. await wrapper.events.click(
  49. wrapper.getByRole('button', { name: 'Delete Object' }),
  50. )
  51. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  52. })
  53. it('renders confirmation dialog with custom values', async () => {
  54. const confirmCallbackSpy = vi.fn()
  55. const wrapper = renderComponent(CommonConfirmationDialog)
  56. confirmationOptions.value = {
  57. text: 'Test heading',
  58. buttonLabel: 'Custom button title',
  59. buttonVariant: 'danger',
  60. confirmCallback: confirmCallbackSpy,
  61. cancelCallback: vi.fn(),
  62. }
  63. await waitForNextTick()
  64. expect(
  65. wrapper.getByRole('button', { name: 'Custom button title' }),
  66. ).toBeInTheDocument()
  67. expect(
  68. wrapper.getByRole('button', { name: 'Custom button title' }),
  69. ).toHaveClass('bg-pink-100')
  70. })
  71. it('closes the confirmation dialog by using cancel', async () => {
  72. const confirmCallbackSpy = vi.fn()
  73. const cancelCallbackSpy = vi.fn()
  74. const wrapper = renderComponent(CommonConfirmationDialog)
  75. confirmationOptions.value = {
  76. text: 'Test heading',
  77. confirmCallback: confirmCallbackSpy,
  78. cancelCallback: cancelCallbackSpy,
  79. }
  80. await waitForNextTick()
  81. await wrapper.events.click(
  82. wrapper.getByRole('button', { name: 'Cancel & Go Back' }),
  83. )
  84. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  85. })
  86. })