mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
refactor feature flag state fetching and update tests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { mock, MockProxy, mockReset } from "jest-mock-extended";
|
||||
import { BehaviorSubject } from "rxjs";
|
||||
import { BehaviorSubject, of } from "rxjs";
|
||||
|
||||
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
||||
@@ -151,6 +151,8 @@ describe("OverlayBackground", () => {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
configService = mock<ConfigService>();
|
||||
configService.getFeatureFlag$.mockImplementation(() => of(true));
|
||||
accountService = mockAccountServiceWith(mockUserId);
|
||||
fakeStateProvider = new FakeStateProvider(accountService);
|
||||
showFaviconsMock$ = new BehaviorSubject(true);
|
||||
|
||||
@@ -94,6 +94,8 @@ describe("OverlayBackground", () => {
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
configService = mock<ConfigService>();
|
||||
configService.getFeatureFlag$.mockImplementation(() => of(true));
|
||||
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, configService);
|
||||
activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Unlocked);
|
||||
authService = mock<AuthService>();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
import { firstValueFrom, of } from "rxjs";
|
||||
import { of } from "rxjs";
|
||||
|
||||
import { PolicyService } from "@bitwarden/common/admin-console/services/policy/policy.service";
|
||||
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
|
||||
@@ -24,15 +24,6 @@ import { FilelessImportPort, FilelessImportType } from "../enums/fileless-import
|
||||
|
||||
import FilelessImporterBackground from "./fileless-importer.background";
|
||||
|
||||
jest.mock("rxjs", () => {
|
||||
const rxjs = jest.requireActual("rxjs");
|
||||
const { firstValueFrom } = rxjs;
|
||||
return {
|
||||
...rxjs,
|
||||
firstValueFrom: jest.fn(firstValueFrom),
|
||||
};
|
||||
});
|
||||
|
||||
describe("FilelessImporterBackground ", () => {
|
||||
let filelessImporterBackground: FilelessImporterBackground;
|
||||
const configService = mock<ConfigService>();
|
||||
@@ -49,6 +40,7 @@ describe("FilelessImporterBackground ", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
domainSettingsService.blockedInteractionsUris$ = of(null);
|
||||
policyService.policyAppliesToActiveUser$.mockImplementation(() => of(true));
|
||||
scriptInjectorService = new BrowserScriptInjectorService(
|
||||
domainSettingsService,
|
||||
platformUtilsService,
|
||||
@@ -102,7 +94,6 @@ describe("FilelessImporterBackground ", () => {
|
||||
});
|
||||
|
||||
it("posts a message to the port indicating that the fileless import feature is disabled if the user's auth status is not unlocked", async () => {
|
||||
(firstValueFrom as jest.Mock).mockResolvedValue(false);
|
||||
jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Locked);
|
||||
|
||||
triggerRuntimeOnConnectEvent(lpImporterPort);
|
||||
@@ -115,8 +106,6 @@ describe("FilelessImporterBackground ", () => {
|
||||
});
|
||||
|
||||
it("posts a message to the port indicating that the fileless import feature is disabled if the user's policy removes individual vaults", async () => {
|
||||
(firstValueFrom as jest.Mock).mockResolvedValue(true);
|
||||
|
||||
triggerRuntimeOnConnectEvent(lpImporterPort);
|
||||
await flushPromises();
|
||||
|
||||
@@ -139,7 +128,8 @@ describe("FilelessImporterBackground ", () => {
|
||||
});
|
||||
|
||||
it("posts a message to the port indicating that the fileless import feature is enabled", async () => {
|
||||
(firstValueFrom as jest.Mock).mockResolvedValue(false);
|
||||
policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
|
||||
|
||||
triggerRuntimeOnConnectEvent(lpImporterPort);
|
||||
await flushPromises();
|
||||
|
||||
@@ -150,6 +140,7 @@ describe("FilelessImporterBackground ", () => {
|
||||
});
|
||||
|
||||
it("triggers an injection of the `lp-suppress-import-download.js` script in manifest v3", async () => {
|
||||
policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
|
||||
manifestVersionSpy.mockReturnValue(3);
|
||||
|
||||
triggerRuntimeOnConnectEvent(lpImporterPort);
|
||||
@@ -163,7 +154,7 @@ describe("FilelessImporterBackground ", () => {
|
||||
});
|
||||
|
||||
it("triggers an injection of the `lp-suppress-import-download-script-append-mv2.js` script in manifest v2", async () => {
|
||||
(firstValueFrom as jest.Mock).mockResolvedValueOnce(false);
|
||||
policyService.policyAppliesToActiveUser$.mockImplementationOnce(() => of(false));
|
||||
manifestVersionSpy.mockReturnValue(2);
|
||||
|
||||
triggerRuntimeOnConnectEvent(lpImporterPort);
|
||||
@@ -182,9 +173,10 @@ describe("FilelessImporterBackground ", () => {
|
||||
let lpImporterPort: chrome.runtime.Port;
|
||||
|
||||
beforeEach(async () => {
|
||||
policyService.policyAppliesToActiveUser$.mockImplementation(() => of(false));
|
||||
jest.spyOn(authService, "getAuthStatus").mockResolvedValue(AuthenticationStatus.Unlocked);
|
||||
jest.spyOn(configService, "getFeatureFlag").mockResolvedValue(true);
|
||||
(firstValueFrom as jest.Mock).mockResolvedValue(false);
|
||||
|
||||
triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.NotificationBar));
|
||||
triggerRuntimeOnConnectEvent(createPortSpyMock(FilelessImportPort.LpImporter));
|
||||
await flushPromises();
|
||||
|
||||
@@ -11,10 +11,9 @@ import { DefaultDomainSettingsService, DomainSettingsService } from "./domain-se
|
||||
|
||||
describe("DefaultDomainSettingsService", () => {
|
||||
let domainSettingsService: DomainSettingsService;
|
||||
const configServiceMock = mock<ConfigService>();
|
||||
let configService: MockProxy<ConfigService>;
|
||||
const mockUserId = Utils.newGuid() as UserId;
|
||||
const accountService: FakeAccountService = mockAccountServiceWith(mockUserId);
|
||||
let mockConfigService: MockProxy<ConfigService>;
|
||||
const fakeStateProvider: FakeStateProvider = new FakeStateProvider(accountService);
|
||||
|
||||
const mockEquivalentDomains = [
|
||||
@@ -24,8 +23,9 @@ describe("DefaultDomainSettingsService", () => {
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, mockConfigService);
|
||||
jest.spyOn(configServiceMock, "getFeatureFlag$").mockReturnValue(of(false));
|
||||
configService = mock<ConfigService>();
|
||||
configService.getFeatureFlag$.mockImplementation(() => of(false));
|
||||
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, configService);
|
||||
|
||||
jest.spyOn(domainSettingsService, "getUrlEquivalentDomains");
|
||||
domainSettingsService.equivalentDomains$ = of(mockEquivalentDomains);
|
||||
|
||||
@@ -99,7 +99,12 @@ export class DefaultDomainSettingsService implements DomainSettingsService {
|
||||
|
||||
this.blockedInteractionsUris$ = this.configService
|
||||
.getFeatureFlag$(FeatureFlag.BlockBrowserInjectionsByDomain)
|
||||
.pipe(switchMap((enabled) => (enabled ? this.blockedInteractionsUrisState.state$ : of({}))));
|
||||
.pipe(
|
||||
switchMap((featureIsEnabled) =>
|
||||
featureIsEnabled ? this.blockedInteractionsUrisState.state$ : of({} as NeverDomains),
|
||||
),
|
||||
map((disabledUris) => (Object.keys(disabledUris).length ? disabledUris : null)),
|
||||
);
|
||||
|
||||
this.equivalentDomainsState = this.stateProvider.getActive(EQUIVALENT_DOMAINS);
|
||||
this.equivalentDomains$ = this.equivalentDomainsState.state$.pipe(map((x) => x ?? null));
|
||||
|
||||
Reference in New Issue
Block a user