mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 21:33:27 +00:00
Allow common get and set operations from state providers (#7824)
* Allow common get and set operations from state providers * Use finnish endings for observables
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
import { Observable } from "rxjs";
|
||||
import { Observable, map } from "rxjs";
|
||||
|
||||
import {
|
||||
GlobalState,
|
||||
@@ -99,11 +99,14 @@ export class FakeSingleUserStateProvider implements SingleUserStateProvider {
|
||||
}
|
||||
|
||||
export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
||||
activeUserId$: Observable<UserId>;
|
||||
establishedMocks: Map<string, FakeActiveUserState<unknown>> = new Map();
|
||||
|
||||
states: Map<string, FakeActiveUserState<unknown>> = new Map();
|
||||
|
||||
constructor(public accountService: FakeAccountService) {}
|
||||
constructor(public accountService: FakeAccountService) {
|
||||
this.activeUserId$ = accountService.activeAccountSubject.asObservable().pipe(map((a) => a.id));
|
||||
}
|
||||
|
||||
get<T>(keyDefinition: KeyDefinition<T>): ActiveUserState<T> {
|
||||
let result = this.states.get(keyDefinition.fullName);
|
||||
@@ -137,6 +140,21 @@ export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
||||
}
|
||||
|
||||
export class FakeStateProvider implements StateProvider {
|
||||
getUserState$<T>(keyDefinition: KeyDefinition<T>, userId?: UserId): Observable<T> {
|
||||
if (userId) {
|
||||
return this.getUser<T>(userId, keyDefinition).state$;
|
||||
}
|
||||
return this.getActive<T>(keyDefinition).state$;
|
||||
}
|
||||
|
||||
async setUserState<T>(keyDefinition: KeyDefinition<T>, value: T, userId?: UserId): Promise<void> {
|
||||
if (userId) {
|
||||
await this.getUser(userId, keyDefinition).update(() => value);
|
||||
} else {
|
||||
await this.getActive(keyDefinition).update(() => value);
|
||||
}
|
||||
}
|
||||
|
||||
getActive<T>(keyDefinition: KeyDefinition<T>): ActiveUserState<T> {
|
||||
return this.activeUser.get(keyDefinition);
|
||||
}
|
||||
@@ -163,6 +181,7 @@ export class FakeStateProvider implements StateProvider {
|
||||
singleUser: FakeSingleUserStateProvider = new FakeSingleUserStateProvider();
|
||||
activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider(this.accountService);
|
||||
derived: FakeDerivedStateProvider = new FakeDerivedStateProvider();
|
||||
activeUserId$: Observable<UserId> = this.activeUser.activeUserId$;
|
||||
}
|
||||
|
||||
export class FakeDerivedStateProvider implements DerivedStateProvider {
|
||||
|
||||
@@ -182,13 +182,13 @@ export class FakeActiveUserState<T> implements ActiveUserState<T> {
|
||||
}
|
||||
const newState = configureState(current, combinedDependencies);
|
||||
this.stateSubject.next([this.userId, newState]);
|
||||
this.nextMock(this.userId, newState);
|
||||
this.nextMock([this.userId, newState]);
|
||||
return newState;
|
||||
}
|
||||
|
||||
updateMock = this.update as jest.MockedFunction<typeof this.update>;
|
||||
|
||||
nextMock = jest.fn<void, [UserId, T]>();
|
||||
nextMock = jest.fn<void, [[UserId, T]]>();
|
||||
|
||||
private _keyDefinition: KeyDefinition<T> | null = null;
|
||||
get keyDefinition() {
|
||||
|
||||
Reference in New Issue
Block a user