diff --git a/examples/react/functional-component-ts/src/core/index.ts b/examples/react/functional-component-ts/src/core/index.ts index c3fa7e91..705736db 100644 --- a/examples/react/functional-component-ts/src/core/index.ts +++ b/examples/react/functional-component-ts/src/core/index.ts @@ -2,14 +2,41 @@ import { Agile, clone, Logger } from '@agile-ts/core'; import API from '@agile-ts/api'; import Event from '@agile-ts/event'; +export const myStorage: any = {}; + export const App = new Agile({ logConfig: { level: Logger.level.DEBUG }, + localStorage: true, }); +// Register custom second Storage +App.registerStorage( + App.createStorage({ + key: 'myStorage', + methods: { + get: (key: string) => { + console.log(`GET '${key}'`); + return myStorage[key]; + }, + set: (key: string, value: any) => { + console.log(`SET '${key}'`, value); + myStorage[key] = value; + }, + remove: (key: string) => { + console.log(`DELETE '${key}'`); + delete myStorage[key]; + }, + }, + }) +); + export const MY_STATE = App.createState('MyState', { key: 'my-state' }); //.persist(); export const MY_STATE_2 = App.createState('MyState2', { key: 'my-state2', -}).persist(); +}).persist({ + storageKeys: ['myStorage', 'localStorage'], + defaultStorageKey: 'localStorage', // where the persisted value gets loaded from (saved is it in all provided Storages (storageKeys)) +}); MY_STATE_2.onLoad(() => { console.log('On Load MY_STATE_2'); }); diff --git a/packages/core/src/collection/collection.persistent.ts b/packages/core/src/collection/collection.persistent.ts index fed0825c..3fe9758d 100644 --- a/packages/core/src/collection/collection.persistent.ts +++ b/packages/core/src/collection/collection.persistent.ts @@ -35,11 +35,13 @@ export class CollectionPersistent extends Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.collection = () => collection; this.instantiatePersistent({ key: config.key, storageKeys: config.storageKeys, + defaultStorageKey: config.defaultStorageKey, }); // Load/Store persisted Value/s for the first Time @@ -96,17 +98,19 @@ export class CollectionPersistent extends Persistent { /** * @internal * Loads Collection from Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async loadPersistedValue(key?: PersistentKey): Promise { + public async loadPersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Check if Collection is Persisted const isPersisted = await this.agileInstance().storages.get( - _key, - this.defaultStorageKey + _storageKey, + this.config.defaultStorageKey as any ); if (!isPersisted) return false; @@ -119,7 +123,7 @@ export class CollectionPersistent extends Persistent { // Persist Default Group and load its Value manually to be 100% sure it got loaded defaultGroup.persist({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); if (defaultGroup.persistent?.ready) { @@ -131,13 +135,13 @@ export class CollectionPersistent extends Persistent { for (const itemKey of defaultGroup._value) { const itemStorageKey = CollectionPersistent.getItemStorageKey( itemKey, - _key + _storageKey ); // Get Storage Value const storageValue = await this.agileInstance().storages.get( itemStorageKey, - this.defaultStorageKey + this.config.defaultStorageKey as any ); if (!storageValue) continue; @@ -149,7 +153,7 @@ export class CollectionPersistent extends Persistent { const success = await loadValuesIntoCollection(); // Persist Collection, so that the Storage Value updates dynamically if the Collection updates - if (success) await this.persistValue(_key); + if (success) await this.persistValue(_storageKey); return success; } @@ -160,19 +164,19 @@ export class CollectionPersistent extends Persistent { /** * @internal * Sets everything up so that the Collection gets saved in the Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async persistValue(key?: PersistentKey): Promise { + public async persistValue(storageKey?: PersistentKey): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; const defaultGroup = this.collection().getGroup( this.collection().config.defaultGroupKey ); if (!defaultGroup) return false; // Set Collection to Persisted (in Storage) - this.agileInstance().storages.set(_key, true, this.storageKeys); + this.agileInstance().storages.set(_storageKey, true, this.storageKeys); // Persist default Group if (!defaultGroup.isPersisted) @@ -181,7 +185,7 @@ export class CollectionPersistent extends Persistent { // Add sideEffect to default Group which adds and removes Items from the Storage depending on the Group Value defaultGroup.addSideEffect( CollectionPersistent.defaultGroupSideEffectKey, - () => this.rebuildStorageSideEffect(defaultGroup, _key), + () => this.rebuildStorageSideEffect(defaultGroup, _storageKey), { weight: 0 } ); @@ -190,7 +194,7 @@ export class CollectionPersistent extends Persistent { const item = this.collection().getItem(itemKey); const itemStorageKey = CollectionPersistent.getItemStorageKey( itemKey, - _key + _storageKey ); item?.persist(itemStorageKey); } @@ -205,19 +209,21 @@ export class CollectionPersistent extends Persistent { /** * @internal * Removes Collection from the Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async removePersistedValue(key?: PersistentKey): Promise { + public async removePersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; const defaultGroup = this.collection().getGroup( this.collection().config.defaultGroupKey ); if (!defaultGroup) return false; // Set Collection to not Persisted - this.agileInstance().storages.remove(_key, this.storageKeys); + this.agileInstance().storages.remove(_storageKey, this.storageKeys); // Remove default Group from Storage defaultGroup.persistent?.removePersistedValue(); diff --git a/packages/core/src/collection/group.ts b/packages/core/src/collection/group.ts index 831249dc..984c71ea 100644 --- a/packages/core/src/collection/group.ts +++ b/packages/core/src/collection/group.ts @@ -270,9 +270,10 @@ export class Group extends State> { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, followCollectionPattern: false, storageKeys: [], + defaultStorageKey: null, }); if (_config.followCollectionPersistKeyPattern) { @@ -283,8 +284,9 @@ export class Group extends State> { } super.persist(key, { - instantiate: _config.instantiate, + loadValue: _config.loadValue, storageKeys: _config.storageKeys, + defaultStorageKey: _config.defaultStorageKey, }); return this; diff --git a/packages/core/src/collection/index.ts b/packages/core/src/collection/index.ts index 426935f0..5ca05bad 100644 --- a/packages/core/src/collection/index.ts +++ b/packages/core/src/collection/index.ts @@ -799,8 +799,9 @@ export class Collection { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, }); if (this.persistent) @@ -810,9 +811,10 @@ export class Collection { // Create persistent -> Persist Value this.persistent = new CollectionPersistent(this, { - instantiate: _config.instantiate, + instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, + defaultStorageKey: _config.defaultStorageKey, }); return this; @@ -1279,12 +1281,14 @@ export interface HasConfigInterface { } /** - * @param instantiate - If Persistent gets instantiated + * @param loadValue - If Persistent loads the persisted value into the Collection * @param storageKeys - Key/Name of Storages which gets used to persist the Collection Value (NOTE: If not passed the default Storage will be used) + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ export interface CollectionPersistentConfigInterface { - instantiate?: boolean; + loadValue?: boolean; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } /** diff --git a/packages/core/src/state/index.ts b/packages/core/src/state/index.ts index 53ebe94f..6b9cb056 100644 --- a/packages/core/src/state/index.ts +++ b/packages/core/src/state/index.ts @@ -397,8 +397,9 @@ export class State { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, }); if (this.persistent) @@ -408,9 +409,10 @@ export class State { // Create persistent -> Persist Value this.persistent = new StatePersistent(this, { - instantiate: _config.instantiate, + instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, + defaultStorageKey: _config.defaultStorageKey, }); return this; @@ -657,12 +659,14 @@ export interface PatchConfigInterface extends StateIngestConfigInterface { } /** - * @param instantiate - If Persistent gets instantiated + * @param loadValue - If Persistent loads the persisted value into the State * @param storageKeys - Key/Name of Storages which gets used to persist the State Value (NOTE: If not passed the default Storage will be used) + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ export interface StatePersistentConfigInterface { - instantiate?: boolean; + loadValue?: boolean; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } export type StateWatcherCallback = (value: T, key: string) => void; diff --git a/packages/core/src/state/state.persistent.ts b/packages/core/src/state/state.persistent.ts index 8cd0336b..5e51ea79 100644 --- a/packages/core/src/state/state.persistent.ts +++ b/packages/core/src/state/state.persistent.ts @@ -27,11 +27,13 @@ export class StatePersistent extends Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.state = () => state; this.instantiatePersistent({ key: config.key, storageKeys: config.storageKeys, + defaultStorageKey: config.defaultStorageKey, }); // Load/Store persisted Value for the first Time @@ -88,16 +90,19 @@ export class StatePersistent extends Persistent { /** * @internal * Loads State Value from the Storage + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async loadPersistedValue(key?: PersistentKey): Promise { + public async loadPersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Load Value from default Storage const loadedValue = await this.agileInstance().storages.get( - _key, - this.defaultStorageKey + _storageKey, + this.config.defaultStorageKey as any ); if (!loadedValue) return false; @@ -105,7 +110,7 @@ export class StatePersistent extends Persistent { this.state().set(loadedValue, { storage: false }); // Persist State, so that the Storage Value updates dynamically if the State updates - await this.persistValue(_key); + await this.persistValue(_storageKey); return true; } @@ -116,23 +121,24 @@ export class StatePersistent extends Persistent { /** * @internal * Sets everything up so that the State gets saved in the Storage on every Value change + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async persistValue(key?: PersistentKey): Promise { + public async persistValue(storageKey?: PersistentKey): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Add SideEffect to State, that updates the saved State Value depending on the current State Value this.state().addSideEffect( StatePersistent.storeValueSideEffectKey, (instance, config) => { - this.rebuildStorageSideEffect(this.state(), _key, config); + this.rebuildStorageSideEffect(this.state(), _storageKey, config); }, { weight: 0 } ); - // Rebuild Storage for saving State Value in the Storage - this.rebuildStorageSideEffect(this.state(), _key); + // Initial rebuild Storage for saving State Value in the Storage + this.rebuildStorageSideEffect(this.state(), _storageKey); this.isPersisted = true; return true; @@ -144,17 +150,20 @@ export class StatePersistent extends Persistent { /** * @internal * Removes State Value form the Storage + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async removePersistedValue(key?: PersistentKey): Promise { + public async removePersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Remove SideEffect this.state().removeSideEffect(StatePersistent.storeValueSideEffectKey); // Remove Value from Storage - this.agileInstance().storages.remove(_key, this.storageKeys); + this.agileInstance().storages.remove(_storageKey, this.storageKeys); this.isPersisted = false; return true; @@ -189,18 +198,18 @@ export class StatePersistent extends Persistent { * @internal * Rebuilds Storage depending on the State Value (Saves current State Value into the Storage) * @param state - State that holds the new Value - * @param key - Key/Name of Persistent + * @param storageKey - StorageKey where value should be persisted * @param config - Config */ public rebuildStorageSideEffect( state: State, - key: PersistentKey, + storageKey: PersistentKey, config: { [key: string]: any } = {} ) { if (config.storage !== undefined && !config.storage) return; this.agileInstance().storages.set( - key, + storageKey, this.state().getPersistableValue(), this.storageKeys ); diff --git a/packages/core/src/storages/index.ts b/packages/core/src/storages/index.ts index 4d372c2c..826269b1 100644 --- a/packages/core/src/storages/index.ts +++ b/packages/core/src/storages/index.ts @@ -11,7 +11,7 @@ import { export class Storages { public agileInstance: () => Agile; - public defaultStorage?: Storage; + public config: StoragesConfigInterface; public storages: { [key: string]: Storage } = {}; // All registered Storages public persistentInstances: Set = new Set(); @@ -21,11 +21,16 @@ export class Storages { * @param agileInstance - An Instance of Agile * @param config - Config */ - constructor(agileInstance: Agile, config: StoragesConfigInterface = {}) { + constructor( + agileInstance: Agile, + config: CreateStoragesConfigInterface = {} + ) { this.agileInstance = () => agileInstance; config = defineConfig(config, { localStorage: false, + defaultStorageKey: null, }); + this.config = { defaultStorageKey: config.defaultStorageKey as any }; if (config.localStorage) this.instantiateLocalStorage(); } @@ -91,20 +96,22 @@ export class Storages { // Register Storage this.storages[storage.key] = storage; - if (config.default) this.defaultStorage = storage; + if (config.default) this.config.defaultStorageKey = storage.key; this.persistentInstances.forEach((persistent) => { - // If Persistent isn't ready and has no default StorageKey.. reassignStorageKeys and try to load it - if (!persistent.defaultStorageKey) { - persistent.assignStorageKeys(); + // Revalidate Persistent that includes the newly registered StorageKey + if (persistent.storageKeys.includes(storage.key)) { const isValid = persistent.validatePersistent(); if (isValid) persistent.initialLoading(); return; } - // Add Value of Persistent to newly registered Storage - if (persistent.storageKeys.includes(storage.key)) - persistent.persistValue(); + // If persistent has no default StorageKey (reassign StorageKeys since this registered Storage might be tagged as default Storage) + if (!persistent.config.defaultStorageKey) { + persistent.assignStorageKeys(); + const isValid = persistent.validatePersistent(); + if (isValid) persistent.initialLoading(); + } }); return true; @@ -116,20 +123,27 @@ export class Storages { /** * @internal * Get Storage at Key/Name - * @param key - Key/Name of Storage + * @param storageKey - Key/Name of Storage */ - public getStorage(key: StorageKey): Storage | undefined { - const storage = this.storages[key]; + public getStorage( + storageKey: StorageKey | undefined | null + ): Storage | undefined { + if (!storageKey) return undefined; + const storage = this.storages[storageKey]; // Check if Storage exists if (!storage) { - Agile.logger.error(`Storage with the key/name '${key}' doesn't exist`); + Agile.logger.error( + `Storage with the key/name '${storageKey}' doesn't exist!` + ); return undefined; } // Check if Storage is ready if (!storage.ready) { - Agile.logger.error(`Storage with the key/name '${key}' isn't ready`); + Agile.logger.error( + `Storage with the key/name '${storageKey}' isn't ready yet!` + ); return undefined; } @@ -163,7 +177,8 @@ export class Storages { } // Call get Method in default Storage - return this.defaultStorage?.get(key) || Promise.resolve(undefined); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + return defaultStorage?.get(key) || Promise.resolve(undefined); } //========================================================================================================= @@ -196,7 +211,8 @@ export class Storages { } // Call set Method in default Storage - this.defaultStorage?.set(key, value); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + defaultStorage?.set(key, value); } //========================================================================================================= @@ -224,7 +240,8 @@ export class Storages { } // Call remove Method in default Storage - this.defaultStorage?.remove(key); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + defaultStorage?.remove(key); } //========================================================================================================= @@ -258,9 +275,18 @@ export class Storages { /** * @param localStorage - If Local Storage should be instantiated + * @param defaultStorage - Default Storage Key */ -export interface StoragesConfigInterface { +export interface CreateStoragesConfigInterface { localStorage?: boolean; + defaultStorageKey?: StorageKey; +} + +/** + * @param defaultStorage - Default Storage Key + */ +export interface StoragesConfigInterface { + defaultStorageKey: StorageKey | null; } /** diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index c3702f70..00269b89 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -1,9 +1,10 @@ -import { Agile, defineConfig, StorageKey } from '../internal'; +import { Agile, copy, defineConfig, StorageKey } from '../internal'; export class Persistent { public agileInstance: () => Agile; public static placeHolderKey = '__THIS_IS_A_PLACEHOLDER__'; + public config: PersistentConfigInterface; public _key: PersistentKey; public ready = false; @@ -12,7 +13,6 @@ export class Persistent { // StorageKeys of Storages in that the Persisted Value gets saved public storageKeys: StorageKey[] = []; - public defaultStorageKey: StorageKey | undefined; /** * @internal @@ -30,13 +30,19 @@ export class Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.agileInstance().storages.persistentInstances.add(this); - if (config.instantiate) + this.config = { defaultStorageKey: config.defaultStorageKey as any }; + + // Instantiate Persistent + if (config.instantiate) { this.instantiatePersistent({ storageKeys: config.storageKeys, key: config.key, + defaultStorageKey: config.defaultStorageKey, }); + } } /** @@ -77,9 +83,11 @@ export class Persistent { * have to define some stuff before being able to instantiate the parent (this) * @param config - Config */ - public instantiatePersistent(config: PersistentConfigInterface = {}) { + public instantiatePersistent( + config: InstantiatePersistentConfigInterface = {} + ) { this._key = this.formatKey(config.key) || Persistent.placeHolderKey; - this.assignStorageKeys(config.storageKeys); + this.assignStorageKeys(config.storageKeys, config.defaultStorageKey); this.validatePersistent(); } @@ -102,13 +110,23 @@ export class Persistent { } // Validate StorageKeys - if (!this.defaultStorageKey || this.storageKeys.length <= 0) { + if (!this.config.defaultStorageKey || this.storageKeys.length <= 0) { Agile.logger.error( 'No persist Storage Key found! Please provide at least one Storage Key.' ); isValid = false; } + // Check if Storages exist + this.storageKeys.map((key) => { + if (!this.agileInstance().storages.storages[key]) { + Agile.logger.error( + `Storage '${key}' doesn't exist yet. Please provide only existing StorageKeys!` + ); + isValid = false; + } + }); + this.ready = isValid; return isValid; } @@ -120,23 +138,33 @@ export class Persistent { * @internal * Assign new StorageKeys to Persistent and overwrite the old ones * @param storageKeys - New Storage Keys + * @param defaultStorageKey - Key of default Storage */ - public assignStorageKeys(storageKeys: StorageKey[] = []) { + public assignStorageKeys( + storageKeys: StorageKey[] = [], + defaultStorageKey?: StorageKey + ): void { const storages = this.agileInstance().storages; + const _storageKeys = copy(storageKeys); - // Set default Agile Storage to defaultStorage if no storageKey provided - if (storageKeys.length <= 0) { - this.storageKeys = []; - if (storages.defaultStorage) { - const key = storages.defaultStorage.key; - this.defaultStorageKey = key; - this.storageKeys.push(key); - } - return; + // Print warning if default StorageKey passed, but it isn't in storageKeys + if (defaultStorageKey && !_storageKeys.includes(defaultStorageKey)) { + Agile.logger.warn( + `Default Storage Key '${defaultStorageKey}' isn't contained in storageKeys!`, + _storageKeys + ); + _storageKeys.push(defaultStorageKey); } - this.storageKeys = storageKeys; - this.defaultStorageKey = storageKeys[0]; + // Add default Storage of AgileTs to storageKeys if no storageKey provided + if (_storageKeys.length <= 0) { + this.config.defaultStorageKey = storages.config.defaultStorageKey as any; + _storageKeys.push(storages.config.defaultStorageKey as any); + } else { + this.config.defaultStorageKey = defaultStorageKey || _storageKeys[0]; + } + + this.storageKeys = _storageKeys; } //========================================================================================================= @@ -146,7 +174,7 @@ export class Persistent { * @internal * Loads/Saves Storage Value for the first Time */ - public async initialLoading() { + public async initialLoading(): Promise { const success = await this.loadPersistedValue(); if (this.onLoad) this.onLoad(success); if (!success) await this.persistValue(); @@ -215,19 +243,30 @@ export type PersistentKey = string | number; /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved + * @param defaultStorage - Default Storage Key * @param instantiate - If Persistent gets Instantiated immediately */ export interface CreatePersistentConfigInterface { key?: PersistentKey; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; instantiate?: boolean; } +/** + * @param defaultStorageKey - Default Storage Key + */ +export interface PersistentConfigInterface { + defaultStorageKey: StorageKey | null; +} + /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ -export interface PersistentConfigInterface { +export interface InstantiatePersistentConfigInterface { key?: PersistentKey; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } diff --git a/packages/core/tests/unit/collection/collection.persistent.test.ts b/packages/core/tests/unit/collection/collection.persistent.test.ts index 6ad34561..a91d4e8e 100644 --- a/packages/core/tests/unit/collection/collection.persistent.test.ts +++ b/packages/core/tests/unit/collection/collection.persistent.test.ts @@ -48,6 +48,7 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.instantiatePersistent).toHaveBeenCalledWith({ key: undefined, storageKeys: [], + defaultStorageKey: null, }); expect(collectionPersistent.initialLoading).not.toHaveBeenCalled(); @@ -56,7 +57,9 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.isPersisted).toBeFalsy(); expect(collectionPersistent.onLoad).toBeUndefined(); expect(collectionPersistent.storageKeys).toStrictEqual([]); - expect(collectionPersistent.defaultStorageKey).toBeUndefined(); + expect(collectionPersistent.config).toStrictEqual({ + defaultStorageKey: null, + }); }); it("should create CollectionPersistent and shouldn't call initialLoading if Persistent isn't ready (specific config)", () => { @@ -71,12 +74,14 @@ describe('CollectionPersistent Tests', () => { const collectionPersistent = new CollectionPersistent(dummyCollection, { key: 'collectionPersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(collectionPersistent).toBeInstanceOf(CollectionPersistent); expect(collectionPersistent.instantiatePersistent).toHaveBeenCalledWith({ key: 'collectionPersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(collectionPersistent.initialLoading).not.toHaveBeenCalled(); @@ -85,7 +90,9 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.isPersisted).toBeFalsy(); expect(collectionPersistent.onLoad).toBeUndefined(); expect(collectionPersistent.storageKeys).toStrictEqual([]); - expect(collectionPersistent.defaultStorageKey).toBeUndefined(); + expect(collectionPersistent.config).toStrictEqual({ + defaultStorageKey: null, // gets set in 'instantiatePersistent' which is mocked + }); }); it('should create CollectionPersistent and should call initialLoading if Persistent is ready (default config)', () => { @@ -258,7 +265,7 @@ describe('CollectionPersistent Tests', () => { let dummyDefaultGroup: Group; beforeEach(() => { - collectionPersistent.defaultStorageKey = 'test'; + collectionPersistent.config.defaultStorageKey = 'test'; dummyDefaultGroup = new Group(dummyCollection, ['1', '2', '3']); dummyDefaultGroup.persistent = new StatePersistent(dummyDefaultGroup); @@ -294,32 +301,32 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); expect(dummyDefaultGroup.persistent?.initialLoading).toHaveBeenCalled(); @@ -355,28 +362,28 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).not.toHaveBeenCalled(); @@ -412,23 +419,23 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( 'dummyKey', - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('1', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('2', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('3', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); expect(dummyDefaultGroup.persistent?.initialLoading).toHaveBeenCalled(); @@ -493,28 +500,28 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).not.toHaveBeenCalled(); diff --git a/packages/core/tests/unit/collection/collection.test.ts b/packages/core/tests/unit/collection/collection.test.ts index d9344955..edee560a 100644 --- a/packages/core/tests/unit/collection/collection.test.ts +++ b/packages/core/tests/unit/collection/collection.test.ts @@ -1032,7 +1032,7 @@ describe('Collection Tests', () => { expect(ComputedTracker.tracked).not.toHaveBeenCalled(); }); - it('should return and track existing placeholder Group (config.notExisting = true)', () => { + it('should return and track existing placeholder Group (config.notExisting = true)', () => { dummyGroup.isPlaceholder = true; const response = collection.getGroup('dummyGroup', { @@ -1588,13 +1588,15 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: collection._key, + defaultStorageKey: null, }); }); it('should create persistent with CollectionKey (specific config)', () => { collection.persist({ storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, + defaultStorageKey: 'test1', }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); @@ -1602,6 +1604,7 @@ describe('Collection Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: collection._key, + defaultStorageKey: 'test1', }); }); @@ -1613,13 +1616,15 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: 'passedKey', + defaultStorageKey: null, }); }); it('should create persistent with passed Key (specific config)', () => { collection.persist('passedKey', { storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, + defaultStorageKey: 'test1', }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); @@ -1627,6 +1632,7 @@ describe('Collection Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: 'passedKey', + defaultStorageKey: 'test1', }); }); @@ -1641,6 +1647,7 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: 'newPersistentKey', + defaultStorageKey: null, }); expect(console.warn).toHaveBeenCalledWith( `Agile Warn: By persisting the Collection '${collection._key}' twice you overwrite the old Persistent Instance!` diff --git a/packages/core/tests/unit/collection/group.test.ts b/packages/core/tests/unit/collection/group.test.ts index 92a9fca4..711c3e00 100644 --- a/packages/core/tests/unit/collection/group.test.ts +++ b/packages/core/tests/unit/collection/group.test.ts @@ -434,17 +434,23 @@ describe('Group Tests', () => { group.persist(); expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, }); }); it('should persist Group with GroupKey (specific config)', () => { - group.persist({ instantiate: false, storageKeys: ['test1', 'test2'] }); + group.persist({ + loadValue: false, + storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', + }); expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); }); @@ -452,20 +458,23 @@ describe('Group Tests', () => { group.persist('dummyKey'); expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, }); }); it('should persist Group with passed Key (specific config)', () => { group.persist('dummyKey', { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); }); @@ -478,8 +487,9 @@ describe('Group Tests', () => { dummyCollection._key ), { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, } ); }); @@ -493,8 +503,9 @@ describe('Group Tests', () => { dummyCollection._key ), { - instantiate: true, + loadValue: true, storageKeys: [], + defaultStorageKey: null, } ); }); diff --git a/packages/core/tests/unit/state/state.persistent.test.ts b/packages/core/tests/unit/state/state.persistent.test.ts index 9fbfa647..ad918ee0 100644 --- a/packages/core/tests/unit/state/state.persistent.test.ts +++ b/packages/core/tests/unit/state/state.persistent.test.ts @@ -38,6 +38,7 @@ describe('StatePersistent Tests', () => { expect(statePersistent.instantiatePersistent).toHaveBeenCalledWith({ key: undefined, storageKeys: [], + defaultStorageKey: null, }); expect(statePersistent.initialLoading).not.toHaveBeenCalled(); @@ -46,7 +47,7 @@ describe('StatePersistent Tests', () => { expect(statePersistent.isPersisted).toBeFalsy(); expect(statePersistent.onLoad).toBeUndefined(); expect(statePersistent.storageKeys).toStrictEqual([]); - expect(statePersistent.defaultStorageKey).toBeUndefined(); + expect(statePersistent.config).toStrictEqual({ defaultStorageKey: null }); }); it("should create StatePersistent and shouldn't call initialLoading if Persistent isn't ready (specific config)", () => { @@ -61,12 +62,14 @@ describe('StatePersistent Tests', () => { const statePersistent = new StatePersistent(dummyState, { key: 'statePersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(statePersistent).toBeInstanceOf(StatePersistent); expect(statePersistent.instantiatePersistent).toHaveBeenCalledWith({ key: 'statePersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(statePersistent.initialLoading).not.toHaveBeenCalled(); @@ -75,7 +78,9 @@ describe('StatePersistent Tests', () => { expect(statePersistent.isPersisted).toBeFalsy(); expect(statePersistent.onLoad).toBeUndefined(); expect(statePersistent.storageKeys).toStrictEqual([]); - expect(statePersistent.defaultStorageKey).toBeUndefined(); + expect(statePersistent.config).toStrictEqual({ + defaultStorageKey: null, // gets set in 'instantiatePersistent' which is mocked + }); }); it('should create StatePersistent and should call initialLoading if Persistent is ready (default config)', () => { @@ -225,7 +230,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeTruthy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( statePersistent._key, - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).toHaveBeenCalledWith('dummyValue', { storage: false, @@ -246,7 +251,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeFalsy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( statePersistent._key, - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).not.toHaveBeenCalled(); expect(statePersistent.persistValue).not.toHaveBeenCalled(); @@ -263,7 +268,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeTruthy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( 'coolKey', - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).toHaveBeenCalledWith('dummyValue', { storage: false, diff --git a/packages/core/tests/unit/state/state.test.ts b/packages/core/tests/unit/state/state.test.ts index 378f384c..86d89205 100644 --- a/packages/core/tests/unit/state/state.test.ts +++ b/packages/core/tests/unit/state/state.test.ts @@ -594,13 +594,15 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: numberState._key, + defaultStorageKey: null, }); }); it('should create persistent with StateKey (specific config)', () => { numberState.persist({ storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, + defaultStorageKey: 'test1', }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); @@ -608,6 +610,7 @@ describe('State Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: numberState._key, + defaultStorageKey: 'test1', }); }); @@ -619,13 +622,15 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: 'passedKey', + defaultStorageKey: null, }); }); it('should create persistent with passed Key (specific config)', () => { numberState.persist('passedKey', { storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, + defaultStorageKey: 'test1', }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); @@ -633,6 +638,7 @@ describe('State Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: 'passedKey', + defaultStorageKey: 'test1', }); }); @@ -647,6 +653,7 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: 'newPersistentKey', + defaultStorageKey: null, }); expect(console.warn).toHaveBeenCalledWith( `Agile Warn: By persisting the State '${numberState._key}' twice you overwrite the old Persistent Instance!` diff --git a/packages/core/tests/unit/storages/persistent.test.ts b/packages/core/tests/unit/storages/persistent.test.ts index 2baf0c61..6b685a87 100644 --- a/packages/core/tests/unit/storages/persistent.test.ts +++ b/packages/core/tests/unit/storages/persistent.test.ts @@ -25,6 +25,7 @@ describe('Persistent Tests', () => { expect(persistent.instantiatePersistent).toHaveBeenCalledWith({ storageKeys: [], key: undefined, + defaultStorageKey: null, }); expect( dummyAgile.storages.persistentInstances.has(persistent) @@ -35,7 +36,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: null }); }); it('should create Persistent (specific config)', () => { @@ -47,12 +48,14 @@ describe('Persistent Tests', () => { const persistent = new Persistent(dummyAgile, { storageKeys: ['test1', 'test2'], key: 'persistentKey', + defaultStorageKey: 'test1', }); expect(persistent).toBeInstanceOf(Persistent); expect(persistent.instantiatePersistent).toHaveBeenCalledWith({ storageKeys: ['test1', 'test2'], key: 'persistentKey', + defaultStorageKey: 'test1', }); expect( dummyAgile.storages.persistentInstances.has(persistent) @@ -63,7 +66,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: 'test1' }); }); it('should create Persistent (config.instantiate = false)', () => { @@ -85,7 +88,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: null }); }); describe('Persistent Function Tests', () => { @@ -122,15 +125,15 @@ describe('Persistent Tests', () => { persistent.instantiatePersistent({ key: 'persistentKey', storageKeys: ['myName', 'is', 'jeff'], + defaultStorageKey: 'jeff', }); expect(persistent._key).toBe('persistentKey'); expect(persistent.formatKey).toHaveBeenCalledWith('persistentKey'); - expect(persistent.assignStorageKeys).toHaveBeenCalledWith([ - 'myName', - 'is', - 'jeff', - ]); + expect(persistent.assignStorageKeys).toHaveBeenCalledWith( + ['myName', 'is', 'jeff'], + 'jeff' + ); expect(persistent.validatePersistent).toHaveBeenCalled(); }); }); @@ -138,7 +141,7 @@ describe('Persistent Tests', () => { describe('validatePersistent function tests', () => { beforeEach(() => { persistent.key = Persistent.placeHolderKey; - persistent.defaultStorageKey = undefined; + persistent.config.defaultStorageKey = null; persistent.storageKeys = []; persistent.ready = undefined as any; }); @@ -167,8 +170,8 @@ describe('Persistent Tests', () => { ); }); - it('should return false if no set key and set StorageKeys', () => { - persistent.defaultStorageKey = 'test'; + it('should return false and print error if no set key and set StorageKeys', () => { + persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; const isValid = persistent.validatePersistent(); @@ -181,9 +184,40 @@ describe('Persistent Tests', () => { ); }); + it('should return false and print error if set key and set StorageKeys but no existing Storage at storageKeys', () => { + persistent.config.defaultStorageKey = 'test'; + persistent.storageKeys = ['test']; + + const isValid = persistent.validatePersistent(); + + expect(isValid).toBeFalsy(); + expect(persistent.ready).toBeFalsy(); + + expect(console.error).toHaveBeenCalledWith( + "Agile Error: Storage 'test' doesn't exist yet. Please provide only existing StorageKeys!" + ); + }); + it('should return true if set key and set StorageKeys', () => { + dummyAgile.storages.register( + dummyAgile.createStorage({ + key: 'test', + methods: { + get: () => { + /* empty */ + }, + set: () => { + /* empty */ + }, + remove: () => { + /* empty */ + }, + }, + }) + ); + persistent._key = 'persistentKey'; - persistent.defaultStorageKey = 'test'; + persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; const isValid = persistent.validatePersistent(); @@ -194,7 +228,7 @@ describe('Persistent Tests', () => { }); describe('assignStorageKeys function tests', () => { - it('should assign passed StorageKeys and set first one as default StorageKey', () => { + it('should assign passed StorageKeys and set first one as default StorageKey if no default Storage Key passed', () => { persistent.assignStorageKeys(['test1', 'test2', 'test3']); expect(persistent.storageKeys).toStrictEqual([ @@ -202,10 +236,41 @@ describe('Persistent Tests', () => { 'test2', 'test3', ]); - expect(persistent.defaultStorageKey).toBe('test1'); + expect(persistent.config.defaultStorageKey).toBe('test1'); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should assign passed StorageKeys and set passed defaultStorageKey as default StorageKey', () => { + persistent.assignStorageKeys(['test1', 'test2', 'test3'], 'test3'); + + expect(persistent.storageKeys).toStrictEqual([ + 'test1', + 'test2', + 'test3', + ]); + expect(persistent.config.defaultStorageKey).toBe('test3'); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should assign passed StorageKeys and set not existing defaultStorageKey as default StorageKey, push it into storageKeys and print warning', () => { + persistent.assignStorageKeys(['test1', 'test2', 'test3'], 'test4'); + + expect(persistent.storageKeys).toStrictEqual([ + 'test1', + 'test2', + 'test3', + 'test4', + ]); + expect(persistent.config.defaultStorageKey).toBe('test4'); + expect( + console.warn + ).toHaveBeenCalledWith( + "Agile Warn: Default Storage Key 'test4' isn't contained in storageKeys!", + ['test1', 'test2', 'test3', 'test4'] + ); }); - it('should try to get default StorageKey from Agile if no StorageKeys passed', () => { + it('should try to get default StorageKey from Agile if no StorageKey got passed', () => { dummyAgile.storages.register( new Storage({ key: 'storage1', @@ -227,7 +292,8 @@ describe('Persistent Tests', () => { persistent.assignStorageKeys(); expect(persistent.storageKeys).toStrictEqual(['storage1']); - expect(persistent.defaultStorageKey).toBe('storage1'); + expect(persistent.config.defaultStorageKey).toBe('storage1'); + expect(console.warn).not.toHaveBeenCalled(); }); }); diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 6f125f9f..6c146bb9 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -16,16 +16,19 @@ describe('Storages Tests', () => { it('should create Storages (default config)', () => { const storages = new Storages(dummyAgile); - expect(storages.defaultStorage).toBeUndefined(); + expect(storages.config).toStrictEqual({ defaultStorageKey: null }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).not.toHaveBeenCalled(); }); - it('should create Storages and should get a warning (config.localStorage = true)', () => { - const storages = new Storages(dummyAgile, { localStorage: true }); + it('should create Storages (specific config)', () => { + const storages = new Storages(dummyAgile, { + defaultStorageKey: 'jeff', + localStorage: true, + }); - expect(storages.defaultStorage).toBeUndefined(); + expect(storages.config).toStrictEqual({ defaultStorageKey: 'jeff' }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).toHaveBeenCalled(); @@ -101,7 +104,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage1'); expect(storages.storages['storage1']).toBe(dummyStorage1); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -114,7 +117,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage1'); expect(storages.storages['storage1']).toBe(dummyStorage1); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -125,7 +128,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage2'); expect(storages.storages['storage2']).toBe(dummyStorage2); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -136,11 +139,11 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage2'); expect(storages.storages['storage2']).toBe(dummyStorage2); - expect(storages.defaultStorage).toBe(dummyStorage2); + expect(storages.config.defaultStorageKey).toBe('storage2'); expect(response).toBeTruthy(); }); - it("shouldn't register Storage with the same key twice", () => { + it("shouldn't register Storage with the same key twice and print error", () => { const dummyStorage = new Storage({ key: 'storage1', methods: dummyStorageMethods, @@ -158,28 +161,38 @@ describe('Storages Tests', () => { expect(response).toBeFalsy(); }); - it('should call updateValue method on all persistent Instances that have the newly registered StorageKey', () => { + it('should revalidate and initialLoad value on all persistent Instances that have the newly registered StorageKey', () => { const dummyPersistent1 = new Persistent(dummyAgile, { storageKeys: ['storage1'], + key: 'dummyPersistent1', }); const dummyPersistent2 = new Persistent(dummyAgile, { storageKeys: ['notExistingStorage'], + key: 'dummyPersistent2', }); - jest.spyOn(dummyPersistent1, 'persistValue'); - jest.spyOn(dummyPersistent2, 'persistValue'); + jest.spyOn(dummyPersistent1, 'validatePersistent'); + jest.spyOn(dummyPersistent1, 'initialLoading'); + jest.spyOn(dummyPersistent2, 'validatePersistent'); + jest.spyOn(dummyPersistent2, 'initialLoading'); const response = storages.register(dummyStorage1); - expect(dummyPersistent1.persistValue).toHaveBeenCalled(); - expect(dummyPersistent2.persistValue).not.toHaveBeenCalled(); + expect(dummyPersistent1.validatePersistent).toHaveBeenCalled(); + expect(dummyPersistent1.initialLoading).toHaveBeenCalled(); + expect(dummyPersistent2.validatePersistent).not.toHaveBeenCalled(); + expect(dummyPersistent2.initialLoading).not.toHaveBeenCalled(); expect(response).toBeTruthy(); }); - it('should reassignStorageKeys, revalidate and initialLoad Persistents that have no defined defaultStorage', () => { - const dummyPersistent1 = new Persistent(dummyAgile); - dummyPersistent1.defaultStorageKey = undefined; - const dummyPersistent2 = new Persistent(dummyAgile); - dummyPersistent2.defaultStorageKey = 'unknown'; + it('should revalidate and initial load Persistents that have no defined defaultStorage', () => { + const dummyPersistent1 = new Persistent(dummyAgile, { + key: 'dummyPersistent1', + }); + const dummyPersistent2 = new Persistent(dummyAgile, { + storageKeys: ['dummy'], + defaultStorageKey: 'dummy', + key: 'dummyPersistent2', + }); jest.spyOn(dummyPersistent1, 'assignStorageKeys'); jest .spyOn(dummyPersistent1, 'validatePersistent') @@ -212,6 +225,14 @@ describe('Storages Tests', () => { const response = storages.getStorage('storage1'); expect(response).toBe(dummyStorage1); + expect(console.error).not.toHaveBeenCalled(); + }); + + it("shouldn't get Storage with undefined key", () => { + const response = storages.getStorage(null); + + expect(response).toBeUndefined(); + expect(console.error).not.toHaveBeenCalled(); }); it("shouldn't get not existing Storage", () => { @@ -219,7 +240,7 @@ describe('Storages Tests', () => { expect(response).toBeUndefined(); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist" + "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist!" ); }); @@ -230,7 +251,7 @@ describe('Storages Tests', () => { expect(response).toBeUndefined(); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'storage1' isn't ready" + "Agile Error: Storage with the key/name 'storage1' isn't ready yet!" ); }); }); @@ -266,7 +287,7 @@ describe('Storages Tests', () => { expect(response).toBe('dummyStorage1Response'); expect(dummyStorage1.get).toHaveBeenCalledWith('value1'); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist" + "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist!" ); });