-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuse-clipboard.test.ts
43 lines (33 loc) · 1.18 KB
/
use-clipboard.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { act, renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { useClipboard } from './use-clipboard';
vi.useFakeTimers();
describe('useClipboard', () => {
it('should copy text to the clipboard and set copied to true', async () => {
const writeTextMock = vi.fn().mockResolvedValue(undefined);
Object.assign(navigator, {
clipboard: { writeText: writeTextMock }
});
const { result } = renderHook(() => useClipboard());
await act(async () => {
const success = await result.current.copy('Hello, world!');
expect(success).toBe(true);
});
expect(writeTextMock).toHaveBeenCalledWith('Hello, world!');
expect(result.current.copied).toBe(true);
act(() => {
vi.advanceTimersByTime(2000);
});
expect(result.current.copied).toBe(false);
});
it('should handle clipboard API not being supported', async () => {
Object.assign(navigator, {
clipboard: undefined
});
const { result } = renderHook(() => useClipboard());
await act(async () => {
const success = await result.current.copy('Hello, world!');
expect(success).toBe(false);
});
});
});