CommonConfirmationDialog.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = 'main-content'
  10. document.body.appendChild(app)
  11. })
  12. afterAll(() => {
  13. document.body.innerHTML = ''
  14. })
  15. describe('dialog confirm behaviour', () => {
  16. beforeEach(() => {
  17. confirmationOptions.value.delete('confirmation')
  18. })
  19. it('renders confirmation dialog with default values', async () => {
  20. const confirmCallbackSpy = vi.fn()
  21. const wrapper = renderComponent(CommonConfirmationDialog, {
  22. props: {
  23. uniqueId: 'confirmation',
  24. },
  25. router: true,
  26. })
  27. confirmationOptions.value.set('confirmation', {
  28. text: 'Test heading',
  29. confirmCallback: confirmCallbackSpy,
  30. cancelCallback: vi.fn(),
  31. closeCallback: vi.fn(),
  32. })
  33. await waitForNextTick()
  34. expect(wrapper.getByText('Test heading')).toBeInTheDocument()
  35. expect(wrapper.getByText('Yes')).toBeInTheDocument()
  36. await wrapper.events.click(wrapper.getByRole('button', { name: 'Yes' }))
  37. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  38. })
  39. it('renders confirmation dialog with variant', async () => {
  40. const confirmCallbackSpy = vi.fn()
  41. const wrapper = renderComponent(CommonConfirmationDialog, {
  42. props: {
  43. uniqueId: 'confirmation',
  44. },
  45. router: true,
  46. })
  47. confirmationOptions.value.set('confirmation', {
  48. confirmationVariant: 'delete',
  49. confirmCallback: confirmCallbackSpy,
  50. cancelCallback: vi.fn(),
  51. closeCallback: vi.fn(),
  52. })
  53. await waitForNextTick()
  54. expect(
  55. wrapper.getByRole('dialog', { name: 'Delete Object' }),
  56. ).toBeInTheDocument()
  57. expect(
  58. wrapper.getByText('Are you sure you want to delete this object?'),
  59. ).toBeInTheDocument()
  60. await wrapper.events.click(
  61. wrapper.getByRole('button', { name: 'Delete Object' }),
  62. )
  63. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  64. })
  65. it('renders confirmation dialog with custom values', async () => {
  66. const confirmCallbackSpy = vi.fn()
  67. const wrapper = renderComponent(CommonConfirmationDialog, {
  68. props: {
  69. uniqueId: 'confirmation',
  70. },
  71. router: true,
  72. })
  73. confirmationOptions.value.set('confirmation', {
  74. text: 'Test heading',
  75. buttonLabel: 'Custom button title',
  76. buttonVariant: 'danger',
  77. confirmCallback: confirmCallbackSpy,
  78. cancelCallback: vi.fn(),
  79. closeCallback: vi.fn(),
  80. })
  81. await waitForNextTick()
  82. expect(
  83. wrapper.getByRole('button', { name: 'Custom button title' }),
  84. ).toBeInTheDocument()
  85. expect(
  86. wrapper.getByRole('button', { name: 'Custom button title' }),
  87. ).toHaveClass('bg-pink-100')
  88. })
  89. it('closes the confirmation dialog by using cancel', async () => {
  90. const confirmCallbackSpy = vi.fn()
  91. const cancelCallbackSpy = vi.fn()
  92. const wrapper = renderComponent(CommonConfirmationDialog, {
  93. props: {
  94. uniqueId: 'confirmation',
  95. },
  96. router: true,
  97. })
  98. confirmationOptions.value.set('confirmation', {
  99. text: 'Test heading',
  100. confirmCallback: confirmCallbackSpy,
  101. cancelCallback: cancelCallbackSpy,
  102. closeCallback: vi.fn(),
  103. })
  104. await waitForNextTick()
  105. await wrapper.events.click(
  106. wrapper.getByRole('button', { name: 'Cancel & Go Back' }),
  107. )
  108. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  109. })
  110. })