@@ -10,6 +10,10 @@ jest.mock('../../inMemory/InMemoryStorageCS', () => {
1010import { IStorageFactoryParams } from '../../types' ;
1111import { assertStorageInterface } from '../../__tests__/testUtils' ;
1212import { fullSettings } from '../../../utils/settingsValidation/__tests__/settings.mocks' ;
13+ import { createMemoryStorage } from './wrapper.mock' ;
14+ import * as storageAdapter from '../storageAdapter' ;
15+
16+ const storageAdapterSpy = jest . spyOn ( storageAdapter , 'storageAdapter' ) ;
1317
1418// Test target
1519import { InLocalStorage } from '../index' ;
@@ -40,7 +44,7 @@ describe('IN LOCAL STORAGE', () => {
4044 expect ( storage ) . toBe ( fakeInMemoryStorage ) ;
4145
4246 // Provided storage is valid
43- storageFactory = InLocalStorage ( { prefix : 'prefix' , wrapper : { getItem : ( ) => Promise . resolve ( null ) , setItem : ( ) => Promise . resolve ( ) , removeItem : ( ) => Promise . resolve ( ) } } ) ;
47+ storageFactory = InLocalStorage ( { prefix : 'prefix' , wrapper : createMemoryStorage ( ) } ) ;
4448 storage = storageFactory ( internalSdkParams ) ;
4549 expect ( storage ) . not . toBe ( fakeInMemoryStorage ) ;
4650
@@ -55,7 +59,31 @@ describe('IN LOCAL STORAGE', () => {
5559
5660 assertStorageInterface ( storage ) ; // the instance must implement the storage interface
5761 expect ( fakeInMemoryStorageFactory ) . not . toBeCalled ( ) ; // doesn't call InMemoryStorage factory
62+ } ) ;
63+
64+ test ( 'calls its own storage factory if the provided storage wrapper is valid' , ( ) => {
65+ storageAdapterSpy . mockClear ( ) ;
66+
67+ // Web Storages should not use the storageAdapter
68+ let storageFactory = InLocalStorage ( { prefix : 'prefix' , wrapper : localStorage } ) ;
69+ let storage = storageFactory ( internalSdkParams ) ;
70+ assertStorageInterface ( storage ) ;
71+ expect ( fakeInMemoryStorageFactory ) . not . toBeCalled ( ) ;
72+ expect ( storageAdapterSpy ) . not . toBeCalled ( ) ;
73+
74+ storageFactory = InLocalStorage ( { prefix : 'prefix' , wrapper : sessionStorage } ) ;
75+ storage = storageFactory ( internalSdkParams ) ;
76+ assertStorageInterface ( storage ) ;
77+ expect ( fakeInMemoryStorageFactory ) . not . toBeCalled ( ) ;
78+ expect ( storageAdapterSpy ) . not . toBeCalled ( ) ;
79+
80+ // Non Web Storages should use the storageAdapter
81+ storageFactory = InLocalStorage ( { prefix : 'prefix' , wrapper : createMemoryStorage ( ) } ) ;
82+ storage = storageFactory ( internalSdkParams ) ;
5883
84+ assertStorageInterface ( storage ) ;
85+ expect ( fakeInMemoryStorageFactory ) . not . toBeCalled ( ) ;
86+ expect ( storageAdapterSpy ) . toBeCalled ( ) ;
5987 } ) ;
6088
6189} ) ;
0 commit comments