1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 06:13:38 +00:00

[PM-5742] Rework Usage of Extension APIs that Cannot be Called with the Background Service Worker (#7667)

* [PM-5742] Rework Usage of Extension APIs that Cannot be Called with the Background Service Worker

* [PM-5742] Implementing jest tests for the updated BrowserApi methods

* [PM-5742] Implementing jest tests to validate logic within added API calls

* [PM-5742] Implementing jest tests to validate logic within added API calls

* [PM-5742] Fixing broken Jest tests

* [PM-5742] Fixing linter error
This commit is contained in:
Cesar Gonzalez
2024-02-07 15:20:53 -06:00
committed by GitHub
parent 2e11fb2a24
commit a1745b2dae
6 changed files with 236 additions and 13 deletions

View File

@@ -3,9 +3,14 @@ import { DeviceType } from "@bitwarden/common/enums";
import BrowserPlatformUtilsService from "./browser-platform-utils.service";
describe("Browser Utils Service", () => {
let browserPlatformUtilsService: BrowserPlatformUtilsService;
beforeEach(() => {
(window as any).matchMedia = jest.fn().mockReturnValueOnce({});
browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null, window);
});
describe("getBrowser", () => {
const originalUserAgent = navigator.userAgent;
// Reset the userAgent.
afterAll(() => {
Object.defineProperty(navigator, "userAgent", {
@@ -13,10 +18,8 @@ describe("Browser Utils Service", () => {
});
});
let browserPlatformUtilsService: BrowserPlatformUtilsService;
beforeEach(() => {
(window as any).matchMedia = jest.fn().mockReturnValueOnce({});
browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null, window);
});
afterEach(() => {
@@ -86,6 +89,45 @@ describe("Browser Utils Service", () => {
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.VivaldiExtension);
});
});
describe("isViewOpen", () => {
beforeEach(() => {
globalThis.chrome = {
// eslint-disable-next-line
// @ts-ignore
extension: {
getViews: jest.fn(),
},
};
});
it("returns true if the user is on Firefox and the sidebar is open", async () => {
chrome.extension.getViews = jest.fn().mockReturnValueOnce([window]);
jest
.spyOn(browserPlatformUtilsService, "getDevice")
.mockReturnValueOnce(DeviceType.FirefoxExtension);
const result = await browserPlatformUtilsService.isViewOpen();
expect(result).toBe(true);
});
it("returns true if a extension view is open as a tab", async () => {
chrome.extension.getViews = jest.fn().mockReturnValueOnce([window]);
const result = await browserPlatformUtilsService.isViewOpen();
expect(result).toBe(true);
});
it("returns false if no extension view is open", async () => {
chrome.extension.getViews = jest.fn().mockReturnValue([]);
const result = await browserPlatformUtilsService.isViewOpen();
expect(result).toBe(false);
});
});
});
describe("Safari Height Fix", () => {