-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseCopyToClipboard.test.ts
103 lines (84 loc) Β· 3.41 KB
/
useCopyToClipboard.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import writeText from 'copy-to-clipboard';
import { act, renderHook } from '@testing-library/react-hooks';
import { useCopyToClipboard } from '../src';
const valueToRaiseMockException = 'fake input causing exception in copy to clipboard';
jest.mock('copy-to-clipboard', () =>
jest.fn().mockImplementation((input) => {
if (input === valueToRaiseMockException) {
throw new Error(input);
}
return true;
})
);
describe('useCopyToClipboard', () => {
let hook;
let consoleErrorSpy = jest.spyOn(global.console, 'error').mockImplementation(() => {});
beforeEach(() => {
hook = renderHook(() => useCopyToClipboard());
});
afterAll(() => {
consoleErrorSpy.mockRestore();
jest.unmock('copy-to-clipboard');
});
it('should be defined ', () => {
expect(useCopyToClipboard).toBeDefined();
});
it('should pass a given value to copy to clipboard and set state', () => {
const testValue = 'test';
let [state, copyToClipboard] = hook.result.current;
act(() => copyToClipboard(testValue));
[state, copyToClipboard] = hook.result.current;
expect(writeText).toBeCalled();
expect(state.value).toBe(testValue);
expect(state.noUserInteraction).toBe(true);
expect(state.error).not.toBeDefined();
});
it('should not call writeText if passed an invalid input and set state', () => {
let testValue = {}; // invalid value
let [state, copyToClipboard] = hook.result.current;
act(() => copyToClipboard(testValue));
[state, copyToClipboard] = hook.result.current;
expect(writeText).not.toBeCalled();
expect(state.value).toBe(testValue);
expect(state.noUserInteraction).toBe(true);
expect(state.error).toBeDefined();
testValue = ''; // empty string is also invalid
act(() => copyToClipboard(testValue));
[state, copyToClipboard] = hook.result.current;
expect(writeText).not.toBeCalled();
expect(state.value).toBe(testValue);
expect(state.noUserInteraction).toBe(true);
expect(state.error).toBeDefined();
});
it('should catch exception thrown by copy-to-clipboard and set state', () => {
let [state, copyToClipboard] = hook.result.current;
act(() => copyToClipboard(valueToRaiseMockException));
[state, copyToClipboard] = hook.result.current;
expect(writeText).toBeCalledWith(valueToRaiseMockException);
expect(state.value).toBe(valueToRaiseMockException);
expect(state.noUserInteraction).not.toBeDefined();
expect(state.error).toStrictEqual(new Error(valueToRaiseMockException));
});
it('should return initial state while unmounted', () => {
hook.unmount();
const [state, copyToClipboard] = hook.result.current;
act(() => copyToClipboard('value'));
expect(state.value).not.toBeDefined();
expect(state.error).not.toBeDefined();
expect(state.noUserInteraction).toBe(true);
});
it('should console error if in dev environment', () => {
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
const testValue = {}; // invalid value
process.env.NODE_ENV = 'development';
let [state, copyToClipboard] = hook.result.current;
act(() => copyToClipboard(testValue));
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
[state, copyToClipboard] = hook.result.current;
expect(writeText).not.toBeCalled();
expect(consoleErrorSpy).toBeCalled();
expect(state.value).toBe(testValue);
expect(state.noUserInteraction).toBe(true);
expect(state.error).toBeDefined();
});
});