1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-18 07:25:15 +02:00
bitwarden-browser/apps/browser/src/platform/offscreen-document/offscreen-document.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
2.4 KiB
TypeScript
Raw Normal View History

[PM-5880] Refactor browser platform utils service to remove `window` references (#7885) * [PM-5880] Refactor Browser Platform Utils Service to Remove Window Service * [PM-5880] Implementing BrowserClipboardService to handle clipboard logic between the BrowserPlatformUtils and offscreen document * [PM-5880] Adjusting how readText is handled within BrowserClipboardService * [PM-5880] Adjusting how readText is handled within BrowserClipboardService * [PM-5880] Working through implementation of chrome offscreen API usage * [PM-5880] Implementing jest tests for the methods added to the BrowserApi class * [PM-5880] Implementing jest tests for the OffscreenDocument class * [PM-5880] Working through jest tests for BrowserClipboardService * [PM-5880] Adding typing information to the clipboard methods present within the BrowserPlatformUtilsService * [PM-5880] Working on adding ServiceWorkerGlobalScope typing information * [PM-5880] Updating window references when calling BrowserPlatformUtils methods * [PM-5880] Finishing out jest tests for the BrowserClipboardService * [PM-5880] Finishing out jest tests for the BrowserClipboardService * [PM-5880] Implementing jest tests to validate the changes within BrowserApi * [PM-5880] Implementing jest tests to ensure coverage within OffscreenDocument * [PM-5880] Implementing jest tests for the BrowserPlatformUtilsService * [PM-5880] Removing unused catch statements * [PM-5880] Implementing jest tests for the BrowserPlatformUtilsService * [PM-5880] Implementing jest tests for the BrowserPlatformUtilsService * [PM-5880] Fixing broken tests
2024-03-06 17:33:38 +01:00
import { flushPromises, sendExtensionRuntimeMessage } from "../../autofill/spec/testing-utils";
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", () => {
sendExtensionRuntimeMessage({ command: "notAValidCommand" });
expect(browserClipboardServiceCopySpy).not.toHaveBeenCalled();
expect(browserClipboardServiceReadSpy).not.toHaveBeenCalled();
});
it("shows a console message if the handler throws an error", async () => {
browserClipboardServiceCopySpy.mockRejectedValueOnce(new Error("test error"));
sendExtensionRuntimeMessage({ command: "offscreenCopyToClipboard", text: "test" });
await flushPromises();
expect(browserClipboardServiceCopySpy).toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(
"Error resolving extension message response: Error: test error",
);
});
describe("handleOffscreenCopyToClipboard", () => {
it("copies the message text", async () => {
const text = "test";
sendExtensionRuntimeMessage({ command: "offscreenCopyToClipboard", text });
await flushPromises();
expect(browserClipboardServiceCopySpy).toHaveBeenCalledWith(window, text);
});
});
describe("handleOffscreenReadFromClipboard", () => {
it("reads the value from the clipboard service", async () => {
sendExtensionRuntimeMessage({ command: "offscreenReadFromClipboard" });
await flushPromises();
expect(browserClipboardServiceReadSpy).toHaveBeenCalledWith(window);
});
});
});
});