Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 2dea27a

Browse files
stratigosjasonLaster
authored andcommitted
Add tests for utils/clipboard (#5188)
1 parent fbab180 commit 2dea27a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/utils/tests/clipboard.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { copyToTheClipboard } from "../clipboard";
2+
3+
let clipboardTestCopyString, expectedCopyEvent;
4+
const addEventListener = jest.fn();
5+
const execCommand = jest.fn();
6+
const removeEventListener = jest.fn();
7+
8+
describe("copyToTheClipboard()", () => {
9+
beforeEach(() => {
10+
expectedCopyEvent = "copy";
11+
clipboardTestCopyString = "content intended for clipboard";
12+
13+
global.document.addEventListener = addEventListener;
14+
global.document.execCommand = execCommand;
15+
global.document.removeEventListener = removeEventListener;
16+
});
17+
18+
it("listens for 'copy' event", () => {
19+
copyToTheClipboard(clipboardTestCopyString);
20+
21+
expect(document.addEventListener).toHaveBeenCalledWith(
22+
expectedCopyEvent,
23+
expect.anything()
24+
);
25+
});
26+
27+
it("calls document.execCommand() with 'copy' command", () => {
28+
copyToTheClipboard(clipboardTestCopyString);
29+
30+
expect(execCommand).toHaveBeenCalledWith(expectedCopyEvent, false, null);
31+
});
32+
33+
it("removes event listener for 'copy' event", () => {
34+
copyToTheClipboard(clipboardTestCopyString);
35+
36+
expect(document.removeEventListener).toHaveBeenCalledWith(
37+
expectedCopyEvent,
38+
expect.anything()
39+
);
40+
});
41+
});

0 commit comments

Comments
 (0)