1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

getUserState$ Helper Improvements (#8267)

* Block Sending Null to `getUser`

* Update Comments & Tests

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* Update Comment

* Update Fake

---------

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
This commit is contained in:
Justin Baur
2024-03-15 08:05:04 -05:00
committed by GitHub
parent c92cdb6e84
commit 1e921eb4f6
4 changed files with 174 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
import { mock } from "jest-mock-extended";
import { Observable, map } from "rxjs";
import { Observable, map, of, switchMap, take } from "rxjs";
import {
GlobalState,
@@ -171,7 +171,30 @@ export class FakeStateProvider implements StateProvider {
if (userId) {
return this.getUser<T>(userId, keyDefinition).state$;
}
return this.getActive<T>(keyDefinition).state$;
return this.getActive(keyDefinition).state$;
}
getUserStateOrDefault$<T>(
keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>,
config: { userId: UserId | undefined; defaultValue?: T },
): Observable<T> {
const { userId, defaultValue = null } = config;
if (isUserKeyDefinition(keyDefinition)) {
this.mock.getUserStateOrDefault$(keyDefinition, config);
} else {
this.mock.getUserStateOrDefault$(keyDefinition, config);
}
if (userId) {
return this.getUser<T>(userId, keyDefinition).state$;
}
return this.activeUserId$.pipe(
take(1),
switchMap((userId) =>
userId != null ? this.getUser(userId, keyDefinition).state$ : of(defaultValue),
),
);
}
async setUserState<T>(