mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 14:23:32 +00: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:
@@ -757,7 +757,7 @@ export default class MainBackground {
|
|||||||
this.platformUtilsService.isSafari() ||
|
this.platformUtilsService.isSafari() ||
|
||||||
this.platformUtilsService.isFirefox() ||
|
this.platformUtilsService.isFirefox() ||
|
||||||
this.platformUtilsService.isOpera();
|
this.platformUtilsService.isOpera();
|
||||||
BrowserApi.reloadExtension(forceWindowReload ? window : null);
|
BrowserApi.reloadExtension(forceWindowReload ? self : null);
|
||||||
return Promise.resolve();
|
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", () => {
|
describe("reloadOpenWindows", () => {
|
||||||
const href = window.location.href;
|
const href = window.location.href;
|
||||||
const reload = window.location.reload;
|
const reload = window.location.reload;
|
||||||
|
|||||||
@@ -381,12 +381,20 @@ export class BrowserApi {
|
|||||||
return chrome.i18n.getUILanguage();
|
return chrome.i18n.getUILanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
static reloadExtension(win: Window) {
|
/**
|
||||||
if (win != null) {
|
* Handles reloading the extension, either by calling the window location
|
||||||
return (win.location as any).reload(true);
|
* to reload or by calling the extension's runtime to reload.
|
||||||
} else {
|
*
|
||||||
return chrome.runtime.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(),
|
addListener: jest.fn(),
|
||||||
removeListener: jest.fn(),
|
removeListener: jest.fn(),
|
||||||
},
|
},
|
||||||
|
reload: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const contextMenus = {
|
const contextMenus = {
|
||||||
|
|||||||
Reference in New Issue
Block a user