2023-11-09 23:06:42 +01:00
|
|
|
import { MockProxy, mock } from "jest-mock-extended";
|
|
|
|
import { Subject } from "rxjs";
|
|
|
|
|
|
|
|
import {
|
|
|
|
AbstractStorageService,
|
|
|
|
StorageUpdate,
|
|
|
|
} from "../src/platform/abstractions/storage.service";
|
|
|
|
import { StorageOptions } from "../src/platform/models/domain/storage-options";
|
|
|
|
|
|
|
|
export class FakeStorageService implements AbstractStorageService {
|
|
|
|
private store: Record<string, unknown>;
|
|
|
|
private updatesSubject = new Subject<StorageUpdate>();
|
2023-11-16 20:15:34 +01:00
|
|
|
private _valuesRequireDeserialization = false;
|
2023-11-09 23:06:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a mock of a {@see AbstractStorageService} for asserting the expected
|
|
|
|
* amount of calls. It is not recommended to use this to mock implementations as
|
|
|
|
* they are not respected.
|
|
|
|
*/
|
|
|
|
mock: MockProxy<AbstractStorageService>;
|
|
|
|
|
|
|
|
constructor(initial?: Record<string, unknown>) {
|
|
|
|
this.store = initial ?? {};
|
|
|
|
this.mock = mock<AbstractStorageService>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the internal store for this fake implementation, this bypasses any mock calls
|
|
|
|
* or updates to the {@link updates$} observable.
|
|
|
|
* @param store
|
|
|
|
*/
|
|
|
|
internalUpdateStore(store: Record<string, unknown>) {
|
|
|
|
this.store = store;
|
|
|
|
}
|
|
|
|
|
2023-11-17 15:20:42 +01:00
|
|
|
get internalStore() {
|
|
|
|
return this.store;
|
|
|
|
}
|
|
|
|
|
2023-11-16 20:15:34 +01:00
|
|
|
internalUpdateValuesRequireDeserialization(value: boolean) {
|
|
|
|
this._valuesRequireDeserialization = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get valuesRequireDeserialization(): boolean {
|
|
|
|
return this._valuesRequireDeserialization;
|
|
|
|
}
|
|
|
|
|
2023-11-09 23:06:42 +01:00
|
|
|
get updates$() {
|
|
|
|
return this.updatesSubject.asObservable();
|
|
|
|
}
|
|
|
|
|
|
|
|
get<T>(key: string, options?: StorageOptions): Promise<T> {
|
|
|
|
this.mock.get(key, options);
|
|
|
|
const value = this.store[key] as T;
|
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
|
|
|
has(key: string, options?: StorageOptions): Promise<boolean> {
|
|
|
|
this.mock.has(key, options);
|
|
|
|
return Promise.resolve(this.store[key] != null);
|
|
|
|
}
|
|
|
|
save<T>(key: string, obj: T, options?: StorageOptions): Promise<void> {
|
2023-12-13 14:06:24 +01:00
|
|
|
this.mock.save(key, obj, options);
|
2023-11-09 23:06:42 +01:00
|
|
|
this.store[key] = obj;
|
2023-11-16 20:15:34 +01:00
|
|
|
this.updatesSubject.next({ key: key, updateType: "save" });
|
2023-11-09 23:06:42 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
remove(key: string, options?: StorageOptions): Promise<void> {
|
|
|
|
this.mock.remove(key, options);
|
|
|
|
delete this.store[key];
|
2023-11-16 20:15:34 +01:00
|
|
|
this.updatesSubject.next({ key: key, updateType: "remove" });
|
2023-11-09 23:06:42 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|