CopyToClipboardInput.test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
  2. import { render, fireEvent, screen, cleanup, waitFor } from '@testing-library/vue';
  3. import CopyToClipboardInput from './CopyToClipboardInput.vue';
  4. // Mock the clipboard API
  5. Object.assign(navigator, {
  6. clipboard: {
  7. writeText: vi.fn(() => Promise.resolve()),
  8. },
  9. });
  10. describe('CopyToClipboardInput', () => {
  11. beforeEach(() => {
  12. render(CopyToClipboardInput, {
  13. props: { value: 'Test text' },
  14. });
  15. });
  16. afterEach(() => {
  17. cleanup();
  18. });
  19. it('renders correctly', () => {
  20. const input = screen.getByRole('textbox');
  21. const button = screen.getByRole('button', { name: 'Copy' });
  22. expect(input).toBeTruthy();
  23. expect(button).toBeTruthy();
  24. });
  25. it('selects text when input is focused', async () => {
  26. const input = screen.getByRole('textbox');
  27. await fireEvent.focus(input);
  28. expect(input.selectionStart).toBe(0);
  29. expect(input.selectionEnd).toBe('Test text'.length);
  30. });
  31. it('copies text to clipboard when button is clicked', async () => {
  32. const button = screen.getByRole('button', { name: 'Copy' });
  33. await fireEvent.click(button);
  34. expect(navigator.clipboard.writeText).toHaveBeenCalledWith('Test text');
  35. await waitFor(() => expect(button.textContent).toBe('Copied!'));
  36. });
  37. });