mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-27 04:03:00 +02:00
51f482dde9
* [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
112 lines
4.0 KiB
TypeScript
112 lines
4.0 KiB
TypeScript
import BrowserClipboardService from "./browser-clipboard.service";
|
|
|
|
describe("BrowserClipboardService", () => {
|
|
let windowMock: any;
|
|
const consoleWarnSpy = jest.spyOn(console, "warn");
|
|
|
|
beforeEach(() => {
|
|
windowMock = {
|
|
navigator: {
|
|
clipboard: {
|
|
writeText: jest.fn(),
|
|
readText: jest.fn(),
|
|
},
|
|
},
|
|
document: {
|
|
body: {
|
|
appendChild: jest.fn((element) => document.body.appendChild(element)),
|
|
removeChild: jest.fn((element) => document.body.removeChild(element)),
|
|
},
|
|
createElement: jest.fn((tagName) => document.createElement(tagName)),
|
|
execCommand: jest.fn(),
|
|
queryCommandSupported: jest.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
describe("copy", () => {
|
|
it("uses the legacy copy method if the clipboard API is not available", async () => {
|
|
const text = "test";
|
|
windowMock.navigator.clipboard = {};
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
|
|
await BrowserClipboardService.copy(windowMock as Window, text);
|
|
|
|
expect(windowMock.document.execCommand).toHaveBeenCalledWith("copy");
|
|
});
|
|
|
|
it("uses the legacy copy method if the clipboard API throws an error", async () => {
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
windowMock.navigator.clipboard.writeText.mockRejectedValue(new Error("test"));
|
|
|
|
await BrowserClipboardService.copy(windowMock as Window, "test");
|
|
|
|
expect(windowMock.document.execCommand).toHaveBeenCalledWith("copy");
|
|
});
|
|
|
|
it("copies the given text to the clipboard", async () => {
|
|
const text = "test";
|
|
|
|
await BrowserClipboardService.copy(windowMock as Window, text);
|
|
|
|
expect(windowMock.navigator.clipboard.writeText).toHaveBeenCalledWith(text);
|
|
});
|
|
|
|
it("prints an warning message to the console if both the clipboard api and legacy method throw an error", async () => {
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
windowMock.navigator.clipboard.writeText.mockRejectedValue(new Error("test"));
|
|
windowMock.document.execCommand.mockImplementation(() => {
|
|
throw new Error("test");
|
|
});
|
|
|
|
await BrowserClipboardService.copy(windowMock as Window, "");
|
|
|
|
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("read", () => {
|
|
it("uses the legacy read method if the clipboard API is not available", async () => {
|
|
const testValue = "test";
|
|
windowMock.navigator.clipboard = {};
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
windowMock.document.execCommand.mockImplementation(() => {
|
|
document.querySelector("textarea").value = testValue;
|
|
return true;
|
|
});
|
|
|
|
const returnValue = await BrowserClipboardService.read(windowMock as Window);
|
|
|
|
expect(windowMock.document.execCommand).toHaveBeenCalledWith("paste");
|
|
expect(returnValue).toBe(testValue);
|
|
});
|
|
|
|
it("uses the legacy read method if the clipboard API throws an error", async () => {
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
windowMock.navigator.clipboard.readText.mockRejectedValue(new Error("test"));
|
|
|
|
await BrowserClipboardService.read(windowMock as Window);
|
|
|
|
expect(windowMock.document.execCommand).toHaveBeenCalledWith("paste");
|
|
});
|
|
|
|
it("reads the text from the clipboard", async () => {
|
|
await BrowserClipboardService.read(windowMock as Window);
|
|
|
|
expect(windowMock.navigator.clipboard.readText).toHaveBeenCalled();
|
|
});
|
|
|
|
it("prints a warning message to the console if both the clipboard api and legacy method throw an error", async () => {
|
|
windowMock.document.queryCommandSupported.mockReturnValue(true);
|
|
windowMock.navigator.clipboard.readText.mockRejectedValue(new Error("test"));
|
|
windowMock.document.execCommand.mockImplementation(() => {
|
|
throw new Error("test");
|
|
});
|
|
|
|
await BrowserClipboardService.read(windowMock as Window);
|
|
|
|
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|