mirror of
https://github.com/bitwarden/browser
synced 2024-12-26 18:04:07 +01:00
211d7a2626
* Expand state provider fakes - default null initial value for fake states - Easier mocking of key definitions through just the use of key names - allows for not exporting KeyDefinition as long as the key doesn't collide - mock of fake state provider to verify `get` calls - `nextMock` for use of the fn mock matchers on emissions of `state$` - `FakeAccountService` which allows for easy initialization and working with account switching * Small bug fix for cache key collision on key definitions unique by only storage location * Fix initial value for test
70 lines
2.2 KiB
TypeScript
70 lines
2.2 KiB
TypeScript
import { mock } from "jest-mock-extended";
|
|
import { Observable, ReplaySubject } from "rxjs";
|
|
|
|
import { AccountInfo, AccountService } from "../src/auth/abstractions/account.service";
|
|
import { AuthenticationStatus } from "../src/auth/enums/authentication-status";
|
|
import { UserId } from "../src/types/guid";
|
|
|
|
export function mockAccountServiceWith(
|
|
userId: UserId,
|
|
info: Partial<AccountInfo> = {},
|
|
): FakeAccountService {
|
|
const fullInfo: AccountInfo = {
|
|
...info,
|
|
...{
|
|
name: "name",
|
|
email: "email",
|
|
status: AuthenticationStatus.Locked,
|
|
},
|
|
};
|
|
const service = new FakeAccountService({ [userId]: fullInfo });
|
|
service.activeAccountSubject.next({ id: userId, ...fullInfo });
|
|
return service;
|
|
}
|
|
|
|
export class FakeAccountService implements AccountService {
|
|
mock = mock<AccountService>();
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
|
|
accountsSubject = new ReplaySubject<Record<UserId, AccountInfo>>(1);
|
|
// eslint-disable-next-line rxjs/no-exposed-subjects -- test class
|
|
activeAccountSubject = new ReplaySubject<{ id: UserId } & AccountInfo>(1);
|
|
private _activeUserId: UserId;
|
|
get activeUserId() {
|
|
return this._activeUserId;
|
|
}
|
|
get accounts$() {
|
|
return this.accountsSubject.asObservable();
|
|
}
|
|
get activeAccount$() {
|
|
return this.activeAccountSubject.asObservable();
|
|
}
|
|
accountLock$: Observable<UserId>;
|
|
accountLogout$: Observable<UserId>;
|
|
|
|
constructor(initialData: Record<UserId, AccountInfo>) {
|
|
this.accountsSubject.next(initialData);
|
|
this.activeAccountSubject.subscribe((data) => (this._activeUserId = data?.id));
|
|
this.activeAccountSubject.next(null);
|
|
}
|
|
|
|
async addAccount(userId: UserId, accountData: AccountInfo): Promise<void> {
|
|
this.mock.addAccount(userId, accountData);
|
|
}
|
|
|
|
async setAccountName(userId: UserId, name: string): Promise<void> {
|
|
this.mock.setAccountName(userId, name);
|
|
}
|
|
|
|
async setAccountEmail(userId: UserId, email: string): Promise<void> {
|
|
this.mock.setAccountEmail(userId, email);
|
|
}
|
|
|
|
async setAccountStatus(userId: UserId, status: AuthenticationStatus): Promise<void> {
|
|
this.mock.setAccountStatus(userId, status);
|
|
}
|
|
|
|
async switchAccount(userId: UserId): Promise<void> {
|
|
this.mock.switchAccount(userId);
|
|
}
|
|
}
|