2024-05-30 15:39:19 +02:00
|
|
|
import { flushPromises, sendMockExtensionMessage } from "../../autofill/spec/testing-utils";
|
2024-03-06 17:33:38 +01:00
|
|
|
import { BrowserApi } from "../browser/browser-api";
|
|
|
|
import BrowserClipboardService from "../services/browser-clipboard.service";
|
|
|
|
|
|
|
|
describe("OffscreenDocument", () => {
|
|
|
|
const browserApiMessageListenerSpy = jest.spyOn(BrowserApi, "messageListener");
|
|
|
|
const browserClipboardServiceCopySpy = jest.spyOn(BrowserClipboardService, "copy");
|
|
|
|
const browserClipboardServiceReadSpy = jest.spyOn(BrowserClipboardService, "read");
|
|
|
|
const consoleErrorSpy = jest.spyOn(console, "error");
|
|
|
|
|
|
|
|
require("../offscreen-document/offscreen-document");
|
|
|
|
|
|
|
|
describe("init", () => {
|
|
|
|
it("sets up a `chrome.runtime.onMessage` listener", () => {
|
|
|
|
expect(browserApiMessageListenerSpy).toHaveBeenCalledWith(
|
|
|
|
"offscreen-document",
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extension message handlers", () => {
|
|
|
|
it("ignores messages that do not have a handler registered with the corresponding command", () => {
|
2024-05-30 15:39:19 +02:00
|
|
|
sendMockExtensionMessage({ command: "notAValidCommand" });
|
2024-03-06 17:33:38 +01:00
|
|
|
|
|
|
|
expect(browserClipboardServiceCopySpy).not.toHaveBeenCalled();
|
|
|
|
expect(browserClipboardServiceReadSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows a console message if the handler throws an error", async () => {
|
2024-04-30 18:58:16 +02:00
|
|
|
const error = new Error("test error");
|
2024-03-06 17:33:38 +01:00
|
|
|
browserClipboardServiceCopySpy.mockRejectedValueOnce(new Error("test error"));
|
|
|
|
|
2024-05-30 15:39:19 +02:00
|
|
|
sendMockExtensionMessage({ command: "offscreenCopyToClipboard", text: "test" });
|
2024-03-06 17:33:38 +01:00
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(browserClipboardServiceCopySpy).toHaveBeenCalled();
|
|
|
|
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
2024-04-30 18:58:16 +02:00
|
|
|
"Error resolving extension message response",
|
|
|
|
error,
|
2024-03-06 17:33:38 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("handleOffscreenCopyToClipboard", () => {
|
|
|
|
it("copies the message text", async () => {
|
|
|
|
const text = "test";
|
|
|
|
|
2024-05-30 15:39:19 +02:00
|
|
|
sendMockExtensionMessage({ command: "offscreenCopyToClipboard", text });
|
2024-03-06 17:33:38 +01:00
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(browserClipboardServiceCopySpy).toHaveBeenCalledWith(window, text);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("handleOffscreenReadFromClipboard", () => {
|
|
|
|
it("reads the value from the clipboard service", async () => {
|
2024-05-30 15:39:19 +02:00
|
|
|
sendMockExtensionMessage({ command: "offscreenReadFromClipboard" });
|
2024-03-06 17:33:38 +01:00
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(browserClipboardServiceReadSpy).toHaveBeenCalledWith(window);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|