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:
parent
220268b25d
commit
57ac4e141b
@ -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();
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,6 +30,7 @@ const runtime = {
|
||||
addListener: jest.fn(),
|
||||
removeListener: jest.fn(),
|
||||
},
|
||||
reload: jest.fn(),
|
||||
};
|
||||
|
||||
const contextMenus = {
|
||||
|
Loading…
Reference in New Issue
Block a user