1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-24 02:41:54 +01:00

[PM-5741] Reworking usage of window object in BrowserApi.reloadExtension method (#8021)

* [PM-5741] Reworking usage of `window` object in BrowserApi.reloadExtension method

* [PM-5741] Reworking usage of `window` object in BrowserApi.reloadExtension method
This commit is contained in:
Cesar Gonzalez 2024-02-22 16:21:24 -06:00 committed by GitHub
parent 220268b25d
commit 57ac4e141b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 6 deletions

View File

@ -757,7 +757,7 @@ export default class MainBackground {
this.platformUtilsService.isSafari() ||
this.platformUtilsService.isFirefox() ||
this.platformUtilsService.isOpera();
BrowserApi.reloadExtension(forceWindowReload ? window : null);
BrowserApi.reloadExtension(forceWindowReload ? self : null);
return Promise.resolve();
};

View File

@ -185,6 +185,31 @@ describe("BrowserApi", () => {
});
});
describe("reloadExtension", () => {
it("reloads the window location if the passed globalContext is for the window", () => {
const windowMock = mock<Window>({
location: { reload: jest.fn() },
}) as unknown as Window & typeof globalThis;
BrowserApi.reloadExtension(windowMock);
expect(windowMock.location.reload).toHaveBeenCalled();
});
it("reloads the extension runtime if the passed globalContext is not for the window", () => {
const globalMock = mock<typeof globalThis>({}) as any;
BrowserApi.reloadExtension(globalMock);
expect(chrome.runtime.reload).toHaveBeenCalled();
});
it("reloads the extension runtime if a null value is passed as the globalContext", () => {
BrowserApi.reloadExtension(null);
expect(chrome.runtime.reload).toHaveBeenCalled();
});
});
describe("reloadOpenWindows", () => {
const href = window.location.href;
const reload = window.location.reload;

View File

@ -381,12 +381,20 @@ export class BrowserApi {
return chrome.i18n.getUILanguage();
}
static reloadExtension(win: Window) {
if (win != null) {
return (win.location as any).reload(true);
} else {
return chrome.runtime.reload();
/**
* Handles reloading the extension, either by calling the window location
* to reload or by calling the extension's runtime to reload.
*
* @param globalContext - The global context to use for the reload.
*/
static reloadExtension(globalContext: (Window & typeof globalThis) | null) {
// The passed globalContext might be a ServiceWorkerGlobalScope, as a result
// we need to check if the location object exists before calling reload on it.
if (typeof globalContext?.location?.reload === "function") {
return (globalContext as any).location.reload(true);
}
return chrome.runtime.reload();
}
/**

View File

@ -30,6 +30,7 @@ const runtime = {
addListener: jest.fn(),
removeListener: jest.fn(),
},
reload: jest.fn(),
};
const contextMenus = {