CommonConfirmation.spec.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  23. await waitForNextTick()
  24. expect(wrapper.getByText('Test heading')).toBeInTheDocument()
  25. expect(wrapper.getByText('OK')).toBeInTheDocument()
  26. await wrapper.events.click(wrapper.getByText('OK'))
  27. expect(confirmCallbackSpy).toHaveBeenCalledTimes(1)
  28. })
  29. it('renders confirmation dialog with custom values', async () => {
  30. const confirmCallbackSpy = vi.fn()
  31. confirmationOptions.value = {
  32. text: 'Test heading',
  33. buttonLabel: 'Custom button title',
  34. buttonVariant: 'danger',
  35. confirmCallback: confirmCallbackSpy,
  36. cancelCallback: vi.fn(),
  37. }
  38. await waitForNextTick()
  39. const button = wrapper.getByRole('button', { name: 'Custom button title' })
  40. expect(button).toBeInTheDocument()
  41. expect(button).toHaveClass('text-red-bright')
  42. })
  43. it('closes the confirmation dialog by using cancel', async () => {
  44. const confirmCallbackSpy = vi.fn()
  45. const cancelCallbackSpy = vi.fn()
  46. confirmationOptions.value = {
  47. text: 'Test heading',
  48. confirmCallback: confirmCallbackSpy,
  49. cancelCallback: cancelCallbackSpy,
  50. }
  51. await waitForNextTick()
  52. await wrapper.events.click(wrapper.getByText('Cancel'))
  53. expect(wrapper.queryByText('Test heading')).not.toBeInTheDocument()
  54. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  55. })
  56. })