|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { renderHook, act } from '@testing-library/react'; |
| 3 | +import { useCopyCode } from './useCopyCode'; |
| 4 | + |
| 5 | +describe('useCopyCode', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers(); |
| 8 | + Object.assign(navigator, { |
| 9 | + clipboard: { writeText: vi.fn().mockResolvedValue(undefined) }, |
| 10 | + }); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + vi.useRealTimers(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('starts with copied = false', () => { |
| 18 | + const { result } = renderHook(() => useCopyCode()); |
| 19 | + expect(result.current.copied).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('copies text to clipboard', async () => { |
| 23 | + const { result } = renderHook(() => useCopyCode()); |
| 24 | + |
| 25 | + await act(async () => { |
| 26 | + await result.current.copyToClipboard('hello'); |
| 27 | + }); |
| 28 | + |
| 29 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('hello'); |
| 30 | + expect(result.current.copied).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('resets copied after timeout', async () => { |
| 34 | + const { result } = renderHook(() => useCopyCode({ timeout: 1000 })); |
| 35 | + |
| 36 | + await act(async () => { |
| 37 | + await result.current.copyToClipboard('hello'); |
| 38 | + }); |
| 39 | + expect(result.current.copied).toBe(true); |
| 40 | + |
| 41 | + act(() => { |
| 42 | + vi.advanceTimersByTime(1000); |
| 43 | + }); |
| 44 | + expect(result.current.copied).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('calls onCopy callback', async () => { |
| 48 | + const onCopy = vi.fn(); |
| 49 | + const { result } = renderHook(() => useCopyCode({ onCopy })); |
| 50 | + |
| 51 | + await act(async () => { |
| 52 | + await result.current.copyToClipboard('hello'); |
| 53 | + }); |
| 54 | + |
| 55 | + expect(onCopy).toHaveBeenCalledOnce(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('reset sets copied to false', async () => { |
| 59 | + const { result } = renderHook(() => useCopyCode()); |
| 60 | + |
| 61 | + await act(async () => { |
| 62 | + await result.current.copyToClipboard('hello'); |
| 63 | + }); |
| 64 | + expect(result.current.copied).toBe(true); |
| 65 | + |
| 66 | + act(() => { |
| 67 | + result.current.reset(); |
| 68 | + }); |
| 69 | + expect(result.current.copied).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it('handles clipboard failure gracefully', async () => { |
| 73 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 74 | + (navigator.clipboard.writeText as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('denied')); |
| 75 | + |
| 76 | + const { result } = renderHook(() => useCopyCode()); |
| 77 | + |
| 78 | + await act(async () => { |
| 79 | + await result.current.copyToClipboard('hello'); |
| 80 | + }); |
| 81 | + |
| 82 | + expect(result.current.copied).toBe(false); |
| 83 | + expect(consoleSpy).toHaveBeenCalled(); |
| 84 | + consoleSpy.mockRestore(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments