CommonConfirmation.spec.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. renderComponent,
  4. type ExtendedRenderResult,
  5. } from '#tests/support/components/index.ts'
  6. import { waitForNextTick } from '#tests/support/utils.ts'
  7. import { useConfirmation } from '#shared/composables/useConfirmation.ts'
  8. import CommonConfirmation from '../CommonConfirmation.vue'
  9. let wrapper: ExtendedRenderResult
  10. const { confirmationOptions } = useConfirmation()
  11. beforeEach(() => {
  12. confirmationOptions.value = undefined
  13. wrapper = renderComponent(CommonConfirmation, { shallow: false })
  14. })
  15. describe('popup confirm behaviour', () => {
  16. it('renders confirmation dialog with default values', async () => {
  17. const confirmCallbackSpy = vi.fn()
  18. confirmationOptions.value = {
  19. text: 'Test heading',
  20. confirmCallback: confirmCallbackSpy,
  21. cancelCallback: vi.fn(),
  22. closeCallback: vi.fn(),
  23. }
  24. await waitForNextTick()
  25. expect(wrapper.getByText('Test heading')).toBeInTheDocument()
  26. expect(wrapper.getByText('OK')).toBeInTheDocument()
  27. await wrapper.events.click(wrapper.getByText('OK'))
  28. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  29. })
  30. it('renders confirmation dialog with custom values', async () => {
  31. const confirmCallbackSpy = vi.fn()
  32. confirmationOptions.value = {
  33. text: 'Test heading',
  34. buttonLabel: 'Custom button title',
  35. buttonVariant: 'danger',
  36. confirmCallback: confirmCallbackSpy,
  37. cancelCallback: vi.fn(),
  38. closeCallback: vi.fn(),
  39. }
  40. await waitForNextTick()
  41. const button = wrapper.getByRole('button', { name: 'Custom button title' })
  42. expect(button).toBeInTheDocument()
  43. expect(button).toHaveClass('text-red-bright')
  44. })
  45. it('closes the confirmation dialog by using cancel', async () => {
  46. const confirmCallbackSpy = vi.fn()
  47. const cancelCallbackSpy = vi.fn()
  48. confirmationOptions.value = {
  49. text: 'Test heading',
  50. confirmCallback: confirmCallbackSpy,
  51. cancelCallback: cancelCallbackSpy,
  52. closeCallback: vi.fn(),
  53. }
  54. await waitForNextTick()
  55. await wrapper.events.click(wrapper.getByText('Cancel'))
  56. expect(wrapper.queryByText('Test heading')).not.toBeInTheDocument()
  57. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  58. })
  59. })