useCopyToClipboard.spec.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import { waitForNextTick } from '#tests/support/utils.ts'
  4. import { useCopyToClipboard } from '#shared/composables/useCopyToClipboard.ts'
  5. const clipboardCopyMock = vi.fn()
  6. const clipboardCopiedMock = ref(false)
  7. vi.mock('@vueuse/core', async () => {
  8. const mod =
  9. await vi.importActual<typeof import('@vueuse/core')>('@vueuse/core')
  10. return {
  11. ...mod,
  12. useClipboard: () => ({
  13. copy: clipboardCopyMock,
  14. copied: clipboardCopiedMock,
  15. }),
  16. }
  17. })
  18. const notifyMock = vi.fn()
  19. vi.mock(
  20. '#shared/components/CommonNotifications/useNotifications.ts',
  21. async () => ({
  22. useNotifications: () => ({
  23. notify: notifyMock,
  24. }),
  25. }),
  26. )
  27. describe('useCopyToClipboard', () => {
  28. it('supports copying text to clipboard', () => {
  29. const { copyToClipboard } = useCopyToClipboard()
  30. copyToClipboard('foobar')
  31. expect(clipboardCopyMock).toHaveBeenCalledWith('foobar')
  32. })
  33. it('shows a notification on success', async () => {
  34. const { copyToClipboard } = useCopyToClipboard()
  35. copyToClipboard('foobar')
  36. clipboardCopiedMock.value = true
  37. await waitForNextTick()
  38. expect(notifyMock).toHaveBeenCalledWith(
  39. expect.objectContaining({ message: 'Copied.' }),
  40. )
  41. })
  42. it('does not copy undefined nor null text to clipboard', () => {
  43. const { copyToClipboard } = useCopyToClipboard()
  44. copyToClipboard(undefined)
  45. expect(clipboardCopyMock).not.toHaveBeenCalled()
  46. copyToClipboard(null)
  47. expect(clipboardCopyMock).not.toHaveBeenCalled()
  48. })
  49. })