mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
* Add New KeyDefinitionOption * Add New Services * Add WebStorageServiceProvider Tests * Update Error Message * Add `UserKeyDefinition` * Fix Deserialization Helpers * Fix KeyDefinition * Add `UserKeyDefinition` * Fix Deserialization Helpers * Fix KeyDefinition * Move `ClearEvent` * Cleanup * Fix Imports * Remove `updateMock` * Call Super in Web Implementation * Use Better Type to Avoid Casting * Better Error Docs * Move StorageKey Creation to Function * Throw Aggregated Error for Failures
86 lines
2.4 KiB
TypeScript
86 lines
2.4 KiB
TypeScript
import { mock } from "jest-mock-extended";
|
|
|
|
import { FakeGlobalStateProvider } from "../../../spec";
|
|
import { AbstractStorageService, ObservableStorageService } from "../abstractions/storage.service";
|
|
import { StorageServiceProvider } from "../services/storage-service.provider";
|
|
|
|
import { StateDefinition } from "./state-definition";
|
|
import { STATE_LOCK_EVENT, StateEventRegistrarService } from "./state-event-registrar.service";
|
|
import { UserKeyDefinition } from "./user-key-definition";
|
|
|
|
describe("StateEventRegistrarService", () => {
|
|
const globalStateProvider = new FakeGlobalStateProvider();
|
|
const lockState = globalStateProvider.getFake(STATE_LOCK_EVENT);
|
|
const storageServiceProvider = mock<StorageServiceProvider>();
|
|
|
|
const sut = new StateEventRegistrarService(globalStateProvider, storageServiceProvider);
|
|
|
|
describe("registerEvents", () => {
|
|
const fakeKeyDefinition = new UserKeyDefinition<boolean>(
|
|
new StateDefinition("fakeState", "disk"),
|
|
"fakeKey",
|
|
{
|
|
deserializer: (s) => s,
|
|
clearOn: ["lock"],
|
|
},
|
|
);
|
|
|
|
beforeEach(() => {
|
|
jest.resetAllMocks();
|
|
});
|
|
|
|
it("adds event on null storage", async () => {
|
|
storageServiceProvider.get.mockReturnValue([
|
|
"disk",
|
|
mock<AbstractStorageService & ObservableStorageService>(),
|
|
]);
|
|
|
|
await sut.registerEvents(fakeKeyDefinition);
|
|
|
|
expect(lockState.nextMock).toHaveBeenCalledWith([
|
|
{
|
|
key: "fakeKey",
|
|
location: "disk",
|
|
state: "fakeState",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("adds event on empty array in storage", async () => {
|
|
lockState.stateSubject.next([]);
|
|
storageServiceProvider.get.mockReturnValue([
|
|
"disk",
|
|
mock<AbstractStorageService & ObservableStorageService>(),
|
|
]);
|
|
|
|
await sut.registerEvents(fakeKeyDefinition);
|
|
|
|
expect(lockState.nextMock).toHaveBeenCalledWith([
|
|
{
|
|
key: "fakeKey",
|
|
location: "disk",
|
|
state: "fakeState",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("doesn't add a duplicate", async () => {
|
|
lockState.stateSubject.next([
|
|
{
|
|
key: "fakeKey",
|
|
location: "disk",
|
|
state: "fakeState",
|
|
},
|
|
]);
|
|
storageServiceProvider.get.mockReturnValue([
|
|
"disk",
|
|
mock<AbstractStorageService & ObservableStorageService>(),
|
|
]);
|
|
|
|
await sut.registerEvents(fakeKeyDefinition);
|
|
|
|
expect(lockState.nextMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|