Skip to content

Commit

Permalink
refactor feature flag state fetching and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jprusik committed Jan 3, 2025
1 parent b962373 commit fdca2e3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion libs/common/src/autofill/services/domain-settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit fdca2e3

Please sign in to comment.