mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 15:23:33 +00:00
[PM-18657] Fix clipboard tests calling console.warn (#13580)
* Fix clipboard tests calling console.warn * Change to jest.SpyInstance
This commit is contained in:
@@ -3,14 +3,19 @@ import { BrowserApi } from "../browser/browser-api";
|
|||||||
import BrowserClipboardService from "../services/browser-clipboard.service";
|
import BrowserClipboardService from "../services/browser-clipboard.service";
|
||||||
|
|
||||||
describe("OffscreenDocument", () => {
|
describe("OffscreenDocument", () => {
|
||||||
const browserApiMessageListenerSpy = jest.spyOn(BrowserApi, "messageListener");
|
let browserClipboardServiceCopySpy: jest.SpyInstance;
|
||||||
const browserClipboardServiceCopySpy = jest.spyOn(BrowserClipboardService, "copy");
|
let browserClipboardServiceReadSpy: jest.SpyInstance;
|
||||||
const browserClipboardServiceReadSpy = jest.spyOn(BrowserClipboardService, "read");
|
let browserApiMessageListenerSpy: jest.SpyInstance;
|
||||||
const consoleErrorSpy = jest.spyOn(console, "error");
|
let consoleErrorSpy: jest.SpyInstance;
|
||||||
|
|
||||||
// FIXME: Remove when updating file. Eslint update
|
beforeEach(async () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
browserApiMessageListenerSpy = jest.spyOn(BrowserApi, "messageListener");
|
||||||
require("../offscreen-document/offscreen-document");
|
browserClipboardServiceCopySpy = jest.spyOn(BrowserClipboardService, "copy");
|
||||||
|
browserClipboardServiceReadSpy = jest.spyOn(BrowserClipboardService, "read");
|
||||||
|
consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
|
||||||
|
|
||||||
|
await import("./offscreen-document");
|
||||||
|
});
|
||||||
|
|
||||||
describe("init", () => {
|
describe("init", () => {
|
||||||
it("sets up a `chrome.runtime.onMessage` listener", () => {
|
it("sets up a `chrome.runtime.onMessage` listener", () => {
|
||||||
@@ -47,6 +52,7 @@ describe("OffscreenDocument", () => {
|
|||||||
it("copies the message text", async () => {
|
it("copies the message text", async () => {
|
||||||
const text = "test";
|
const text = "test";
|
||||||
|
|
||||||
|
browserClipboardServiceCopySpy.mockResolvedValueOnce(undefined);
|
||||||
sendMockExtensionMessage({ command: "offscreenCopyToClipboard", text });
|
sendMockExtensionMessage({ command: "offscreenCopyToClipboard", text });
|
||||||
await flushPromises();
|
await flushPromises();
|
||||||
|
|
||||||
@@ -56,6 +62,7 @@ describe("OffscreenDocument", () => {
|
|||||||
|
|
||||||
describe("handleOffscreenReadFromClipboard", () => {
|
describe("handleOffscreenReadFromClipboard", () => {
|
||||||
it("reads the value from the clipboard service", async () => {
|
it("reads the value from the clipboard service", async () => {
|
||||||
|
browserClipboardServiceReadSpy.mockResolvedValueOnce("");
|
||||||
sendMockExtensionMessage({ command: "offscreenReadFromClipboard" });
|
sendMockExtensionMessage({ command: "offscreenReadFromClipboard" });
|
||||||
await flushPromises();
|
await flushPromises();
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ import BrowserClipboardService from "./browser-clipboard.service";
|
|||||||
|
|
||||||
describe("BrowserClipboardService", () => {
|
describe("BrowserClipboardService", () => {
|
||||||
let windowMock: any;
|
let windowMock: any;
|
||||||
const consoleWarnSpy = jest.spyOn(console, "warn");
|
let consoleWarnSpy: any;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
|
||||||
windowMock = {
|
windowMock = {
|
||||||
navigator: {
|
navigator: {
|
||||||
clipboard: {
|
clipboard: {
|
||||||
@@ -104,8 +105,6 @@ describe("BrowserClipboardService", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await BrowserClipboardService.read(windowMock as Window);
|
await BrowserClipboardService.read(windowMock as Window);
|
||||||
|
|
||||||
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -185,7 +185,9 @@ describe("Browser Utils Service", () => {
|
|||||||
describe("copyToClipboard", () => {
|
describe("copyToClipboard", () => {
|
||||||
const getManifestVersionSpy = jest.spyOn(BrowserApi, "manifestVersion", "get");
|
const getManifestVersionSpy = jest.spyOn(BrowserApi, "manifestVersion", "get");
|
||||||
const sendMessageToAppSpy = jest.spyOn(SafariApp, "sendMessageToApp");
|
const sendMessageToAppSpy = jest.spyOn(SafariApp, "sendMessageToApp");
|
||||||
const clipboardServiceCopySpy = jest.spyOn(BrowserClipboardService, "copy");
|
const clipboardServiceCopySpy = jest
|
||||||
|
.spyOn(BrowserClipboardService, "copy")
|
||||||
|
.mockResolvedValue(undefined);
|
||||||
let triggerOffscreenCopyToClipboardSpy: jest.SpyInstance;
|
let triggerOffscreenCopyToClipboardSpy: jest.SpyInstance;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -281,7 +283,9 @@ describe("Browser Utils Service", () => {
|
|||||||
describe("readFromClipboard", () => {
|
describe("readFromClipboard", () => {
|
||||||
const getManifestVersionSpy = jest.spyOn(BrowserApi, "manifestVersion", "get");
|
const getManifestVersionSpy = jest.spyOn(BrowserApi, "manifestVersion", "get");
|
||||||
const sendMessageToAppSpy = jest.spyOn(SafariApp, "sendMessageToApp");
|
const sendMessageToAppSpy = jest.spyOn(SafariApp, "sendMessageToApp");
|
||||||
const clipboardServiceReadSpy = jest.spyOn(BrowserClipboardService, "read");
|
const clipboardServiceReadSpy = jest
|
||||||
|
.spyOn(BrowserClipboardService, "read")
|
||||||
|
.mockResolvedValue("");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
getManifestVersionSpy.mockReturnValue(2);
|
getManifestVersionSpy.mockReturnValue(2);
|
||||||
|
|||||||
Reference in New Issue
Block a user