mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
Ps/improve state provider fakers (#7494)
* 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
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { mock } from "jest-mock-extended";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import {
|
||||
@@ -16,6 +17,7 @@ import {
|
||||
import { UserId } from "../src/types/guid";
|
||||
import { DerivedStateDependencies } from "../src/types/state";
|
||||
|
||||
import { FakeAccountService } from "./fake-account-service";
|
||||
import {
|
||||
FakeActiveUserState,
|
||||
FakeDerivedState,
|
||||
@@ -24,56 +26,114 @@ import {
|
||||
} from "./fake-state";
|
||||
|
||||
export class FakeGlobalStateProvider implements GlobalStateProvider {
|
||||
mock = mock<GlobalStateProvider>();
|
||||
establishedMocks: Map<string, FakeGlobalState<unknown>> = new Map();
|
||||
states: Map<string, GlobalState<unknown>> = new Map();
|
||||
get<T>(keyDefinition: KeyDefinition<T>): GlobalState<T> {
|
||||
let result = this.states.get(keyDefinition.buildCacheKey("global")) as GlobalState<T>;
|
||||
this.mock.get(keyDefinition);
|
||||
let result = this.states.get(keyDefinition.buildCacheKey("global"));
|
||||
|
||||
if (result == null) {
|
||||
let fake: FakeGlobalState<T>;
|
||||
// Look for established mock
|
||||
if (this.establishedMocks.has(keyDefinition.key)) {
|
||||
fake = this.establishedMocks.get(keyDefinition.key) as FakeGlobalState<T>;
|
||||
} else {
|
||||
fake = new FakeGlobalState<T>();
|
||||
}
|
||||
fake.keyDefinition = keyDefinition;
|
||||
result = fake;
|
||||
this.states.set(keyDefinition.buildCacheKey("global"), result);
|
||||
|
||||
result = new FakeGlobalState<T>();
|
||||
this.states.set(keyDefinition.buildCacheKey("global"), result);
|
||||
}
|
||||
return result;
|
||||
return result as GlobalState<T>;
|
||||
}
|
||||
|
||||
getFake<T>(keyDefinition: KeyDefinition<T>): FakeGlobalState<T> {
|
||||
return this.get(keyDefinition) as FakeGlobalState<T>;
|
||||
}
|
||||
|
||||
mockFor<T>(keyDefinitionKey: string, initialValue?: T): FakeGlobalState<T> {
|
||||
if (!this.establishedMocks.has(keyDefinitionKey)) {
|
||||
this.establishedMocks.set(keyDefinitionKey, new FakeGlobalState<T>(initialValue));
|
||||
}
|
||||
return this.establishedMocks.get(keyDefinitionKey) as FakeGlobalState<T>;
|
||||
}
|
||||
}
|
||||
|
||||
export class FakeSingleUserStateProvider implements SingleUserStateProvider {
|
||||
mock = mock<SingleUserStateProvider>();
|
||||
establishedMocks: Map<string, FakeSingleUserState<unknown>> = new Map();
|
||||
states: Map<string, SingleUserState<unknown>> = new Map();
|
||||
get<T>(userId: UserId, keyDefinition: KeyDefinition<T>): SingleUserState<T> {
|
||||
let result = this.states.get(keyDefinition.buildCacheKey("user", userId)) as SingleUserState<T>;
|
||||
this.mock.get(userId, keyDefinition);
|
||||
let result = this.states.get(keyDefinition.buildCacheKey("user", userId));
|
||||
|
||||
if (result == null) {
|
||||
result = new FakeSingleUserState<T>(userId);
|
||||
let fake: FakeSingleUserState<T>;
|
||||
// Look for established mock
|
||||
if (this.establishedMocks.has(keyDefinition.key)) {
|
||||
fake = this.establishedMocks.get(keyDefinition.key) as FakeSingleUserState<T>;
|
||||
} else {
|
||||
fake = new FakeSingleUserState<T>(userId);
|
||||
}
|
||||
fake.keyDefinition = keyDefinition;
|
||||
result = fake;
|
||||
this.states.set(keyDefinition.buildCacheKey("user", userId), result);
|
||||
}
|
||||
return result;
|
||||
return result as SingleUserState<T>;
|
||||
}
|
||||
|
||||
getFake<T>(userId: UserId, keyDefinition: KeyDefinition<T>): FakeSingleUserState<T> {
|
||||
return this.get(userId, keyDefinition) as FakeSingleUserState<T>;
|
||||
}
|
||||
|
||||
mockFor<T>(userId: UserId, keyDefinitionKey: string, initialValue?: T): FakeSingleUserState<T> {
|
||||
if (!this.establishedMocks.has(keyDefinitionKey)) {
|
||||
this.establishedMocks.set(keyDefinitionKey, new FakeSingleUserState<T>(userId, initialValue));
|
||||
}
|
||||
return this.establishedMocks.get(keyDefinitionKey) as FakeSingleUserState<T>;
|
||||
}
|
||||
}
|
||||
|
||||
export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
||||
states: Map<string, ActiveUserState<unknown>> = new Map();
|
||||
establishedMocks: Map<string, FakeActiveUserState<unknown>> = new Map();
|
||||
|
||||
states: Map<string, FakeActiveUserState<unknown>> = new Map();
|
||||
|
||||
constructor(public accountService: FakeAccountService) {}
|
||||
|
||||
get<T>(keyDefinition: KeyDefinition<T>): ActiveUserState<T> {
|
||||
let result = this.states.get(
|
||||
keyDefinition.buildCacheKey("user", "active"),
|
||||
) as ActiveUserState<T>;
|
||||
let result = this.states.get(keyDefinition.buildCacheKey("user", "active"));
|
||||
|
||||
if (result == null) {
|
||||
result = new FakeActiveUserState<T>();
|
||||
// Look for established mock
|
||||
if (this.establishedMocks.has(keyDefinition.key)) {
|
||||
result = this.establishedMocks.get(keyDefinition.key);
|
||||
} else {
|
||||
result = new FakeActiveUserState<T>(this.accountService);
|
||||
}
|
||||
result.keyDefinition = keyDefinition;
|
||||
this.states.set(keyDefinition.buildCacheKey("user", "active"), result);
|
||||
}
|
||||
return result;
|
||||
return result as ActiveUserState<T>;
|
||||
}
|
||||
|
||||
getFake<T>(keyDefinition: KeyDefinition<T>): FakeActiveUserState<T> {
|
||||
return this.get(keyDefinition) as FakeActiveUserState<T>;
|
||||
}
|
||||
|
||||
mockFor<T>(keyDefinitionKey: string, initialValue?: T): FakeActiveUserState<T> {
|
||||
if (!this.establishedMocks.has(keyDefinitionKey)) {
|
||||
this.establishedMocks.set(
|
||||
keyDefinitionKey,
|
||||
new FakeActiveUserState<T>(this.accountService, initialValue),
|
||||
);
|
||||
}
|
||||
return this.establishedMocks.get(keyDefinitionKey) as FakeActiveUserState<T>;
|
||||
}
|
||||
}
|
||||
|
||||
export class FakeStateProvider implements StateProvider {
|
||||
@@ -97,9 +157,11 @@ export class FakeStateProvider implements StateProvider {
|
||||
return this.derived.get(parentState$, deriveDefinition, dependencies);
|
||||
}
|
||||
|
||||
constructor(public accountService: FakeAccountService) {}
|
||||
|
||||
global: FakeGlobalStateProvider = new FakeGlobalStateProvider();
|
||||
singleUser: FakeSingleUserStateProvider = new FakeSingleUserStateProvider();
|
||||
activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider();
|
||||
activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider(this.accountService);
|
||||
derived: FakeDerivedStateProvider = new FakeDerivedStateProvider();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user