forked from cpojer/copy-as-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.tsx
162 lines (128 loc) · 3.73 KB
/
index.test.tsx
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {fireEvent} from '@testing-library/react';
import {renderHook, act} from '@testing-library/react-hooks';
import useCopyAsMarkdown from './index.js';
type SetDataType = (type: string, data: string) => void;
let element: HTMLDivElement;
let fakeClipboardEvent: ClipboardEvent;
let setData: jest.Mock<void, [string, string]>;
const mockSelection = (ancestor?: Element, selectedElement?: Element) =>
(window.getSelection = jest.fn(
() =>
(({
rangeCount: 1,
getRangeAt: () => ({
commonAncestorContainer: ancestor || element,
cloneContents: () => selectedElement || element.children[0],
}),
} as unknown) as Selection),
));
beforeEach(() => {
element = document.createElement('div');
element.innerHTML =
"<h1>Banana Banana Banana?</h1><b>apple</b><code>Don't escape.</code>";
mockSelection();
fakeClipboardEvent = new Event('copy', {
bubbles: true,
cancelable: true,
}) as ClipboardEvent;
fakeClipboardEvent.preventDefault = jest.fn();
setData = jest.fn();
type FakeClipboardData = {
clipboardData: {setData: SetDataType};
};
((fakeClipboardEvent as unknown) as FakeClipboardData).clipboardData = {
setData,
};
});
test('copies the header', () => {
const {result} = renderHook(() => useCopyAsMarkdown());
act(() => {
result.current(element);
});
fireEvent(element, fakeClipboardEvent);
expect(setData.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"text/plain",
"# Banana Banana Banana?",
],
]
`);
});
test('copies everything', () => {
mockSelection(element, element);
const {result} = renderHook(() => useCopyAsMarkdown());
act(() => {
result.current(element);
});
fireEvent(element, fakeClipboardEvent);
expect(setData.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"text/plain",
"# Banana Banana Banana?
**apple**\`Don't escape.\`",
],
]
`);
});
test('copies the code without escaping characters', () => {
mockSelection(element.children[2], element.children[2]);
const {result} = renderHook(() => useCopyAsMarkdown());
act(() => {
result.current(element);
});
fireEvent(element, fakeClipboardEvent);
// Expect no clipboard interception to have happened. This means
// we let the browser handle the copy event.
expect(setData).not.toHaveBeenCalled();
});
test('uses backticks for multiline code examples', () => {
element.innerHTML =
'<h1>Banana Banana Banana?</h1><pre><code class="language-javascript">' +
'const variable = `this is a multiline code example`;\n' +
'console.log(`banana banana banana`);</code></pre>';
mockSelection(element, element.children[1]);
const {result} = renderHook(() => useCopyAsMarkdown());
act(() => {
result.current(element);
});
fireEvent(element, fakeClipboardEvent);
expect(setData.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"text/plain",
"\`\`\`javascript
const variable = \`this is a multiline code example\`;
console.log(\`banana banana banana\`);
\`\`\`",
],
]
`);
});
test('processes the HTML node before copying', () => {
const {result} = renderHook(() =>
useCopyAsMarkdown(null, {
processNode: (element) => {
const a = document.createElement('a');
a.href = 'https://cpojer.net';
a.innerHTML = 'cpojer.net';
element.appendChild(a);
return element;
},
}),
);
act(() => {
result.current(element);
});
fireEvent(element, fakeClipboardEvent);
expect(setData.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"text/plain",
"# Banana Banana Banana?
[cpojer.net](https://cpojer.net)",
],
]
`);
});