CommonConfirmationDialog.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. closeCallback: vi.fn(),
  27. }
  28. await waitForNextTick()
  29. expect(wrapper.getByText('Test heading')).toBeInTheDocument()
  30. expect(wrapper.getByText('Yes')).toBeInTheDocument()
  31. await wrapper.events.click(wrapper.getByRole('button', { name: 'Yes' }))
  32. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  33. })
  34. it('renders confirmation dialog with variant', async () => {
  35. const confirmCallbackSpy = vi.fn()
  36. const wrapper = renderComponent(CommonConfirmationDialog)
  37. confirmationOptions.value = {
  38. confirmationVariant: 'delete',
  39. confirmCallback: confirmCallbackSpy,
  40. cancelCallback: vi.fn(),
  41. closeCallback: vi.fn(),
  42. }
  43. await waitForNextTick()
  44. expect(
  45. wrapper.getByRole('dialog', { name: 'Delete Object' }),
  46. ).toBeInTheDocument()
  47. expect(
  48. wrapper.getByText('Are you sure you want to delete this object?'),
  49. ).toBeInTheDocument()
  50. await wrapper.events.click(
  51. wrapper.getByRole('button', { name: 'Delete Object' }),
  52. )
  53. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  54. })
  55. it('renders confirmation dialog with custom values', async () => {
  56. const confirmCallbackSpy = vi.fn()
  57. const wrapper = renderComponent(CommonConfirmationDialog)
  58. confirmationOptions.value = {
  59. text: 'Test heading',
  60. buttonLabel: 'Custom button title',
  61. buttonVariant: 'danger',
  62. confirmCallback: confirmCallbackSpy,
  63. cancelCallback: vi.fn(),
  64. closeCallback: vi.fn(),
  65. }
  66. await waitForNextTick()
  67. expect(
  68. wrapper.getByRole('button', { name: 'Custom button title' }),
  69. ).toBeInTheDocument()
  70. expect(
  71. wrapper.getByRole('button', { name: 'Custom button title' }),
  72. ).toHaveClass('bg-pink-100')
  73. })
  74. it('closes the confirmation dialog by using cancel', async () => {
  75. const confirmCallbackSpy = vi.fn()
  76. const cancelCallbackSpy = vi.fn()
  77. const wrapper = renderComponent(CommonConfirmationDialog)
  78. confirmationOptions.value = {
  79. text: 'Test heading',
  80. confirmCallback: confirmCallbackSpy,
  81. cancelCallback: cancelCallbackSpy,
  82. closeCallback: vi.fn(),
  83. }
  84. await waitForNextTick()
  85. await wrapper.events.click(
  86. wrapper.getByRole('button', { name: 'Cancel & Go Back' }),
  87. )
  88. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  89. })
  90. })