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. expect(wrapper.getByText('Custom button title')).toBeInTheDocument()
  40. expect(wrapper.getByText('Custom button title')).toHaveClass(
  41. 'text-red-bright',
  42. )
  43. })
  44. it('closes the confirmation dialog by using cancel', async () => {
  45. const confirmCallbackSpy = vi.fn()
  46. const cancelCallbackSpy = vi.fn()
  47. confirmationOptions.value = {
  48. text: 'Test heading',
  49. confirmCallback: confirmCallbackSpy,
  50. cancelCallback: cancelCallbackSpy,
  51. }
  52. await waitForNextTick()
  53. await wrapper.events.click(wrapper.getByText('Cancel'))
  54. expect(wrapper.queryByText('Test heading')).not.toBeInTheDocument()
  55. expect(cancelCallbackSpy).toHaveBeenCalledTimes(1)
  56. })
  57. })