1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-11 05:43:41 +00:00

[PM-4783] Special case browser error (#13242)

* Special case browser error

* Add & Fix Tests

* Fix _other_ tests
This commit is contained in:
Justin Baur
2025-02-05 15:30:46 -05:00
committed by GitHub
parent aedb899401
commit 3c3cc897d1
2 changed files with 45 additions and 7 deletions

View File

@@ -147,7 +147,9 @@ describe("Browser Utils Service", () => {
describe("isViewOpen", () => {
it("returns false if a heartbeat response is not received", async () => {
BrowserApi.sendMessageWithResponse = jest.fn().mockResolvedValueOnce(undefined);
chrome.runtime.sendMessage = jest.fn().mockImplementation((message, callback) => {
callback(undefined);
});
const isViewOpen = await browserPlatformUtilsService.isViewOpen();
@@ -155,16 +157,29 @@ describe("Browser Utils Service", () => {
});
it("returns true if a heartbeat response is received", async () => {
BrowserApi.sendMessageWithResponse = jest
.fn()
.mockImplementationOnce((subscriber) =>
Promise.resolve((subscriber === "checkVaultPopupHeartbeat") as any),
);
chrome.runtime.sendMessage = jest.fn().mockImplementation((message, callback) => {
callback(message.command === "checkVaultPopupHeartbeat");
});
const isViewOpen = await browserPlatformUtilsService.isViewOpen();
expect(isViewOpen).toBe(true);
});
it("returns false if special error is sent", async () => {
chrome.runtime.sendMessage = jest.fn().mockImplementation((message, callback) => {
chrome.runtime.lastError = new Error(
"Could not establish connection. Receiving end does not exist.",
);
callback(undefined);
});
const isViewOpen = await browserPlatformUtilsService.isViewOpen();
expect(isViewOpen).toBe(false);
chrome.runtime.lastError = null;
});
});
describe("copyToClipboard", () => {
@@ -228,6 +243,7 @@ describe("Browser Utils Service", () => {
});
it("copies the passed text using the offscreen document if the extension is using manifest v3", async () => {
BrowserApi.sendMessageWithResponse = jest.fn();
const text = "test";
offscreenDocumentService.offscreenApiSupported.mockReturnValue(true);
getManifestVersionSpy.mockReturnValue(3);
@@ -302,6 +318,7 @@ describe("Browser Utils Service", () => {
});
it("reads the clipboard text using the offscreen document", async () => {
BrowserApi.sendMessageWithResponse = jest.fn();
offscreenDocumentService.offscreenApiSupported.mockReturnValue(true);
getManifestVersionSpy.mockReturnValue(3);
offscreenDocumentService.withDocument.mockImplementationOnce((_, __, callback) =>

View File

@@ -169,7 +169,28 @@ export abstract class BrowserPlatformUtilsService implements PlatformUtilsServic
// Query views on safari since chrome.runtime.sendMessage does not timeout and will hang.
return BrowserApi.isPopupOpen();
}
return Boolean(await BrowserApi.sendMessageWithResponse("checkVaultPopupHeartbeat"));
return new Promise<boolean>((resolve, reject) => {
chrome.runtime.sendMessage({ command: "checkVaultPopupHeartbeat" }, (response) => {
if (chrome.runtime.lastError != null) {
// This error means that nothing was there to listen to the message,
// meaning the view is not open.
if (
chrome.runtime.lastError.message ===
"Could not establish connection. Receiving end does not exist."
) {
resolve(false);
return;
}
// All unhandled errors still reject
reject(chrome.runtime.lastError);
return;
}
resolve(Boolean(response));
});
});
}
lockTimeout(): number {