1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 14:23:32 +00:00

Allow common get and set operations from state providers (#7824)

* Allow common get and set operations from state providers

* Use finnish endings for observables
This commit is contained in:
Matt Gibson
2024-02-06 11:35:22 -05:00
committed by GitHub
parent cc88826be4
commit 166269520c
8 changed files with 189 additions and 7 deletions

View File

@@ -21,9 +21,10 @@ describe("DefaultStateProvider", () => {
let globalStateProvider: FakeGlobalStateProvider;
let derivedStateProvider: FakeDerivedStateProvider;
let accountService: FakeAccountService;
const userId = "fakeUserId" as UserId;
beforeEach(() => {
accountService = mockAccountServiceWith("fakeUserId" as UserId);
accountService = mockAccountServiceWith(userId);
activeUserStateProvider = new FakeActiveUserStateProvider(accountService);
singleUserStateProvider = new FakeSingleUserStateProvider();
globalStateProvider = new FakeGlobalStateProvider();
@@ -36,6 +37,74 @@ describe("DefaultStateProvider", () => {
);
});
describe("activeUserId$", () => {
it("should track the active User id from active user state provider", () => {
expect(sut.activeUserId$).toBe(activeUserStateProvider.activeUserId$);
});
});
describe("getUserState$", () => {
const keyDefinition = new KeyDefinition<string>(new StateDefinition("test", "disk"), "test", {
deserializer: (s) => s,
});
it("should get the state for the active user if no userId is provided", () => {
const state = sut.getUserState$(keyDefinition);
expect(state).toBe(activeUserStateProvider.get(keyDefinition).state$);
});
it("should not return state for a single user if no userId is provided", () => {
const state = sut.getUserState$(keyDefinition);
expect(state).not.toBe(singleUserStateProvider.get(userId, keyDefinition).state$);
});
it("should get the state for the provided userId", () => {
const userId = "user" as UserId;
const state = sut.getUserState$(keyDefinition, userId);
expect(state).toBe(singleUserStateProvider.get(userId, keyDefinition).state$);
});
it("should not get the active user state if userId is provided", () => {
const userId = "user" as UserId;
const state = sut.getUserState$(keyDefinition, userId);
expect(state).not.toBe(activeUserStateProvider.get(keyDefinition).state$);
});
});
describe("setUserState", () => {
const keyDefinition = new KeyDefinition<string>(new StateDefinition("test", "disk"), "test", {
deserializer: (s) => s,
});
it("should set the state for the active user if no userId is provided", async () => {
const value = "value";
await sut.setUserState(keyDefinition, value);
const state = activeUserStateProvider.getFake(keyDefinition);
expect(state.nextMock).toHaveBeenCalledWith([expect.any(String), value]);
});
it("should not set state for a single user if no userId is provided", async () => {
const value = "value";
await sut.setUserState(keyDefinition, value);
const state = singleUserStateProvider.getFake(userId, keyDefinition);
expect(state.nextMock).not.toHaveBeenCalled();
});
it("should set the state for the provided userId", async () => {
const value = "value";
await sut.setUserState(keyDefinition, value, userId);
const state = singleUserStateProvider.getFake(userId, keyDefinition);
expect(state.nextMock).toHaveBeenCalledWith(value);
});
it("should not set the active user state if userId is provided", async () => {
const value = "value";
await sut.setUserState(keyDefinition, value, userId);
const state = activeUserStateProvider.getFake(keyDefinition);
expect(state.nextMock).not.toHaveBeenCalled();
});
});
it("should bind the activeUserStateProvider", () => {
const keyDefinition = new KeyDefinition(new StateDefinition("test", "disk"), "test", {
deserializer: () => null,