1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-08 09:43:42 +01:00
bitwarden-browser/libs/common/spec/fake-account-service.ts
Matt Gibson 8d698d9d84
[PM-7169][PM-5267] Remove auth status from account info (#8539)
* remove active account unlocked from state service

* Remove status from account service `AccountInfo`

* Fixup lingering usages of status

Fixup missed factories

* Fixup account info usage

* fixup CLI build

* Fixup current account type

* Add helper for all auth statuses to auth service

* Fix tests

* Uncomment mistakenly commented code

* Rework logged out account exclusion tests

* Correct test description

* Avoid getters returning observables

* fixup type
2024-04-12 09:25:45 +02:00

63 lines
2.1 KiB
TypeScript

import { mock } from "jest-mock-extended";
import { ReplaySubject } from "rxjs";
import { AccountInfo, AccountService } from "../src/auth/abstractions/account.service";
import { UserId } from "../src/types/guid";
export function mockAccountServiceWith(
userId: UserId,
info: Partial<AccountInfo> = {},
): FakeAccountService {
const fullInfo: AccountInfo = {
...info,
...{
name: "name",
email: "email",
},
};
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;
}
accounts$ = this.accountsSubject.asObservable();
activeAccount$ = this.activeAccountSubject.asObservable();
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> {
const current = this.accountsSubject["_buffer"][0] ?? {};
this.accountsSubject.next({ ...current, [userId]: accountData });
await this.mock.addAccount(userId, accountData);
}
async setAccountName(userId: UserId, name: string): Promise<void> {
await this.mock.setAccountName(userId, name);
}
async setAccountEmail(userId: UserId, email: string): Promise<void> {
await this.mock.setAccountEmail(userId, email);
}
async switchAccount(userId: UserId): Promise<void> {
const next =
userId == null ? null : { id: userId, ...this.accountsSubject["_buffer"]?.[0]?.[userId] };
this.activeAccountSubject.next(next);
await this.mock.switchAccount(userId);
}
}