1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-11 13:53:34 +00:00

[PM-6382] Migrate disableContextMenuItem to Autofill Settings state (#8246)

* add enableContextMenu to autofillSettings and migrate disableContextMenuItem

* replace usages of disableContextMenuItem with autofill settings service global enableContextMenu
This commit is contained in:
Jonathan Prusik
2024-03-08 10:34:07 -05:00
committed by GitHub
parent 4cd8b07ea3
commit f90de62adc
11 changed files with 182 additions and 33 deletions

View File

@@ -1,6 +1,8 @@
import { mock, MockProxy } from "jest-mock-extended";
import { of } from "rxjs";
import { NOOP_COMMAND_SUFFIX } from "@bitwarden/common/autofill/constants";
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { CipherType } from "@bitwarden/common/vault/enums";
@@ -13,6 +15,7 @@ import { MainContextMenuHandler } from "./main-context-menu-handler";
describe("context-menu", () => {
let stateService: MockProxy<BrowserStateService>;
let autofillSettingsService: MockProxy<AutofillSettingsServiceAbstraction>;
let i18nService: MockProxy<I18nService>;
let logService: MockProxy<LogService>;
@@ -26,6 +29,7 @@ describe("context-menu", () => {
beforeEach(() => {
stateService = mock();
autofillSettingsService = mock();
i18nService = mock();
logService = mock();
@@ -41,14 +45,20 @@ describe("context-menu", () => {
});
i18nService.t.mockImplementation((key) => key);
sut = new MainContextMenuHandler(stateService, i18nService, logService);
sut = new MainContextMenuHandler(
stateService,
autofillSettingsService,
i18nService,
logService,
);
autofillSettingsService.enableContextMenu$ = of(true);
});
afterEach(() => jest.resetAllMocks());
describe("init", () => {
it("has menu disabled", async () => {
stateService.getDisableContextMenuItem.mockResolvedValue(true);
autofillSettingsService.enableContextMenu$ = of(false);
const createdMenu = await sut.init();
expect(createdMenu).toBeFalsy();
@@ -56,8 +66,6 @@ describe("context-menu", () => {
});
it("has menu enabled, but does not have premium", async () => {
stateService.getDisableContextMenuItem.mockResolvedValue(false);
stateService.getCanAccessPremium.mockResolvedValue(false);
const createdMenu = await sut.init();
@@ -66,8 +74,6 @@ describe("context-menu", () => {
});
it("has menu enabled and has premium", async () => {
stateService.getDisableContextMenuItem.mockResolvedValue(false);
stateService.getCanAccessPremium.mockResolvedValue(true);
const createdMenu = await sut.init();

View File

@@ -1,3 +1,5 @@
import { firstValueFrom } from "rxjs";
import {
AUTOFILL_CARD_ID,
AUTOFILL_ID,
@@ -14,6 +16,7 @@ import {
ROOT_ID,
SEPARATOR_ID,
} from "@bitwarden/common/autofill/constants";
import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { StateFactory } from "@bitwarden/common/platform/factories/state-factory";
@@ -22,6 +25,7 @@ import { GlobalState } from "@bitwarden/common/platform/models/domain/global-sta
import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { autofillSettingsServiceFactory } from "../../autofill/background/service_factories/autofill-settings-service.factory";
import { Account } from "../../models/account";
import { CachedServices } from "../../platform/background/service-factories/factory-options";
import {
@@ -156,6 +160,7 @@ export class MainContextMenuHandler {
constructor(
private stateService: BrowserStateService,
private autofillSettingsService: AutofillSettingsServiceAbstraction,
private i18nService: I18nService,
private logService: LogService,
) {}
@@ -183,6 +188,7 @@ export class MainContextMenuHandler {
return new MainContextMenuHandler(
await stateServiceFactory(cachedServices, serviceOptions),
await autofillSettingsServiceFactory(cachedServices, serviceOptions),
await i18nServiceFactory(cachedServices, serviceOptions),
await logServiceFactory(cachedServices, serviceOptions),
);
@@ -193,8 +199,8 @@ export class MainContextMenuHandler {
* @returns a boolean showing whether or not items were created
*/
async init(): Promise<boolean> {
const menuDisabled = await this.stateService.getDisableContextMenuItem();
if (menuDisabled) {
const menuEnabled = await firstValueFrom(this.autofillSettingsService.enableContextMenu$);
if (!menuEnabled) {
await MainContextMenuHandler.removeAll();
return false;
}

View File

@@ -950,6 +950,7 @@ export default class MainBackground {
if (!this.popupOnlyContext) {
this.mainContextMenuHandler = new MainContextMenuHandler(
this.stateService,
this.autofillSettingsService,
this.i18nService,
this.logService,
);

View File

@@ -105,7 +105,9 @@ export class OptionsComponent implements OnInit {
this.userNotificationSettingsService.enableChangedPasswordPrompt$,
);
this.enableContextMenuItem = !(await this.stateService.getDisableContextMenuItem());
this.enableContextMenuItem = await firstValueFrom(
this.autofillSettingsService.enableContextMenu$,
);
this.showCardsCurrentTab = !(await this.stateService.getDontShowCardsCurrentTab());
this.showIdentitiesCurrentTab = !(await this.stateService.getDontShowIdentitiesCurrentTab());
@@ -143,7 +145,7 @@ export class OptionsComponent implements OnInit {
}
async updateContextMenuItem() {
await this.stateService.setDisableContextMenuItem(!this.enableContextMenuItem);
await this.autofillSettingsService.setEnableContextMenu(this.enableContextMenuItem);
this.messagingService.send("bgUpdateContextMenu");
}