mirror of
https://github.com/bitwarden/browser
synced 2026-02-10 21:50:15 +00:00
35 lines
855 B
TypeScript
35 lines
855 B
TypeScript
import { BehaviorSubject, firstValueFrom } from "rxjs";
|
|
|
|
import { UserState } from "../src/platform/interfaces/user-state";
|
|
|
|
export class TestUserState<T> implements UserState<T> {
|
|
private _state$: BehaviorSubject<T>;
|
|
get state$() {
|
|
return this._state$.asObservable();
|
|
}
|
|
|
|
constructor(initialValue: T) {
|
|
this._state$ = new BehaviorSubject(initialValue);
|
|
}
|
|
|
|
getFromState = jest.fn();
|
|
update = jest.fn().mockImplementation(this.minimalUpdate);
|
|
updateFor = jest.fn();
|
|
createDerived = jest.fn();
|
|
|
|
next(next: T) {
|
|
this._state$.next(next);
|
|
}
|
|
|
|
complete() {
|
|
this._state$.complete();
|
|
}
|
|
|
|
private async minimalUpdate(configureState: (state: T) => T) {
|
|
const currentState = await firstValueFrom(this.state$);
|
|
const newState = configureState(currentState);
|
|
this._state$.next(newState);
|
|
return newState;
|
|
}
|
|
}
|