-
-
Notifications
You must be signed in to change notification settings - Fork 352
fix(ui): clipboard copy for http://0.0.0.0:3000 and unify implementations #2487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4549c99
de9324c
12f2d90
c552e5c
5d8b3d8
e5c383c
250384e
84d40a6
c246fd4
4c43d03
bc4fafe
e08f47d
675015b
5c43c6a
b6ff3e3
9698664
43a038d
3a72895
3971752
54c82e9
aeccb4f
a550902
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const mockToast = vi.fn(); | ||
| vi.mock('@/hooks/use-toast', () => ({ | ||
| useToast: () => ({ toast: mockToast }), | ||
| })); | ||
|
|
||
| const mockCopyToClipboard = vi.fn(); | ||
| vi.mock('@/lib/utils', () => ({ | ||
| copyToClipboard: (text: string) => mockCopyToClipboard(text), | ||
| })); | ||
|
|
||
| const { useCopyToClipboard } = await import('@/hooks/use-copy-to-clipboard'); | ||
|
|
||
| describe('useCopyToClipboard', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockCopyToClipboard.mockResolvedValue(undefined); | ||
| }); | ||
|
|
||
| it('copies text and shows toast', async () => { | ||
| const { result } = renderHook(() => useCopyToClipboard()); | ||
|
|
||
| expect(result.current.copied).toBe(false); | ||
|
|
||
| let success: boolean | undefined; | ||
| await act(async () => { | ||
| success = await result.current.copy('test text'); | ||
| }); | ||
|
|
||
| expect(success).toBe(true); | ||
| expect(result.current.copied).toBe(true); | ||
| expect(mockCopyToClipboard).toHaveBeenCalledWith('test text'); | ||
| expect(mockToast).toHaveBeenCalledWith({ | ||
| description: 'Copied!', | ||
| title: 'Copied!', | ||
| }); | ||
| }); | ||
|
|
||
| it('uses custom success message from options', async () => { | ||
| const { result } = renderHook(() => useCopyToClipboard({ successMessage: 'URL copied!' })); | ||
|
|
||
| await act(async () => { | ||
| await result.current.copy('http://example.com'); | ||
| }); | ||
|
|
||
| expect(mockToast).toHaveBeenCalledWith({ | ||
| description: 'URL copied!', | ||
| title: 'Copied!', | ||
| }); | ||
| }); | ||
|
|
||
| it('handles errors and shows error toast', async () => { | ||
| mockCopyToClipboard.mockRejectedValue(new Error('Copy failed')); | ||
|
|
||
| const { result } = renderHook(() => useCopyToClipboard()); | ||
|
|
||
| let success: boolean | undefined; | ||
| await act(async () => { | ||
| success = await result.current.copy('test text'); | ||
| }); | ||
|
|
||
| expect(success).toBe(false); | ||
| expect(result.current.copied).toBe(false); | ||
| expect(mockToast).toHaveBeenCalledWith({ | ||
| description: 'Failed to copy to clipboard', | ||
| title: 'Error', | ||
| variant: 'destructive', | ||
| }); | ||
| }); | ||
| }); | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.