CommonInputCopyToClipboard.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import getUuid from '#shared/utils/getUuid.ts'
  4. import CommonInputCopyToClipboard from '../CommonInputCopyToClipboard.vue'
  5. const clipboardCopyMock = vi.fn()
  6. vi.mock('@vueuse/core', async () => {
  7. const mod =
  8. await vi.importActual<typeof import('@vueuse/core')>('@vueuse/core')
  9. return {
  10. ...mod,
  11. useClipboard: () => ({
  12. copy: clipboardCopyMock,
  13. copied: vi.fn(),
  14. }),
  15. }
  16. })
  17. const renderCopyToClipboard = (
  18. props: Record<string, unknown> = {},
  19. options: any = {},
  20. ) => {
  21. return renderComponent(CommonInputCopyToClipboard, {
  22. props,
  23. ...options,
  24. form: true,
  25. })
  26. }
  27. const uuidValue = getUuid()
  28. describe('CommonInputCopyToClipboard.vue', () => {
  29. it('show disabled input field with value and copy button', async () => {
  30. const view = renderCopyToClipboard({
  31. value: uuidValue,
  32. label: 'A Label',
  33. })
  34. const input = view.getByLabelText('A Label')
  35. expect(input).toHaveValue(uuidValue)
  36. expect(input).toHaveAttribute('readonly')
  37. expect(view.getByRole('button', { name: 'Copy Text' })).toBeInTheDocument()
  38. })
  39. it('click copy button with a custom copy label', async () => {
  40. const view = renderCopyToClipboard({
  41. value: uuidValue,
  42. label: 'A Label',
  43. copyButtonText: 'Copy Token',
  44. })
  45. await view.events.click(view.getByRole('button', { name: 'Copy Token' }))
  46. expect(clipboardCopyMock).toHaveBeenCalledWith(uuidValue)
  47. })
  48. })